forked from unimatrix/web
71 lines
3.9 KiB
JavaScript
71 lines
3.9 KiB
JavaScript
const get=id=>document.getElementById(id);
|
||
const canvas=get('skin'),ctx=canvas.getContext('2d',{willReadFrequently:true}),status=get('status');
|
||
const library='/api/tailor/library',history=[];
|
||
let previous=null;
|
||
function snapshot(){
|
||
history.push(ctx.getImageData(0,0,64,64));
|
||
if(history.length>20)history.shift();
|
||
}
|
||
function point(event){
|
||
const rect=canvas.getBoundingClientRect();
|
||
return [Math.max(0,Math.min(63,Math.floor((event.clientX-rect.left)*64/rect.width))),Math.max(0,Math.min(63,Math.floor((event.clientY-rect.top)*64/rect.height)))];
|
||
}
|
||
function line(from,to){
|
||
const steps=Math.max(1,Math.abs(from[0]-to[0]),Math.abs(from[1]-to[1]));
|
||
ctx.fillStyle=get('color').value;
|
||
for(let i=0;i<=steps;i++){
|
||
const x=Math.round(from[0]+(to[0]-from[0])*i/steps),y=Math.round(from[1]+(to[1]-from[1])*i/steps);
|
||
if(get('erase').getAttribute('aria-pressed')==='true')ctx.clearRect(x,y,1,1);
|
||
else ctx.fillRect(x,y,1,1);
|
||
}
|
||
}
|
||
canvas.onpointerdown=event=>{
|
||
if(!event.isPrimary||event.button!==0)return;
|
||
canvas.setPointerCapture(event.pointerId);snapshot();previous=point(event);line(previous,previous);
|
||
};
|
||
canvas.onpointermove=event=>{if(previous&&canvas.hasPointerCapture(event.pointerId)){const next=point(event);line(previous,next);previous=next;}};
|
||
canvas.onlostpointercapture=()=>{previous=null;};
|
||
get('erase').onclick=event=>{const button=event.currentTarget;button.setAttribute('aria-pressed',String(button.getAttribute('aria-pressed')!=='true'));};
|
||
get('undo').onclick=()=>{if(history.length)ctx.putImageData(history.pop(),0,0);};
|
||
async function openSkin(blob){
|
||
if(blob.type!=='image/png')throw Error('Choose a PNG file.');
|
||
if(blob.size>65536)throw Error('Choose a skin PNG up to 64 KB.');
|
||
const bitmap=await createImageBitmap(blob);
|
||
try{
|
||
if(bitmap.width!==64||bitmap.height!==64)throw Error('Use a 64 × 64 Minecraft skin.');
|
||
snapshot();ctx.clearRect(0,0,64,64);ctx.drawImage(bitmap,0,0);
|
||
}finally{bitmap.close();}
|
||
}
|
||
function showError(error){status.textContent=error.message;}
|
||
get('file').onchange=event=>{const file=event.currentTarget.files[0];if(file)openSkin(file).catch(showError);};
|
||
async function request(path,options){
|
||
const response=await fetch(path,options);
|
||
if(!response.ok)throw Error((await response.json()).message||'The Skin Workshop is unavailable.');
|
||
return response;
|
||
}
|
||
const png=()=>new Promise(resolve=>canvas.toBlob(resolve,'image/png'));
|
||
function button(label,action){const control=document.createElement('button');control.textContent=label;control.onclick=()=>action().catch(showError);return control;}
|
||
async function refresh(){
|
||
const {skins}=await (await request(library)).json();
|
||
const list=get('library');list.replaceChildren();
|
||
if(!skins.length){const item=document.createElement('li');item.textContent='No saved skins yet.';list.append(item);}
|
||
for(const skin of skins){
|
||
const item=document.createElement('li'),label=document.createElement('strong'),id=document.createElement('code');
|
||
label.textContent=skin.name;id.textContent='#'+skin.id;
|
||
const url=library+'/'+encodeURIComponent(skin.id);
|
||
item.append(label,id,button('Edit',async()=>{await openSkin(await(await request(url)).blob());get('name').value=skin.name;}),button('Remove',async()=>{await request(url,{method:'DELETE'});await refresh();}));
|
||
list.append(item);
|
||
}
|
||
}
|
||
get('save').onsubmit=async event=>{
|
||
event.preventDefault();const submit=event.submitter;if(submit)submit.disabled=true;
|
||
try{
|
||
const response=await request(library,{method:'POST',headers:{'Content-Type':'image/png','X-Skin-Name':encodeURIComponent(get('name').value)},body:await png()});
|
||
const skin=await response.json();status.textContent='Saved #'+skin.id;await refresh();
|
||
}catch(error){showError(error);}finally{if(submit)submit.disabled=false;}
|
||
};
|
||
get('download').onclick=async()=>{
|
||
const url=URL.createObjectURL(await png()),link=document.createElement('a');
|
||
link.href=url;link.download='dunerealms-skin.png';link.click();setTimeout(()=>URL.revokeObjectURL(url),1000);
|
||
};
|
||
refresh().catch(showError);
|

Staff