PlotDirector/PlotLine/wwwroot/js/maps-appearance-engine.js

13 lines
2.4 KiB
JavaScript

(function(root,factory){if(typeof module==='object'&&module.exports)module.exports=factory();else root.PlotDirectorMapAppearanceEngine=factory();})(typeof globalThis!=='undefined'?globalThis:this,()=>{
const copy=o=>JSON.parse(JSON.stringify(o));
const presets={light:{backgroundColor:'#c9dfdf',landColor:'#e9e4c5'},paper:{backgroundColor:'#f4eddb',landColor:'#dfd0ab'},dark:{backgroundColor:'#182c35',landColor:'#3a514a'}};
function appearance(map){let value;try{value=JSON.parse(map?.appearanceJson||'{}');}catch{}return {preset:'light',...presets.light,...value};}
function reference(map){let value;try{value=JSON.parse(map?.referenceJson||'{}');}catch{}return value?.imageID?{x:0,y:0,width:800,height:600,rotation:0,opacity:.35,isLocked:true,isVisible:true,...value}:null;}
const luminance=hex=>{const v=hex.slice(1).match(/../g).map(x=>parseInt(x,16)/255).map(x=>x<=.04045?x/12.92:((x+.055)/1.055)**2.4);return .2126*v[0]+.7152*v[1]+.0722*v[2];};
function palette(map){const a=appearance(map),base=presets[a.preset]||a,dark=luminance(base.backgroundColor)<.2;return {...base,dark,ink:dark?'#edf4ed':'#243f38',halo:dark?'#102128':'#fffef3',marker:dark?'#304a50':'#faf5df',selection:dark?'#ffe19a':'#136883',water:dark?'#85cbe3':'#4c92ae',route:dark?'#e2bc83':'#99764b',region:dark?'#d2b6fa':'#80699b',story:dark?['#80c9fa','#e2b0e4','#ffc977']:['#265e83','#7c4f83','#936027']};}
function fit(image,view,rotation=0){const c=Math.abs(Math.cos(rotation*Math.PI/180)),s=Math.abs(Math.sin(rotation*Math.PI/180)),factor=Math.min(view.w*.8/(image.width*c+image.height*s),view.h*.8/(image.width*s+image.height*c));return {imageID:image.imageID,x:view.x+view.w/2,y:view.y+view.h/2,width:image.width*factor,height:image.height*factor,rotation,opacity:.35,isVisible:true,isLocked:true};}
const angle=n=>((n+180)%360+360)%360-180;
function transform(original,kind,start,point){const r=copy(original),dx=point[0]-start[0],dy=point[1]-start[1];if(kind==='move'){r.x+=dx;r.y+=dy;}if(kind==='scale'){const factor=Math.max(.02,Math.min(100,Math.hypot(point[0]-r.x,point[1]-r.y)/Math.hypot(start[0]-r.x,start[1]-r.y)));r.width=Math.max(10,Math.min(100000,r.width*factor));r.height=Math.max(10,Math.min(100000,r.height*factor));}if(kind==='rotate')r.rotation=angle(original.rotation+(Math.atan2(point[1]-r.y,point[0]-r.x)-Math.atan2(start[1]-r.y,start[0]-r.x))*180/Math.PI);return r;}
return {presets,appearance,reference,palette,fit,transform,angle,luminance};
});