import {createDocument,addMark,undo,reset,imagePoint} from './model.mjs'; const $=id=>document.getElementById(id),canvas=$('image'),ctx=canvas.getContext('2d');let doc=null,base=null,tool='arrow',start=null,log=[];const signature=[137,80,78,71,13,10,26,10]; function status(text){$('status').textContent=text}function record(action,detail={}){log.push({action,...detail});$('evidence-json').textContent=JSON.stringify({document:doc,actions:log},null,2)} function render(){if(!doc)return;canvas.width=doc.width;canvas.height=doc.height;ctx.drawImage(base,0,0);let number=0;for(const m of doc.marks){ctx.strokeStyle='#ea580c';ctx.fillStyle='#ea580c';ctx.lineWidth=4;ctx.lineJoin='round';if(m.type==='box')ctx.strokeRect(m.x,m.y,m.x2-m.x,m.y2-m.y);if(m.type==='arrow'){const a=Math.atan2(m.y2-m.y,m.x2-m.x);ctx.beginPath();ctx.moveTo(m.x,m.y);ctx.lineTo(m.x2,m.y2);ctx.stroke();ctx.beginPath();ctx.moveTo(m.x2,m.y2);ctx.lineTo(m.x2-18*Math.cos(a-.5),m.y2-18*Math.sin(a-.5));ctx.lineTo(m.x2-18*Math.cos(a+.5),m.y2-18*Math.sin(a+.5));ctx.closePath();ctx.fill()}if(m.type==='number'){number++;ctx.beginPath();ctx.arc(m.x,m.y,18,0,Math.PI*2);ctx.fill();ctx.fillStyle='white';ctx.font='bold 20px Arial';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(String(number),m.x,m.y);ctx.textAlign='start';ctx.textBaseline='alphabetic'}}$('dimensions').textContent=`${doc.name} · ${doc.width} × ${doc.height} pixels · ${doc.marks.length} marks`} async function importImage(file){try{const bytes=new Uint8Array(await file.arrayBuffer());if(!signature.every((v,i)=>bytes[i]===v))throw Error('Choose a PNG file.');const bitmap=await createImageBitmap(file);const next=createDocument({name:file.name||'owned-sample.png',width:bitmap.width,height:bitmap.height});const image=document.createElement('canvas');image.width=bitmap.width;image.height=bitmap.height;image.getContext('2d').drawImage(bitmap,0,0);bitmap.close();base=image;doc=next;canvas.hidden=false;$('empty').hidden=true;$('export-panel').hidden=true;render();record('import',{name:doc.name,width:doc.width,height:doc.height,bytes:bytes.length});status('PNG imported at original resolution. No marks added.')}catch(e){status(e.message);record('import rejected',{error:e.message})}} $('file').onchange=()=>{if($('file').files[0])importImage($('file').files[0])};$('sample').onclick=async()=>{const r=await fetch('sample.png');if(!r.ok)return status('Could not load sample.');importImage(new File([await r.blob()],'owned-sample.png',{type:'image/png'}))}; document.querySelectorAll('[data-tool]').forEach(b=>b.onclick=()=>{tool=b.dataset.tool;document.querySelectorAll('[data-tool]').forEach(v=>v.setAttribute('aria-pressed',v===b));status(`${b.textContent} tool active.`)}); function point(e){const r=canvas.getBoundingClientRect();return imagePoint(e.clientX-r.left,e.clientY-r.top,{width:r.width,height:r.height},doc)} canvas.onpointerdown=e=>{if(!doc)return;canvas.setPointerCapture(e.pointerId);start=point(e)};canvas.onpointerup=e=>{if(!start||!doc)return;const end=point(e);doc=addMark(doc,{type:tool,x:start.x,y:start.y,...(tool==='number'?{}:{x2:end.x,y2:end.y})});start=null;render();record('mark added',{type:tool,count:doc.marks.length});status(`${tool} added. ${doc.marks.length} marks.`)};canvas.onpointercancel=()=>{start=null}; $('undo').onclick=()=>{if(!doc)return status('Import a PNG first.');const before=doc.marks.length;doc=undo(doc);render();record('undo',{before,after:doc.marks.length});status(before?`Last mark removed. ${doc.marks.length} marks remain.`:'Nothing to undo. The image is unchanged.')}; $('reset').onclick=()=>{if(!doc)return status('Import a PNG first.');doc=reset(doc);render();const a=ctx.getImageData(0,0,doc.width,doc.height).data,b=base.getContext('2d').getImageData(0,0,doc.width,doc.height).data;let changed=0;for(let i=0;i{if(!doc)return status('Import a PNG first.');try{const blob=await new Promise(r=>canvas.toBlob(r,'image/png'));const bitmap=await createImageBitmap(blob);const check=document.createElement('canvas');check.width=bitmap.width;check.height=bitmap.height;check.getContext('2d').drawImage(bitmap,0,0);bitmap.close();const a=ctx.getImageData(0,0,doc.width,doc.height).data,b=check.getContext('2d').getImageData(0,0,check.width,check.height).data;let changed=0;for(let i=0;i