Lava lamp v1 · OpenCode21.11.2026 20:17

Source Qwen3.8 27B, Medium effort

index.html · 14.0 KB · 374 lines. Exactly as the agent wrote it.

<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"><meta name="theme-color" content="#0a0c12"><title>Lava Lamp</title><style>  html,body{margin:0;height:100%;overflow:hidden;background:#0a0c12;overscroll-behavior:none}  body{font-family:system-ui,-apple-system,'Segoe UI',sans-serif;-webkit-user-select:none;user-select:none}  #wrap{position:fixed;inset:0;display:flex;align-items:center;justify-content:center;touch-action:manipulation}  #stage{position:relative;width:360px;height:640px;flex:none}  #stage canvas{position:absolute;left:0;top:0;width:360px;height:640px}  #glow{position:absolute;left:40px;top:40px;width:280px;height:560px;pointer-events:none;    background:radial-gradient(circle at 50% 84%, rgba(255,105,138,.55), rgba(255,80,110,.16) 38%, rgba(255,80,110,0) 68%);    mix-blend-mode:screen;opacity:1}  #switch{position:absolute;left:288px;top:468px;width:46px;height:88px;padding:0;border-radius:10px;    border:1px solid #05060a;cursor:pointer;touch-action:manipulation;    background:linear-gradient(180deg,#3d3745,#1e1a25 55%,#15121a);    box-shadow:inset 0 1px 0 rgba(255,255,255,.12), inset 0 -3px 8px rgba(0,0,0,.55), 0 5px 12px rgba(0,0,0,.5)}  #switch .knob{position:absolute;left:9px;top:10px;width:28px;height:28px;border-radius:50%;    background:radial-gradient(circle at 35% 30%,#d8cfdc,#7a7080 55%,#322b38 92%);    box-shadow:0 2px 5px rgba(0,0,0,.6), inset 0 1px 1px rgba(255,255,255,.4);    transition:top .22s cubic-bezier(.45,1.5,.4,1)}  #switch.off .knob{top:50px}  #switch:active .knob{filter:brightness(.9)}  #switch:focus-visible{outline:2px solid #ff7d9d;outline-offset:2px}  #hint{position:absolute;left:0;right:0;top:612px;text-align:center;font-size:11px;letter-spacing:.5px;    color:#514a5e;pointer-events:none}</style></head><body><div id="wrap">  <div id="stage">    <canvas id="bg"></canvas>    <div id="glow"></div>    <canvas id="blobs"></canvas>    <canvas id="fg"></canvas>    <button id="switch" aria-pressed="true" aria-label="Lamp power" title="Lamp power"><span class="knob"></span></button>    <div id="hint">tap the lamp or the switch</div>  </div></div><script>'use strict';/* ---------- constants & dom ---------- */const VW=360, VH=640, CX=180;const GL_TOP=150, GL_BOT=452;            // glass interior top/bottom (virtual px)const YMIN=168, YMAX=446;                // vertical travel range for blobsconst GX=112, GY=162, GW=136, GH=286;    // metaball field grid rectconst COLS=56, ROWS=116;const CELLX=GW/COLS, CELLY=GH/ROWS, TH=0.9;const stage=document.getElementById('stage');const bgC=document.getElementById('bg'), blobC=document.getElementById('blobs'), fgC=document.getElementById('fg');const bgX=bgC.getContext('2d'), blX=blobC.getContext('2d'), fgX=fgC.getContext('2d');const glowEl=document.getElementById('glow'), swEl=document.getElementById('switch');const smallC=document.createElement('canvas');smallC.width=COLS; smallC.height=ROWS;const sX=smallC.getContext('2d');const img=sX.createImageData(COLS,ROWS);const FA=new Float32Array(COLS*ROWS), FT=new Float32Array(COLS*ROWS);let RS=1, on=true, glowT=1;/* ---------- geometry ---------- */// half-width of the glass wall at height y (narrow at the base, wide at the top)function hw(y){  let t=(y-GL_TOP)/(GL_BOT-GL_TOP); t=t<0?0:t>1?1:t;  return 40+24*Math.pow(1-t,1.15);}// inner glass outline, inset by `ins` px (open-topped path)function glassPath(c,ins){  const N=22;  c.beginPath();  for(let i=0;i<=N;i++){const y=GL_BOT-(GL_BOT-GL_TOP)*i/N; const x=CX-hw(y)+ins; i?c.lineTo(x,y):c.moveTo(x,y);}  for(let i=0;i<=N;i++){const y=GL_TOP+(GL_BOT-GL_TOP)*i/N; const x=CX+hw(y)-ins; c.lineTo(x,y);}  c.closePath();}/* ---------- sizing (fits phones, keeps sharpness) ---------- */function resize(){  const s=Math.min(innerWidth/VW, innerHeight/VH)*0.985;  stage.style.transform='scale('+s+')';  RS=Math.min(2.5,Math.max(1,(devicePixelRatio||1)*s));  for(const c of [bgC,blobC,fgC]){c.width=Math.round(VW*RS); c.height=Math.round(VH*RS);}  drawBG(); drawFG();}addEventListener('resize',resize);/* ---------- static scene: room, desk, liquid ---------- */function drawBG(){  const c=bgX;  c.setTransform(RS,0,0,RS,0,0);  c.clearRect(0,0,VW,VH);  let g=c.createLinearGradient(0,0,0,VH);  g.addColorStop(0,'#0c0e16');  g.addColorStop(0.55,'#101320');  g.addColorStop(1,'#0a0c12');  c.fillStyle=g; c.fillRect(0,0,VW,VH);  // desk  g=c.createLinearGradient(0,548,0,640);  g.addColorStop(0,'#33261a');  g.addColorStop(0.10,'#2b2015');  g.addColorStop(1,'#120d08');  c.fillStyle=g; c.fillRect(0,548,VW,92);  c.fillStyle='rgba(255,205,150,.10)'; c.fillRect(0,548,VW,2);  // wood grain (deterministic)  let seed=1234567;  const rnd=()=>{seed=(seed*16807)%2147483647;return seed/2147483647;};  c.lineWidth=1;  for(let i=0;i<9;i++){    const y=558+rnd()*74;    c.strokeStyle='rgba(0,0,0,'+(0.05+rnd()*0.08).toFixed(3)+')';    c.beginPath();    for(let x=0;x<=VW;x+=36)c.lineTo(x,y+Math.sin(x*0.045+i*2.1)*1.6);    c.stroke();  }  // shadow under the lamp  c.save();  c.translate(CX,554); c.scale(1,0.16);  g=c.createRadialGradient(0,0,8,0,0,110);  g.addColorStop(0,'rgba(0,0,0,.55)');  g.addColorStop(1,'rgba(0,0,0,0)');  c.fillStyle=g;  c.beginPath(); c.arc(0,0,110,0,6.2832); c.fill();  c.restore();  // liquid inside the glass  glassPath(c,2.5);  g=c.createLinearGradient(0,GL_TOP,0,GL_BOT);  g.addColorStop(0,'#2b1442');  g.addColorStop(0.65,'#1d0d2d');  g.addColorStop(1,'#170a22');  c.fillStyle=g; c.fill();  c.lineWidth=5; c.strokeStyle='rgba(6,3,10,.4)';  glassPath(c,2.5); c.stroke();}/* ---------- static scene: base, cap, glass shine (over the wax) ---------- */function metalGrad(c,x0,x1){  const g=c.createLinearGradient(x0,0,x1,0);  g.addColorStop(0,'#1c161c');  g.addColorStop(0.18,'#5a4c57');  g.addColorStop(0.42,'#8f7c8b');  g.addColorStop(0.50,'#a38e9f');  g.addColorStop(0.62,'#7c6a77');  g.addColorStop(0.85,'#3b313a');  g.addColorStop(1,'#171217');  return g;}function drawFG(){  const c=fgX;  c.setTransform(RS,0,0,RS,0,0);  c.clearRect(0,0,VW,VH);  // glass shine + edges (under the cap)  let st=c.createLinearGradient(0,190,0,430);  st.addColorStop(0,'rgba(255,255,255,0)');  st.addColorStop(0.25,'rgba(255,255,255,.20)');  st.addColorStop(0.80,'rgba(255,255,255,.05)');  st.addColorStop(1,'rgba(255,255,255,0)');  c.lineWidth=4; c.strokeStyle=st;  c.beginPath();  for(let i=0;i<=20;i++){const y=190+(430-190)*i/20; const x=CX-hw(y)+10; i?c.lineTo(x,y):c.moveTo(x,y);}  c.stroke();  st=c.createLinearGradient(0,210,0,420);  st.addColorStop(0,'rgba(255,255,255,0)');  st.addColorStop(0.5,'rgba(255,255,255,.08)');  st.addColorStop(1,'rgba(255,255,255,0)');  c.lineWidth=2; c.strokeStyle=st;  c.beginPath();  for(let i=0;i<=16;i++){const y=210+(420-210)*i/16; const x=CX+hw(y)-9; i?c.lineTo(x,y):c.moveTo(x,y);}  c.stroke();  c.lineWidth=1.5; c.strokeStyle='rgba(255,255,255,.10)';  glassPath(c,1); c.stroke();  // metal base  c.beginPath();  c.moveTo(CX-44,GL_BOT-4);  c.bezierCurveTo(CX-54,472,CX-62,522,CX-66,548);  c.lineTo(CX+66,548);  c.bezierCurveTo(CX+62,522,CX+54,472,CX+44,GL_BOT-4);  c.closePath();  c.fillStyle=metalGrad(c,CX-66,CX+66); c.fill();  c.fillStyle='rgba(0,0,0,.35)'; c.fillRect(CX-66,546,132,3);  // base rim  c.beginPath(); c.ellipse(CX,GL_BOT-4,44,7,0,0,6.2832);  c.fillStyle='#141016'; c.fill();  c.lineWidth=1; c.strokeStyle='rgba(255,255,255,.10)'; c.stroke();  // brand  c.textAlign='center'; c.textBaseline='middle';  c.font='700 11px system-ui,-apple-system,sans-serif';  c.fillStyle='rgba(0,0,0,.55)'; c.fillText('L A V A',CX,506.5);  c.fillStyle='rgba(255,150,175,.30)'; c.fillText('L A V A',CX,505.5);  // metal cap  c.beginPath();  c.moveTo(CX-64,GL_TOP+2);  c.lineTo(CX-8,96);  c.quadraticCurveTo(CX,87,CX+8,96);  c.lineTo(CX+64,GL_TOP+2);  c.closePath();  c.fillStyle=metalGrad(c,CX-64,CX+64); c.fill();  // rim where the cap meets the glass  c.beginPath(); c.ellipse(CX,GL_TOP+2,64,8,0,0,6.2832);  c.fillStyle='#150f17'; c.fill();  c.strokeStyle='rgba(255,255,255,.12)'; c.stroke();}/* ---------- wax blobs ---------- */const blobs=[];for(let i=0;i<7;i++){  blobs.push({    x:150+Math.random()*60, y:200+Math.random()*230,    r:12+Math.random()*5,    vx:(Math.random()-.5)*20, vy:(Math.random()-.5)*20,    t:0.35+Math.random()*0.45,              // 0 = cool, 1 = hot    ph:Math.random()*6.28, bf:0.35+Math.random()*0.5,    hr:0.22+Math.random()*0.16,             // heat/cool rate    cd:0                                    // split cooldown  });}function doMerge(i,j){  const A=blobs[i],B=blobs[j];  const ra=A.r*A.r, rb=B.r*B.r, rs=ra+rb;  const nb={    x:(A.x*ra+B.x*rb)/rs, y:(A.y*ra+B.y*rb)/rs,    r:Math.min(32,Math.sqrt(rs)),    vx:(A.vx*ra+B.vx*rb)/rs, vy:(A.vy*ra+B.vy*rb)/rs,    t:(A.t*ra+B.t*rb)/rs,    ph:Math.random()*6.28, bf:0.35+Math.random()*0.5, hr:A.hr, cd:1.0  };  blobs.splice(Math.max(i,j),1);  blobs.splice(Math.min(i,j),1);  blobs.push(nb);}function doSplit(i){  const b=blobs[i], r=b.r/Math.SQRT2;  const a={x:b.x-r*0.35-2, y:b.y-2, r:r,    vx:b.vx-16-Math.random()*8, vy:b.vy-9, t:b.t,    ph:Math.random()*6.28, bf:0.35+Math.random()*0.5, hr:b.hr, cd:2.2};  const z={x:b.x+r*0.35+2, y:b.y+2, r:r,    vx:b.vx+16+Math.random()*8, vy:b.vy+9, t:b.t,    ph:Math.random()*6.28, bf:0.35+Math.random()*0.5, hr:b.hr, cd:2.2};  blobs.splice(i,1,a,z);}function step(dt,time){  const g=glowT;  for(const b of blobs){    // heat: hot near the base, cool near the top; cools down when the lamp is off    let hT=(YMAX-b.y)/(YMAX-YMIN); if(hT<0)hT=0; else if(hT>1)hT=1;    const target=g*hT+(1-g)*0.10;    b.t+=(target-b.t)*Math.min(1,b.hr*dt);    b.t+=Math.sin(time*0.8+b.ph*2)*0.010*dt*g;    if(b.t<0)b.t=0; else if(b.t>1)b.t=1;    // buoyancy + gentle wobble + drag    b.vy+=140*(1-2.1*b.t)*dt;    b.vx+=Math.sin(time*(0.4+b.bf)+b.ph)*26*dt*(0.25+0.75*g);    const dv=Math.exp(-1.5*dt), dh=Math.exp(-2.1*dt);    b.vx*=dh; b.vy*=dv;    b.x+=b.vx*dt; b.y+=b.vy*dt;    // walls (the glass tapers), floor, ceiling    const lim=hw(b.y)-5-b.r;    if(lim>0){      if(b.x>CX+lim){b.x=CX+lim; b.vx*=-0.25;}      else if(b.x<CX-lim){b.x=CX-lim; b.vx*=-0.25;}    }    if(b.y>YMAX-b.r){b.y=YMAX-b.r; if(b.vy>0)b.vy*=-0.15;}    if(b.y<YMIN+b.r){b.y=YMIN+b.r; if(b.vy<0)b.vy*=-0.3;}    b.cd-=dt;  }  // merge: warm blobs that overlap deeply coalesce  let merged=false;  for(let i=0;i<blobs.length&&!merged;i++)    for(let j=i+1;j<blobs.length;j++){      const A=blobs[i],B=blobs[j];      const dx=A.x-B.x,dy=A.y-B.y,S=A.r+B.r;      if(A.t>0.38&&B.t>0.38&&blobs.length<14&&dx*dx+dy*dy<0.25*S*S){doMerge(i,j);merged=true;break;}    }  // split: large blobs that have cooled at the top break apart  for(let i=blobs.length-1;i>=0;i--){    const b=blobs[i];    if(b.cd<=0&&blobs.length>5&&b.r>17&&b.t<0.34){doSplit(i);break;}  }  // keep the population in a pleasing band  if(blobs.length<6){    let bi=0;    for(let i=1;i<blobs.length;i++)if(blobs[i].r>blobs[bi].r)bi=i;    if(blobs[bi].r>14&&(blobs[bi].cd<=0||blobs.length<4))doSplit(bi);  }else if(blobs.length>13){    let bi=0,bj=1,bd=Infinity;    for(let i=0;i<blobs.length;i++)for(let j=i+1;j<blobs.length;j++){      const dx=blobs[i].x-blobs[j].x,dy=blobs[i].y-blobs[j].y;      const d2=dx*dx+dy*dy; if(d2<bd){bd=d2;bi=i;bj=j;}    }    const S=blobs[bi].r+blobs[bj].r;    if(bd<4.84*S*S)doMerge(bi,bj);  }}/* ---------- rendering: metaball field -> pixels ---------- */function ss(x){return x<=0?0:x>=1?1:x*x*(3-2*x);}function render(time){  blX.setTransform(RS,0,0,RS,0,0);  blX.clearRect(0,0,VW,VH);  // accumulate the blob field (and a temperature-weighted copy)  FA.fill(0); FT.fill(0);  for(let bi=0;bi<blobs.length;bi++){    const b=blobs[bi];    const r=b.r*(1+0.05*Math.sin(time*1.3+b.ph));   // slow organic pulsing    const invK=1/(0.15*r*r);    const cx0=(b.x-GX)/CELLX, cy0=(b.y-GY)/CELLY;    const rx=r/CELLX+1.5, ry=r/CELLY+1.5;    let x0=(cx0-rx)|0; if(x0<0)x0=0;    let x1=(cx0+rx)|0; if(x1>=COLS)x1=COLS-1;    let y0=(cy0-ry)|0; if(y0<0)y0=0;    let y1=(cy0+ry)|0; if(y1>=ROWS)y1=ROWS-1;    for(let gy=y0;gy<=y1;gy++){      const dy=(gy+0.5)*CELLY+GY-b.y, dy2=dy*dy;      let idx=gy*COLS+x0;      for(let gx=x0;gx<=x1;gx++){        const dx=(gx+0.5)*CELLX+GX-b.x;        const f=6.6667/(1+(dx*dx+dy2)*invK);        FA[idx]+=f; FT[idx]+=f*b.t; idx++;      }    }  }  // colorize: soft edges, bright core, color shifts with temperature  const d=img.data;  for(let i=0;i<FA.length;i++){    const f=FA[i], o=i*4;    if(f<0.58*TH){d[o+3]=0; continue;}    const v=f/TH;    let t=FT[i]/f; if(t>1)t=1; else if(t<0)t=0;    const core=ss((v-1)/1.1);    const edge=ss((v-0.62)/0.34);    const m=(0.80+0.45*core)*(0.62+0.38*edge);    d[o]  =(150+(255-150)*t)*m;    d[o+1]=(24+(128-24)*t)*m;    d[o+2]=(66+(146-66)*t)*m;    d[o+3]=255*ss((v-0.78)/0.42);  }  sX.putImageData(img,0,0);  blX.save();  glassPath(blX,4); blX.clip();  blX.imageSmoothingEnabled=true;  blX.drawImage(smallC,GX,GY,GW,GH);  blX.restore();}/* ---------- main loop ---------- */let last=performance.now();function frame(now){  let dt=(now-last)/1000; last=now;  if(dt>0.05)dt=0.05;  glowT+=((on?1:0)-glowT)*Math.min(1,2.2*dt);  step(dt,now/1000);  render(now/1000);  glowEl.style.opacity=(glowT*(0.92+0.05*Math.sin(now*0.0035)+0.03*Math.sin(now*0.00113))).toFixed(3);  requestAnimationFrame(frame);}/* ---------- power switch ---------- */function setOn(v){  on=v;  swEl.classList.toggle('off',!v);  swEl.setAttribute('aria-pressed',String(v));}swEl.addEventListener('click',e=>{e.stopPropagation();setOn(!on);});stage.addEventListener('click',()=>setOn(!on));resize();requestAnimationFrame(frame);</script></body></html>