Lava lamp v1 · OpenCode21.11.2026 07:24
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"><title>Lava Lamp</title><style> :root { --bg-top: #2b2437; --bg-bottom: #17121f; --desk: #6b4a2f; --desk-dark: #4e3421; } * { box-sizing: border-box; margin: 0; padding: 0; } html, body { height: 100%; } body { background: radial-gradient(120% 90% at 50% 0%, var(--bg-top) 0%, var(--bg-bottom) 70%); display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; color: #f3ece2; overflow: hidden; padding: 12px; gap: 10px; } h1 { font-size: clamp(0.9rem, 4vw, 1.2rem); letter-spacing: 0.35em; text-transform: uppercase; font-weight: 600; opacity: 0.85; } .stage { position: relative; height: min(72vh, 78vw * 1.6); aspect-ratio: 3 / 4; max-height: 560px; } canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; border-radius: 18px; box-shadow: 0 18px 50px rgba(0,0,0,0.55); } .controls { display: flex; align-items: center; gap: 12px; } .switch { display: inline-flex; align-items: center; gap: 10px; background: #241c2e; border: 1px solid rgba(255,255,255,0.14); border-radius: 999px; padding: 8px 14px 8px 10px; cursor: pointer; color: #f3ece2; font: inherit; font-size: 0.85rem; user-select: none; transition: border-color 0.2s, transform 0.1s; } .switch:active { transform: scale(0.96); } .switch:focus-visible { outline: 2px solid #ffb347; outline-offset: 3px; } .track { width: 52px; height: 28px; border-radius: 999px; background: #3a2f45; position: relative; transition: background 0.25s; box-shadow: inset 0 2px 5px rgba(0,0,0,0.5); flex: none; } .thumb { position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #d8cfc4; transition: transform 0.25s cubic-bezier(.6,-0.2,.3,1.4), background 0.25s; box-shadow: 0 2px 4px rgba(0,0,0,0.5); } .switch[aria-pressed="true"] .track { background: #b4501f; } .switch[aria-pressed="true"] .thumb { transform: translateX(24px); background: #ffd9a0; box-shadow: 0 0 10px rgba(255,170,80,0.9); } .state-label { min-width: 2.2em; text-align: left; opacity: 0.85; } .hint { font-size: 0.72rem; opacity: 0.55; letter-spacing: 0.05em; }</style></head><body> <h1>Lava Lamp</h1> <div class="stage"> <canvas id="scene" aria-label="Animated lava lamp on a desk"></canvas> </div> <div class="controls"> <button class="switch" id="lampSwitch" aria-pressed="true"> <span class="track"><span class="thumb"></span></span> <span class="state-label" id="stateLabel">On</span> </button> </div> <p class="hint">tap the switch to toggle the lamp</p><script>(() => { const canvas = document.getElementById('scene'); const ctx = canvas.getContext('2d'); // Offscreen canvases for metaball rendering const offA = document.createElement('canvas'); // blob silhouettes const offB = document.createElement('canvas'); // filtered + colored const offActx = offA.getContext('2d'); const offBctx = offB.getContext('2d'); const state = { on: true, glow: 1, // smoothed lamp glow 0..1 time: 0, blobs: [], W: 0, H: 0, dpr: 1, glass: null, // {x,y,w,h,r} }; // ---------- sizing ---------- function resize() { const rect = canvas.getBoundingClientRect(); state.dpr = Math.min(window.devicePixelRatio || 1, 2); state.W = Math.max(1, Math.round(rect.width * state.dpr)); state.H = Math.max(1, Math.round(rect.height * state.dpr)); canvas.width = state.W; canvas.height = state.H; offA.width = offB.width = state.W; offA.height = offB.height = state.H; const W = state.W, H = state.H; const lampH = H * 0.74; const lampW = Math.min(W * 0.46, lampH * 0.42); const baseH = H * 0.055; const glassH = lampH - baseH - H * 0.045; // cap height const glassW = lampW * 0.62; state.glass = { x: (W - glassW) / 2, y: H * 0.13, w: glassW, h: glassH, baseY: H * 0.13 + glassH + baseH, }; } // ---------- blobs ---------- function makeBlob(x, y, r, temp) { return { x, y, r, temp: temp ?? 0.5, vx: 0, vy: 0, phase: Math.random() * Math.PI * 2, wobbleSpeed: 0.6 + Math.random() * 0.9, pulse: Math.random() * Math.PI * 2, }; } function seedBlobs() { state.blobs = []; const g = state.glass; const cx = g.x + g.w / 2; for (let i = 0; i < 6; i++) { const y = g.y + g.h * (0.15 + 0.7 * (i / 5)); const r = g.w * (0.20 + Math.random() * 0.10); state.blobs.push(makeBlob( cx + (Math.random() - 0.5) * g.w * 0.4, y, r, Math.random() )); } } function initBlobsOnce() { if (state.blobs.length === 0 && state.glass) seedBlobs(); } // ---------- simulation ---------- function step(dt) { const g = state.glass; if (!g) return; const H = g.h; const cx = g.x + g.w / 2; for (const b of state.blobs) { // temperature: hot near bottom when lamp on const depth = (b.y - g.y) / H; // 0 top, 1 bottom const targetT = state.on ? (0.15 + 0.85 * depth) : 0.05; b.temp += (targetT - b.temp) * Math.min(1, dt * 0.9); // vertical velocity: warm rises, cool sinks const vyTarget = (0.42 - b.temp) * H * 0.45; b.vy += (vyTarget - b.vy) * Math.min(1, dt * 2.2); // gentle sway b.phase += dt * b.wobbleSpeed; const swayTarget = cx + Math.sin(b.phase) * g.w * 0.10; b.vx += ((swayTarget - b.x) * 0.15 - b.vx * 0.6) * Math.min(1, dt * 1.5); b.y += b.vy * dt; b.x += b.vx * dt; // keep inside glass const margin = b.r * 0.75; const minX = g.x + margin, maxX = g.x + g.w - margin; const minY = g.y + margin * 0.8, maxY = g.y + g.h - margin * 0.8; if (b.x < minX) { b.x = minX; b.vx *= -0.4; } if (b.x > maxX) { b.x = maxX; b.vx *= -0.4; } if (b.y < minY) { b.y = minY; b.vy = Math.abs(b.vy) * 0.3; } if (b.y > maxY) { b.y = maxY; b.vy = -Math.abs(b.vy) * 0.3; } b.pulse += dt * 2.1; } // merge let merged = true; while (merged) { merged = false; outer: for (let i = 0; i < state.blobs.length; i++) { for (let j = i + 1; j < state.blobs.length; j++) { const a = state.blobs[i], b = state.blobs[j]; const dx = a.x - b.x, dy = a.y - b.y; const dist = Math.hypot(dx, dy); if (dist < (a.r + b.r) * 0.55) { const ar = a.r * a.r, br = b.r * b.r; const totalArea = ar + br; const totalR = Math.sqrt(totalArea); a.x = (a.x * ar + b.x * br) / totalArea; a.y = (a.y * ar + b.y * br) / totalArea; a.temp = (a.temp * ar + b.temp * br) / totalArea; a.r = totalR; a.vy = (a.vy + b.vy) * 0.5; state.blobs.splice(j, 1); merged = true; break outer; } } } } // split large blobs const toSplit = []; for (let i = 0; i < state.blobs.length; i++) { const b = state.blobs[i]; const maxR = g.w * 0.42; if (b.r > maxR && state.blobs.length < 9 && Math.random() < dt * 0.5) { toSplit.push(i); } } for (let k = toSplit.length - 1; k >= 0; k--) { const i = toSplit[k]; const b = state.blobs[i]; const newR = b.r / Math.sqrt(2); const spread = newR * 0.9; const childA = makeBlob(b.x - spread, b.y, newR, b.temp * (0.9 + Math.random() * 0.2)); const childB = makeBlob(b.x + spread, b.y, newR, b.temp * (0.9 + Math.random() * 0.2)); childA.vy = b.vy; childB.vy = b.vy; childA.vx = b.vx - 30 * state.dpr; childB.vx = b.vx + 30 * state.dpr; state.blobs.splice(i, 1, childA, childB); } } // ---------- drawing ---------- function drawBackground(W, H) { // wall const wall = ctx.createLinearGradient(0, 0, 0, H); wall.addColorStop(0, '#342b44'); wall.addColorStop(0.62, '#241c30'); wall.addColorStop(1, '#1a1424'); ctx.fillStyle = wall; ctx.fillRect(0, 0, W, H); // soft glow behind lamp const g = state.glass; const cx = W / 2; const cy = g.y + g.h * 0.75; const glowR = g.w * 1.15; const glow = ctx.createRadialGradient(cx, cy, 0, cx, cy, glowR); glow.addColorStop(0, `rgba(255,140,50,${0.38 * state.glow})`); glow.addColorStop(1, 'rgba(255,140,50,0)'); ctx.fillStyle = glow; ctx.fillRect(cx - glowR, cy - glowR, glowR * 2, glowR * 2); // desk const deskY = g.baseY + H * 0.035; const desk = ctx.createLinearGradient(0, deskY, 0, H); desk.addColorStop(0, '#7a5433'); desk.addColorStop(0.12, '#5f3f24'); desk.addColorStop(1, '#3c2716'); ctx.fillStyle = desk; ctx.fillRect(0, deskY, W, H - deskY); ctx.fillStyle = 'rgba(255,220,180,0.14)'; ctx.fillRect(0, deskY, W, 2 * state.dpr); // mug (left of lamp) drawMug(W * 0.16, deskY, state.dpr); drawMugCup2(W * 0.85, deskY, state.dpr); } function drawMug(x, deskY, d) { const w = 34 * d, h = 26 * d; ctx.save(); ctx.fillStyle = '#8d6e63'; roundRect(x - w / 2, deskY - h, w, h, 5 * d); ctx.fill(); ctx.strokeStyle = '#8d6e63'; ctx.lineWidth = 4 * d; ctx.beginPath(); ctx.arc(x + w / 2 + 4 * d, deskY - h / 2, 7 * d, -Math.PI / 2, Math.PI / 2); ctx.stroke(); ctx.fillStyle = 'rgba(255,255,255,0.15)'; roundRect(x - w / 2 + 4 * d, deskY - h + 3 * d, 6 * d, h - 6 * d, 3 * d); ctx.fill(); ctx.restore(); } function drawMugCup2(x, deskY, d) { const w = 30 * d, h = 22 * d; ctx.save(); ctx.fillStyle = '#546a7b'; roundRect(x - w / 2, deskY - h, w, h, 4 * d); ctx.fill(); ctx.strokeStyle = '#546a7b'; ctx.lineWidth = 4 * d; ctx.beginPath(); ctx.arc(x - w / 2 - 4 * d, deskY - h / 2, 6 * d, -Math.PI / 2, Math.PI / 2, true); ctx.stroke(); ctx.restore(); } function roundRect(x, y, w, h, r) { ctx.beginPath(); ctx.moveTo(x + r, y); ctx.arcTo(x + w, y, x + w, y + h, r); ctx.arcTo(x + w, y + h, x, y + h, r); ctx.arcTo(x, y + h, x, y, r); ctx.arcTo(x, y, x + w, y, r); ctx.closePath(); } function glassPath(g) { const r = g.w * 0.32; ctx.beginPath(); ctx.moveTo(g.x + r, g.y); ctx.lineTo(g.x + g.w - r, g.y); ctx.quadraticCurveTo(g.x + g.w, g.y, g.x + g.w, g.y + r); ctx.lineTo(g.x + g.w, g.y + g.h - r); ctx.quadraticCurveTo(g.x + g.w, g.y + g.h, g.x + g.w - r, g.y + g.h); ctx.lineTo(g.x + r, g.y + g.h); ctx.quadraticCurveTo(g.x, g.y + g.h, g.x, g.y + g.h - r); ctx.lineTo(g.x, g.y + r); ctx.quadraticCurveTo(g.x, g.y, g.x + r, g.y); ctx.closePath(); } function drawWax() { const g = state.glass; const d = state.dpr; // silhouette pass offActx.clearRect(0, 0, offA.width, offA.height); offActx.fillStyle = '#fff'; for (const b of state.blobs) { const wob = Math.sin(b.pulse) * b.r * 0.16; const wob2 = Math.cos(b.pulse * 0.7) * b.r * 0.14; offActx.beginPath(); offActx.arc(b.x, b.y, b.r * (1 + wob * 0.002), 0, Math.PI * 2); offActx.fill(); offActx.beginPath(); offActx.arc(b.x + wob2, b.y + wob, b.r * 0.72, 0, Math.PI * 2); offActx.fill(); } // filtered pass -> colored silhouettes offBctx.clearRect(0, 0, offB.width, offB.height); offBctx.filter = `blur(${Math.round(10 * d)}px) contrast(28)`; offBctx.drawImage(offA, 0, 0); offBctx.filter = 'none'; offBctx.globalCompositeOperation = 'source-in'; const waxGrad = offBctx.createLinearGradient(0, g.y, 0, g.y + g.h); const dim = 0.55 + 0.45 * state.glow; waxGrad.addColorStop(0, `rgba(${Math.round(178 * dim + 60)},${Math.round(48 * dim + 18)},40,1)`); waxGrad.addColorStop(0.6, `rgba(${Math.round(226 * dim + 40)},${Math.round(110 * dim)},34,1)`); waxGrad.addColorStop(1, `rgba(${Math.round(255 * dim + 20)},${Math.round(168 * dim)},52,1)`); offBctx.fillStyle = waxGrad; offBctx.fillRect(0, 0, offB.width, offB.height); offBctx.globalCompositeOperation = 'source-over'; // clip to glass and draw ctx.save(); glassPath(g); ctx.clip(); // inner bulb glow const bulbY = g.y + g.h * 0.88; const bulbR = g.w * 0.75; const bulb = ctx.createRadialGradient(g.x + g.w / 2, bulbY, 0, g.x + g.w / 2, bulbY, bulbR); bulb.addColorStop(0, `rgba(255,190,90,${0.75 * state.glow})`); bulb.addColorStop(1, 'rgba(255,160,60,0)'); ctx.fillStyle = bulb; ctx.fillRect(g.x - bulbR, bulbY - bulbR, bulbR * 2, bulbR * 2); ctx.drawImage(offB, 0, 0); // wax top sheen ctx.restore(); } function drawLamp() { const g = state.glass; const d = state.dpr; const cx = g.x + g.w / 2; // cap (top) const capW = g.w * 0.5, capH = state.H * 0.045; ctx.fillStyle = '#26202f'; roundRect(cx - capW / 2, g.y - capH * 0.85, capW, capH, 6 * d); ctx.fill(); ctx.fillStyle = 'rgba(255,255,255,0.12)'; roundRect(cx - capW / 2 + 4 * d, g.y - capH * 0.85 + 3 * d, capW - 8 * d, 3 * d, 2 * d); ctx.fill(); // base const baseW = g.w * 1.45; const baseH = state.H * 0.055; const baseY = g.baseY; const baseGrad = ctx.createLinearGradient(0, baseY, 0, baseY + baseH); baseGrad.addColorStop(0, '#3a3145'); baseGrad.addColorStop(0.5, '#241d2e'); baseGrad.addColorStop(1, '#171220'); ctx.fillStyle = baseGrad; roundRect(cx - baseW / 2, baseY, baseW, baseH, 8 * d); ctx.fill(); ctx.fillStyle = 'rgba(255,255,255,0.10)'; roundRect(cx - baseW / 2 + 6 * d, baseY + 3 * d, baseW - 12 * d, 3 * d, 2 * d); ctx.fill(); // bulb window on base front ctx.fillStyle = state.glow > 0.05 ? `rgba(255,${Math.round(140 + 60 * state.glow)},60,${0.85 * state.glow})` : '#332b3e'; roundRect(cx - baseW * 0.2, baseY + baseH * 0.32, baseW * 0.4, baseH * 0.38, 6 * d); ctx.fill(); // glass body ctx.save(); glassPath(g); ctx.fillStyle = 'rgba(200,225,255,0.10)'; ctx.fill(); ctx.lineWidth = 2.5 * d; ctx.strokeStyle = 'rgba(220,235,255,0.35)'; ctx.stroke(); // inner tint const tint = ctx.createLinearGradient(0, g.y, 0, g.y + g.h); tint.addColorStop(0, 'rgba(160,190,230,0.05)'); tint.addColorStop(1, 'rgba(255,150,60,0.10)'); ctx.fillStyle = tint; ctx.fill(); // highlight streaks ctx.clip(); ctx.fillStyle = 'rgba(255,255,255,0.20)'; roundRect(g.x + g.w * 0.14, g.y + g.h * 0.06, g.w * 0.09, g.h * 0.80, 8 * d); ctx.fill(); ctx.fillStyle = 'rgba(255,255,255,0.10)'; roundRect(g.x + g.w * 0.30, g.y + g.h * 0.10, g.w * 0.05, g.h * 0.55, 6 * d); ctx.fill(); ctx.restore(); } function render() { ctx.clearRect(0, 0, state.W, state.H); const W = state.W, H = state.H; drawBackground(W, H); drawWax(); drawLamp(); // flicker subtle when on if (state.glow > 0.05) { const flick = 0.03 * Math.sin(state.time * 13.7) + 0.02 * Math.sin(state.time * 31.3); ctx.fillStyle = `rgba(255,170,80,${Math.max(0, flick * state.glow)})`; ctx.fillRect(0, 0, W, H); } } // ---------- loop ---------- let last = performance.now(); function frame(now) { const dt = Math.min(0.05, (now - last) / 1000); last = now; state.time += dt; const glowTarget = state.on ? 1 : 0; state.glow += (glowTarget - state.glow) * Math.min(1, dt * 3); step(dt); render(); requestAnimationFrame(frame); } // ---------- switch ---------- const btn = document.getElementById('lampSwitch'); const label = document.getElementById('stateLabel'); btn.addEventListener('click', () => { state.on = !state.on; btn.setAttribute('aria-pressed', String(state.on)); label.textContent = state.on ? 'On' : 'Off'; }); // ---------- boot ---------- resize(); seedBlobs(); window.addEventListener('resize', () => { const old = state.glass; resize(); const g = state.glass; if (old && old.h > 0) { const sx = g.w / old.w, sy = g.h / old.h; for (const b of state.blobs) { b.x = g.x + (b.x - old.x) * sx; b.y = g.y + (b.y - old.y) * sy; b.r = Math.min(b.r * Math.sqrt(sx * sy), g.w * 0.42); } } for (const b of state.blobs) { const g2 = state.glass; b.x = Math.min(Math.max(b.x, g2.x + b.r * 0.75), g2.x + g2.w - b.r * 0.75); b.y = Math.min(Math.max(b.y, g2.y + b.r * 0.6), g2.y + g2.h - b.r * 0.6); } }); // avoid double-seed on resize (blobs keep existing) initBlobsOnce(); requestAnimationFrame(frame);})();</script></body></html>