import { applyJacquardArtwork, artworkColours, artworkWeave, paletteArtworkWeave, weaveChoices, MAX_ARTWORK_ENDS, MAX_ARTWORK_PICKS } from './jacquard-artwork.mjs'; const dialog=document.createElement('dialog'); dialog.id='jacquardArtworkDialog'; dialog.innerHTML=`
Choose a PNG or JPEG.
`; document.body.append(dialog); const $=id=>document.getElementById(id); const makeStatus=text=>{$('jacquardArtworkStatus').textContent=text;}; let palette=new Map(); let current, bitmap, draft, pending, revision, inputVersion=0, nextRequest=1850000; window.addEventListener('cad:document',event=>{current=event.detail;}); window.addEventListener('cad:jacquard-artwork',()=>{ if(!dialog.open)dialog.showModal(); preview(); }); function paletteMapping(pixels){ const colours=artworkColours(pixels); const container=$('jacquardPalette'); container.replaceChildren(); const choices=Object.entries(weaveChoices); return Object.fromEntries(colours.map((colour,i)=>{ const label=document.createElement('label'), swatch=document.createElement('span'); swatch.textContent=colour+' '; swatch.style.borderLeft='12px solid '+colour; const select=document.createElement('select'); select.setAttribute('aria-label','Weave for '+colour); for(const [key,title] of choices)select.add(new Option(title,key)); const choice=palette.get(colour)||choices[i%choices.length][0]; select.value=choice; select.onchange=()=>{ palette.set(colour,select.value); preview(); [...$('jacquardPalette').querySelectorAll('select')].find(el=>el.getAttribute('aria-label')===select.getAttribute('aria-label'))?.focus(); }; label.append(swatch,select); container.append(label); return [colour,choice]; })); } function preview(){ draft=null; $('jacquardArtworkApply').disabled=true; const canvas=$('jacquardArtworkPreview'); const context=canvas.getContext('2d',{willReadFrequently:true}); context.clearRect(0,0,canvas.width,canvas.height); const paletteMode=$('jacquardMode').value==='palette'; $('jacquardPalette').hidden=!paletteMode; $('jacquardThresholdControls').hidden=paletteMode; $('jacquardPalette').replaceChildren(); if(!bitmap)return; try{ if(current?.raster||current?.indexed||!current?.project) throw Error('Open a single-cloth yarn design to preview the artwork.'); const width=Number($('jacquardEnds').value),height=Number($('jacquardPicks').value); if(!Number.isInteger(width)||width<4||width>MAX_ARTWORK_ENDS||width%4|| !Number.isInteger(height)||height<4||height>MAX_ARTWORK_PICKS||height%4) throw Error('Use multiples of 4, up to 248 ends and 1024 picks.'); canvas.width=width; canvas.height=height; context.imageSmoothingEnabled=false; context.drawImage(bitmap,0,0,width,height); const frame=context.getImageData(0,0,width,height); const weave=paletteMode?paletteArtworkWeave(frame.data,width,height,paletteMapping(frame.data)):artworkWeave(frame.data,width,height,{ threshold:Number($('jacquardThreshold').value),switchFaces:$('jacquardSwap').checked, }); const result=applyJacquardArtwork(current.project,weave); for(let y=0;y