|
| 1 | +// Shim for requestAnimationFrame with setTimeout fallback |
| 2 | +const requestAnimFrame = |
| 3 | + window.requestAnimationFrame || |
| 4 | + window.webkitRequestAnimationFrame || |
| 5 | + window.mozRequestAnimationFrame || |
| 6 | + function (callback) { |
| 7 | + return window.setTimeout(callback, 1000 / 60) |
| 8 | + } |
| 9 | + |
| 10 | +// Snowflake class |
| 11 | +class Snowflake { |
| 12 | + constructor(width, height) { |
| 13 | + this.width = width |
| 14 | + this.height = height |
| 15 | + this.reset() |
| 16 | + } |
| 17 | + |
| 18 | + // Reset snowflake to a new random position and velocity |
| 19 | + reset() { |
| 20 | + this.x = Math.random() * this.width |
| 21 | + this.y = Math.random() * -this.height |
| 22 | + this.vy = 1 + (Math.random() * 3) |
| 23 | + this.vx = 0.5 - Math.random() |
| 24 | + this.r = 1 + (Math.random() * 2) |
| 25 | + this.o = 0.5 + (Math.random() * 0.5) |
| 26 | + } |
| 27 | + |
| 28 | + // Move the snowflake |
| 29 | + updatePosition() { |
| 30 | + this.y += this.vy |
| 31 | + this.x += this.vx |
| 32 | + } |
| 33 | + |
| 34 | + // Check if the snowflake has moved beyond the bottom |
| 35 | + isOutOfView() { |
| 36 | + return this.y > this.height |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Main function to create snowfall |
| 41 | +const snowflakes = (target, count = 200) => { |
| 42 | + // Determine if the target is a selector string or a DOM element |
| 43 | + const container = typeof target === 'string' ? document.querySelector(target) : target |
| 44 | + |
| 45 | + if (!container) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Create and configure canvas |
| 50 | + const canvas = document.createElement('canvas') |
| 51 | + const ctx = canvas.getContext('2d') |
| 52 | + |
| 53 | + let width = container.clientWidth |
| 54 | + let height = container.clientHeight |
| 55 | + let active = false |
| 56 | + |
| 57 | + // Initialize canvas styles |
| 58 | + canvas.style.position = 'absolute' |
| 59 | + canvas.style.left = '0' |
| 60 | + canvas.style.top = '0' |
| 61 | + canvas.style.pointerEvents = 'none' // Allows clicks to pass through |
| 62 | + |
| 63 | + // Snowflake instances |
| 64 | + const flakes = [] |
| 65 | + |
| 66 | + // Create snowflakes |
| 67 | + for (let i = 0; i < count; i++) { |
| 68 | + flakes.push(new Snowflake(width, height)) |
| 69 | + } |
| 70 | + |
| 71 | + // Handle canvas resizing |
| 72 | + const onResize = () => { |
| 73 | + width = container.clientWidth |
| 74 | + height = container.clientHeight |
| 75 | + |
| 76 | + canvas.width = width |
| 77 | + canvas.height = height |
| 78 | + ctx.fillStyle = '#fff' |
| 79 | + |
| 80 | + // Reactivate animation only if width > 600 |
| 81 | + const wasActive = active |
| 82 | + active = width > 600 |
| 83 | + |
| 84 | + // If animation was inactive but is now active, request next frame |
| 85 | + if (!wasActive && active) { |
| 86 | + requestAnimFrame(update) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Animation loop |
| 91 | + const update = () => { |
| 92 | + // Clear the canvas |
| 93 | + ctx.clearRect(0, 0, width, height) |
| 94 | + |
| 95 | + // Stop updating if not active |
| 96 | + if (!active) { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // Update and draw each snowflake |
| 101 | + flakes.forEach(flake => { |
| 102 | + flake.updatePosition() |
| 103 | + |
| 104 | + ctx.globalAlpha = flake.o |
| 105 | + ctx.beginPath() |
| 106 | + ctx.arc(flake.x, flake.y, flake.r, 0, Math.PI * 2) |
| 107 | + ctx.closePath() |
| 108 | + ctx.fill() |
| 109 | + |
| 110 | + // If out of view, reset the flake |
| 111 | + if (flake.isOutOfView()) { |
| 112 | + flake.reset() |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + requestAnimFrame(update) |
| 117 | + } |
| 118 | + |
| 119 | + // Initial setup |
| 120 | + onResize() |
| 121 | + window.addEventListener('resize', onResize, false) |
| 122 | + container.append(canvas) |
| 123 | +} |
| 124 | + |
| 125 | +if (document.querySelector('.banner-xmas')) { |
| 126 | + snowflakes('.banner-xmas', 200) |
| 127 | +} |
0 commit comments