UXInteract / task_044 /solution /code /aquarium.svg
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
|
Raw History Blame Contribute Delete
3.21 kB
<svg xmlns="http://www.w3.org/2000/svg" width="520" height="300" viewBox="0 0 520 300">
<defs><linearGradient id="glass-water" x1="0" y1="0" x2="1" y2="0"><stop offset="0" stop-color="#b8e8fb" stop-opacity=".55"/><stop offset=".6" stop-color="#9bd9ff" stop-opacity=".3"/><stop offset="1" stop-color="#dff5ff" stop-opacity=".6"/></linearGradient></defs>
<rect width="520" height="300" fill="url(#glass-water)"/>
<rect x="10" y="10" width="500" height="280" fill="none" stroke="#d8f4ff" stroke-width="4"/>
<path d="M 28 20 L 28 182 M 37 22 L 37 110 M 490 25 L 490 90" fill="none" stroke="#ffffff" stroke-opacity=".58" stroke-width="5" stroke-linecap="round"/>
<text x="20" y="30">Interactive koi aquarium</text>
<g id="fish-big" data-fish="0" transform="translate(160 150)"><ellipse rx="45" ry="22" fill="gold"/><polygon class="tail" data-tail="0" points="-45,0 -70,-20 -70,20" fill="orange"/><circle cx="15" cy="-5" r="4" fill="black"/></g>
<g id="fish-small" data-fish="1" transform="translate(330 120)"><ellipse rx="32" ry="16" fill="gold"/><polygon class="tail" data-tail="1" points="-32,0 -52,-15 -52,15" fill="#e66"/><circle cx="12" cy="-4" r="4" fill="black"/></g>
<rect x="35" y="220" width="16" height="60" fill="green"/>
<rect x="55" y="205" width="14" height="75" fill="green"/>
<circle cx="430" cy="245" r="20" fill="#795548"/><circle cx="470" cy="260" r="14" fill="#8b5a2b"/>
<g id="food-layer" data-food-layer=""/>
<script><![CDATA[
const svg=document.documentElement, foodLayer=document.getElementById('food-layer');
const state={time:0, fish:[{x:160,y:150,tail:0},{x:330,y:120,tail:1.7}], food:[]};
function render(){state.fish.forEach((f,i)=>{document.getElementById(i?'fish-small':'fish-big').setAttribute('transform',`translate(${f.x} ${f.y})`);document.querySelectorAll('.tail')[i].setAttribute('transform',`rotate(${Math.sin(f.tail)*18} ${i ? -32 : -45} 0)`)});[...foodLayer.children].forEach(n=>n.remove());state.food.forEach(p=>{let c=document.createElementNS(svg.namespaceURI,'circle');c.setAttribute('cx',p.x);c.setAttribute('cy',p.y);c.setAttribute('r',4);c.setAttribute('fill','#8b5a2b');foodLayer.append(c)})}
function feed(x,y){for(let i=0;i<3;i++)state.food.push({x:x+(i-1)*6,y});render()}
function step(ms){state.time+=ms;state.food.forEach(p=>p.y=Math.min(270,p.y+ms*.025));state.fish.forEach((f,i)=>{f.tail+=ms*(i?0.011:0.008);if(state.food.length){let p=state.food[0],dx=p.x-f.x,dy=p.y-f.y,d=Math.hypot(dx,dy)||1,s=Math.min(d,ms*.08);f.x+=dx/d*s;f.y+=dy/d*s;if(d<12)state.food.shift()}else{f.x=260+(i?120:100)*Math.cos(state.time/(i?1350:1700)+i);f.y=145+(i?38:55)*Math.sin(state.time/(i?900:1200)+i)}});render()}
svg.addEventListener('click',e=>{let p=svg.createSVGPoint();p.x=e.clientX;p.y=e.clientY;p=p.matrixTransform(svg.getScreenCTM().inverse());feed(p.x,p.y)});
let manual=false,last=performance.now();function loop(now){if(!manual)step(Math.min(50,now-last));last=now;requestAnimationFrame(loop)}
window.aquariumTest={feed:(x,y)=>{manual=true;feed(x,y)},step:(ms)=>{manual=true;step(ms)},snapshot:()=>{manual=true;return JSON.parse(JSON.stringify(state))}};render();requestAnimationFrame(loop);
]]></script>
</svg>