Download spinning_hexagon_html_ours.html from enver/rootformer-code-v25-showcase: direct link, hf CLI and curl.
- Browser
- Download file 9.75 kB
-
https://huggingface.co/spaces/enver/rootformer-code-v25-showcase/resolve/main/spinning_hexagon_html_ours.html
- Command line
-
hf download hf://spaces/enver/rootformer-code-v25-showcase/spinning_hexagon_html_ours.html
-
curl -L -o spinning_hexagon_html_ours.html https://huggingface.co/spaces/enver/rootformer-code-v25-showcase/resolve/main/spinning_hexagon_html_ours.html
9.75 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Neon Hexagon Physics</title> | |
| <style> | |
| html, body { | |
| margin: 0; | |
| padding: 0; | |
| background: #05080f; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| min-height: 100vh; | |
| overflow: hidden; | |
| } | |
| canvas { | |
| background: #0b1120; | |
| border: 1px solid #1e293b; | |
| border-radius: 8px; | |
| box-shadow: 0 0 40px rgba(34, 211, 238, 0.15), 0 0 80px rgba(16, 185, 129, 0.08); | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <canvas id="c" width="900" height="650"></canvas> | |
| <script> | |
| (() => { | |
| "use strict"; | |
| const canvas = document.getElementById("c"); | |
| const ctx = canvas.getContext("2d"); | |
| const W = 900, H = 650; | |
| const CX = W / 2, CY = H / 2 + 10; | |
| const HEX_R = 265; | |
| // --- Constants --- | |
| const RPM = 18; | |
| const OMEGA = RPM * 2 * Math.PI / 60; // rad/s | |
| const GRAVITY = 900; // px/s^2 | |
| const WALL_RESTITUTION = 0.92; | |
| const BALL_RESTITUTION = 0.96; | |
| const AIR_DRAG = 0.9995; | |
| const SUBSTEPS = 4; | |
| const TRAIL_LEN = 16; | |
| const BALL_COUNT = 18; | |
| // --- State --- | |
| let rotation = 0; | |
| let balls = []; | |
| let lastTime = performance.now(); | |
| let fps = 60; | |
| let fpsSmooth = 60; | |
| // --- Background grid (pre-rendered) --- | |
| const bg = document.createElement("canvas"); | |
| bg.width = W; bg.height = H; | |
| { | |
| const b = bg.getContext("2d"); | |
| b.fillStyle = "#0b1120"; | |
| b.fillRect(0, 0, W, H); | |
| // subtle grid | |
| b.strokeStyle = "rgba(56, 189, 248, 0.05)"; | |
| b.lineWidth = 1; | |
| for (let x = 0; x <= W; x += 40) { | |
| b.beginPath(); b.moveTo(x + 0.5, 0); b.lineTo(x + 0.5, H); b.stroke(); | |
| } | |
| for (let y = 0; y <= H; y += 40) { | |
| b.beginPath(); b.moveTo(0, y + 0.5); b.lineTo(W, y + 0.5); b.stroke(); | |
| } | |
| // radial vignette | |
| const g = b.createRadialGradient(CX, CY, 100, CX, CY, 520); | |
| g.addColorStop(0, "rgba(11, 17, 32, 0)"); | |
| g.addColorStop(1, "rgba(2, 4, 10, 0.85)"); | |
| b.fillStyle = g; | |
| b.fillRect(0, 0, W, H); | |
| } | |
| // --- Ball factory --- | |
| const PALETTE = [ | |
| "#22d3ee", "#34d399", "#f472b6", "#facc15", "#a78bfa", | |
| "#fb7185", "#4ade80", "#60a5fa", "#fbbf24", "#2dd4bf", | |
| "#e879f9", "#f87171", "#a3e635", "#38bdf8", "#fde047", | |
| "#c084fc", "#5eead4", "#fb923c" | |
| ]; | |
| function makeBalls() { | |
| balls = []; | |
| let attempts = 0; | |
| while (balls.length < BALL_COUNT && attempts < 5000) { | |
| attempts++; | |
| const r = 9 + Math.random() * 13; | |
| const a = Math.random() * Math.PI * 2; | |
| const d = Math.random() * (HEX_R - r - 40); | |
| const x = CX + Math.cos(a) * d; | |
| const y = CY + Math.sin(a) * d; | |
| // reject if overlapping existing balls | |
| let ok = true; | |
| for (const o of balls) { | |
| const dx = o.x - x, dy = o.y - y; | |
| if (dx * dx + dy * dy < (o.r + r + 4) * (o.r + r + 4)) { ok = false; break; } | |
| } | |
| if (!ok) continue; | |
| balls.push({ | |
| x, y, | |
| vx: (Math.random() - 0.5) * 300, | |
| vy: (Math.random() - 0.5) * 300, | |
| r, | |
| m: r * r, | |
| color: PALETTE[balls.length % PALETTE.length], | |
| trail: [] | |
| }); | |
| } | |
| } | |
| makeBalls(); | |
| // --- Hexagon geometry (world space) --- | |
| function hexVerts(rot) { | |
| const v = []; | |
| for (let i = 0; i < 6; i++) { | |
| const a = rot + i * Math.PI / 3; | |
| v.push({ x: CX + Math.cos(a) * HEX_R, y: CY + Math.sin(a) * HEX_R }); | |
| } | |
| return v; | |
| } | |
| // --- Physics --- | |
| function wallVelocityAt(px, py) { | |
| // v = omega x r (2D cross: omega * (-ry, rx)) | |
| const rx = px - CX, ry = py - CY; | |
| return { x: -OMEGA * ry, y: OMEGA * rx }; | |
| } | |
| function collideWalls(ball) { | |
| const verts = hexVerts(rotation); | |
| for (let i = 0; i < 6; i++) { | |
| const a = verts[i], b = verts[(i + 1) % 6]; | |
| const ex = b.x - a.x, ey = b.y - a.y; | |
| const len = Math.hypot(ex, ey); | |
| // inward normal (points toward center) | |
| let nx = ey / len, ny = -ex / len; | |
| if (nx * (CX - a.x) + ny * (CY - a.y) < 0) { nx = -nx; ny = -ny; } | |
| const dist = (ball.x - a.x) * nx + (ball.y - a.y) * ny; | |
| if (dist < ball.r) { | |
| // push out | |
| const pen = ball.r - dist; | |
| ball.x += nx * pen; | |
| ball.y += ny * pen; | |
| // contact point & wall velocity there | |
| const cxp = ball.x - nx * ball.r; | |
| const cyp = ball.y - ny * ball.r; | |
| const wv = wallVelocityAt(cxp, cyp); | |
| // relative velocity | |
| let rvx = ball.vx - wv.x; | |
| let rvy = ball.vy - wv.y; | |
| const vn = rvx * nx + rvy * ny; | |
| if (vn < 0) { | |
| rvx -= (1 + WALL_RESTITUTION) * vn * nx; | |
| rvy -= (1 + WALL_RESTITUTION) * vn * ny; | |
| ball.vx = rvx + wv.x; | |
| ball.vy = rvy + wv.y; | |
| } | |
| } | |
| } | |
| } | |
| function collideBalls() { | |
| for (let i = 0; i < balls.length; i++) { | |
| for (let j = i + 1; j < balls.length; j++) { | |
| const a = balls[i], b = balls[j]; | |
| const dx = b.x - a.x, dy = b.y - a.y; | |
| const minD = a.r + b.r; | |
| const d2 = dx * dx + dy * dy; | |
| if (d2 >= minD * minD || d2 === 0) continue; | |
| const d = Math.sqrt(d2); | |
| const nx = dx / d, ny = dy / d; | |
| // positional correction (mass-weighted) | |
| const overlap = minD - d; | |
| const total = a.m + b.m; | |
| a.x -= nx * overlap * (b.m / total); | |
| a.y -= ny * overlap * (b.m / total); | |
| b.x += nx * overlap * (a.m / total); | |
| b.y += ny * overlap * (a.m / total); | |
| // elastic impulse | |
| const rvx = b.vx - a.vx, rvy = b.vy - a.vy; | |
| const vn = rvx * nx + rvy * ny; | |
| if (vn < 0) { | |
| const jImp = -(1 + BALL_RESTITUTION) * vn / (1 / a.m + 1 / b.m); | |
| const ix = jImp * nx, iy = jImp * ny; | |
| a.vx -= ix / a.m; a.vy -= iy / a.m; | |
| b.vx += ix / b.m; b.vy += iy / b.m; | |
| } | |
| } | |
| } | |
| } | |
| function step(dt) { | |
| const h = dt / SUBSTEPS; | |
| for (let s = 0; s < SUBSTEPS; s++) { | |
| rotation += OMEGA * h; | |
| for (const ball of balls) { | |
| ball.vy += GRAVITY * h; | |
| ball.vx *= AIR_DRAG; | |
| ball.vy *= AIR_DRAG; | |
| ball.x += ball.vx * h; | |
| ball.y += ball.vy * h; | |
| collideWalls(ball); | |
| } | |
| collideBalls(); | |
| } | |
| // record trails | |
| for (const ball of balls) { | |
| ball.trail.push({ x: ball.x, y: ball.y }); | |
| if (ball.trail.length > TRAIL_LEN) ball.trail.shift(); | |
| } | |
| } | |
| // --- Rendering --- | |
| function drawHexagon() { | |
| const verts = hexVerts(rotation); | |
| ctx.save(); | |
| ctx.lineJoin = "round"; | |
| // outer glow pass | |
| ctx.shadowColor = "#22d3ee"; | |
| ctx.shadowBlur = 28; | |
| ctx.strokeStyle = "rgba(34, 211, 238, 0.9)"; | |
| ctx.lineWidth = 4; | |
| ctx.beginPath(); | |
| verts.forEach((v, i) => i === 0 ? ctx.moveTo(v.x, v.y) : ctx.lineTo(v.x, v.y)); | |
| ctx.closePath(); | |
| ctx.stroke(); | |
| // inner emerald pass | |
| ctx.shadowColor = "#34d399"; | |
| ctx.shadowBlur = 14; | |
| ctx.strokeStyle = "rgba(52, 211, 153, 0.55)"; | |
| ctx.lineWidth = 1.5; | |
| ctx.stroke(); | |
| // vertex nodes | |
| ctx.shadowBlur = 18; | |
| ctx.fillStyle = "#a7f3d0"; | |
| for (const v of verts) { | |
| ctx.beginPath(); | |
| ctx.arc(v.x, v.y, 4, 0, Math.PI * 2); | |
| ctx.fill(); | |
| } | |
| ctx.restore(); | |
| } | |
| function drawBalls() { | |
| ctx.save(); | |
| ctx.lineCap = "round"; | |
| for (const ball of balls) { | |
| // trail | |
| const t = ball.trail; | |
| for (let i = 1; i < t.length; i++) { | |
| const alpha = (i / t.length) * 0.45; | |
| ctx.strokeStyle = ball.color; | |
| ctx.globalAlpha = alpha; | |
| ctx.lineWidth = ball.r * 0.9 * (i / t.length); | |
| ctx.beginPath(); | |
| ctx.moveTo(t[i - 1].x, t[i - 1].y); | |
| ctx.lineTo(t[i].x, t[i].y); | |
| ctx.stroke(); | |
| } | |
| ctx.globalAlpha = 1; | |
| // glow body | |
| ctx.shadowColor = ball.color; | |
| ctx.shadowBlur = 18; | |
| ctx.fillStyle = ball.color; | |
| ctx.beginPath(); | |
| ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2); | |
| ctx.fill(); | |
| // hot core | |
| ctx.shadowBlur = 0; | |
| ctx.fillStyle = "rgba(255,255,255,0.85)"; | |
| ctx.beginPath(); | |
| ctx.arc(ball.x - ball.r * 0.25, ball.y - ball.r * 0.25, ball.r * 0.35, 0, Math.PI * 2); | |
| ctx.fill(); | |
| } | |
| ctx.restore(); | |
| } | |
| function totalKE() { | |
| let ke = 0; | |
| for (const b of balls) ke += 0.5 * b.m * (b.vx * b.vx + b.vy * b.vy); | |
| return ke; | |
| } | |
| function drawHUD() { | |
| const ke = totalKE(); | |
| const lines = [ | |
| ["FPS", fpsSmooth.toFixed(1)], | |
| ["RPM", RPM.toFixed(0)], | |
| ["BALLS", balls.length.toString()], | |
| ["KE", ke >= 1e6 ? (ke / 1e6).toFixed(2) + " MJ" : (ke / 1e3).toFixed(1) + " kJ"] | |
| ]; | |
| ctx.save(); | |
| ctx.font = "600 14px 'Courier New', monospace"; | |
| ctx.textBaseline = "top"; | |
| // panel | |
| ctx.fillStyle = "rgba(8, 14, 28, 0.72)"; | |
| ctx.strokeStyle = "rgba(34, 211, 238, 0.45)"; | |
| ctx.lineWidth = 1; | |
| const px = 14, py = 14, pw = 178, ph = 104; | |
| ctx.beginPath(); | |
| ctx.roundRect ? ctx.roundRect(px, py, pw, ph, 6) : ctx.rect(px, py, pw, ph); | |
| ctx.fill(); | |
| ctx.stroke(); | |
| lines.forEach((row, i) => { | |
| const y = py + 12 + i * 21; | |
| ctx.fillStyle = "#67e8f9"; | |
| ctx.fillText(row[0], px + 12, y); | |
| ctx.fillStyle = "#e2e8f0"; | |
| ctx.textAlign = "right"; | |
| ctx.fillText(row[1], px + pw - 12, y); | |
| ctx.textAlign = "left"; | |
| }); | |
| ctx.restore(); | |
| } | |
| // --- Main loop --- | |
| function frame(now) { | |
| let dt = (now - lastTime) / 1000; | |
| lastTime = now; | |
| if (dt > 0.05) dt = 0.05; // clamp after tab-switch | |
| if (dt > 0) { | |
| const inst = 1 / dt; | |
| fpsSmooth += (inst - fpsSmooth) * 0.08; | |
| } | |
| step(dt); | |
| ctx.drawImage(bg, 0, 0); | |
| drawHexagon(); | |
| drawBalls(); | |
| drawHUD(); | |
| requestAnimationFrame(frame); | |
| } | |
| requestAnimationFrame(frame); | |
| })(); | |
| </script> | |
| </body> | |
| </html> |