Skip to content
VibeCodeBench
Back to the app

The code

Jelly Cat by GPT-6.2 Astra

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

works · auto-checked (Checked by an automated pass. Not hand-played yet.)66.4 KB · 1,115 lines · no libraries
Download
<!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="#faf8f0">
<meta name="color-scheme" content="light">
<title>Jelly Cat · Wobble Lab</title>
<style>
:root{--paper:#faf8f0;--ink:#352a23;--muted:#70665c;--accent:#cf4c28;--line:#ded9cc;--peach:#fae8d4;--display:"Gloock","Cooper Black",Georgia,"Times New Roman",serif;--sans:"DM Sans","Aptos","Segoe UI",Arial,sans-serif;--mono:"IBM Plex Mono","SFMono-Regular",Consolas,monospace}
*{box-sizing:border-box}
html{background:var(--paper)}
body{margin:0;color:var(--ink);font-family:var(--sans);font-size:16px;-webkit-font-smoothing:antialiased}
button,input{font:inherit}
button{color:inherit;cursor:pointer;touch-action:manipulation;-webkit-tap-highlight-color:transparent}
button:focus-visible,input:focus-visible{outline:3px solid #774823;outline-offset:5px}
svg{display:block}
.icon-bank{position:absolute;width:0;height:0;overflow:hidden}
.wrap{width:min(90%,1320px);margin-inline:auto}
.masthead{border-bottom:1px solid var(--line)}
.masthead-inner{height:clamp(75px,6vw,93px);display:flex;align-items:center;justify-content:space-between}
.brand{display:flex;align-items:center;gap:10px}
.brand-mark{width:46px;height:46px}
.wordmark{font-family:"Bricolage Grotesque","Trebuchet MS",var(--sans);font-size:clamp(24px,2.1vw,31px);letter-spacing:-.085em;line-height:1;font-weight:900}
.sound{display:inline-flex;align-items:center;justify-content:center;gap:9px;background:#fffdf7;border:1px solid #d9d3c7;border-radius:999px;padding:0 18px;min-height:43px;font-size:14px;font-weight:650}
.sound svg{width:18px;height:18px}
.sound .silent,.sound[aria-pressed=false] .audible{display:none}
.sound[aria-pressed=false] .silent{display:block}
main{padding-block:clamp(25px,2.8vw,48px) 32px}
.playroom{display:grid;grid-template-columns:minmax(0,1.72fr) minmax(300px,.9fr);grid-template-rows:1fr auto auto 1fr;grid-template-areas:"stage ." "stage intro" "stage controls" "stage .";column-gap:clamp(30px,5.2vw,87px);align-items:start}
.stage{grid-area:stage;align-self:stretch;position:relative;isolation:isolate;width:100%;aspect-ratio:1.38;min-height:422px;overflow:hidden;border-radius:27px;background:radial-gradient(ellipse at 29% 22%,#ffe7cb,#f3c39f);box-shadow:inset 0 0 0 1px #dfbda1;touch-action:none;user-select:none;-webkit-user-select:none;cursor:grab;-webkit-tap-highlight-color:transparent}
.stage::after{content:"";position:absolute;inset:0;border:1px solid rgba(110,67,34,.15);border-radius:inherit;pointer-events:none;z-index:5}
.stage.grabbed{cursor:grabbing}
.scene,.scene2{position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none}
.scene{z-index:1}
[hidden]{display:none!important}
.sparkles{width:100%;height:100%;position:absolute;inset:0;pointer-events:none;z-index:2}
.stage-bar{position:absolute;inset:clamp(14px,1.65vw,23px) clamp(14px,1.65vw,23px) auto;z-index:4;display:flex;align-items:center;justify-content:space-between;pointer-events:none}
.status,.demo{display:inline-flex;align-items:center;justify-content:center;gap:8px;color:#64432e;font-size:12px;line-height:1;font-weight:650;border-radius:40px;white-space:nowrap}
.status{padding:9px 13px 9px 10px;background:rgba(255,250,232,.76);border:1px solid rgba(117,76,43,.14)}
.status-dot{width:8px;height:8px;background:#ce5c30;border-radius:50%;box-shadow:0 0 0 3px rgba(211,99,45,.105)}
.stage[data-active=true] .status-dot{animation:pulse .85s ease-in-out infinite alternate}
@keyframes pulse{to{box-shadow:0 0 0 4px rgba(211,99,45,.23)}}
.demo{pointer-events:auto;padding:0 11px;min-height:44px;gap:6px;border:0;background:transparent}
.demo svg{width:14px;height:14px}
.demo .stop,.demo[aria-pressed=true] .play{display:none}
.demo[aria-pressed=true] .stop{display:inline}
.hint{position:absolute;z-index:3;left:5.5%;bottom:17%;width:clamp(89px,9vw,145px);color:#854c2b;pointer-events:none;transition:opacity .35s;transform:rotate(-8deg)}
.hint span{font:italic 600 clamp(18px,1.7vw,26px)/1.1 var(--display);letter-spacing:-.055em}
.hint svg{width:72%;margin:-4px 0 0 20%;height:auto}
.stage.touched .hint{opacity:0}
.emotes{position:absolute;inset:0;pointer-events:none;z-index:3;overflow:hidden}
.heart{position:absolute;color:#bb4a2d;line-height:1;font:700 22px Georgia,serif;animation:float-heart 1.2s ease-out forwards;pointer-events:none}
@keyframes float-heart{0%{opacity:0;transform:translate(-50%,0) scale(.45) rotate(-12deg)}18%{opacity:.9}100%{opacity:0;transform:translate(calc(-50% + var(--dx)), -53px) scale(1.15) rotate(12deg)}}
.intro{grid-area:intro;align-self:end;padding:0 0 clamp(20px,2vw,34px)}
.flavor{display:inline-flex;align-items:center;gap:9px;color:#a94823;font:700 11px/1.3 var(--mono);text-transform:uppercase;letter-spacing:.15em}
.flavor svg{width:24px;height:24px}
h1{margin:12px 0 15px;font-family:var(--display);font-size:clamp(65px,6.65vw,101px);line-height:.98;font-weight:700;letter-spacing:-.077em;white-space:nowrap}
.cat-word{position:relative;display:inline-block;font-style:italic;padding-right:.05em}
.cat-word svg{position:absolute;top:-.055em;right:-.18em;width:.29em;height:.29em;color:var(--accent);pointer-events:none}
.intro p{max-width:32ch;margin:0;font-size:clamp(16px,1.22vw,19px);line-height:1.52;color:var(--muted);letter-spacing:-.014em}
.controls{grid-area:controls;border-top:1px solid var(--line);padding-top:clamp(19px,1.85vw,29px)}
.setting-head{display:flex;align-items:center;justify-content:space-between;gap:10px}
.setting-head label{font-size:18px;font-weight:730;letter-spacing:-.03em}
output{color:#a24724;background:#f5e6d5;border-radius:7px;min-width:46px;padding:5px 8px;text-align:center;font:650 12px/1.15 var(--mono);font-variant-numeric:tabular-nums}
.range-wrap{position:relative;--fill:36%;padding-top:4px}
input[type=range]{appearance:none;-webkit-appearance:none;display:block;background:transparent;width:100%;margin:0;height:44px;cursor:pointer;touch-action:pan-y;position:relative;z-index:1}
input[type=range]::-webkit-slider-runnable-track{height:7px;border-radius:10px;background:linear-gradient(to right,var(--accent) 0 var(--fill),#e1d9ca var(--fill) 100%)}
input[type=range]::-moz-range-track{height:7px;border-radius:10px;background:#e1d9ca}
input[type=range]::-moz-range-progress{height:7px;border-radius:10px;background:var(--accent)}
input[type=range]::-webkit-slider-thumb{appearance:none;-webkit-appearance:none;margin-top:-9px;width:25px;height:25px;border-radius:50%;background:radial-gradient(circle at 36% 28%,#fff1cb 0 9%,transparent 12%),linear-gradient(145deg,#f18d4c,#d75b2c);border:3px solid #fff9eb;box-shadow:0 0 0 1.3px #b95029,0 2px 4px #6a402d38}
input[type=range]::-moz-range-thumb{width:20px;height:20px;border-radius:50%;background:#e7773c;border:3px solid #fff9eb;box-shadow:0 0 0 1.3px #b95029}
.ticks{display:flex;justify-content:space-between;padding:0 12px;position:absolute;bottom:5px;left:0;right:0;pointer-events:none}
.ticks i{width:1px;height:4px;background:#c9bdaa}
.ends{display:flex;justify-content:space-between;align-items:center;gap:6px;margin-top:1px;color:#736354;font-size:12.5px}
.ends span{display:inline-flex;align-items:center;gap:5px}
.ends svg{width:17px;height:17px}
.drop{width:100%;display:flex;justify-content:center;align-items:center;gap:10px;min-height:54px;padding:11px 22px;margin-top:clamp(20px,1.9vw,30px);border-radius:15px;border:1px solid #b64424;background:#ce4d29;color:#fffaf0;box-shadow:0 3px 0 #a33d20;font-weight:750;font-size:17px;letter-spacing:-.025em;transition:transform .15s,background .15s,box-shadow .15s}
.drop svg{width:25px;height:25px}
.drop:active{transform:translateY(2px);box-shadow:0 1px 0 #a33d20}
@media(hover:hover){.drop:hover{background:#bc4424}.sound:hover{background:#f7eddf}.demo:hover{background:#fff4e570}}
.guide{margin-top:clamp(26px,2.5vw,42px)}
.guide-heading{display:flex;align-items:center;gap:14px;margin:0 0 16px;color:#7f6e5d;font:700 10.5px/1.4 var(--mono);letter-spacing:.16em;text-transform:uppercase}
.guide-heading::after{content:"";height:1px;background:var(--line);flex:1}
.guide-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:17px}
.gesture{display:flex;align-items:center;gap:clamp(12px,1.25vw,21px);padding:clamp(13px,1.15vw,20px);min-height:85px;border:1px solid #e4ddcf;border-radius:17px;background:#f7f4eae6}
.gesture-art{width:51px;height:51px;flex:0 0 auto;border-radius:13px;background:#f8e0c8;padding:5px;color:#8e472b}
.gesture:nth-child(2) .gesture-art{background:#f0e5c6}
.gesture:nth-child(3) .gesture-art{background:#f6ded3}
.gesture h3{font-size:clamp(15px,1.18vw,18px);line-height:1.18;letter-spacing:-.035em;margin:0 0 4px}
.gesture p{font-size:clamp(12px,.97vw,14.5px);line-height:1.38;color:var(--muted);margin:0}
@media(min-width:1001px) and (max-height:810px){.masthead-inner{height:68px}main{padding-top:24px}.stage{aspect-ratio:1.52;min-height:410px}.guide{margin-top:22px}.guide-heading{margin-bottom:10px}.gesture{min-height:72px;padding:10px 14px}.gesture-art{width:46px;height:46px}}
@media(min-width:1600px){main{padding-top:46px}.masthead-inner{height:91px}}
@media(max-width:1000px) and (min-width:761px){.wrap{width:93%}.playroom{grid-template-columns:minmax(0,1.34fr) minmax(260px,.85fr);column-gap:26px}.stage{min-height:420px}.gesture{gap:9px;padding:11px}.gesture-art{width:43px;height:43px}.guide-grid{gap:10px}h1{font-size:66px}}
@media(max-width:760px){.wrap{width:min(91.5%,590px)}.masthead-inner{height:66px}.brand-mark{width:38px;height:38px}.wordmark{font-size:24px}.sound{font-size:13px;padding-inline:13px;min-height:42px}.playroom{display:grid;grid-template-columns:1fr;grid-template-rows:auto auto auto;grid-template-areas:"intro" "stage" "controls";gap:0}main{padding-top:21px}.intro{padding-bottom:16px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;column-gap:12px}.flavor{order:2;font-size:9px;gap:6px;letter-spacing:.11em}.flavor svg{width:19px;height:19px}h1{order:1;font-size:clamp(45px,8.8vw,61px);margin:0}.intro p{order:3;flex-basis:100%;max-width:none;font-size:14px;margin-top:8px}.stage{min-height:0;aspect-ratio:1.15;border-radius:22px}.hint{width:80px;left:4%;bottom:10%}.hint span{font-size:18px}.stage-bar{inset:11px 12px auto}.status{font-size:11px}.demo{font-size:11px}.controls{margin-top:20px;padding-top:0;border:0}.drop{margin-top:17px;min-height:50px}.guide{margin-top:30px}.guide-grid{grid-template-columns:1fr;gap:8px}.gesture{min-height:64px;padding:9px 13px}.gesture-art{width:42px;height:42px}.gesture h3{font-size:15px;margin-bottom:2px}.gesture p{font-size:13px}.guide-heading{margin-bottom:10px}}
@media(max-width:360px){.flavor{font-size:8px}.intro p{font-size:13px}.hint{left:3%;bottom:7%}}
@media(prefers-reduced-motion:reduce){*,*::before,*::after{scroll-behavior:auto!important}.status-dot{animation:none!important}.hint,.drop{transition:none}}
</style>
</head>
<body>
<svg class="icon-bank" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><defs>
<symbol id="brand-art" viewBox="0 0 52 52">
 <path d="M5.4 32.7C6.9 27.1 9.3 23 10.3 16.9c.8-4.7 2.3-9.6 5.3-9.3 2.3.3 5.2 4.7 7.3 5.1 2.2-.5 5.2-.6 7.1.1 3.1-1.5 5.6-6.4 8.3-5.5 3.3 1.1 2.5 7.5 3.4 11.8.9 4.4 4.2 9.5 5.2 14.1 1.9 8.3-4.6 12.1-10.5 10.5-3.6 3.5-7.3 2.8-10.6 1.4-3.9 2.1-8.1 1.6-10.5-.9-6.6 1.7-12-3.1-9.9-11.5Z" fill="#e66c36" stroke="#a94624" stroke-width="1.5" stroke-linejoin="round"/>
 <path d="M10.6 32.5c.5-2.8 1.9-6.2 3.4-8.6m1-6.2.8-3.2" fill="none" stroke="#ffdb99" stroke-width="3.2" stroke-linecap="round"/>
 <ellipse cx="21.1" cy="27.5" rx="1.6" ry="2.4" fill="#532d20"/><ellipse cx="33" cy="27.5" rx="1.6" ry="2.4" fill="#532d20"/><path d="M25.1 31c.8 2.1 2.9 2.1 3.9-.1" fill="none" stroke="#532d20" stroke-width="1.6" stroke-linecap="round"/>
</symbol>
<symbol id="volume" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M3.5 9h4.1L13 4.6v14.8L7.6 15H3.5z"/><path d="M16 8a6 6 0 0 1 0 8m2.8-11a10 10 0 0 1 0 14"/></g></symbol>
<symbol id="mute" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M3.5 9h4.1L13 4.6v14.8L7.6 15H3.5z"/><path d="m16.7 9.2 4.8 5.6m0-5.6-4.8 5.6"/></g></symbol>
<symbol id="orange" viewBox="0 0 28 28"><path d="M14.4 7.6c2.7-4.8 6.8-4.3 8.2-4.2-1.1 3.4-4 5.2-8.2 4.2Z" fill="#9caa69"/><path d="M14.5 9.1c.6-2.2-.3-4.6-1.7-6" fill="none" stroke="#715136" stroke-width="1.5" stroke-linecap="round"/><path d="M24.4 16.8c0 5.6-4.8 9-10.2 9-5.6 0-10.5-3.7-10.5-9.3 0-5.2 4.9-8.9 9.2-8.2 1 .3 1.7.4 2.8.1 4.6-.6 8.7 3 8.7 8.4Z" fill="#eb8a3c" stroke="#b15a29" stroke-width="1.2"/><path d="M7.4 15.4c.4-1.9 1.6-3.2 3.2-3.9" stroke="#ffdc9a" stroke-width="2.1" fill="none" stroke-linecap="round"/><g fill="#ca642b"><circle cx="19.5" cy="18.3" r=".65"/><circle cx="17.7" cy="21" r=".6"/><circle cx="21" cy="16.4" r=".5"/></g></symbol>
<symbol id="pudding" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="1.55" stroke-linecap="round" stroke-linejoin="round"><path d="m4.5 16.7 2.8-10c1-2.2 8.8-2.2 9.5 0l2.7 10M3 17.2c2.4 3.6 15.6 3.6 18 0M7.8 7.8c2.6 1.3 6.1 1.3 8.6-.1"/></g></symbol>
<symbol id="gummy" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="1.55" stroke-linejoin="round"><path d="M3.5 7.5 12 3.3l8.5 4.1v9.4L12 21l-8.5-4.3zM3.8 7.5l8.2 4.2 8.2-4.2M12 11.7v9"/></g></symbol>
<symbol id="drop-art" viewBox="0 0 28 28"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.8"><path d="M14 3v13m-5-5 5 5 5-5M5.1 18.8C3.5 19.4 3 20 3 20.8c0 2 5 3.6 11 3.6s11-1.6 11-3.6c0-.8-.7-1.6-2.2-2.1M6.5 8.2v4m15.1-6v4.5"/></g></symbol>
<symbol id="poke-art" viewBox="0 0 48 48"><g stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M4 34c3-9 6.5-13.4 12.3-13.2 4.8.1 5.1 5.6 10.2 5.4 5.4-.4 9.2-2 14.4 8.3 2.5 5-3.2 6.7-18.3 6.7S2 39.9 4 34Z" fill="#f8ac69"/><path d="M27 24.5c-1.8-.7-6.6-5.8-5.2-7.1 1.1-1.1 2.8-.1 4.3 1.6L17.7 8.8C15.5 5.9 18 3.4 20.2 5.7l10.4 11c-2.4-4.2-.2-6.1 2.1-4l1.8 1.6c-.7-2.7 1.6-3.5 3.3-1.7l4.1 4.5c2.3 2.8 1.8 6.6-.2 9.3l-3.1 3.2c-3.2 2.7-6.8-1.8-11.6-5.1Z" fill="#ffedcd"/><path d="m14 20-2.6-2.4m1.6 7-3.4.3m9 8.8c3.3 1 7.3.8 10.2-.5" fill="none"/></g></symbol>
<symbol id="pull-art" viewBox="0 0 48 48"><g stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M7 31.7c2.2-5.3 4.6-10 6.7-12.4 2.1-2.4 3.2 2.1 5 2.5 3.4-.9 5.8-1.6 8.1-3.9 3.7-3.4 5.4-9.3 7.5-9 2 .4.5 8.4-.6 13.8-.7 4.5 2.7 6.5 3.1 10.5.6 4.7-3.7 6.8-14.9 6.8-12.7 0-17.3-1.7-14.9-8.3Z" fill="#f6be78"/><path d="M28.4 21.2c1.7-1.3 3.5-4 4.3-6.1M7 16.7c1.4-4.5 4.4-7.2 8.1-9.1m-4.5-.9 5.1.3-.8 5.4M36.4 5.5l2-2.5m-.4 8.4 3.3-1.5" fill="none"/><path d="M17 28.5v1m9-1v1m-6 2.3q1.6 1.7 3.1-.2" fill="none"/></g></symbol>
<symbol id="rub-art" viewBox="0 0 48 48"><g stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M5.2 33.3c0-3.9 2.3-6.5 3.1-10.6.5-2.4.3-7.3 2.7-7 1.9.3 3.9 4.1 6.4 4 3.2-.7 6.8-.7 10.1.2 2.6-1.4 4.8-5.3 6.6-4.5 1.7.8 1 5.7 1.8 8.6 1.4 3.8 3.9 6 3.8 10.1-.1 4.7-7.8 6.9-17.2 6.9-10.3 0-17.3-2-17.3-7.7Z" fill="#f7b47f"/><path d="M11.9 30.6q2.7-3.4 5.6-.2m9.3.2q2.7-3.4 5.5-.3m-12 3.2q1.7 2.1 3.5-.1M12.8 11.7c3.6-4.7 11.2-5.4 16.3-1.8m-3.4.9 4-.3-.9-4" fill="none"/><path d="M36.7 6.6c-1.6-2.9-4.9-.4-3.5 2.1.9 1.5 3.7 2.9 3.7 2.9s3.1-2.4 3.5-4.2c.7-2.9-2.7-3.5-3.7-.8Z" fill="#df805f" stroke-width="1.1"/></g></symbol>
</defs></svg>
<header class="masthead"><div class="wrap masthead-inner">
 <div class="brand"><svg class="brand-mark" aria-hidden="true"><use href="#brand-art"/></svg><span class="wordmark">Wobble Lab</span></div>
 <button class="sound" id="sound" type="button" aria-pressed="true" aria-label="Sound on. Mute sounds"><svg class="audible" aria-hidden="true"><use href="#volume"/></svg><svg class="silent" aria-hidden="true"><use href="#mute"/></svg><span id="sound-label">Sound on</span></button>
</div></header>
<main class="wrap">
 <div class="playroom">
  <div class="stage" id="stage" role="group" aria-label="Orange jelly cat playground" aria-describedby="guide-title">
   <canvas id="drawing" class="scene2" aria-hidden="true"></canvas>
   <canvas id="glass" class="scene" aria-hidden="true" hidden></canvas>
   <svg class="sparkles" viewBox="0 0 800 580" preserveAspectRatio="none" aria-hidden="true"><g fill="#fff2d9" opacity=".92"><path d="M133 115q1.9 12.6 11.5 14.5-9.6 1.8-11.5 14.5-1.9-12.7-11.5-14.5 9.6-1.9 11.5-14.5Z"/><path d="M150 106q.8 6 5.4 6.9-4.6.9-5.4 6.9-.8-6-5.3-6.9 4.5-.9 5.3-6.9Z"/><path d="M682 324q1.1 8 7.7 9.4-6.6 1.4-7.7 9.5-1.3-8.1-7.7-9.5 6.4-1.4 7.7-9.4Z"/></g></svg>
   <div class="stage-bar"><span class="status"><i class="status-dot"></i><span id="activity" role="status" aria-live="polite">Free play</span></span><button type="button" id="demo" class="demo" aria-pressed="false" aria-label="Play hands-free demo"><svg class="play" viewBox="0 0 16 16" aria-hidden="true"><path d="m5 2.7 7.6 5.3L5 13.3Z" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/></svg><svg class="stop" viewBox="0 0 16 16" aria-hidden="true"><rect x="3.5" y="3.5" width="9" height="9" rx="1.4" fill="currentColor"/></svg><span id="demo-label">Demo</span></button></div>
   <div class="hint" aria-hidden="true"><span>Poke me!</span><svg viewBox="0 0 98 45"><path d="M3 16c11 19 39 27 44 14 4.2-11-14.2-12.7-14.3-2.6.1 14.2 36.4 10.3 55.1-17.3m-11.5 3.1L90.1 8l-1.7 13" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg></div>
   <div class="emotes" id="emotes" aria-hidden="true"></div>
  </div>
  <section class="intro" aria-labelledby="title">
   <div class="flavor"><svg aria-hidden="true"><use href="#orange"/></svg>Tangerine jelly</div>
   <h1 id="title">Jelly <span class="cat-word">Cat<svg viewBox="0 0 28 28" aria-hidden="true"><path d="m5 12 2-8m4 12 7-9m-2 15 8-3" fill="none" stroke="currentColor" stroke-width="2.7" stroke-linecap="round"/></svg></span></h1>
   <p>Give its belly a poke, then let go and watch it wobble.</p>
  </section>
  <section class="controls" aria-label="Jelly controls">
   <div class="setting-head"><label for="firmness">Jelly firmness</label><output for="firmness" id="firm-value">36%</output></div>
   <div class="range-wrap" id="range-wrap"><input id="firmness" type="range" min="0" max="100" value="36" aria-valuetext="36 percent firm"><div class="ticks" aria-hidden="true"><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i></div></div>
   <div class="ends"><span><svg aria-hidden="true"><use href="#pudding"/></svg>Soft pudding</span><span>Firm gummy<svg aria-hidden="true"><use href="#gummy"/></svg></span></div>
   <button type="button" class="drop" id="drop"><svg aria-hidden="true"><use href="#drop-art"/></svg>Drop the cat</button>
  </section>
 </div>
 <section class="guide" aria-labelledby="guide-title">
  <h2 id="guide-title" class="guide-heading">Touch guide</h2>
  <div class="guide-grid">
   <div class="gesture"><svg class="gesture-art" aria-hidden="true"><use href="#poke-art"/></svg><div><h3>Poke &amp; squeeze</h3><p>Press and hold its belly.</p></div></div>
   <div class="gesture"><svg class="gesture-art" aria-hidden="true"><use href="#pull-art"/></svg><div><h3>Pull &amp; stretch</h3><p>Drag an ear, cheek, or tail.</p></div></div>
   <div class="gesture"><svg class="gesture-art" aria-hidden="true"><use href="#rub-art"/></svg><div><h3>Rub &amp; purr</h3><p>Stroke its head back and forth.</p></div></div>
  </div>
 </section>
</main>
<script id="gel-shader" type="x-shader/x-fragment">
precision highp float;
uniform vec2 u_size;
uniform float u_half;
uniform float u_time;
uniform vec3 u_shape;
uniform vec3 u_grip;
uniform vec4 u_drag;
uniform vec3 u_gaze;
const vec3 focus=vec3(.13,1.35,0.);
const vec2 dishCenter=vec2(.12,-.01);
float sat(float x){return clamp(x,0.,1.);}
float square(float x){return x*x;}
float smoothUnion(float a,float b,float k){
 float h=max(k-abs(a-b),0.)/k;
 return min(a,b)-.25*k*h*h;
}
float ell(vec3 p,vec3 r){return (length(p/r)-1.)*min(min(r.x,r.y),r.z);}
float capsule(vec3 p,vec3 a,vec3 b,float ra,float rb){
 vec3 ab=b-a;float t=sat(dot(p-a,ab)/dot(ab,ab));
 return length(p-a-ab*t)-mix(ra,rb,t);
}
vec3 restPoint(vec3 p){
 float sy=1.+u_shape.y;
 p.y=.14+(p.y-u_shape.x-.14)/sy;
 p.x-=u_shape.z*(p.y-.14)*.46;
 p.xz*=sqrt(sy);
 vec3 v=p-u_grip;
 float k=sat(dot(v,u_drag.xyz)/max(dot(u_drag.xyz,u_drag.xyz),.00001));
 vec3 d=v-u_drag.xyz*k;
 float reach=max(.001,u_drag.w*u_drag.w);
 float w=sat(1.-dot(d,d)/reach);
 return p-u_drag.xyz*w*w*(3.-2.*w);
}
float jellyForm(vec3 p){
 vec3 mirrorP=vec3(abs(p.x),p.yz);
 float body=ell(p-vec3(0.,.91,-.09),vec3(1.045,.83,.825));
 float thigh=ell(mirrorP-vec3(.795,.55,-.065),vec3(.406,.416,.571));
 body=smoothUnion(body,thigh,.17);
 vec3 fore=mirrorP-vec3(.49,.68,.53);
 fore.x+=.14*fore.y;
 float arm=ell(fore,vec3(.256,.478,.281));
 float paw=ell(mirrorP-vec3(.52,.313,.63),vec3(.305,.195,.347));
 body=smoothUnion(body,smoothUnion(arm,paw,.105),.093);
 float head=ell(p-vec3(0.,1.742,.054),vec3(1.058,.792,.816));
 float cheek=ell(mirrorP-vec3(.706,1.52,.267),vec3(.387,.344,.485));
 head=smoothUnion(head,cheek,.18);
 float muzzle=ell(mirrorP-vec3(.204,1.548,.69),vec3(.263,.184,.169));
 head=smoothUnion(head,muzzle,.075);
 float d=smoothUnion(head,body,.23);
 if(p.y>1.74){
  vec3 ep=vec3(mirrorP.x,p.y,p.z*1.40);
  float ear=capsule(ep,vec3(.599,2.105,.014),vec3(.817,2.782,-.075),.391,.071)/1.40;
  d=smoothUnion(d,ear,.135);
 }
 if(p.x>.49 && p.y<1.91){
  vec3 a=vec3(.80,.410,-.32),b=vec3(1.19,.427,-.365),c=vec3(1.438,.581,-.35);
  vec3 e=vec3(1.545,.839,-.306),f=vec3(1.52,1.077,-.237),g=vec3(1.403,1.228,-.158),h=vec3(1.30,1.215,-.124);
  float tail=min(capsule(p,a,b,.216,.201),capsule(p,b,c,.201,.183));
  tail=min(tail,capsule(p,c,e,.183,.166));
  tail=min(tail,capsule(p,e,f,.166,.145));
  tail=min(tail,capsule(p,f,g,.145,.128));
  tail=min(tail,capsule(p,g,h,.128,.115));
  d=smoothUnion(d,tail,.11);
 }
 return d;
}
float gelDist(vec3 p){
 float sy=1.+u_shape.y;
 return jellyForm(restPoint(p))*min(sy,inversesqrt(sy));
}
vec3 gelNormal(vec3 p){
 vec2 e=vec2(.0032,-.0032);
 return normalize(e.xyy*gelDist(p+e.xyy)+e.yyx*gelDist(p+e.yyx)+e.yxy*gelDist(p+e.yxy)+e.xxx*gelDist(p+e.xxx));
}
float plateDist(vec3 p){
 float r=length(p.xz-dishCenter);
 float v=min(r,1.872);
 float rise=smoothstep(.98,1.872,v);
 float cy=.088+.143*rise*rise;
 float shell=length(vec2(r-v,p.y-cy))-.050;
 float foot=length(vec2(r-clamp(r,.87,1.12),p.y-.058))-.053;
 return min(shell,foot);
}
vec3 plateNormal(vec3 p){
 vec2 e=vec2(.0024,0.);
 return normalize(vec3(plateDist(p+e.xyy)-plateDist(p-e.xyy),plateDist(p+e.yxy)-plateDist(p-e.yxy),plateDist(p+e.yyx)-plateDist(p-e.yyx)));
}
vec3 paper(vec2 uv){
 float t=sat(.18+.53*uv.x+.34*(1.-uv.y));
 vec3 col=mix(vec3(1.,.906,.765),vec3(.948,.758,.611),t);
 float glow=exp(-dot((uv-vec2(.24,.66))*vec2(1.35,1.12),(uv-vec2(.24,.66))*vec2(1.35,1.12))*4.);
 return col+vec3(.012,.017,.013)*glow;
}
float gauss(vec2 p,vec2 r){p/=r;return exp(-dot(p,p));}
vec3 floorColor(vec3 p,vec2 uv){
 vec3 col=paper(uv);
 vec2 v=p.xz-dishCenter;
 float soft=gauss(v-vec2(.34,-.20),vec2(1.92,1.27));
 float contact=gauss(v-vec2(.10,-.06),vec2(1.40,.97));
 float outer=exp(-square(max(0.,length((v-vec2(.14,-.10))/vec2(1.88,1.63))-.70)*3.95));
 col*=1.-vec3(.146,.216,.253)*soft-vec3(.065,.097,.109)*contact;
 return col-vec3(.029,.037,.037)*outer;
}
float lineRing(float r,float center,float w){return 1.-smoothstep(w,w+.005,abs(r-center));}
vec3 pottery(vec3 p,vec3 n,vec3 ray){
 float r=length(p.xz-dishCenter);
 float top=smoothstep(-.5,.65,n.y);
 vec3 col=mix(vec3(.766,.682,.568),vec3(.994,.966,.858),top);
 float phase=atan(p.z-dishCenter.y,p.x-dishCenter.x);
 float ringR=r+.002*sin(phase*7.);
 float stripes=max(lineRing(ringR,1.760,.009)*.76,lineRing(ringR,1.819,.014)*.86)*smoothstep(.35,.85,n.y);
 col=mix(col,vec3(.745,.387,.222),stripes);
 float h=u_shape.x;
 float lift=exp(-h*.81);
 float shade=gauss(p.xz-vec2(.10+h*.38,-.21-h*.18),vec2(1.12+h*.21,.82+h*.13));
 float feet=gauss(vec2(abs(p.x)-.54,p.z-.48),vec2(.44,.39));
 col*=1.-vec3(.15,.33,.44)*shade*(.25+.75*lift);
 col*=1.-vec3(.10,.17,.23)*feet*lift;
 float caustic=exp(-square((length((p.xz-vec2(.07,-.01))/vec2(1.13,.88))-1.01)/.16));
 col=mix(col,vec3(1.,.757,.407),caustic*.31*lift);
 float edge=pow(1.-max(0.,dot(n,-ray)),4.);
 col=mix(col,vec3(1.,.967,.88),edge*.22);
 return col;
}
vec2 bounds(vec3 ro,vec3 rd,vec3 lo,vec3 hi){
 vec3 a=(lo-ro)/rd,b=(hi-ro)/rd;
 vec3 t0=min(a,b),t1=max(a,b);
 return vec2(max(t0.x,max(t0.y,t0.z)),min(t1.x,min(t1.y,t1.z)));
}
float lightPanel(vec3 ray,vec3 axis,vec2 extent,float twist,float softness){
 axis=normalize(axis);
 vec3 tangent=normalize(cross(vec3(0.,1.,0.),axis));
 vec3 bitangent=cross(axis,tangent);
 float side=dot(ray,axis);
 vec2 uv=vec2(dot(ray,tangent),dot(ray,bitangent))/max(.09,side);
 float c=cos(twist),s=sin(twist);
 uv=mat2(c,-s,s,c)*uv;
 vec2 q=abs(uv)-extent+vec2(.075);
 float d=length(max(q,vec2(0.)))+min(max(q.x,q.y),0.)-.075;
 return (1.-smoothstep(-softness,softness,d))*smoothstep(.14,.65,side);
}
vec3 throughSet(vec3 from,vec3 dir){
 vec3 sky=mix(vec3(.916,.697,.522),vec3(1.,.916,.806),sat(.52+.49*dir.y-.13*dir.x));
 if(dir.y<-.026){
  float t=(.185-from.y)/dir.y;
  if(t>0.){
   vec2 xz=from.xz+dir.xz*t;
   float r=length(xz-dishCenter);
   float stripe=exp(-square((r-1.79)/.041));
   vec3 dish=mix(vec3(.984,.939,.817),vec3(.753,.473,.280),stripe*.58);
   sky=mix(sky,dish,(1.-smoothstep(1.88,1.96,r))*.91);
  }
 }
 return sky;
}
float seg2(vec2 p,vec2 a,vec2 b){vec2 d=b-a;return length(p-a-d*sat(dot(p-a,d)/dot(d,d)));}
float cross2(vec2 a,vec2 b){return a.x*b.y-a.y*b.x;}
float tri2(vec2 p,vec2 a,vec2 b,vec2 c){
 float d=min(seg2(p,a,b),min(seg2(p,b,c),seg2(p,c,a)));
 float e0=cross2(b-a,p-a),e1=cross2(c-b,p-b),e2=cross2(a-c,p-c);
 return (min(e0,min(e1,e2))>=0. || max(e0,max(e1,e2))<=0.)?-d:d;
}
float softDot(vec2 p,float radius,float softness){return 1.-smoothstep(radius-softness,radius+softness,length(p));}
vec3 facialDetails(vec3 col,vec3 q,vec3 n){
 vec2 m=vec2(abs(q.x),q.y);
 if(q.y>2.23 && q.z>-.065){
  float inner=tri2(m,vec2(.525,2.277),vec2(.812,2.667),vec2(.897,2.305))-.023;
  float mask=(1.-smoothstep(-.012,.026,inner))*smoothstep(-.035,.152,q.z);
  col=mix(col,col*vec3(.982,.691,.648)+vec3(.009,.022,.012),mask*.86);
  col+=vec3(.017,.042,.011)*exp(-square((inner+.021)/.023))*mask;
 }
 if(q.y<.52 && q.z>.70){
  float toe=abs(abs(m.x-.52)-(.077+.09*(q.y-.30)));
  float ends=smoothstep(.237,.282,q.y)*(1.-smoothstep(.336,.387,q.y))*smoothstep(.70,.87,q.z);
  col*=1.-vec3(.105,.174,.19)*(1.-smoothstep(.004,.014,toe))*ends;
 }
 if(q.z<.265 || q.y<1.20 || q.y>2.56)return col;
 float front=smoothstep(.26,.59,q.z)*smoothstep(-.10,.32,n.z);
 float whisk=min(seg2(m,vec2(.745,1.604),vec2(.923,1.646)),seg2(m,vec2(.748,1.545),vec2(.902,1.525)));
 col=mix(col,vec3(.700,.305,.110),(1.-smoothstep(.005,.012,whisk))*.42*front);
 vec2 blush=(m-vec2(.649,1.592))/vec2(.225,.105);
 col=mix(col,vec3(.889,.306,.150),exp(-dot(blush,blush)*1.75)*.37*front);
 float flecks=softDot(m-vec2(.62,1.578),.016,.006);
 flecks=max(flecks,softDot(m-vec2(.705,1.602),.014,.006));
 flecks=max(flecks,softDot(m-vec2(.678,1.534),.011,.004));
 col=mix(col,vec3(.617,.275,.116),flecks*.60*front);
 if(q.y>2.11){
  vec2 grain=vec2((m.x-.174-.24*(m.y-2.31))/.040,(m.y-2.319)/.13);
  float tabby=exp(-dot(grain,grain)*1.5)+.8*exp(-dot(vec2(q.x/.044,(q.y-2.347)/.14),vec2(q.x/.044,(q.y-2.347)/.14))*1.4);
  col=mix(col,vec3(.83,.37,.075),sat(tabby)*.24*front);
 }
 float squint=u_gaze.z;
 float side=q.x<0.?-1.:1.;
 float ex=q.x-side*.405-u_gaze.x*.019;
 float bend=squint*.050*(1.-min(1.4,(ex/.145)*(ex/.145)));
 float ey=q.y-1.839-u_gaze.y*.015-bend;
 float eh=mix(.150,.0125,squint);
 vec2 e=vec2(ex/.139,ey/eh);
 float er=length(e);
 float edge=(1.-smoothstep(.962,1.045,er))*front;
 if(edge>.001){
  vec3 bead=mix(vec3(.154,.070,.039),vec3(.438,.248,.114),sat(.23-.5*e.y+.55*er));
  vec2 iris=(e-vec2(u_gaze.x*.18,u_gaze.y*.14))*vec2(1.14,1.06);
  float pupil=1.-smoothstep(.735,.852,length(iris));
  bead=mix(bead,vec3(.102,.064,.053),pupil);
  float glint=softDot(iris-vec2(-.30,.385),.223,.035);
  glint=max(glint,softDot(iris-vec2(.235,.594),.069,.025));
  bead=mix(bead,vec3(1.,.970,.868),glint*(1.-squint));
  bead+=vec3(.16,.095,.042)*exp(-square((er-.82)/.15))*sat(-e.y)*(1.-squint);
  col=mix(col,mix(bead,vec3(.275,.135,.070),squint),edge);
 }
 float nose=tri2(vec2(q.x,q.y),vec2(-.056,1.660),vec2(.056,1.660),vec2(0.,1.599))-.021;
 vec3 noseColor=mix(vec3(.389,.179,.099),vec3(.57,.289,.167),sat((q.y-1.606)*15.));
 noseColor+=vec3(.31,.26,.19)*exp(-dot(vec2((q.x+.023)/.023,(q.y-1.654)/.011),vec2((q.x+.023)/.023,(q.y-1.654)/.011)));
 col=mix(col,noseColor,(1.-smoothstep(-.004,.005,nose))*front);
 float w=.141+.022*squint;
 float v=sat(m.x/w);
 float mouthY=1.558-.058*4.*v*(1.-v)+.013*v;
 float mouth=length(vec2(m.x-w*v,q.y-mouthY));
 mouth=min(mouth,seg2(vec2(q.x,q.y),vec2(0.,1.598),vec2(0.,1.555)));
 col=mix(col,vec3(.373,.179,.082),(1.-smoothstep(.010,.019,mouth))*front);
 return col;
}
vec4 bubbleAt(int i){
 if(i==0)return vec4(-.655,2.097,.388,.047);
 if(i==1)return vec4(-.56,2.175,.324,.021);
 if(i==2)return vec4(.62,2.135,.39,.036);
 if(i==3)return vec4(.819,1.734,.409,.025);
 if(i==4)return vec4(-.775,.825,.373,.048);
 if(i==5)return vec4(.291,1.07,.566,.035);
 if(i==6)return vec4(-.088,.736,.532,.046);
 if(i==7)return vec4(.015,.817,.489,.020);
 if(i==8)return vec4(.849,.569,.368,.033);
 if(i==9)return vec4(1.528,.847,-.252,.032);
 if(i==10)return vec4(.767,2.543,.02,.025);
 return vec4(-.349,1.104,.391,.018);
}
vec3 airBubbles(vec3 col,vec3 q,vec3 dir,float thickness){
 for(int i=0;i<12;i++){
  vec4 b=bubbleAt(i);
  b.y+=.006*sin(u_time*.74+float(i)*1.73);
  float t=dot(b.xyz-q,dir);
  vec3 offset=q+dir*t-b.xyz;
  float r=length(offset)/b.w;
  if(t>0. && t<thickness && r<1.10){
   vec3 bn=normalize(offset/b.w-dir*sqrt(max(.001,1.-r*r)));
   float strength=exp(-.76*t)*(1.-smoothstep(.94,1.10,r));
   float ring=smoothstep(.58,.99,r);
   vec3 air=mix(col*1.12,vec3(.655,.331,.088),ring*.67);
   float flash=pow(max(0.,dot(bn,normalize(vec3(-.48,.67,.70)))),12.);
   air+=vec3(.65,.61,.49)*flash+vec3(.18,.18,.09)*ring*sat(-bn.y);
   col=mix(col,air,strength);
  }
 }
 return col;
}
vec3 glassColor(vec3 p,vec3 ray){
 vec3 n=gelNormal(p),q=restPoint(p);
 float facing=sat(dot(n,-ray));
 vec3 inside=refract(ray,n,1./1.365);
 float depth=.019;
 for(int i=0;i<30;i++){
  float d=gelDist(p+inside*depth);
  if(d>.002 && depth>.036)break;
  depth+=clamp(-d*1.01+.009,.012,.285);
  if(depth>3.2)break;
 }
 vec3 exitP=p+inside*depth;
 vec3 endN=gelNormal(exitP);
 vec3 outgoing=refract(inside,-endN,1.365);
 if(dot(outgoing,outgoing)<.1)outgoing=reflect(inside,-endN);
 vec3 back=throughSet(exitP,normalize(outgoing+.0001));
 vec3 attenuation=exp(-vec3(.108,1.19,3.40)*(depth+.045));
 vec3 col=back*attenuation+vec3(.985,.286,.023)*(1.-attenuation);
 vec3 lamp=normalize(vec3(-.55,.83,.42));
 float ndl=dot(n,lamp);
 col*=.836+.164*sat(.38+.64*ndl);
 float pool=exp(-square((n.y+.46+.085*sin(q.x*4.1))/.37));
 col+=vec3(.042,.112,.014)*pool*(.40+.60*smoothstep(.22,1.1,depth));
 float glow=exp(-dot((q-vec3(.07,.94,.53))/vec3(.82,.93,1.),(q-vec3(.07,.94,.53))/vec3(.82,.93,1.)));
 col+=vec3(.013,.057,.008)*glow;
 float occl=sat((.167-gelDist(p+n*.19))/.18);
 col*=1.-vec3(.063,.171,.19)*occl;
 vec3 reflectDir=reflect(ray,n);
 float rim=pow(1.-facing,2.9);
 vec3 reflected=mix(vec3(.472,.260,.121),vec3(1.,.884,.692),smoothstep(-.53,.58,reflectDir.y));
 col=mix(col,reflected,.035+rim*.69);
 float internalLight=lightPanel(reflect(inside,endN),vec3(.53,.68,-.29),vec2(.30,.15),.36,.11);
 col+=vec3(.062,.076,.024)*internalLight*(.32+.68*facing);
 vec3 localDir=normalize(restPoint(p+inside*.025)-q+vec3(.000001));
 col=airBubbles(col,q,localDir,depth);
 col=facialDetails(col,q,n);
 float primary=lightPanel(reflectDir,vec3(-.54,.74,.54),vec2(.34,.145),-.28,.046);
 float strip=lightPanel(reflectDir,vec3(-.93,.22,-.18),vec2(.10,.49),-.14,.047);
 float fill=lightPanel(reflectDir,vec3(.70,.56,-.40),vec2(.22,.43),.12,.095);
 float soft=pow(max(0.,dot(reflectDir,normalize(vec3(-.46,.71,.55)))),9.);
 float shine=sat(primary*.86+strip*.61+fill*.56+soft*.105);
 col=mix(col,vec3(1.,.966,.856),shine);
 return col;
}
void main(){
 vec2 uv=gl_FragCoord.xy/u_size;
 vec2 screen=(2.*gl_FragCoord.xy-u_size)/u_size.y;
 vec3 ray=normalize(vec3(-.105,-.35,-1.));
 vec3 right=normalize(cross(ray,vec3(0.,1.,0.))),up=cross(right,ray);
 vec3 ro=focus-ray*8.+u_half*(screen.x*right+screen.y*up);
 float groundT=-ro.y/ray.y;
 vec3 groundP=ro+ray*groundT;
 vec3 col=floorColor(groundP,uv);
 float closest=groundT;
 if(length(groundP.xz-dishCenter)<3.03){
  float t=(.301-ro.y)/ray.y;
  for(int i=0;i<34;i++){
   vec3 p=ro+ray*t;
   float d=plateDist(p);
   if(d<.0018){col=pottery(p,plateNormal(p),ray);closest=t;break;}
   t+=max(.0015,d*.92);
   if(t>groundT)break;
  }
 }
 float sy=1.+u_shape.y;
 vec2 span=bounds(ro,ray,vec3(-2.43,u_shape.x+.025,-1.65),vec3(2.79,u_shape.x+3.87*max(1.,sy),1.75));
 float t=max(0.,span.x),limit=min(closest,span.y);
 float tolerance=clamp(u_half/u_size.y*.43,.00115,.0034);
 for(int i=0;i<89;i++){
  if(t>limit)break;
  vec3 p=ro+ray*t;
  float d=gelDist(p);
  if(d<tolerance){col=glassColor(p,ray);break;}
  t+=max(tolerance*.78,d*.845);
 }
 float noise=fract(52.9829189*fract(dot(gl_FragCoord.xy,vec2(.06711056,.00583715))))-.5;
 gl_FragColor=vec4(clamp(col+noise*.0053,0.,1.),1.);
}
</script>
<script>
(function(){
'use strict';
const byId=id=>document.getElementById(id);
const stage=byId('stage'), glass=byId('glass'), drawing=byId('drawing');
const ctx=drawing.getContext('2d'), firmnessInput=byId('firmness'), demoButton=byId('demo');
const soundButton=byId('sound'), activity=byId('activity');
const clamp=(n,lo,hi)=>Math.max(lo,Math.min(hi,n)), unit=n=>clamp(n,0,1);
const mix=(a,b,n)=>a+(b-a)*n;
const dot=(a,b)=>a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
const normal=v=>{const l=Math.hypot(v[0],v[1],v[2])||1;return v.map(n=>n/l);};
const cross=(a,b)=>[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]];
const ray=normal([-.105,-.35,-1]),right=normal(cross(ray,[0,1,0])),up=cross(right,ray),focus=[.13,1.35,0];
const reduceMotion=window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const spring=()=>({x:0,v:0});
const squish=spring(),lean=spring(),pull=[spring(),spring(),spring()];
const shape={height:0,stretch:0,lean:0};
const gaze={x:0,y:0,tx:0,ty:0,eyeClose:0};
let anchor=[0,1,.73],radius=.72,firm=.36,grip=null,pointerId=null;
let targetPull=[0,0,0],targetLean=0,targetSquish=0,fallSpeed=0,airborne=false;
let rubEnergy=0,affection=0,heartClock=0,lastContact=-100,landedAt=-100;
let liveTime=0,blink=0,nextBlink=3.8,lastPointerTime=-100;
let width=1,height=1,half=1.99,scale=1,ox=0,oy=0,drawRatio=1,pixelScale=1;
let gl=null,program=null,locations=null,usingGlass=false,needSize=true;
let activeDemo=false,demoStarted=0,demoPhase=-1,savedFirm=36,stopClick=false,demoHand=null;
let soundEnabled=true,audioCtx=null,master=null,purrGain=null,noiseBuffer=null,lastNoise=-9,lastPurrVolume=-1;
function warp(p){
 const dx=p[0]-anchor[0],dy=p[1]-anchor[1],dz=p[2]-anchor[2];
 let w=unit(1-(dx*dx+dy*dy+dz*dz)/(radius*radius));
 w=w*w*(3-2*w);
 const x=p[0]+pull[0].x*w,y=p[1]+pull[1].x*w,z=p[2]+pull[2].x*w;
 const sy=1+shape.stretch,sx=1/Math.sqrt(sy);
 return [x*sx+shape.lean*(y-.14)*.46,.14+(y-.14)*sy+shape.height,z*sx];
}
function project(p,deform=true){
 const v=deform?warp(p):p;
 return [ox+dot(v,right)*scale,oy-dot(v,up)*scale];
}
function resize(){
 const box=stage.getBoundingClientRect();
 width=Math.max(1,box.width);height=Math.max(1,box.height);
 half=Math.max(1.98,2.15*height/width);
 scale=height/(2*half);
 ox=width*.5-dot(focus,right)*scale;oy=height*.5+dot(focus,up)*scale;
 drawRatio=Math.min(window.devicePixelRatio||1,1.75);
 if(ctx){
  drawing.width=Math.max(1,Math.round(width*drawRatio));
  drawing.height=Math.max(1,Math.round(height*drawRatio));
 }
 if(gl){
  const maxRatio=Math.min(window.devicePixelRatio||1,1.68,Math.sqrt(690000/(width*height)));
  const ratio=maxRatio*pixelScale;
  glass.width=Math.max(2,Math.round(width*ratio));glass.height=Math.max(2,Math.round(height*ratio));
  gl.viewport(0,0,glass.width,glass.height);
 }
 needSize=false;
}
function setFirmness(value){
 const number=clamp(Math.round(value),0,100);
 firm=number/100;
 firmnessInput.value=String(number);firmnessInput.setAttribute('aria-valuetext',number+' percent firm');
 byId('firm-value').textContent=number+'%';byId('range-wrap').style.setProperty('--fill',number+'%');
}
function prepareSound(){
 if(!soundEnabled)return;
 try{
  if(!audioCtx){
   const AC=window.AudioContext||window.webkitAudioContext;
   if(!AC)return;
   audioCtx=new AC();
   master=audioCtx.createGain();master.gain.value=.64;master.connect(audioCtx.destination);
   const sr=audioCtx.sampleRate;
   noiseBuffer=audioCtx.createBuffer(1,Math.floor(sr*.4),sr);
   const noise=noiseBuffer.getChannelData(0);
   let brown=0;
   for(let i=0;i<noise.length;i++){brown=(brown+.16*(Math.random()*2-1))/1.05;noise[i]=brown*1.9;}
   const motor=audioCtx.createBuffer(1,sr*2,sr),data=motor.getChannelData(0);
   let grain=0;
   for(let i=0;i<data.length;i++){
    const t=i/sr,phase=2*Math.PI*t;
    const pulse=Math.pow(.5+.5*Math.cos(phase*24+.67*Math.sin(phase)),2.8);
    grain=.91*grain+.09*(2*Math.random()-1);
    const breath=.75+.25*Math.cos(phase*.5);
    data[i]=breath*(.24+.76*pulse)*(.56*Math.sin(phase*61)+.20*Math.sin(phase*122)+.12*Math.sin(phase*183)+.47*grain);
   }
   const source=audioCtx.createBufferSource();source.buffer=motor;source.loop=true;
   const warm=audioCtx.createBiquadFilter();warm.type='lowpass';warm.frequency.value=370;
   purrGain=audioCtx.createGain();purrGain.gain.value=0;
   source.connect(warm);warm.connect(purrGain);purrGain.connect(master);source.start();
  }
  if(audioCtx.state==='suspended')audioCtx.resume().catch(()=>{});
 }catch(_){}
}
function purrVolume(amount){
 if(!audioCtx||!purrGain)return;
 const level=soundEnabled?amount:0;
 if(Math.abs(level-lastPurrVolume)<.012&&level!==0)return;
 if(level===0&&lastPurrVolume===0)return;
 lastPurrVolume=level;
 purrGain.gain.setTargetAtTime(.265*level,audioCtx.currentTime,.085);
}
function wetSound(kind,strength=1){
 if(!audioCtx||audioCtx.state==='closed'||!soundEnabled||!master)return;
 const t=audioCtx.currentTime;
 if(kind==='stretch'&&t-lastNoise<.17)return;
 lastNoise=t;
 try{
  const heavy=kind==='land',lift=kind==='release',slide=kind==='stretch';
  const length=heavy ? .265:slide ? .135:lift ? .142:.168;
  const vol=(heavy ? .255:slide ? .075:lift ? .12:.16)*clamp(strength,.18,1.3);
  const start=(heavy?174:lift?272:slide?173:239)*(.95+Math.random()*.11);
  const o=audioCtx.createOscillator(),gain=audioCtx.createGain();
  o.type='sine';o.frequency.setValueAtTime(start,t);
  o.frequency.exponentialRampToValueAtTime(start*(lift?1.25:.38),t+length*.40);
  o.frequency.exponentialRampToValueAtTime(heavy?44:lift?94:68,t+length);
  gain.gain.setValueAtTime(.0001,t);gain.gain.exponentialRampToValueAtTime(Math.max(.001,vol),t+.010);
  gain.gain.exponentialRampToValueAtTime(.0001,t+length);
  o.connect(gain);gain.connect(master);o.start(t);o.stop(t+length+.012);
  const drop=audioCtx.createOscillator(),dg=audioCtx.createGain();
  const offset=heavy ? .039:.020;
  drop.type='sine';drop.frequency.setValueAtTime(start*2.4,t+offset);
  drop.frequency.exponentialRampToValueAtTime(start*.8,t+offset+.071);
  dg.gain.setValueAtTime(.0001,t+offset);dg.gain.exponentialRampToValueAtTime(Math.max(.0002,vol*.28),t+offset+.007);dg.gain.exponentialRampToValueAtTime(.0001,t+offset+.077);
  drop.connect(dg);dg.connect(master);drop.start(t+offset);drop.stop(t+offset+.081);
  const source=audioCtx.createBufferSource(),filter=audioCtx.createBiquadFilter(),ng=audioCtx.createGain();
  source.buffer=noiseBuffer;filter.type='bandpass';filter.Q.value=1.6;
  filter.frequency.setValueAtTime(heavy?840:1350,t);filter.frequency.exponentialRampToValueAtTime(heavy?178:420,t+length*.68);
  ng.gain.setValueAtTime(.0001,t);ng.gain.exponentialRampToValueAtTime(Math.max(.001,vol*.48),t+.006);ng.gain.exponentialRampToValueAtTime(.0001,t+length*.79);
  source.connect(filter);filter.connect(ng);ng.connect(master);source.start(t);source.stop(t+length);
 }catch(_){}
}
function initGlass(){
 try{
  const options={alpha:false,antialias:false,depth:false,stencil:false,preserveDrawingBuffer:false,powerPreference:'high-performance'};
  gl=glass.getContext('webgl',options)||glass.getContext('experimental-webgl',options);
  if(!gl)return;
  function compile(kind,code){
   const shader=gl.createShader(kind);gl.shaderSource(shader,code);gl.compileShader(shader);
   if(!gl.getShaderParameter(shader,gl.COMPILE_STATUS))throw new Error('Shader unavailable');
   return shader;
  }
  const vertex=compile(gl.VERTEX_SHADER,'attribute vec2 corner; void main(){gl_Position=vec4(corner,0.0,1.0);}');
  const fragment=compile(gl.FRAGMENT_SHADER,byId('gel-shader').textContent);
  program=gl.createProgram();gl.attachShader(program,vertex);gl.attachShader(program,fragment);gl.linkProgram(program);
  if(!gl.getProgramParameter(program,gl.LINK_STATUS))throw new Error('Renderer unavailable');
  gl.useProgram(program);gl.deleteShader(vertex);gl.deleteShader(fragment);
  const triangle=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,triangle);
  gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,3,-1,-1,3]),gl.STATIC_DRAW);
  const pos=gl.getAttribLocation(program,'corner');gl.enableVertexAttribArray(pos);gl.vertexAttribPointer(pos,2,gl.FLOAT,false,0,0);
  locations={};
  for(const name of ['size','half','time','shape','grip','drag','gaze'])locations[name]=gl.getUniformLocation(program,'u_'+name);
  usingGlass=true;resize();renderGlass();glass.hidden=false;
 }catch(_){usingGlass=false;gl=null;glass.hidden=true;}
}
function renderGlass(){
 const u=locations;
 gl.uniform2f(u.size,glass.width,glass.height);gl.uniform1f(u.half,half);gl.uniform1f(u.time,liveTime%600);
 gl.uniform3f(u.shape,shape.height,shape.stretch,shape.lean);
 gl.uniform3f(u.grip,anchor[0],anchor[1],anchor[2]);
 gl.uniform4f(u.drag,pull[0].x,pull[1].x,pull[2].x,radius);
 gl.uniform3f(u.gaze,gaze.x,gaze.y,gaze.eyeClose);
 gl.drawArrays(gl.TRIANGLES,0,3);
}
glass.addEventListener('webglcontextlost',e=>{
 e.preventDefault();usingGlass=false;glass.hidden=true;renderPainting();
});
glass.addEventListener('webglcontextrestored',()=>{gl=null;initGlass();});
// A projected, painted version shares the same pose, picking, and springs.
const silhouette=[
 [-.38,2.426],[-.52,2.52,-.697,2.846,-.798,2.851],[-.929,2.865,-.973,2.502,-.936,2.241],
 [-1.016,2.058,-1.105,1.871,-1.084,1.640],[-1.083,1.468,-1.007,1.369,-.921,1.290],
 [-1.011,1.150,-1.031,.950,-1.066,.857],[-1.309,.672,-1.254,.289,-1.001,.198],
 [-.795,.109,-.62,.150,-.476,.160],[-.163,.111,.190,.112,.512,.16],
 [.798,.124,1.054,.153,1.172,.353],[1.323,.608,1.188,.786,1.061,.884],
 [1.049,1.073,.977,1.189,.920,1.289],[1.057,1.408,1.112,1.545,1.092,1.722],
 [1.080,1.950,1.008,2.108,.937,2.250],[.956,2.445,.933,2.816,.838,2.849],
 [.741,2.886,.540,2.595,.391,2.432],[.137,2.54,-.166,2.525,-.38,2.426]
];
const tailOutline=[
 [.743,.546],[.935,.545,1.232,.496,1.352,.715],[1.483,.914,1.388,1.063,1.295,1.096],
 [1.149,1.154,1.254,1.386,1.435,1.333],[1.713,1.263,1.783,.914,1.626,.581],
 [1.48,.238,1.094,.164,.78,.254],[.655,.296,.645,.475,.743,.546]
];
function plane(x,y,z){
 const p=warp([x,y,z]);
 return [dot(p,right),dot(p,up)];
}
function curvePath(points,z,closed=false){
 const path=new Path2D(),first=plane(points[0][0],points[0][1],z);
 path.moveTo(first[0],first[1]);
 for(let i=1;i<points.length;i++){
  const a=points[i];
  if(a.length===6)path.bezierCurveTo(...plane(a[0],a[1],z),...plane(a[2],a[3],z),...plane(a[4],a[5],z));
  else if(a.length===4)path.quadraticCurveTo(...plane(a[0],a[1],z),...plane(a[2],a[3],z));
  else path.lineTo(...plane(a[0],a[1],z));
 }
 if(closed)path.closePath();return path;
}
function addStops(gradient,stops){for(const s of stops)gradient.addColorStop(s[0],s[1]);return gradient;}
function glaze(a,b,z,stops){
 const start=plane(a[0],a[1],z),end=plane(b[0],b[1],z);
 return addStops(ctx.createLinearGradient(...start,...end),stops);
}
function ellipseAt(x,y,rx,ry,fill,stroke=null,weight=.01){
 ctx.beginPath();ctx.ellipse(x,y,Math.max(.0001,rx),Math.max(.0001,ry),0,0,2*Math.PI);
 if(fill){ctx.fillStyle=fill;ctx.fill();}
 if(stroke){ctx.strokeStyle=stroke;ctx.lineWidth=weight;ctx.stroke();}
}
function haze(x,y,rx,ry,stops){
 ctx.save();ctx.translate(x,y);ctx.scale(rx,ry);
 const grad=addStops(ctx.createRadialGradient(0,0,0,0,0,1),stops);
 ellipseAt(0,0,1,1,grad);ctx.restore();
}
function highlight(points,z,color,weight){
 ctx.strokeStyle=color;ctx.lineWidth=weight;ctx.lineCap='round';ctx.lineJoin='round';
 ctx.stroke(curvePath(points,z));
}
function renderPainting(){
 if(!ctx)return;
 ctx.setTransform(drawRatio,0,0,drawRatio,0,0);
 const bg=addStops(ctx.createLinearGradient(0,0,width*.87,height),[[0,'#ffe7c8'],[.46,'#f7d8b7'],[1,'#edbf9d']]);
 ctx.fillStyle=bg;ctx.fillRect(0,0,width,height);
 ctx.setTransform(drawRatio*scale,0,0,-drawRatio*scale,drawRatio*ox,drawRatio*oy);
 const px=.122,py=.208,fallFade=Math.exp(-shape.height*.9);
 haze(.29,-.08,2.45,.80,[[0,'rgba(112,62,36,.32)'],[.53,'rgba(140,81,48,.24)'],[1,'rgba(140,81,48,0)']]);
 haze(.19,-.108,1.75,.55,[[0,'rgba(105,57,32,.33)'],[.68,'rgba(117,65,37,.12)'],[1,'rgba(117,65,37,0)']]);
 const underside=addStops(ctx.createLinearGradient(0,.48,0,-.57),[[0,'#f3dfbf'],[.70,'#ead5b5'],[.87,'#d5bc9d'],[1,'#bda180']]);
 ellipseAt(px,py-.071,1.92,.634,underside,'#c8ac8b',.010);
 const porcelain=addStops(ctx.createLinearGradient(-.35,.92,.16,-.52),[[0,'#fff9e6'],[.39,'#f1e5ca'],[.68,'#fff8e4'],[1,'#fff6db']]);
 ellipseAt(px,py,1.918,.622,porcelain,'#fff9e9',.014);
 ellipseAt(px,py+.003,1.826,.578,null,'#c27c55',.016);
 ellipseAt(px,py+.002,1.766,.555,null,'#d7a079',.009);
 ctx.save();ctx.beginPath();ctx.ellipse(px,py,1.85,.60,0,0,Math.PI*2);ctx.clip();
 haze(.15+shape.height*.10,.086,1.40,.443,[[0,'rgba(155,78,22,'+(.36*fallFade+.07)+')'],[.68,'rgba(200,104,33,'+(.23*fallFade)+')'],[1,'rgba(213,126,38,0)']]);
 haze(-.05,.087,1.15,.35,[[0,'rgba(231,119,23,0)'],[.62,'rgba(249,163,68,'+(.22*fallFade)+')'],[.82,'rgba(255,188,91,'+(.44*fallFade)+')'],[1,'rgba(244,169,57,0)']]);
 ctx.restore();
 const tail=curvePath(tailOutline,-.29,true);
 ctx.fillStyle=glaze([1.,1.48],[1.57,.22],-.29,[[0,'rgba(255,201,109,.95)'],[.35,'rgba(246,143,34,.96)'],[.68,'rgba(243,117,18,.94)'],[1,'rgba(213,82,15,.93)']]);ctx.fill(tail);
 ctx.strokeStyle='rgba(177,76,13,.59)';ctx.lineWidth=.015;ctx.stroke(tail);
 highlight([[1.303,1.278],[1.408,1.328,1.53,1.196,1.549,1.115]],-.21,'rgba(255,246,199,.85)',.038);
 highlight([[1.615,.94],[1.644,.746,1.515,.53,1.405,.468]],-.21,'rgba(255,211,118,.69)',.031);
 const body=curvePath(silhouette,.015,true);
 ctx.fillStyle=glaze([-.83,2.70],[.84,.16],.015,[[0,'rgba(255,204,109,.92)'],[.19,'rgba(255,175,57,.94)'],[.44,'rgba(250,141,32,.93)'],[.70,'rgba(244,118,17,.94)'],[.92,'rgba(226,97,16,.94)'],[1,'rgba(247,155,48,.92)']]);
 ctx.fill(body);
 ctx.save();ctx.clip(body);
 const sx=1/Math.sqrt(1+shape.stretch),sy=1+shape.stretch;
 function glowAt(x,y,z,rx,ry,stops){const p=plane(x,y,z);haze(p[0],p[1],rx*sx,ry*sy,stops);}
 glowAt(-.25,2.09,.02,1.34,1.02,[[0,'rgba(255,213,109,.67)'],[.56,'rgba(255,184,75,.32)'],[1,'rgba(255,182,70,0)']]);
 glowAt(.16,.89,.02,.90,.81,[[0,'rgba(255,201,78,.62)'],[.63,'rgba(255,170,36,.38)'],[1,'rgba(255,173,48,0)']]);
 glowAt(.62,1.52,.04,.58,.61,[[0,'rgba(239,100,16,.29)'],[1,'rgba(220,86,16,0)']]);
 highlight([[-.98,2.18],[-1.10,1.94,-1.09,1.6,-1.038,1.49]],.00,'rgba(255,219,145,.58)',.073);
 highlight([[-1.09,.63],[-1.11,.285,-.88,.214,-.67,.239]],.01,'rgba(255,196,86,.68)',.058);
 highlight([[-.77,.24],[-.23,.146,.40,.141,.886,.238]],.01,'rgba(255,202,98,.64)',.042);
 for(const side of [-1,1]){
  const ear=curvePath([[side*.518,2.293],[side*.63,2.390,side*.761,2.641,side*.807,2.681],[side*.851,2.666,side*.887,2.392,side*.862,2.298],[side*.748,2.244,side*.615,2.262,side*.518,2.293]],.115,true);
  ctx.fillStyle=glaze([side*.70,2.71],[side*.71,2.25],.115,[[0,'rgba(195,77,17,.59)'],[.64,'rgba(230,100,32,.48)'],[1,'rgba(255,173,76,.13)']]);ctx.fill(ear);
  highlight([[side*.532,2.416],[side*.622,2.548,side*.743,2.764,side*.801,2.786]],.10,'rgba(255,244,196,.80)',.038);
  const thigh=curvePath([[side*.864,.86],[side*1.087,.879,side*1.226,.703,side*1.188,.486],[side*1.153,.245,side*.929,.201,side*.784,.271],[side*.672,.39,side*.692,.658,side*.864,.86]],.18,true);
  ctx.fillStyle=glaze([side*.72,.88],[side*1.07,.20],.18,[[0,'rgba(255,173,52,.05)'],[.64,'rgba(237,104,18,.33)'],[.90,'rgba(227,96,14,.48)'],[1,'rgba(255,183,64,.66)']]);ctx.fill(thigh);
 }
 ctx.restore();
 ctx.strokeStyle='rgba(168,75,18,.43)';ctx.lineWidth=.014;ctx.stroke(body);
 for(const s of [-1,1]){
  const leg=curvePath([[s*.44,1.063],[s*.622,1.073,s*.714,.829,s*.716,.561],[s*.733,.455,s*.835,.372,s*.802,.257],[s*.773,.103,s*.387,.090,s*.275,.214],[s*.216,.304,s*.302,.45,s*.292,.609],[s*.288,.888,s*.299,1.004,s*.44,1.063]],.50,true);
  ctx.fillStyle=glaze([s*.35,1.03],[s*.63,.142],.50,[[0,'rgba(253,143,39,.12)'],[.44,'rgba(251,146,39,.73)'],[.78,'rgba(255,171,55,.93)'],[.95,'rgba(228,98,18,.88)'],[1,'rgba(255,188,78,.91)']]);ctx.fill(leg);
  highlight([[s*.308,.591],[s*.324,.431,s*.221,.348,s*.298,.240]],.51,'rgba(203,95,22,.43)',.013);
  highlight([[s*.39,.325],[s*.444,.369,s*.496,.365,s*.537,.352]],.57,'rgba(255,232,159,.71)',.035);
  highlight([[s*.695,.233],[s*.634,.194,s*.500,.161,s*.417,.201]],.53,'rgba(255,210,105,.75)',.026);
  const b=plane(s*.649,1.586,.66);
  haze(b[0],b[1],.221*sx,.108*sy,[[0,'rgba(224,94,48,.46)'],[1,'rgba(238,106,45,0)']]);
  for(const dotPos of [[.62,1.578,.014],[.703,1.602,.012],[.679,1.533,.010]]){
   const p=plane(s*dotPos[0],dotPos[1],.651);ellipseAt(...p,dotPos[2],dotPos[2]*.88,'rgba(150,76,24,.58)');
  }
 }
 for(const b of [[-.655,2.097,.39,.044],[-.56,2.175,.325,.018],[.62,2.135,.39,.032],[.819,1.734,.409,.023],[-.775,.825,.373,.042],[.291,1.07,.566,.033],[-.088,.736,.532,.043],[.015,.817,.489,.018],[.849,.569,.368,.028],[1.528,.847,-.252,.025],[.767,2.543,.02,.022]]){
  const p=plane(b[0],b[1]+.006*Math.sin(liveTime*.74+b[0]*3),b[2]),r=b[3]*sx;
  const grad=addStops(ctx.createRadialGradient(p[0]-r*.24,p[1]+r*.31,r*.07,p[0],p[1],r),[[0,'rgba(255,244,190,.75)'],[.40,'rgba(255,210,109,.19)'],[.72,'rgba(247,143,37,.28)'],[1,'rgba(166,91,22,.61)']]);
  ellipseAt(...p,r,r*1.04,grad);ellipseAt(p[0]-r*.31,p[1]+r*.43,r*.25,r*.18,'rgba(255,250,209,.91)');
 }
 // Curved softbox reflections and a second, softer edge light.
 const shine=curvePath([[-.807,2.094],[-.763,2.256,-.625,2.339,-.531,2.322],[-.489,2.302,-.521,2.246,-.574,2.214],[-.662,2.160,-.695,2.068,-.753,2.049],[-.799,2.022,-.824,2.043,-.807,2.094]],.402,true);
 ctx.fillStyle='rgba(255,247,211,.86)';ctx.fill(shine);
 highlight([[-.902,1.987],[-.939,1.909,-.949,1.852,-.946,1.813]],.34,'rgba(255,236,178,.85)',.043);
 highlight([[.961,2.032],[1.026,1.886,1.020,1.757,.997,1.669]],.15,'rgba(255,218,133,.72)',.038);
 highlight([[-.976,.834],[-1.050,.802,-1.096,.712,-1.077,.648]],.19,'rgba(255,235,170,.75)',.038);
 const mouthZ=.866,eyeY=1.839+gaze.y*.016;
 for(const s of [-1,1]){
  const p=plane(s*.405+gaze.x*.018,eyeY,.794);
  const rx=.138*sx,ry=.150*sy*(1-gaze.eyeClose*.92);
  if(gaze.eyeClose>.84){
   highlight([[s*.405-.128+gaze.x*.018,eyeY],[s*.405-.053,eyeY+.063,s*.405+.057,eyeY+.061,s*.405+.128+gaze.x*.018,eyeY]],.794,'#703819',.029);
  }else{
   ctx.save();ctx.translate(...p);ctx.scale(rx,ry);
   const eye=addStops(ctx.createRadialGradient(-.29,.39,.07,0,.06,1.13),[[0,'#422719'],[.47,'#30221e'],[.77,'#4a2c1b'],[1,'#976132']]);
   ellipseAt(0,0,1,1,eye);ellipseAt(gaze.x*.16,gaze.y*.12,.75,.81,'#2c211c');
   ellipseAt(-.27+gaze.x*.15,.38+gaze.y*.12,.205,.193,'#fff4d8');
   ellipseAt(.24+gaze.x*.14,.56+gaze.y*.12,.064,.061,'#fff6e2');ctx.restore();
  }
 }
 const nose=curvePath([[-.068,1.663],[-.036,1.682,.055,1.679,.074,1.654],[.085,1.638,.030,1.589,.005,1.590],[-.022,1.586,-.084,1.639,-.068,1.663]],mouthZ,true);
 ctx.fillStyle=glaze([0,1.681],[0,1.59],mouthZ,[[0,'#a45d34'],[1,'#733a1e']]);ctx.fill(nose);
 highlight([[-.044,1.652],[-.032,1.66,-.017,1.66,-.007,1.658]],mouthZ,'#d89b6d',.014);
 highlight([[0,1.599],[0,1.556]],mouthZ,'#79411f',.023);
 for(const s of [-1,1])highlight([[0,1.558],[s*.04,1.474,s*.101,1.491,s*.154,1.568]],mouthZ,'#79411f',.023);
}
const proxies=[
 [0,.91,-.09,1.06,.84,.85],[-.80,.54,-.065,.41,.43,.58],[.80,.54,-.065,.41,.43,.58],
 [-.5,.62,.55,.30,.49,.32],[.5,.62,.55,.30,.49,.32],
 [-.52,.31,.64,.32,.204,.35],[.52,.31,.64,.32,.204,.35],
 [0,1.742,.054,1.075,.807,.833],[-.70,1.52,.265,.40,.35,.50],[.70,1.52,.265,.40,.35,.50],
 [-.62,2.18,.012,.38,.34,.274,'ear'],[.62,2.18,.012,.38,.34,.274,'ear'],
 [-.742,2.49,-.015,.222,.283,.166,'ear'],[.742,2.49,-.015,.222,.283,.166,'ear'],
 [-.806,2.721,-.045,.110,.154,.097,'ear'],[.806,2.721,-.045,.110,.154,.097,'ear'],
 [1.068,.426,-.34,.39,.215,.22,'tail'],[1.37,.566,-.35,.252,.221,.21,'tail'],
 [1.525,.813,-.314,.18,.31,.18,'tail'],[1.491,1.071,-.223,.179,.235,.169,'tail'],[1.351,1.218,-.145,.197,.144,.145,'tail']
];
function inversePose(p){
 const sy=1+shape.stretch,y=.14+(p[1]-.14-shape.height)/sy;
 return [(p[0]-shape.lean*(y-.14)*.46)*Math.sqrt(sy),y,p[2]*Math.sqrt(sy)];
}
function pick(x,y){
 const horizontal=(x-width*.5)/scale,vertical=(height*.5-y)/scale;
 const worldStart=focus.map((v,i)=>v-8*ray[i]+horizontal*right[i]+vertical*up[i]);
 const start=inversePose(worldStart),next=inversePose(worldStart.map((v,i)=>v+ray[i]));
 const dir=next.map((v,i)=>v-start[i]);
 let best=Infinity,point=null,special=null;
 for(const lobe of proxies){
  const center=inversePose(warp(lobe));
  const a=[dir[0]/lobe[3],dir[1]/lobe[4],dir[2]/lobe[5]];
  const b=[(start[0]-center[0])/lobe[3],(start[1]-center[1])/lobe[4],(start[2]-center[2])/lobe[5]];
  const aa=dot(a,a),ab=dot(a,b),discriminant=ab*ab-aa*(dot(b,b)-1);
  if(discriminant<0)continue;
  const hit=(-ab-Math.sqrt(discriminant))/aa;
  if(hit<=0||hit>=best)continue;
  best=hit;point=start.map((v,i)=>v+dir[i]*hit-center[i]+lobe[i]);special=lobe[6];
 }
 if(!point)return null;
 let kind='belly',ax=Math.abs(point[0]);
 if(special==='tail')kind='tail';
 else if(point[1]>2.275&&ax>.405)kind='ear';
 else if(point[1]>1.30&&point[1]<1.93&&ax>.515)kind='cheek';
 else if(point[1]>1.68)kind='head';
 return {kind,point};
}
function gazeAt(x,y){
 const face=project([0,1.84,.79]);
 gaze.tx=clamp((x-face[0])/(scale*.96),-1,1);
 gaze.ty=clamp((face[1]-y)/(scale*.84),-1,1);
 lastPointerTime=liveTime;
}
function feel(kind,point,synthetic=false){
 anchor=point.slice();
 radius=kind==='tail' ? .59:kind==='ear' ? .75:kind==='cheek' ? .68:.825;
 grip={kind,synthetic,since:liveTime,dx:0,dy:0,path:0,heading:0,lastSound:0};
 targetPull=[0,0,kind==='belly'?-.14:kind==='head'?-.047:0];
 targetSquish=kind==='belly'?-.075:-.024;
 targetLean=0;
 const strength=1.17-.55*firm;
 squish.v-=(kind==='belly' ? .80:.28)*strength;
 pull[2].v-=(kind==='belly' ? .71:.20)*strength;
 if(!synthetic)prepareSound();
 wetSound('poke',kind==='tail' ? .54:.83);
 stage.classList.add('touched','grabbed');lastContact=liveTime;
}
function dragBy(dx,dy,travel){
 if(!grip)return;
 const previousX=grip.dx;
 grip.dx=dx;grip.dy=dy;grip.path+=travel;
 const part=grip.kind;
 const max=part==='tail' ? .89:part==='ear' ? .91:part==='cheek' ? .72:.52;
 const reach=max*(1.14-.42*firm);
 const dist=Math.hypot(dx,dy),ratio=dist>.0001?Math.tanh(dist/reach)*reach/dist:1;
 const x=dx*ratio,y=dy*ratio,soft=1.05-.29*firm;
 if(part==='belly'){
  targetPull[0]=x*.74;targetPull[1]=y*.64;
  targetLean=x*.30;
 }else if(part==='head'){
  targetPull=[x*.31,y*.35,-.065];
  targetLean=x*.24;
  if(Math.abs(dx)<1.40 && Math.abs(dy)<.76){
   const step=dx-previousX,dir=Math.sign(step);
   if(Math.abs(step)>.008){
    if(grip.heading&&dir!==grip.heading)rubEnergy=Math.min(1.85,rubEnergy+.15);
    grip.heading=dir;
   }
   rubEnergy=Math.min(1.85,rubEnergy+Math.min(.46,travel)*1.76);
  }
 }else{
  targetPull=[x*soft,y*soft,.037*unit(dist)];
  targetLean=x*(part==='tail' ? .11:.21);
  targetSquish=clamp(y*(part==='ear' ? .087:.046),-.118,.092);
  if(grip.path-grip.lastSound>.55){
   wetSound('stretch',.60+Math.min(.33,dist*.36));grip.lastSound=grip.path;
  }
 }
}
function releaseGrip(sing=true){
 if(!grip)return;
 const part=grip.kind,amount=Math.hypot(pull[0].x,pull[1].x,pull[2].x);
 if(sing)wetSound('release',.49+amount*.68);
 for(const a of pull)a.v-=a.x*(1.7+.6*firm);
 squish.v+=part==='belly' ? .45:.12;
 lastContact=liveTime;grip=null;
 targetPull=[0,0,0];targetSquish=0;targetLean=0;
 stage.classList.remove('grabbed');
 if(pointerId!==null){
  const captured=pointerId;pointerId=null;
  try{if(stage.hasPointerCapture(captured))stage.releasePointerCapture(captured);}catch(_){}
 }
}
function dropCat(){
 releaseGrip(false);
 targetPull=[0,0,0];targetLean=0;targetSquish=0;
 for(const a of pull){a.x=0;a.v=0;}
 shape.height=4.24;fallSpeed=-.29;airborne=true;landedAt=-100;
 squish.x=.058;squish.v=.10;lean.x=-.068;lean.v=.11;
 rubEnergy=0;affection=0;purrVolume(0);lastContact=liveTime;
 stage.classList.add('touched');
}
function stepSpring(s,target,w,damping,dt){
 s.v+=((target-s.x)*w*w-2*damping*w*s.v)*dt;
 s.x+=s.v*dt;
 if(Math.abs(s.x-target)<.000012&&Math.abs(s.v)<.00017){s.x=target;s.v=0;}
}
function physics(dt){
 const frequency=8.15+10.0*firm;
 const damping=grip ? .69:.193+.145*firm;
 if(airborne){
  fallSpeed-=18.5*dt;shape.height+=fallSpeed*dt;
  if(shape.height<=0){
   const impact=Math.abs(fallSpeed);shape.height=0;
   if(impact>3.1){
    squish.v-=Math.min(5.58,impact*(.461-.111*firm));
    lean.v+=.98*(1.13-.37*firm);
    wetSound('land',Math.min(1.23,impact*.102));
    fallSpeed=impact*(.105+.06*firm);landedAt=liveTime;
   }else{
    squish.v-=impact*.16;fallSpeed=0;airborne=false;
   }
  }
 }
 if(grip&&grip.kind==='belly'){
  const held=unit((liveTime-grip.since)/.54);
  targetSquish=-(.076+.147*held)*(1.20-.67*firm)+clamp(grip.dy*.115,-.10,.047);
  targetPull[2]=-(.109+.119*held)*(1.16-.51*firm);
 }
 stepSpring(squish,targetSquish,frequency*(grip?1.34:1),damping,dt);
 stepSpring(lean,targetLean,frequency*.80,damping*.96,dt);
 for(let i=0;i<3;i++)stepSpring(pull[i],targetPull[i],frequency*(grip?1.69:1.10)*(1+i*.031),grip ? .73:damping,dt);
}
function heart(){
 const p=project([.66,2.51,.045]),el=document.createElement('span');
 el.className='heart';el.textContent='♥';el.style.left=(p[0]+Math.random()*13)+'px';el.style.top=p[1]+'px';
 el.style.setProperty('--dx',(10+Math.random()*20)+'px');el.style.fontSize=(16+Math.random()*8)+'px';
 byId('emotes').appendChild(el);el.addEventListener('animationend',()=>el.remove(),{once:true});setTimeout(()=>el.remove(),1600);
}
function updateExpression(dt){
 rubEnergy=Math.max(0,rubEnergy-dt*(grip ? .63:.82));
 const happy=unit((rubEnergy-.23)/.72);
 affection+= (happy-affection)*(1-Math.exp(-dt*(happy>affection?10:3.8)));
 if(affection<.003)affection=0;
 purrVolume(affection);
 heartClock-=dt;
 if(affection>.65&&heartClock<=0&&!reduceMotion){heart();heartClock=.56+Math.random()*.19;}
 if(!grip&&!activeDemo&&liveTime-lastPointerTime>3.2){gaze.tx=0;gaze.ty=0;}
 const rate=1-Math.exp(-dt*12);
 gaze.x+=(gaze.tx-gaze.x)*rate;gaze.y+=(gaze.ty-gaze.y)*rate;
 if(liveTime>nextBlink){blink=.235;nextBlink=liveTime+3.9+Math.random()*3.2;}
 blink=Math.max(0,blink-dt);
 const lid=blink>0?Math.pow(Math.sin(Math.PI*blink/.235),1.35):0;
 gaze.eyeClose=clamp(Math.max(lid,affection*.989),0,1);
}
function styleState(){
 let label='Free play';
 if(airborne&&landedAt<0)label='Dropping';
 else if(affection>.48)label='Purring';
 else if(grip){
  if(grip.kind==='belly')label=liveTime-grip.since>.32?'Squeezing':'Poking';
  else if(grip.kind==='head')label=grip.path>.12?'Head rub':'Poking';
  else label=grip.kind==='ear'?'Ear stretch':grip.kind==='tail'?'Tail stretch':'Cheek stretch';
 }else if(liveTime-landedAt<1.42||Math.abs(squish.v)>.13||Math.abs(lean.v)>.14||Math.abs(pull[0].v)+Math.abs(pull[1].v)>.19)label='Wobbling';
 else if(activeDemo)label='Demo playing';
 if(activity.textContent!==label){activity.textContent=label;stage.dataset.active=String(label!=='Free play');}
}
function localPointer(e){
 const b=stage.getBoundingClientRect();
 return [e.clientX-b.left,e.clientY-b.top];
}
stage.addEventListener('pointerdown',e=>{
 if(demoButton.contains(e.target)||pointerId!==null||e.button!==0)return;
 const pos=localPointer(e);gazeAt(...pos);const hit=pick(...pos);
 if(!hit)return;
 e.preventDefault();pointerId=e.pointerId;feel(hit.kind,hit.point);
 grip.start=pos;grip.last=pos;
 try{stage.setPointerCapture(e.pointerId);}catch(_){}
});
window.addEventListener('pointermove',e=>{
 if(activeDemo)return;
 if(pointerId!==null&&e.pointerId!==pointerId)return;
 const pos=localPointer(e);
 gazeAt(...pos);
 if(!grip||grip.synthetic||e.pointerId!==pointerId)return;
 const dx=(pos[0]-grip.start[0])/scale,dy=(grip.start[1]-pos[1])/(scale*up[1]);
 const travel=Math.hypot(pos[0]-grip.last[0],pos[1]-grip.last[1])/scale;
 dragBy(dx,dy,travel);grip.last=pos;
},{passive:true});
function finishPointer(e){if(pointerId!==null&&e.pointerId===pointerId)releaseGrip(e.type!=='pointercancel');}
window.addEventListener('pointerup',finishPointer);
window.addEventListener('pointercancel',finishPointer);
stage.addEventListener('lostpointercapture',finishPointer);
stage.addEventListener('contextmenu',e=>e.preventDefault());
window.addEventListener('blur',()=>{if(grip&&!grip.synthetic)releaseGrip(false);});
function makeDemoHand(){
 const el=document.createElement('div');el.setAttribute('aria-hidden','true');
 el.style.cssText='position:absolute;top:0;left:0;width:37px;height:43px;pointer-events:none;z-index:3;filter:drop-shadow(0 2px 2px #66412345);will-change:transform';
 el.innerHTML='<svg viewBox="0 0 38 44"><circle cx="8" cy="8" r="6.5" fill="#fff7de" fill-opacity=".42" stroke="#fff8e7" stroke-width="1.5"/><path d="M8.2 7.3c1.4-.9 2.8.2 3.7 1.7l6 9.1-1.3-3.9c-.8-2.8 2.6-4.1 4-1.8l2.8 5.2-.8-3.6c-.5-2.4 2.6-3.4 3.7-1.1l2.3 5.5-.4-2.6c-.4-2.2 2.2-2.6 3.1-.5l3 7.4c2 5.1 1 8.5-1.6 11.3l-5.4 4.9c-1.5 1.2-3.5.9-4.8-.3L10.2 28.1c-2-1.9-.3-4.8 1.9-3.7l5.5 3L7.1 11.1c-.9-1.5-.5-3 .9-3.8Z" fill="#fff7df" stroke="#714a32" stroke-width="1.35" stroke-linejoin="round"/></svg>';
 stage.appendChild(el);el.hidden=true;return el;
}
function beginDemo(){
 if(activeDemo)return;
 releaseGrip(false);
 savedFirm=Number(firmnessInput.value);activeDemo=true;demoPhase=-1;demoStarted=performance.now();
 if(!demoHand)demoHand=makeDemoHand();
 stage.classList.add('touched');
 demoButton.setAttribute('aria-pressed','true');demoButton.setAttribute('aria-label','Stop hands-free demo');byId('demo-label').textContent='Stop';
}
function stopDemo(){
 if(!activeDemo)return;
 activeDemo=false;demoPhase=-1;
 if(grip&&grip.synthetic)releaseGrip(false);
 rubEnergy=0;if(demoHand)demoHand.hidden=true;
 setFirmness(savedFirm);demoButton.setAttribute('aria-pressed','false');demoButton.setAttribute('aria-label','Play hands-free demo');byId('demo-label').textContent='Demo';
}
const beats=[0,.86,2.67,3.68,5.63,6.52,8.10,8.86,10.48,11.20,14.37,15.72,18.00,19.17,20];
const demoGrabs={
 1:['belly',[.02,.91,.749]],3:['ear',[-.80,2.733,.022]],5:['cheek',[.832,1.596,.589]],
 7:['tail',[1.385,1.251,-.089]],9:['head',[0,2.199,.681]],12:['belly',[-.12,.859,.73]]
};
function demoStep(now){
 const t=((now-demoStarted)/1000)%20;
 let index=0;while(index<beats.length-2&&t>=beats[index+1])index++;
 if(demoPhase!==index){
  if(grip&&grip.synthetic)releaseGrip();
  demoPhase=index;
  if(index===0)setFirmness(20);
  if(index===7)setFirmness(54);
  if(index===11){setFirmness(24);dropCat();}
  if(index===12)setFirmness(84);
  const action=demoGrabs[index];if(action)feel(action[0],action[1],true);
 }
 if(!grip||!grip.synthetic){demoHand.hidden=true;return;}
 const a=unit((t-beats[index])/(beats[index+1]-beats[index])),s=unit(a*1.72),ease=s*s*(3-2*s);
 let dx=0,dy=0;
 if(index===1||index===12){dx=.055*Math.sin(a*Math.PI);dy=-.32*ease;}
 if(index===3){dx=-.34*ease;dy=.73*ease;}
 if(index===5){dx=.75*ease;dy=.085*ease;}
 if(index===7){dx=.53*ease;dy=.58*ease;}
 if(index===9){dx=.38*Math.sin(a*Math.PI*8.2);dy=.028*Math.cos(a*Math.PI*8.2);}
 const distance=Math.hypot(dx-grip.dx,dy-grip.dy);
 dragBy(dx,dy,distance);
 const spot=anchor.slice();if(index===9){spot[0]+=dx;spot[1]+=dy;}
 const p=project(spot);
 gazeAt(p[0],p[1]);demoHand.hidden=false;demoHand.style.transform='translate('+(p[0]-8)+'px,'+(p[1]-8)+'px)';
}
window.addEventListener('pointerdown',e=>{
 stopClick=activeDemo&&demoButton.contains(e.target);
 if(activeDemo)stopDemo();
 if(!soundButton.contains(e.target))prepareSound();
},true);
demoButton.addEventListener('click',()=>{
 if(stopClick){stopClick=false;return;}
 prepareSound();if(activeDemo)stopDemo();else beginDemo();
});
byId('drop').addEventListener('click',()=>{stopDemo();prepareSound();dropCat();});
firmnessInput.addEventListener('input',e=>{
 const n=Number(e.target.value),prior=firm;
 stopDemo();setFirmness(n);
 if(!airborne)squish.v-=Math.min(.22,Math.abs(firm-prior)*.58);
});
soundButton.addEventListener('click',()=>{
 soundEnabled=!soundEnabled;
 if(soundEnabled){prepareSound();if(master)master.gain.setTargetAtTime(.64,audioCtx.currentTime,.016);wetSound('release',.48);}
 else{purrVolume(0);if(master)master.gain.setTargetAtTime(0,audioCtx.currentTime,.016);}
 soundButton.setAttribute('aria-pressed',String(soundEnabled));
 soundButton.setAttribute('aria-label',soundEnabled?'Sound on. Mute sounds':'Sound off. Enable sounds');
 byId('sound-label').textContent=soundEnabled?'Sound on':'Sound off';
});
window.addEventListener('resize',()=>{needSize=true;});
if(window.ResizeObserver)new ResizeObserver(()=>{needSize=true;}).observe(stage);
document.addEventListener('visibilitychange',()=>{
 if(document.hidden){purrVolume(0);if(grip&&!grip.synthetic)releaseGrip(false);}
});
let previous=performance.now(),lastDraw=-1000,slowFrames=0;
function tick(now){
 const elapsed=Math.max(0,(now-previous)/1000);previous=now;
 if(document.hidden){requestAnimationFrame(tick);return;}
 if(needSize)resize();
 const dt=Math.min(.12,elapsed);liveTime+=dt;
 if(activeDemo)demoStep(now);
 const steps=Math.max(1,Math.ceil(dt*128));
 for(let i=0;i<steps;i++)physics(dt/steps);
 const breathing=reduceMotion?0:.0040*Math.sin(liveTime*2.11);
 shape.stretch=clamp(squish.x+breathing,-.423,.307);
 shape.lean=clamp(lean.x+(reduceMotion?0:.005*Math.sin(liveTime*1.16))+.009*affection*Math.sin(liveTime*26),-.53,.53);
 updateExpression(dt);styleState();
 const moving=grip||airborne||activeDemo||affection>.05||blink>0||liveTime-lastContact<2.6||liveTime-lastPointerTime<.45;
 if(moving||now-lastDraw>36){
  if(usingGlass)renderGlass();else renderPainting();lastDraw=now;
 }
 if(usingGlass&&liveTime>1.25){
  slowFrames=elapsed>.052?slowFrames+1:Math.max(0,slowFrames-1);
  if(slowFrames>=7){
   if(pixelScale>.60){pixelScale=Math.max(.59,pixelScale*.825);needSize=true;}
   else if(elapsed>.115){usingGlass=false;glass.hidden=true;renderPainting();}
   slowFrames=0;
  }
 }
 requestAnimationFrame(tick);
}
resize();renderPainting();
if(location.hash.toLowerCase()==='#demo')beginDemo();
requestAnimationFrame(()=>requestAnimationFrame(()=>{
 initGlass();previous=performance.now();requestAnimationFrame(tick);
}));
})();
</script>
</body>
</html>