text: export default class kortjs extends HTMLElement{constructor(){const t=super(),e=(()=>{const e=t.dataset.url.replace("${id}",t.dataset.id).replace("${token}",t.dataset.token),n=t.dataset.tilesize?parseInt(t.dataset.tilesize):256,o=t.dataset.minzoom?parseInt(t.dataset.minzoom):0,i=t.dataset.maxzoom?parseInt(t.dataset.maxzoom):19,s=t.dataset.setLatitude?parseFloat(t.dataset.setLatitude):51.479263,a=t.dataset.setLongitude?parseFloat(t.dataset.setLongitude):0,r=Math.max(Math.min(t.dataset.setZoom?parseInt(t.dataset.setZoom):10,i),o);return{getURL:function([t,n,o]){return e.replace("${z}",t).replace("${x}",n).replace("${y}",o)},getTileSize:function(){return n},getCoordinatesAndZoom:function(){return[r,a,s]},getMinMaxZoom:function(){return[o,i]}}})(),n=t.attachShadow({mode:"open"}),o=document.createElement("div");o.setAttribute("class","viewport");const i=document.createElement("div");i.setAttribute("class","attribution-container"),i.setAttribute("part","attribution-container");const s=document.createElement("a");s.textContent="kortjs |",s.setAttribute("href","https://github.com/DLFDK/kortjs/"),s.setAttribute("target","_blank"),s.setAttribute("rel","noreferrer noopener");const a=document.createElement("slot");a.setAttribute("name","attribution-text"),i.append(s,a);const r=document.createElement("slot");r.setAttribute("name","attribution-icon");const c=document.createElement("slot");c.setAttribute("name","marker");const l=document.createElement("style");l.textContent=".viewport{box-sizing:border-box;position:relative;width:100%;height:100%;overflow:hidden;touch-action:none;user-select:none;cursor:grab;background:var(--kortjs-backgroundColor, #dddddd)}.layer{position:absolute;top:0;left:0}.hidden{opacity:0}.middle{z-index:1}.top{z-index:2}canvas{position:absolute}.attribution-container{position:absolute;bottom:0;right:0;z-index:3;display:flex;gap:4px;padding:2px 6px 6px 6px;background:rgba(255,255,255,.5);border-top-left-radius:4px;font-size:clamp(9px,2vw,13px);margin:0}::slotted(*){z-index:3}::slotted(p){margin:0}slot[name=marker]{display:none}.default-marker{position:absolute;z-index:3;width:48px;height:48px}",n.append(o,l),o.append(i,r,c);const d=(()=>{const t=new Set,n=new Worker(URL.createObjectURL(new Blob([`(${function(){onmessage=async({data:{topic:t,...e}})=>{if("fetch"===t){const{id:t,url:n,tileX:o,tileY:i}=e,s=await fetch(n).catch((t=>{console.log(t)}));if(s?.ok){const e=await s.blob(),n=await createImageBitmap(e);postMessage({id:t,bitmap:n,tileX:o,tileY:i},[n])}}}}.toString()})()`],{type:"text/javascript"})));return n.onmessage=function({data:{id:e,bitmap:n,tileX:o,tileY:i}}){t.delete(e),u.addTile(e,n,o,i)},{paint:function(o,i,s,a,r){const c=[],l=2**o.zoom;for(let d=0;d<r;d++)for(let r=0;r<a;r++){const a=`${o.zoom},${i+r+o.origin.tileX},${s+d+o.origin.tileY}`,u=((i+r+o.origin.tileX)%l+l)%l,m=((s+d+o.origin.tileY)%l+l)%l,p=e.getURL([o.zoom,u,m]);c.push(a),!o.isActive||o.tiles.has(a)||t.has(a)||(t.add(a),o.tiles.set(a),n.postMessage({topic:"fetch",id:a,url:p,tileX:i+r,tileY:s+d}))}const d=[...o.tiles.keys()].filter((t=>!c.includes(t)));for(const t of d){if(o.tiles.get(t)){const[e,n]=o.tiles.get(t);e&&(e.classList.add("hidden"),o.tileStorage.push([e,n]))}o.tiles.delete(t)}}}})(),u=(()=>{let{width:n,height:i}=o.getBoundingClientRect(),s=n/2,a=i/2;const r=e.getTileSize(),[l,u]=e.getMinMaxZoom(),m=[];let p,f;function h(t,e,n){const o=r*t.scale,i=e-s/o;t.origin.tileX=Math.floor(i);const c=i-t.origin.tileX,l=n-a/o;t.origin.tileY=Math.floor(l);const d=l-t.origin.tileY;t.position.x=Math.round(-c*o),t.position.y=Math.round(-d*o),g(t,0,0,{round:!1})}function g(t,e,n,o={}){const{round:i}=o;i?(t.position.x=Math.round(t.position.x+e),t.position.y=Math.round(t.position.y+n)):(t.position.x+=e,t.position.y+=n),t.element.style.transform=`translate3d(${t.position.x}px, ${t.position.y}px, 0px) scale(${t.scale})`}function x(t){for(const e of m)if(e.isActive||e.tiles.size){if(e.position.x=(e.position.x-s)*t+s,e.position.y=(e.position.y-a)*t+a,e.scale*=t,e.scale<.0625){v(e);continue}g(e,0,0,{round:!1})}}function b(t,e){if(f===m[0].zoom)return;const n=r*m[0].scale,o=(m[0].origin.tileX+(s-m[0].position.x)/n)*t,i=(m[0].origin.tileY+(a-m[0].position.y)/n)*t;v(m[2]),m[0].isActive=!1,m[0].element.classList.remove("top"),m[0].element.classList.add("middle"),m[1].element.classList.remove("middle"),m[2].element.classList.add("top"),m[2].zoom=f,m[2].scale=e,h(m[2],o,i),m[2].isActive=!0,m.unshift(m.pop()),A()}function v(t){if(t.tiles.size){for(const[e,n]of t.tiles)if(n){const[e,o]=n;e.classList.add("hidden"),t.tileStorage.push([e,o])}t.tiles.clear()}}function A(){for(const t of m)if(t.isActive||t.tiles.size){const[n,o,i,s]=e(t);if(t.lastZoom===t.zoom&&t.lastTileX===n&&t.lastTileY===o&&t.lastGridLengthX===i&&t.lastGridLengthY===s)continue;t.lastZoom=t.zoom,t.lastTileX=n,t.lastTileY=o,t.lastGridLengthX=i,t.lastGridLengthY=s,d.paint(t,n,o,i,s,{useWorker:!0})}function e(t){const e=r*t.scale,o=Math.floor(-(t.position.x+0)/e),s=Math.floor(-(t.position.y+0)/e),a=(-(t.position.x+0)%e+e)%e,c=(-(t.position.y+0)%e+e)%e;return[o,s,Math.ceil((a+n+0)/e),Math.ceil((c+i+0)/e)]}!function(){const{longitude:e,latitude:n}=function(){const t=2**m[0].zoom,e=r*m[0].scale,n=((m[0].origin.tileX+(s-m[0].position.x)/e)%t+t)%t,o=((m[0].origin.tileY+(a-m[0].position.y)/e)%t+t)%t,i=360*n/t-180,c=180*Math.atan(Math.sinh(Math.PI-2*Math.PI*o/t))/Math.PI;return{longitude:i,latitude:c}}();t.dataset.getLatitude=n,t.dataset.getLongitude=e,t.dataset.getZoom=m[0].zoom}()}function z(t,e,n){g(m[0],t,e,{round:n});for(let o=1;o<m.length;o++)m[o].tiles.size&&g(m[o],t,e,{round:n});A()}return new ResizeObserver((()=>{const{width:t,height:e}=o.getBoundingClientRect(),r=t-n,c=e-i;n=t,i=e,s=n/2,a=i/2,p&&p(),z(r/2,c/2,!0)})).observe(o),function(){!function(){for(let t=0;t<3;t++){const t=document.createElement("div");t.setAttribute("class","layer"),o.appendChild(t),m.push({tiles:new Map,tileStorage:[],scale:1,origin:{},position:{x:0,y:0},element:t,isActive:!1})}}();const[t,n,i]=e.getCoordinatesAndZoom();f=t,m[0].zoom=t,m[0].element.classList.add("top");const[s,a]=function(t,e){const n=2**m[0].zoom,o=n*(t+180)/360,i=n*(1-Math.log(Math.tan(e*Math.PI/180)+1/Math.cos(e*Math.PI/180))/Math.PI)/2;return[o,i]}(n,i);h(m[0],s,a),m[0].isActive=!0}(),function(){if(c.assignedNodes()[0]){const t=c.assignedNodes()[0].cloneNode("deep");t.setAttribute("class","default-marker"),o.append(t),p=()=>{const{width:e,height:n}=t.getBoundingClientRect(),o=t.dataset.offsetx?parseFloat(t.dataset.offsetx):-e/2,i=t.dataset.offsety?parseFloat(t.dataset.offsety):-n;t.style.transform=`translate3d(${s+o}px, ${a+i}px, 0px)`},p()}}(),A(),{move:z,pinch:function(t){m[0].isActive=!1;let e=m[0].scale*t;if(e>1&&m[0].zoom===u||e<1&&m[0].zoom===l)return;let n=t;if(e>=2&&m[0].zoom===u-1?n=2/m[0].scale:e<=.5&&m[0].zoom===l+1&&(n=.5/m[0].scale),x(n),m[0].scale>=2||m[0].scale<=.5){let t,e;m[0].scale>=2?(t=1,e=2):(t=-1,e=.5),f=m[0].zoom+t,b(e,m[0].scale/e)}else A()},addTile:function(t,n,o,i){if(m[0].tiles.has(t))if(m[0].tileStorage.length){const[e,s]=m[0].tileStorage.pop();e.classList.remove("hidden"),s.transferFromImageBitmap(n),e.style.transform=`translate3d(${o*r}px, ${i*r}px, 0px)`,m[0].tiles.set(t,[e,s]),e.animate({opacity:0,offset:0},{duration:300})}else{const s=document.createElement("canvas"),a=s.getContext("bitmaprenderer");s.width=e.getTileSize(),s.height=e.getTileSize(),m[0].element.appendChild(s),a.transferFromImageBitmap(n),s.style.transform=`translate3d(${o*r}px, ${i*r}px, 0px)`,m[0].tiles.set(t,[s,a]),s.animate({opacity:0,offset:0},{duration:300})}},getAbsoluteScale:function(){return m[0].scale},setAbsoluteScales:function(t){m[0].isActive=1===t,x(t/m[0].scale)},zoom:b,changeZoomLevel:function(t){const e=f+t;return!(e>u||e<l)&&(f+=t,!0)}}})();(()=>{const t=new Map;let e,n,i,s,a,r,c,l,d,m,p,f=[0,0,0];let h,g=10,x=0;function b(e){t.has(e.pointerId)&&t.set(e.pointerId,[e.clientX,e.clientY])}function v(m){t.has(m.pointerId)&&(t.delete(m.pointerId),t.size?(cancelAnimationFrame(s),function(){cancelAnimationFrame(e);const t=u.getAbsoluteScale();let n;t<.75?(p=.5,n=-1):t>1.5?(p=2,n=1):(p=1,n=0);u.changeZoomLevel(n)&&L(t,250)}(),[a,r]=[...t.values()][0],c=0,l=0,n=requestAnimationFrame((t=>{A(t,performance.now(),0,0)}))):(o.style.cursor="grab",cancelAnimationFrame(n),document.removeEventListener("pointerup",v),document.removeEventListener("pointercancel",v),document.removeEventListener("pointermove",b),Math.sqrt(c**2+l**2)>.2&&(d=performance.now(),i=requestAnimationFrame((t=>{z(t,d)})))))}function A(e,o,i,s){const d=e-o,[m,p]=[...t.values()][0],f=m-a,h=p-r,g=f-i,x=h-s;if(d>0){const t=1-Math.exp(-d/32);c=t*g/d+(1-t)*c,l=t*x/d+(1-t)*l}n=requestAnimationFrame((t=>{A(t,e,f,h)})),u.move(g,x,!0)}function z(t,e){const n=t-e,o=Math.exp(-(e-d)/2e3),s=o*c*n,a=o*l*n;let r=!0;o>.05&&(r=!1,i=requestAnimationFrame((e=>{z(e,t)}))),u.move(s,a,r)}function M(e,n){const[o,i]=[...t.values()],a=Math.sqrt((o[0]-i[0])**2+(o[1]-i[1])**2);u.pinch(a/n),s=requestAnimationFrame((t=>{M(t,a)}))}function L(t,n){function o(i,s,a){const r=i-s+a;if(r<n){const s=t+(p-t)*(1-(1-r/n)**3);u.setAbsoluteScales(s),e=requestAnimationFrame((t=>{o(t,i,r)}))}else m=!1,u.setAbsoluteScales(p),u.zoom(p,1)}cancelAnimationFrame(e),e=requestAnimationFrame((t=>{o(t,t,0)}))}o.addEventListener("pointerdown",(function(d){switch(t.size){case 0:o.style.cursor="grabbing",function(t){return Math.abs(f[0]-t.clientX)<50&&Math.abs(f[1]-t.clientY)<50&&Math.abs(f[2]-t.timeStamp)<250||(f=[t.clientX,t.clientY,t.timeStamp],!1)}(d)&&function(){const t=2;if(u.changeZoomLevel(1)){const e=u.getAbsoluteScale();m?p*=t:(m=!0,p=t),L(e,250)}}(),cancelAnimationFrame(i),t.set(d.pointerId,[d.clientX,d.clientY]),a=d.clientX,r=d.clientY,c=0,l=0,document.addEventListener("pointerup",v),document.addEventListener("pointercancel",v),document.addEventListener("pointermove",b),n=requestAnimationFrame((t=>{A(t,performance.now(),0,0)}));break;case 1:cancelAnimationFrame(e),cancelAnimationFrame(n),t.set(d.pointerId,[d.clientX,d.clientY]);const[h,g]=[...t.values()],x=Math.sqrt((h[0]-g[0])**2+(h[1]-g[1])**2);s=requestAnimationFrame((t=>{M(t,x)}))}})),o.addEventListener("wheel",(function(t){let e,n;t.preventDefault(),clearTimeout(h),h=setTimeout((()=>{g=10,x=0}),1e3),x+=t.deltaY,x>g?(n=.5,e=-1,x=0,g=80):x<-1*g&&(n=2,e=1,x=0,g=80);if(e&&u.changeZoomLevel(e)){const t=u.getAbsoluteScale();m?p*=n:(m=!0,p=n),L(t,250)}}),{passive:!1})})()}connectedCallback(){this.isConnected?this.dataset.isConnected="true":this.dataset.isConnected="false"}disconnectedCallback(){this.dataset.isConnected="false"}}customElements.define("kort-js",kortjs);