Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

11.8 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bouncing Balls in a Spinning Hexagon</title>
<style>
  :root {
    --bg: #0b0f1a;
    --panel: #131a2b;
    --accent: #5eead4;
    --text: #e6edf3;
    --muted: #8b98b8;
  }
  * { box-sizing: border-box; }
  html, body {
    margin: 0;
    height: 100%;
    background: radial-gradient(1200px 800px at 50% -10%, #16203a 0%, var(--bg) 60%);
    color: var(--text);
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
  }
  .app {
    display: grid;
    grid-template-rows: auto 1fr auto;
    height: 100%;
  }
  header {
    padding: 14px 20px 6px;
    display: flex;
    align-items: baseline;
    gap: 12px;
    flex-wrap: wrap;
  }
  header h1 {
    font-size: 18px;
    margin: 0;
    letter-spacing: 0.3px;
    font-weight: 650;
  }
  header p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
  }
  .stage {
    position: relative;
    display: grid;
    place-items: center;
    min-height: 0;
  }
  canvas {
    display: block;
    border-radius: 16px;
    background: linear-gradient(180deg, rgba(255,255,255,0.02), rgba(0,0,0,0.15));
    box-shadow: 0 20px 60px rgba(0,0,0,0.45), inset 0 0 0 1px rgba(255,255,255,0.04);
  }
  .hud {
    position: absolute;
    top: 16px;
    left: 16px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    pointer-events: none;
  }
  .chip {
    background: rgba(19,26,43,0.7);
    border: 1px solid rgba(255,255,255,0.08);
    color: var(--text);
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    backdrop-filter: blur(6px);
  }
  .chip b { color: var(--accent); font-variant-numeric: tabular-nums; }
  footer {
    padding: 10px 16px 18px;
    display: flex;
    gap: 18px;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    background: linear-gradient(0deg, rgba(0,0,0,0.25), transparent);
  }
  .control {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--muted);
  }
  .control input[type="range"] { accent-color: var(--accent); width: 120px; }
  .control input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
  button {
    background: var(--accent);
    color: #06231f;
    border: none;
    padding: 8px 14px;
    border-radius: 10px;
    font-weight: 650;
    cursor: pointer;
    font-size: 13px;
    transition: transform .08s ease, filter .15s ease;
  }
  button:hover { filter: brightness(1.08); }
  button:active { transform: translateY(1px); }
  button.ghost {
    background: transparent;
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.18);
  }
</style>
</head>
<body>
<div class="app">
  <header>
    <h1>Bouncing Balls in a Spinning Hexagon</h1>
    <p>Real-time physics · elastic collisions · rotating walls</p>
  </header>

  <div class="stage">
    <canvas id="c"></canvas>
    <div class="hud">
      <span class="chip">FPS <b id="fps">–</b></span>
      <span class="chip">Balls <b id="count">10</b></span>
      <span class="chip">Spin <b id="spinVal">0.6</b> rad/s</span>
    </div>
  </div>

  <footer>
    <button id="reset">Reset</button>
    <button id="pause" class="ghost">Pause</button>
    <div class="control">
      <label for="spin">Spin</label>
      <input id="spin" type="range" min="-2" max="2" step="0.05" value="0.6" />
    </div>
    <div class="control">
      <label for="grav">Gravity</label>
      <input id="grav" type="range" min="0" max="1200" step="10" value="500" />
    </div>
    <div class="control">
      <label for="rest">Bounciness</label>
      <input id="rest" type="range" min="0.5" max="1" step="0.01" value="0.92" />
    </div>
    <div class="control">
      <label for="trails">Trails</label>
      <input id="trails" type="checkbox" checked />
    </div>
  </footer>
</div>

