|
| 1 | +function Matrix(canvasName) { |
| 2 | + const { floor: f, random: rand } = Math; |
| 3 | + let canvas = document.getElementById(canvasName); |
| 4 | + |
| 5 | + const ctx = canvas.getContext("2d"); |
| 6 | + canvas.height = window.innerHeight; |
| 7 | + canvas.width = window.innerWidth; |
| 8 | + |
| 9 | + let gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height); |
| 10 | + gradient.addColorStop(0, "red"); |
| 11 | + gradient.addColorStop(0.2, "yellow"); |
| 12 | + gradient.addColorStop(0.4, "green"); |
| 13 | + gradient.addColorStop(0.6, "cyan"); |
| 14 | + gradient.addColorStop(0.8, "blue"); |
| 15 | + gradient.addColorStop(1, "magenta"); |
| 16 | + |
| 17 | + class Symbol { |
| 18 | + constructor(x, y, fontSize, canvasHeight) { |
| 19 | + this.characters = |
| 20 | + "アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 21 | + this.x = x; |
| 22 | + this.y = y; |
| 23 | + this.fontSize = fontSize; |
| 24 | + this.font_size = this.fontSize; |
| 25 | + this.text = ""; |
| 26 | + this.canvasHeight = canvasHeight; |
| 27 | + } |
| 28 | + draw(context) { |
| 29 | + this.text = this.characters.charAt(f(rand() * this.characters.length)); |
| 30 | + context.fillText( |
| 31 | + this.text, |
| 32 | + this.x * this.fontSize, |
| 33 | + this.y * this.fontSize |
| 34 | + ); |
| 35 | + if (this.y * this.fontSize > this.canvasHeight && rand() > 0.95) { |
| 36 | + this.y = 0; |
| 37 | + } else { |
| 38 | + this.y += 1; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + class Effect { |
| 44 | + constructor(canvasWidth, canvasHeight) { |
| 45 | + this.canvasWidth = canvasWidth; |
| 46 | + this.canvasHeight = canvasHeight; |
| 47 | + this.fontSize = 20; |
| 48 | + this.columns = this.canvasWidth / this.fontSize; |
| 49 | + this.symbols = []; |
| 50 | + this.#initialize(); |
| 51 | + } |
| 52 | + #initialize() { |
| 53 | + for (let i = 0; i < this.columns + 1; i++) { |
| 54 | + this.symbols[i] = new Symbol(i, 0, this.fontSize, this.canvasHeight); |
| 55 | + } |
| 56 | + } |
| 57 | + resize(width, height) { |
| 58 | + this.canvasWidth = width; |
| 59 | + this.canvasHeight = height; |
| 60 | + this.columns = this.canvasWidth / this.fontSize; |
| 61 | + this.symbols = []; |
| 62 | + this.#initialize(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const effect = new Effect(canvas.width, canvas.height); |
| 67 | + |
| 68 | + let lastTime = 0; |
| 69 | + const FPS = 62; |
| 70 | + const nextFrame = 1000 / FPS; |
| 71 | + let timer = 0; |
| 72 | + |
| 73 | + function animate(timeStamp) { |
| 74 | + const deltaTime = timeStamp - lastTime; |
| 75 | + lastTime = timeStamp; |
| 76 | + |
| 77 | + if (timer > nextFrame) { |
| 78 | + ctx.fillStyle = "rgba(0, 0, 0, 0.1)"; |
| 79 | + ctx.textAlign = "center"; |
| 80 | + ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 81 | + // ctx.fillStyle = "#0aff0a"; |
| 82 | + ctx.fillStyle = gradient; |
| 83 | + ctx.font = effect.fontSize + "px monospace"; |
| 84 | + effect.symbols.forEach((symbol) => symbol.draw(ctx)); |
| 85 | + timer = 0; |
| 86 | + } else { |
| 87 | + timer += deltaTime; |
| 88 | + } |
| 89 | + |
| 90 | + requestAnimationFrame(animate); |
| 91 | + } |
| 92 | + |
| 93 | + animate(0); |
| 94 | + |
| 95 | + window.addEventListener("resize", () => { |
| 96 | + canvas.width = window.innerWidth; |
| 97 | + canvas.height = window.innerHeight; |
| 98 | + gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height); |
| 99 | + gradient.addColorStop(0, "red"); |
| 100 | + gradient.addColorStop(0.2, "yellow"); |
| 101 | + gradient.addColorStop(0.4, "green"); |
| 102 | + gradient.addColorStop(0.6, "cyan"); |
| 103 | + gradient.addColorStop(0.8, "blue"); |
| 104 | + gradient.addColorStop(1, "magenta"); |
| 105 | + effect.resize(canvas.width, canvas.height); |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +Matrix("canvas"); |
0 commit comments