// tiny isometric renderer on canvas 2d const PAL = { mint: { top:'#d6f6f1', left:'#bde7ee', right:'#a6d2e8', ink:'#7fb6cf' }, lav: { top:'#f4e3f7', left:'#e1cdf2', right:'#c9b6ec', ink:'#a993dc' }, peri: { top:'#e2e6fd', left:'#c6cdf5', right:'#aab4ec', ink:'#8a95dd' }, cream: { top:'#fff6f2', left:'#f9e4ed', right:'#ebd1e8', ink:'#d4b0d2' }, rose: { top:'#ffe0ee', left:'#ffc9e2', right:'#f2b2d6', ink:'#e58fbf' }, ghost: { top:'rgba(255,255,255,.18)', left:'rgba(255,255,255,.12)', right:'rgba(255,255,255,.08)', ink:'rgba(120,90,170,.55)' }, }; const C30=Math.cos(Math.PI/6), S30=.5; class Iso { constructor(canvas){ this.cv=canvas; this.ctx=canvas.getContext('2d'); this.scale=40; this.ox=0; this.oy=0; this.dpr=1; this.imgs={}; } resize(){ const r=this.cv.getBoundingClientRect(); this.dpr=Math.min(devicePixelRatio||1,2); this.cv.width=r.width*this.dpr; this.cv.height=r.height*this.dpr; this.w=r.width; this.h=r.height; } P(x,y,z){ return [this.ox+(x-y)*C30*this.scale, this.oy+(x+y)*S30*this.scale - z*this.scale]; } img(src){ if(!this.imgs[src]){const i=new Image();i.src=src;this.imgs[src]=i;} const i=this.imgs[src]; return i.complete&&i.naturalWidth?i:null; } poly(pts,fill,stroke,dash){ const c=this.ctx; c.beginPath(); pts.forEach((p,i)=>i?c.lineTo(p[0],p[1]):c.moveTo(p[0],p[1])); c.closePath(); if(fill){c.fillStyle=fill;c.fill();} if(stroke){c.strokeStyle=stroke;c.lineWidth=1;c.setLineDash(dash||[]);c.stroke();c.setLineDash([]);} } // map unit square onto a face. origin=top-left corner of face, u=across, v=down face(o,u,v,fn){ const c=this.ctx; c.save(); c.transform(u[0]-o[0],u[1]-o[1],v[0]-o[0],v[1]-o[1],o[0],o[1]); c.beginPath(); c.rect(0,0,1,1); c.clip(); fn(c); c.restore(); } box(x,y,z,w,d,h,pal,deco,opt={}){ const P=this.P.bind(this); const t=[P(x,y,z+h),P(x+w,y,z+h),P(x+w,y+d,z+h),P(x,y+d,z+h)]; const L=[P(x,y+d,z+h),P(x+w,y+d,z+h),P(x+w,y+d,z),P(x,y+d,z)]; const R=[P(x+w,y,z+h),P(x+w,y+d,z+h),P(x+w,y+d,z),P(x+w,y,z)]; if(opt.ghost){ const s=opt.stroke||'rgba(110,80,170,.6)'; this.poly(t,'rgba(255,255,255,.14)',s,[4,4]); this.poly(L,'rgba(255,255,255,.07)',s,[4,4]); this.poly(R,'rgba(255,255,255,.04)',s,[4,4]); return; } this.poly(L,pal.left); this.poly(R,pal.right); this.poly(t,pal.top); if(deco){ deco.left&&this.face(L[0],L[1],L[3],c=>deco.left(c,w,h)); deco.right&&this.face(R[0],R[1],R[3],c=>deco.right(c,d,h)); deco.top&&this.face(t[0],t[1],t[3],c=>deco.top(c,w,d)); } // crisp highlight edges const c=this.ctx; c.strokeStyle='rgba(255,255,255,.85)'; c.lineWidth=1; c.beginPath(); c.moveTo(...t[3]); c.lineTo(...t[0]); c.lineTo(...t[1]); c.stroke(); c.beginPath(); c.moveTo(...L[1]); c.lineTo(...L[2]); c.strokeStyle='rgba(255,255,255,.5)'; c.stroke(); if(opt.stroke){ this.poly(t,null,opt.stroke); this.poly(L,null,opt.stroke); this.poly(R,null,opt.stroke); } } outline(x,y,z,w,d,h){ // hexagon silhouette for hit testing const P=this.P.bind(this); const p=new Path2D(); const pts=[P(x,y,z+h),P(x+w,y,z+h),P(x+w,y,z),P(x+w,y+d,z),P(x,y+d,z),P(x,y+d,z+h)]; pts.forEach((q,i)=>i?p.lineTo(q[0],q[1]):p.moveTo(q[0],q[1])); p.closePath(); return p; } } // face decorations const DECO = { windows(color,cols=3,rowsPerUnit=1.6){ return (c,w,h)=>{ const rows=Math.max(1,Math.round(h*rowsPerUnit)); const n=Math.max(1,Math.round(w*cols/2)); c.fillStyle=color; for(let r=0;r{ const rows=Math.max(2,Math.round(h*3)); for(let r=0;r.2; c.fillStyle=on?led[k%led.length]:'rgba(255,255,255,.35)'; c.fillRect(.1+k*.06,(r+.32)/rows,.03,.25/rows);} } }; }, picture(iso,src,frame){ return (c,w,h)=>{ c.fillStyle=frame; c.fillRect(.08,.12,.84,.76); const im=iso.img(src); if(im){ c.drawImage(im,.11,.16,.78,.68);} else { c.fillStyle='rgba(255,255,255,.5)'; c.fillRect(.11,.16,.78,.68);} }; }, stripes(color,n=6){ return (c)=>{ c.fillStyle=color; for(let i=0;i{ c.fillStyle=bg; c.fillRect(.08,.2,.84,.5); c.save(); c.scale(1/(w*40),1/(h*40)); c.fillStyle=fg; c.font='10px Silkscreen'; c.textBaseline='middle'; c.fillText(text,.12*w*40,.45*h*40,.76*w*40); c.restore(); }; }, };