<script>
(() => {
  const canvas = document.getElementById('c');
  const ctx = canvas.getContext('2d');

  // ---- Config / state ----
  const cfg = {
    spin: 0.6,          // rad/s
    gravity: 500,       // px/s^2
    restitution: 0.92,  // wall bounciness
    ballRestitution: 0.95,
    trails: true,
    paused: false,
    count: 10,
  };

  const hex = {
    cx: 0, cy: 0,
    radius: 240,   // circumradius (center -> vertex)
    angle: 0,      // current rotation
  };

  let balls = [];
  const palette = [
    '#f87171','#fb923c','#fbbf24','#a3e635','#34d399',
    '#22d3ee','#60a5fa','#a78bfa','#f472b6','#e879f9',
  ];

  // ---- Sizing ----
  let dpr = Math.max(1, Math.min(2, window.devicePixelRatio || 1));
  function resize() {
    const stage = canvas.parentElement;
    const rect = stage.getBoundingClientRect();
    const size = Math.max(280, Math.min(rect.width, rect.height) - 24);
    canvas.style.width = size + 'px';
    canvas.style.height = size + 'px';
    canvas.width = Math.round(size * dpr);
    canvas.height = Math.round(size * dpr);
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    hex.cx = size / 2;
    hex.cy = size / 2;
    hex.radius = size * 0.42;
  }

  // ---- Hexagon geometry ----
  function hexVertices() {
    const v = [];
    for (let i = 0; i < 6; i++) {
      const a = hex.angle + i * Math.PI / 3;
      v.push({
        x: hex.cx + hex.radius * Math.cos(a),
        y: hex.cy + hex.radius * Math.sin(a),
      });
    }
    return v;
  }

  // ---- Ball creation ----
  function makeBall(i) {
    const r = 10 + Math.random() * 12;
    // spawn inside the inscribed circle so it never starts outside a wall
    const maxR = hex.radius * Math.cos(Math.PI / 6) - r - 4;
    const a = Math.random() * Math.PI * 2;
    const d = Math.random() * maxR;
    return {
      x: hex.cx + Math.cos(a) * d,
      y: hex.cy + Math.sin(a) * d,
      vx: (Math.random() * 2 - 1) * 260,
      vy: (Math.random() * 2 - 1) * 260,
      r,
      color: palette[i % palette.length],
    };
  }

  function reset() {
    balls = [];
    for (let i = 0; i < cfg.count; i++) balls.push(makeBall(i));
  }

  // ---- Physics ----
  function collideWalls(b, dt) {
    const verts = hexVertices();
    for (let i = 0; i < 6; i++) {
      const a = verts[i];
      const c = verts[(i + 1) % 6];
      const ex = c.x - a.x, ey = c.y - a.y;
      const len = Math.hypot(ex, ey) || 1;
      // inward normal (hexagon center is inside)
      let nx = -ey / len, ny = ex / len;
      const midx = (a.x + c.x) / 2, midy = (a.y + c.y) / 2;
      if ((hex.cx - midx) * nx + (hex.cy - midy) * ny < 0) { nx = -nx; ny = -ny; }

      // signed distance from ball center to wall plane (positive = inside)
      const dist = (b.x - a.x) * nx + (b.y - a.y) * ny;
      if (dist < b.r) {
        // velocity of the wall point due to rotation (v = omega x r)
        const rx = b.x - hex.cx, ry = b.y - hex.cy;
        const wvx = -cfg.spin * ry;
        const wvy =  cfg.spin * rx;

        // relative velocity
        let rvx = b.vx - wvx;
        let rvy = b.vy - wvy;
        const vn = rvx * nx + rvy * ny; // component along normal

        if (vn < 0) {
          // reflect relative velocity, then add wall velocity back
          const j = -(1 + cfg.restitution) * vn;
          rvx += j * nx;
          rvy += j * ny;
          b.vx = rvx + wvx;
          b.vy = rvy + wvy;
        }
        // push out of penetration
        const pen = b.r - dist;
        b.x += nx * pen;
        b.y += ny * pen;
      }
    }
  }

  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];
        let dx = b.x - a.x, dy = b.y - a.y;
        let d = Math.hypot(dx, dy);
        const min = a.r + b.r;
        if (d === 0) { d = 0.01; dx = 0.01; }
        if (d < min) {
          const nx = dx / d, ny = dy / d;
          const overlap = (min - d) / 2;
          a.x -= nx * overlap; a.y -= ny * overlap;
          b.x += nx * overlap; b.y += ny * overlap;

          const rvx = b.vx - a.vx, rvy = b.vy - a.vy;
          const vn = rvx * nx + rvy * ny;
          if (vn < 0) {
            const ma = a.r * a.r, mb = b.r * b.r; // mass ~ area
            const jImp = -(1 + cfg.ballRestitution) * vn / (1 / ma + 1 / mb);
            const ix = jImp * nx, iy = jImp * ny;
            a.vx -= ix / ma; a.vy -= iy / ma;
            b.vx += ix / mb; b.vy += iy / mb;
          }
        }
      }
    }
  }

  function step(dt) {
    hex.angle += cfg.spin * dt;
    for (const b of balls) {
      b.vy += cfg.gravity * dt;
      b.x += b.vx * dt;
      b.y += b.vy * dt;
      collideWalls(b, dt);
    }
    collideBalls();
    // safety: keep strays inside
    for (const b of balls) {
      const dx = b.x - hex.cx, dy = b.y - hex.cy;
      const d = Math.hypot(dx, dy);
      const maxD = hex.radius * Math.cos(Math.PI / 6) - b.r;
      if (d > maxD) {
        const nx = dx / (d || 1), ny = dy / (d || 1);
        b.x = hex.cx + nx * maxD;
        b.y = hex.cy + ny * maxD;
      }
    }
  }

  // ---- Render ----
  function draw() {
    const w = canvas.clientWidth, h = canvas.clientHeight;
    if (cfg.trails) {
      ctx.fillStyle = 'rgba(11,15,26,0.22)';
      ctx.fillRect(0, 0, w, h);
    } else {
      ctx.clearRect(0, 0, w, h);
    }

    // hexagon
    const v = hexVertices();
    ctx.beginPath();
    ctx.moveTo(v[0].x, v[0].y);
    for (let i = 1; i < 6; i++) ctx.lineTo(v[i].x, v[i].y);
    ctx.closePath();
    ctx.lineWidth = 3;
    ctx.strokeStyle = 'rgba(94,234,212,0.9)';
    ctx.shadowColor = 'rgba(94,234,212,0.5)';
    ctx.shadowBlur = 18;
    ctx.stroke();
    ctx.shadowBlur = 0;

    // balls
    for (const b of balls) {
      const g = ctx.createRadialGradient(
        b.x - b.r * 0.35, b.y - b.r * 0.35, b.r * 0.15,
        b.x, b.y, b.r
      );
      g.addColorStop(0, '#ffffff');
      g.addColorStop(0.25, b.color);
      g.addColorStop(1, shade(b.color, -0.35));
      ctx.beginPath();
      ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2);
      ctx.fillStyle = g;
      ctx.shadowColor = b.color;
      ctx.shadowBlur = 12;
      ctx.fill();
      ctx.shadowBlur = 0;
    }
  }

  function shade(hex, amt) {
    const c = hex.replace('#','');
    const num = parseInt(c, 16);
    let r = (num >> 16) & 255, g = (num >> 8) & 255, bl = num & 255;
    r = Math.max(0, Math.min(255, Math.round(r + 255 * amt)));
    g = Math.max(0, Math.min(255, Math.round(g + 255 * amt)));
    bl = Math.max(0, Math.min(255, Math.round(bl + 255 * amt)));
    return `rgb(${r},${g},${bl})`;
  }

  // ---- Loop ----
  let last = performance.now();
  let fpsAcc = 0, fpsCount = 0, fpsTimer = 0;
  const fpsEl = document.getElementById('fps');

  function frame(now) {
    let dt = (now - last) / 1000;
    last = now;
    if (dt > 0.05) dt = 0.05; // clamp big gaps

    if (!cfg.paused) {
      // substeps for stable collisions
      const sub = 4;
      for (let s = 0; s < sub; s++) step(dt / sub);
    }
    draw();

    // fps
    fpsAcc += dt; fpsCount++; fpsTimer += dt;
    if (fpsTimer >= 0.5) {
      fpsEl.textContent = Math.round(fpsCount / fpsAcc);
      fpsAcc = 0; fpsCount = 0; fpsTimer = 0;
    }
    requestAnimationFrame(frame);
  }

  // ---- Controls ----
  const $ = id => document.getElementById(id);
  $('spin').addEventListener('input', e => {
    cfg.spin = parseFloat(e.target.value);
    $('spinVal').textContent = cfg.spin.toFixed(2);
  });
  $('grav').addEventListener('input', e => cfg.gravity = parseFloat(e.target.value));
  $('rest').addEventListener('input', e => cfg.restitution = parseFloat(e.target.value));
  $('trails').addEventListener('change', e => cfg.trails = e.target.checked);
  $('reset').addEventListener('click', reset);
  $('pause').addEventListener('click', e => {
    cfg.paused = !cfg.paused;
    e.target.textContent = cfg.paused ? 'Play' : 'Pause';
  });

  window.addEventListener('resize', () => { resize(); });

  // ---- Init ----
  resize();
  reset();
  requestAnimationFrame(frame);
})();
</script>
</body>
</html>