|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +// Brand palette from tailwind.config.js, plus white so the fall reads on the dark theme. |
| 4 | +const COLORS = ["#ff5547", "#806dfe", "#e1761a", "#5945d8", "#ffffff"] |
| 5 | +const PIECES = 150 |
| 6 | +const GRAVITY = 0.04 |
| 7 | +const MAX_FALL = 3.5 |
| 8 | +// Pieces start staggered above the fold so they keep arriving instead of landing all at once. |
| 9 | +const STAGGER = 1.1 |
| 10 | +const FADE_FROM = 0.82 |
| 11 | + |
| 12 | +export default class extends Controller { |
| 13 | + connect() { |
| 14 | + this.motion = window.matchMedia("(prefers-reduced-motion: reduce)") |
| 15 | + this.pieces = [] |
| 16 | + this.frame = null |
| 17 | + this.onResize = () => this.#resize() |
| 18 | + |
| 19 | + this.burst() |
| 20 | + } |
| 21 | + |
| 22 | + disconnect() { |
| 23 | + this.#stop() |
| 24 | + this.#unmount() |
| 25 | + } |
| 26 | + |
| 27 | + burst() { |
| 28 | + if (this.motion.matches) { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + this.#mount() |
| 33 | + |
| 34 | + const width = window.innerWidth |
| 35 | + const height = window.innerHeight |
| 36 | + |
| 37 | + for (let i = 0; i < PIECES; i++) { |
| 38 | + this.pieces.push({ |
| 39 | + x: Math.random() * width, |
| 40 | + y: -20 - Math.random() * height * STAGGER, |
| 41 | + vx: (Math.random() - 0.5) * 0.7, |
| 42 | + vy: 1 + Math.random() * 1.5, |
| 43 | + sway: 0.4 + Math.random() * 1.1, |
| 44 | + swaySpeed: 0.01 + Math.random() * 0.03, |
| 45 | + phase: Math.random() * Math.PI * 2, |
| 46 | + width: 5 + Math.random() * 5, |
| 47 | + height: 9 + Math.random() * 6, |
| 48 | + rotation: Math.random() * Math.PI * 2, |
| 49 | + spin: (Math.random() - 0.5) * 0.12, |
| 50 | + flip: Math.random() * Math.PI * 2, |
| 51 | + flipSpeed: 0.05 + Math.random() * 0.09, |
| 52 | + color: COLORS[Math.floor(Math.random() * COLORS.length)], |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + this.#start() |
| 57 | + } |
| 58 | + |
| 59 | + #tick() { |
| 60 | + const context = this.context |
| 61 | + const height = window.innerHeight |
| 62 | + |
| 63 | + context.clearRect(0, 0, window.innerWidth, height) |
| 64 | + |
| 65 | + this.pieces = this.pieces.filter((piece) => { |
| 66 | + piece.vy = Math.min(piece.vy + GRAVITY, MAX_FALL) |
| 67 | + piece.phase += piece.swaySpeed |
| 68 | + piece.y += piece.vy |
| 69 | + piece.x += piece.vx + Math.sin(piece.phase) * piece.sway |
| 70 | + piece.rotation += piece.spin |
| 71 | + piece.flip += piece.flipSpeed |
| 72 | + |
| 73 | + if (piece.y > height + 40) { |
| 74 | + return false |
| 75 | + } |
| 76 | + |
| 77 | + const travelled = piece.y / height |
| 78 | + |
| 79 | + context.save() |
| 80 | + context.translate(piece.x, piece.y) |
| 81 | + context.rotate(piece.rotation) |
| 82 | + // Squashing the height as it tumbles fakes a flat piece turning over. |
| 83 | + context.scale(1, Math.abs(Math.cos(piece.flip))) |
| 84 | + context.globalAlpha = travelled > FADE_FROM ? Math.max(0, 1 - (travelled - FADE_FROM) / (1 - FADE_FROM)) : 1 |
| 85 | + context.fillStyle = piece.color |
| 86 | + context.fillRect(-piece.width / 2, -piece.height / 2, piece.width, piece.height) |
| 87 | + context.restore() |
| 88 | + |
| 89 | + return true |
| 90 | + }) |
| 91 | + |
| 92 | + if (this.pieces.length === 0) { |
| 93 | + this.#stop() |
| 94 | + this.#unmount() |
| 95 | + |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + this.frame = window.requestAnimationFrame(() => this.#tick()) |
| 100 | + } |
| 101 | + |
| 102 | + #mount() { |
| 103 | + if (this.canvas) { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + this.canvas = document.createElement("canvas") |
| 108 | + this.canvas.setAttribute("aria-hidden", "true") |
| 109 | + this.canvas.style.cssText = "position:fixed;inset:0;pointer-events:none;z-index:60" |
| 110 | + document.body.appendChild(this.canvas) |
| 111 | + this.context = this.canvas.getContext("2d") |
| 112 | + |
| 113 | + this.#resize() |
| 114 | + window.addEventListener("resize", this.onResize) |
| 115 | + } |
| 116 | + |
| 117 | + #resize() { |
| 118 | + if (!this.canvas) { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + const ratio = window.devicePixelRatio || 1 |
| 123 | + |
| 124 | + this.canvas.width = window.innerWidth * ratio |
| 125 | + this.canvas.height = window.innerHeight * ratio |
| 126 | + this.canvas.style.width = `${window.innerWidth}px` |
| 127 | + this.canvas.style.height = `${window.innerHeight}px` |
| 128 | + this.context.setTransform(ratio, 0, 0, ratio, 0, 0) |
| 129 | + } |
| 130 | + |
| 131 | + #unmount() { |
| 132 | + window.removeEventListener("resize", this.onResize) |
| 133 | + |
| 134 | + if (this.canvas) { |
| 135 | + this.canvas.remove() |
| 136 | + this.canvas = null |
| 137 | + this.context = null |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + #start() { |
| 142 | + if (this.frame === null) { |
| 143 | + this.frame = window.requestAnimationFrame(() => this.#tick()) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + #stop() { |
| 148 | + if (this.frame !== null) { |
| 149 | + window.cancelAnimationFrame(this.frame) |
| 150 | + this.frame = null |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments