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 159 160 161 162 163 164 165 166 167 168 169
| function getRandom(min, max) { return Math.floor(Math.random() * (max + 1 - min) + min); }
function getCurrentTimeString() { return new Date().toTimeString().slice(0, 8); }
class ParticleClick { constructor(el) { this.canvas = document.querySelector(el); this.ctx = this.canvas.getContext("2d", { willReadFrequently: true, }); this.text = null; this.particles = []; this.init(); } init() { this.canvas.width = window.innerWidth * devicePixelRatio; this.canvas.height = window.innerHeight * devicePixelRatio; } clear() { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); } draw() { this.clear(); this.drawText(); this.particles.forEach((p) => p.draw()); requestAnimationFrame(() => this.draw()); } drawText() { const { ctx, canvas: { width, height }, } = this; const newText = getCurrentTimeString() if (newText === this.text) { return } this.text = newText ctx.beginPath(); ctx.fillStyle = "#000"; ctx.textBaseline = "middle"; ctx.font = "140px sans-serif"; ctx.fillText( this.text, (width - ctx.measureText(this.text).width) / 2, height / 2 );
const points = this.getPoints(); this.clear(); for (let i = 0; i < points.length; i++) { let p = this.particles[i]; if (!p) { p = new Particle(this.canvas, this.ctx); this.particles.push(p); } const [x, y] = points[i]; p.moveTo(x, y); }
if (points.length < this.particles.length) { this.particles.splice(points.length); } } getPoints() { const gap = 6; const { ctx, canvas: { width, height }, } = this; const { data } = ctx.getImageData(0, 0, width, height); const points = []; for (let i = 0; i < width; i += gap) { for (let j = 0; j < height; j += gap) { const index = (i + j * width) * 4; const r = data[index]; const g = data[index + 1]; const b = data[index + 2]; const a = data[index + 3]; if (r === 0 && g === 0 && b === 0 && a === 255) { points.push([i, j]); } } } return points; } }
class Particle {
constructor(canvas, ctx) { this.canvas = canvas; this.ctx = ctx; const r = Math.min(canvas.width, canvas.height) / 2; const cx = canvas.width / 2; const cy = canvas.height / 2; const rad = (getRandom(0, 360) / 180) * Math.PI; this.x = cx + r * Math.cos(rad); this.y = cy + r * Math.sin(rad); this.size = getRandom(4, 8); } draw() { const { ctx } = this; ctx.beginPath(); ctx.fillStyle = "#5445544d"; ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fill(); }
moveTo(dx, dy) { const duration = 500; const sx = this.x, sy = this.y; const xSpeed = (dx - sx) / duration; const ySpeed = (dy - sy) / duration; const startTime = Date.now(); const _move = () => { const elapsedTime = Date.now() - startTime; this.x = sx + xSpeed * elapsedTime; this.y = sy + ySpeed * elapsedTime; if (elapsedTime >= duration) { return; } requestAnimationFrame(_move); }; _move(); } }
const clock = new ParticleClick("canvas"); clock.draw();
|