import Editor from '/dmpaint/vendor/Editor.js'; const status=document.querySelector('#paint-status'),svgButton=document.querySelector('#download-svg'),pngButton=document.querySelector('#download-png'); const mode=new URL(location.href).searchParams.get('mode')||'clan'; const names={clan:'Clan logo',company:'Company logo',seal:'Staff seal',award:'Staff award'}; let editor; const filename=()=>document.querySelector('#filename').value.replace(/[^a-zA-Z0-9_-]/g,'-').slice(0,64)||'dmpaint-design'; function download(blob,extension){const url=URL.createObjectURL(blob),link=document.createElement('a');link.href=url;link.download=filename()+'.'+extension;link.click();setTimeout(()=>URL.revokeObjectURL(url),30000);} async function start(){ const response=await fetch('/api/dmpaint',{credentials:'same-origin',cache:'no-store'}),access=await response.json(); if(!response.ok||!access.modes?.includes(mode))throw Error(access.message||'You do not have access to this design type.'); document.querySelector('#design-kind').textContent=names[mode]+' · Design & download'; document.querySelector('#filename').value='my-'+mode; editor=new Editor(document.querySelector('#editor-container')); editor.setConfig({canvasName:'dmpaint',dimensions:[512,512],imgPath:'/dmpaint/vendor/images',extPath:'/dmpaint/vendor/extensions',lang:'en',initFill:{color:'D9BB69',opacity:1},initStroke:{color:'12362B',opacity:1,width:2},noDefaultExtensions:true,extensions:['ext-opensave','ext-polystar','ext-eyedropper','ext-layer_view','ext-panning','ext-grid'],userExtensions:[],allowedOrigins:[],preventAllURLConfig:true,preventURLContentLoading:true,lockExtensions:true,noStorageOnLoad:true,no_save_warning:false}); editor.ready(()=>{svgButton.disabled=pngButton.disabled=false;status.textContent='Draw, add text or open an existing image. Download before leaving this page.';}); await editor.init(); } svgButton.onclick=()=>{try{download(new Blob([editor.svgCanvas.getSvgString()],{type:'image/svg+xml'}),'svg');status.textContent='SVG downloaded. Keep this editable file to continue your design later.';}catch(error){status.textContent=error.message;}}; pngButton.onclick=async()=>{ pngButton.disabled=true;let url; try{ const svg=editor.svgCanvas.getSvgString(),doc=new DOMParser().parseFromString(svg,'image/svg+xml'),root=doc.documentElement,view=root.getAttribute('viewBox')?.split(/[ ,]+/).map(Number); const width=Number.parseFloat(root.getAttribute('width'))||view?.[2]||512,height=Number.parseFloat(root.getAttribute('height'))||view?.[3]||512; if(!(width>0&&height>0&&width<=4096&&height<=4096))throw Error('Use a canvas size up to 4096 × 4096 pixels for PNG export.'); url=URL.createObjectURL(new Blob([svg],{type:'image/svg+xml'}));const image=new Image();image.src=url;await image.decode(); const canvas=document.createElement('canvas');canvas.width=Math.ceil(width);canvas.height=Math.ceil(height);canvas.getContext('2d').drawImage(image,0,0,canvas.width,canvas.height); const blob=await new Promise(resolve=>canvas.toBlob(resolve,'image/png'));if(!blob)throw Error('PNG export failed.');download(blob,'png');status.textContent='PNG downloaded. Upload it using the existing logo or artwork settings.'; }catch(error){status.textContent='PNG could not be exported: '+error.message;}finally{if(url)URL.revokeObjectURL(url);pngButton.disabled=false;} }; start().catch(error=>{status.textContent='DMPaint could not open: '+error.message;});