1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| (function () { class RainDrop { constructor(params) { const { width, height, ctx, canvas, speed, rangeY, targetY, color, rainDropWidth, rainDropHeight, rainDropXScale, rainDropYScale, rainDropXExpandOffset, rainDropYExpendOffset, } = params; this.params = params; this.x = Math.random() * width; this.y = rangeY[0] + Math.random() * (rangeY[1] - rangeY[0]); this.complete = false; this.width = rainDropWidth[0] + Math.random() * (rainDropWidth[1] - rainDropWidth[0]); this.height = rainDropHeight[0] + Math.random() * (rainDropHeight[1] - rainDropHeight[0]); this.targetWidth = this.width * rainDropXScale; this.targetHeight = this.height / rainDropYScale; this.speed = speed[0] + Math.random() * (speed[1] - speed[0]); this.targetY = targetY[0] + Math.random() * (targetY[1] - targetY[0]); this.color = color; }
expand() { if (this.height > this.targetHeight) { return (this.height -= this.params.rainDropYExpendOffset); } if (this.width < this.targetWidth) { return (this.width += this.params.rainDropXExpandOffset); } this.complete = true; }
animate() { if (this.y >= this.targetY) { this.expand(); } else { this.y += this.speed; } } }
class Rain { constructor(params) { this.drops = []; if (!params.canvas) { this.canvas = document.createElement("canvas"); this.ctx = this.canvas.getContext("2d"); } else { this.canvas = params.canvas; this.ctx = this.canvas.getContext("2d"); } this.params = { width: window.innerWidth, height: window.innerHeight, rainDropWidth: [1, 5], rainDropHeight: [2, 12], rainDropYScale: 5, rainDropXScale: 5, rainDropXExpandOffset: 1.5, rainDropYExpendOffset: 1.5, speed: [4, 12], targetY: [window.innerHeight * 0.7, window.innerHeight * 0.9], rangeY: [0, 100], color: "#05a2eb", ...params, }; this.canvas.width = this.params.width; this.canvas.height = this.params.height; this.canvas.style.zIndex = -9999; this.canvas.style.pointerEvents = "none"; this.canvas.style.backgroundColor = "rgba(0, 0, 0, 1)"; document.body.appendChild(this.canvas); }
drawDrop(drop) { this.ctx.beginPath(); this.ctx.fillStyle = drop.color; this.ctx.fillRect( drop.x - drop.width / 2, drop.y - drop.height, drop.width, drop.height ); this.ctx.closePath(); }
animate() { this.ctx.clearRect(0, 0, this.params.width, this.params.height); this.drops.forEach((drop) => { this.drawDrop(drop); drop.animate(); }); this.drops = this.drops.filter((drop) => !drop.complete); }
init() { setInterval(() => { this.drops.push(new RainDrop(this.params)); }, 20); } }
const rain = new Rain({ width: window.innerWidth, height: window.innerHeight, rainDropWidth: [1, 5], rainDropHeight: [2, 12], rainDropYScale: 5, rainDropXScale: 5, rainDropXExpandOffset: 1.5, rainDropYExpendOffset: 1.5, speed: [4, 12], targetY: [window.innerHeight * 0.7, window.innerHeight * 0.9], rangeY: [0, 100], color: "#05a2eb", canvas: null, }); rain.init(); const render = () => { rain.animate(); requestAnimationFrame(render); }; render(); })();
|