|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function randomBetween(min: number, max: number) { |
| 4 | + return min + Math.random() * (max - min); |
| 5 | +} |
| 6 | + |
| 7 | +class Snowflake { |
| 8 | + alpha = 0; |
| 9 | + radius = 0; |
| 10 | + x = 0; |
| 11 | + y = 0; |
| 12 | + |
| 13 | + private _vx = 0; |
| 14 | + private _vy = 0; |
| 15 | + |
| 16 | + constructor() { |
| 17 | + this.reset(); |
| 18 | + } |
| 19 | + |
| 20 | + reset() { |
| 21 | + this.alpha = randomBetween(0.1, 0.9); |
| 22 | + this.radius = randomBetween(1, 4); |
| 23 | + this.x = randomBetween(0, window.innerWidth); |
| 24 | + this.y = randomBetween(0, -window.innerHeight); |
| 25 | + this._vx = randomBetween(-3, 3); |
| 26 | + this._vy = randomBetween(2, 5); |
| 27 | + } |
| 28 | + |
| 29 | + update() { |
| 30 | + this.x += this._vx; |
| 31 | + this.y += this._vy; |
| 32 | + |
| 33 | + if (this.y + this.radius > window.innerHeight) { |
| 34 | + this.reset(); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export class Snow { |
| 40 | + snowing = false; |
| 41 | + |
| 42 | + private readonly _canvas: any; |
| 43 | + private readonly _ctx: any; |
| 44 | + private _height: number = 0; |
| 45 | + private _width: number = 0; |
| 46 | + private readonly _snowflakes: Snowflake[] = []; |
| 47 | + |
| 48 | + private readonly _clearBound: any; |
| 49 | + private readonly _updateBound: any; |
| 50 | + |
| 51 | + constructor() { |
| 52 | + this._clearBound = this.clear.bind(this); |
| 53 | + this._updateBound = this.update.bind(this); |
| 54 | + |
| 55 | + this._canvas = document.querySelector('canvas.snow'); |
| 56 | + this._ctx = this._canvas.getContext('2d'); |
| 57 | + |
| 58 | + const trigger = document.querySelector('.snow__trigger'); |
| 59 | + if (trigger) { |
| 60 | + trigger.addEventListener('click', () => this.onToggle()); |
| 61 | + } |
| 62 | + |
| 63 | + window.addEventListener('resize', () => this.onResize()); |
| 64 | + this.onResize(); |
| 65 | + |
| 66 | + this.onToggle(); |
| 67 | + } |
| 68 | + |
| 69 | + onToggle() { |
| 70 | + this.snowing = !this.snowing; |
| 71 | + if (this.snowing) { |
| 72 | + this.createSnowflakes(); |
| 73 | + requestAnimationFrame(this._updateBound); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + onResize() { |
| 78 | + this._height = window.innerHeight; |
| 79 | + this._width = window.innerWidth; |
| 80 | + this._canvas.width = this._width; |
| 81 | + this._canvas.height = this._height; |
| 82 | + } |
| 83 | + |
| 84 | + clear() { |
| 85 | + this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); |
| 86 | + this._snowflakes.length = 0; |
| 87 | + } |
| 88 | + |
| 89 | + createSnowflakes() { |
| 90 | + const flakes = window.innerWidth / 4; |
| 91 | + |
| 92 | + for (let i = 0; i < flakes; i++) { |
| 93 | + this._snowflakes.push(new Snowflake()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + update() { |
| 98 | + this._ctx.clearRect(0, 0, this._width, this._height); |
| 99 | + |
| 100 | + const color = document.body.classList.contains('vscode-light') ? '#424242' : '#fff'; |
| 101 | + |
| 102 | + for (const flake of this._snowflakes) { |
| 103 | + flake.update(); |
| 104 | + |
| 105 | + this._ctx.save(); |
| 106 | + this._ctx.fillStyle = color; |
| 107 | + this._ctx.beginPath(); |
| 108 | + this._ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2); |
| 109 | + this._ctx.closePath(); |
| 110 | + this._ctx.globalAlpha = flake.alpha; |
| 111 | + this._ctx.fill(); |
| 112 | + this._ctx.restore(); |
| 113 | + } |
| 114 | + |
| 115 | + requestAnimationFrame(this.snowing ? this._updateBound : this._clearBound); |
| 116 | + } |
| 117 | +} |
0 commit comments