Кометы по многоугольникам
Опубликовано 27 ноября, 2026 - 11:02 пользователем Бастрыкин Валентин
Анимация на canvas: 13 вложенных правильных многоугольников, от 15-угольника до треугольника. По периметру каждого бежит светящаяся комета с затухающим хвостом, и чем ближе кольцо к центру, тем быстрее оно вращается
JS-исходник:
<canvas id="drawing" width=800 height=800></canvas> <script> const canvas = document.getElementById('drawing'); const ctx = canvas.getContext('2d'); const W = canvas.width, H = canvas.height; const CX = W / 2, CY = H / 2; const RINGS = 13; const MAX_R = 260; const R_STEP = 17; let time = 0; const colors = [ '#00e5ff', '#00b8ff', '#4d7dff', '#7d5cff', '#a84dff', '#d13dff', '#f03df0', '#ff4dc4', '#ff6b8a', '#ff8a5c', '#ffb84d', '#d1ff4d', '#4dffa6' ]; const laps = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; function sideDist(r, n, rot, a) { const step = Math.PI * 2 / n; let t = ((a - rot) % step + step) % step; return r * Math.cos(Math.PI / n) / Math.cos(t - step / 2); } function pointOnPolygon(r, n, rot, a) { const d = sideDist(r, n, rot, a); return { x: CX + d * Math.cos(a), y: CY + d * Math.sin(a) }; } function drawTrail(r, n, rot, headAngle, tailLength, color) { const steps = 60; const pts = []; for (let i = 0; i <= steps; i++) { const a = headAngle - (i / steps) * tailLength; pts.push(pointOnPolygon(r, n, rot, a)); } for (let i = 0; i < steps; i++) { const t = 1 - i / steps; const alpha = t * t; ctx.beginPath(); ctx.moveTo(pts[i].x, pts[i].y); ctx.lineTo(pts[i + 1].x, pts[i + 1].y); ctx.strokeStyle = color; ctx.globalAlpha = alpha; ctx.lineWidth = 0.5 + 4.5 * alpha; ctx.lineCap = 'round'; ctx.stroke(); } ctx.globalAlpha = 1; return pts[0]; } function frame() { time += 0.001; ctx.fillStyle = '#06060c'; ctx.fillRect(0, 0, W, H); ctx.globalCompositeOperation = 'lighter'; for (let k = 0; k < RINGS; k++) { const n = 15 - k; let rot = -Math.PI / 2; if (n % 2 === 0) rot += Math.PI / n; const r = MAX_R - k * R_STEP; const color = colors[k]; ctx.beginPath(); for (let i = 0; i <= n; i++) { const a = rot + i * Math.PI * 2 / n; const x = CX + r * Math.cos(a); const y = CY + r * Math.sin(a); i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y); } ctx.strokeStyle = color; ctx.globalAlpha = 0.08; ctx.lineWidth = 1; ctx.stroke(); ctx.globalAlpha = 1; const headAngle = Math.PI / 4 + time * laps[k] * Math.PI * 2; const tailLength = Math.PI * 1.1; const head = drawTrail(r, n, rot, headAngle, tailLength, color); ctx.beginPath(); ctx.arc(head.x, head.y, 12, 0, Math.PI * 2); ctx.fillStyle = color; ctx.globalAlpha = 0.25; ctx.fill(); ctx.beginPath(); ctx.arc(head.x, head.y, 5, 0, Math.PI * 2); ctx.fillStyle = color; ctx.globalAlpha = 1; ctx.fill(); ctx.beginPath(); ctx.arc(head.x, head.y, 2, 0, Math.PI * 2); ctx.fillStyle = '#ffffff'; ctx.fill(); } ctx.globalCompositeOperation = 'source-over'; requestAnimationFrame(frame); } frame(); </script>
