@niuee/board
Version:
<h1 align="center"> board </h1> <p align="center"> board supercharges your html canvas element giving it the capabilities to pan, zoom, rotate, and much more. </p> <p align="center"> <a href="https://www.npmjs.com/package/@niuee/board">
1 lines • 68.5 kB
JavaScript
"use strict";const t=()=>{};class e{constructor(t,e,o){this._states=t,this._currentState=e,this._context=o,this._statesArray=Object.keys(t),this._stateChangeCallbacks=[],this._happensCallbacks=[]}switchTo(t){this._currentState=t}happens(t,e){this._happensCallbacks.forEach((o=>o(t,e,this._context)));const o=this._states[this._currentState].handles(t,e,this._context);if(void 0!==o&&o!==this._currentState){const t=this._currentState;this._states[this._currentState].uponLeave(this._context),this.switchTo(o),this._states[this._currentState].uponEnter(this._context),this._stateChangeCallbacks.forEach((e=>e(t,this._currentState)))}return o}onStateChange(t){this._stateChangeCallbacks.push(t)}onHappens(t){this._happensCallbacks.push(t)}get currentState(){return this._currentState}setContext(t){this._context=t}get possibleStates(){return this._statesArray}get states(){return this._states}}class o{constructor(){this._guards={},this._eventGuards={}}get guards(){return this._guards}get eventGuards(){return this._eventGuards}uponEnter(t){}uponLeave(t){}handles(t,e,o){if(this.eventReactions[t]){this.eventReactions[t].action(o,e);const i=this.eventReactions[t].defaultTargetState,n=this._eventGuards[t];if(n){const t=n.find((t=>!!this.guards[t.guard]&&this.guards[t.guard](o)));return t?t.target:i}return i}}}function i(t,e){return n(t,e)||void 0===e||(e.max&&(t=Math.min(e.max,t)),e.min&&(t=Math.max(e.min,t))),t}function n(t,e){return void 0===e||!(t<=0||void 0!==e&&(void 0!==e.max&&e.max<t||void 0!==e.min&&e.min>t))}class a{static addVector(t,e){return null==t.z&&null==e.z?{x:t.x+e.x,y:t.y+e.y}:(null!=t.z&&null!=e.z||(null==t.z&&(t.z=0),null==e.z&&(e.z=0)),{x:t.x+e.x,y:t.y+e.y,z:t.z+e.z})}static subVector(t,e){return null==t.z&&null==e.z?{x:t.x-e.x,y:t.y-e.y}:(null!=t.z&&null!=e.z||(null==t.z&&(t.z=0),null==e.z&&(e.z=0)),{x:t.x-e.x,y:t.y-e.y,z:t.z-e.z})}static multiplyVectorByScalar(t,e){return null==t.z?{x:t.x*e,y:t.y*e}:{x:t.x*e,y:t.y*e,z:t.z*e}}static divideVectorByScalar(t,e){return 0==e?{x:t.x,y:t.y}:null==t.z?{x:t.x/e,y:t.y/e}:{x:t.x/e,y:t.y/e,z:t.z/e}}static magnitude(t){return null==t.z&&(t.z=0),Math.sqrt(t.x*t.x+t.y*t.y+t.z*t.z)}static unitVector(t){return null==t.z&&(t.z=0),0!=this.magnitude(t)?{x:t.x/this.magnitude(t),y:t.y/this.magnitude(t),z:t.z/this.magnitude(t)}:{x:0,y:0,z:0}}static dotProduct(t,e){return null==t.z&&null==e.z?t.x*e.x+t.y*e.y:(null!=t.z&&null!=e.z||(null==t.z&&(t.z=0),null==e.z&&(e.z=0)),t.x*e.x+t.y*e.y+t.z*e.z)}static crossProduct(t,e){return null!=t.z&&null!=e.z||(null==t.z&&(t.z=0),null==e.z&&(e.z=0)),{x:t.y*e.z-t.z*e.y,y:t.z*e.x-t.x*e.z,z:t.x*e.y-t.y*e.x}}static unitVectorFromA2B(t,e){return this.unitVector(this.subVector(e,t))}static rotatePoint(t,e){return{x:t.x*Math.cos(e)-t.y*Math.sin(e),y:t.x*Math.sin(e)+t.y*Math.cos(e)}}static transform2NewAxis(t,e){return{x:t.x*Math.cos(e)+t.y*Math.sin(e),y:-t.x*Math.sin(e)+t.y*Math.cos(e)}}static angleFromA2B(t,e){return Math.atan2(t.x*e.y-t.y*e.x,t.x*e.x+t.y*e.y)}static transformPointWRTAnchor(t,e,o){let i=this.rotatePoint(this.subVector(t,e),o);return this.addVector(i,e)}static distanceBetweenPoints(t,e){return this.magnitude(this.subVector(t,e))}static flipYAxis(t){return{x:t.x,y:-t.y,z:t.z}}static linearInterpolation(t,e,o){return null==t.z||null==e.z?{x:t.x+(e.x-t.x)*o,y:t.y+(e.y-t.y)*o}:{x:t.x+(e.x-t.x)*o,y:t.y+(e.y-t.y)*o,z:t.z+(e.z-t.z)*o}}static isEqual(t,e){return null==t.z&&(t.z=0),null==e.z&&(e.z=0),t.x==e.x&&t.y==e.y&&t.z==e.z}static getLineIntersection(t,e,o,i){const n=(i.x-o.x)*(t.y-o.y)-(i.y-o.y)*(t.x-o.x),r=(i.y-o.y)*(e.x-t.x)-(i.x-o.x)*(e.y-t.y);if(0===r)return{intersects:!1};const s=n/r;return s>=0&&s<=1?{intersects:!0,intersection:a.linearInterpolation(t,e,s),offset:s}:{intersects:!1}}}function r(t,e,o,i,n,r){let s={x:o/2,y:i/2},l=a.subVector(e,s);return l=a.multiplyVectorByScalar(l,1/n),l=a.rotatePoint(l,r),a.addVector(t,l)}function s(t,e,o,i){const n=a.multiplyVectorByScalar(t,1/o),r=a.rotatePoint(n,i);return a.addVector(r,e)}function l(t,e,o,i){const n=a.subVector(t,e),r=a.multiplyVectorByScalar(n,o);return a.rotatePoint(r,-i)}function c(t,e,o,i,n,r){let s={x:e/2,y:o/2},l=a.subVector(t,i);return l=a.rotatePoint(l,-r),l=a.multiplyVectorByScalar(l,n),a.addVector(s,l)}function h(t,e,o){return a.multiplyVectorByScalar(a.rotatePoint(t,o),1/e)}function u(t,e){if(null==e)return!0;let o=!1,i=!1,n=!1,a=!1;return(null==e.max||null==e.max.x||t.x<=e.max.x)&&(i=!0),(null==e.min||null==e.min.x||t.x>=e.min.x)&&(o=!0),(null==e.max||null==e.max.y||t.y<=e.max.y)&&(n=!0),(null==e.min||null==e.min.y||t.y>=e.min.y)&&(a=!0),o&&i&&n&&a}function d(t){return null!=t&&(null!=t.max&&null!=t.min&&(null!=t.max.x&&null!=t.max.y&&null!=t.min.x&&null!=t.min.y))}function m(t,e){if(u(t,e)||null==e)return t;let o={x:t.x,y:t.y},i=e.min;return null!=i&&(null!=i.x&&(o.x=Math.max(o.x,i.x)),null!=i.y&&(o.y=Math.max(o.y,i.y))),i=e.max,null!=i&&(null!=i.x&&(o.x=Math.min(o.x,i.x)),null!=i.y&&(o.y=Math.min(o.y,i.y))),o}function p(t){if(null!=t&&null!=t.min&&null!=t.max&&null!=t.min.x&&null!=t.max.x)return t.max.x-t.min.x}function _(t){const e=p(t);return null!=e?e/2:void 0}function y(t){if(null!=t&&null!=t.min&&null!=t.max&&null!=t.min.y&&null!=t.max.y)return t.max.y-t.min.y}function x(t){const e=y(t);return null!=e?e/2:void 0}function v(t,e,o,i,n,s){if(null==i)return t;let l=r(t,{x:0,y:o},e,o,n,s),c=r(t,{x:0,y:0},e,o,n,s),h=r(t,{x:e,y:o},e,o,n,s),u=r(t,{x:e,y:0},e,o,n,s),d=m(l,i),p=m(h,i),_=m(c,i),y=m(u,i),x=[a.subVector(d,l),a.subVector(p,h),a.subVector(_,c),a.subVector(y,u)],v=Math.abs(x[0].x),f=Math.abs(x[0].y),P=x[0];return x.forEach((t=>{Math.abs(t.x)>v&&(v=Math.abs(t.x),P.x=t.x),Math.abs(t.y)>f&&(f=Math.abs(t.y),P.y=t.y)})),a.addVector(t,P)}function f(t,e){if(P(t,e)||void 0===e)return t;t=g(t);const o=b(e.start,t),i=b(e.end,t);if(e.ccw&&(o<0||i>0)||!e.ccw&&(o>0||i<0)){if(Math.abs(o)===Math.abs(i))return e.startAsTieBreaker?e.start:e.end;return Math.abs(o)<Math.abs(i)?e.start:e.end}return t}function P(t,e){if(void 0===e)return!0;if(g(e.start)===g(e.end))return!0;const o=g(t),i=b(e.start,o),n=b(e.end,o);return!(e.ccw&&(i<0||n>0)||!e.ccw&&(i>0||n<0))}function g(t){return t=((t%=2*Math.PI)+2*Math.PI)%(2*Math.PI)}function b(t,e){t=g(t);let o=(e=g(e))-t;return o>Math.PI&&(o=-(2*Math.PI-o)),o<-Math.PI&&(o+=2*Math.PI),o}function T(...t){const e=Array.isArray(t[0])?t[0]:t;return(t,...o)=>e.reduce(((t,e)=>e(t,...o)),t)}function w(){return T(I,C)}function B(){return T(S,z)}function I(t,e,o){let i=a.subVector(t,e.position);if(i=R(i,e,o),0===i.x&&0===i.y)return t;return a.addVector(e.position,i)}function S(t,e,o){return t=R(t,e,o)}function C(t,e,o){if(!o.clampTranslation)return t;let i=m(t,e.boundaries);return o.limitEntireViewPort&&(i=v(t,e.viewPortWidth,e.viewPortHeight,e.boundaries,e.zoomLevel,e.rotation)),i}function z(t,e,o){if(!o.clampTranslation)return t;let i=a.subVector(m(a.addVector(e.position,t),e.boundaries),e.position);return o.limitEntireViewPort&&(i=a.subVector(v(a.addVector(e.position,t),e.viewPortWidth,e.viewPortHeight,e.boundaries,e.zoomLevel,e.rotation),e.position)),i}function R(t,e,o){if(o.restrictXTranslation&&o.restrictYTranslation)return{x:0,y:0};if(o.restrictRelativeXTranslation&&o.restrictRelativeYTranslation)return{x:0,y:0};if(o.restrictXTranslation&&(t.x=0),o.restrictYTranslation&&(t.y=0),o.restrictRelativeXTranslation){const o=a.rotatePoint({x:0,y:1},e.rotation),i=a.dotProduct(o,t);t=a.multiplyVectorByScalar(o,i)}if(o.restrictRelativeYTranslation){const o=a.rotatePoint({x:1,y:0},e.rotation),i=a.dotProduct(o,t);t=a.multiplyVectorByScalar(o,i)}return t}function M(t,e,o){if(!o.clampRotation)return t;const i=f(g(e.rotation+t),e.rotationBoundaries);return b(e.rotation,i)}function A(t,e,o){return o.restrictRotation?0:t}function O(t,e,o){if(!o.clampRotation)return t;const i=f(t,e.rotationBoundaries);return b(e.rotation,i)}function H(t,e,o){return o.restrictRotation?e.rotation:t}function E(){return T(A,M)}function V(){return T(H,O)}function L(t,e,o){return o.clampZoom?i(t,e.zoomBoundaries):t}function Z(t,e,o){if(!o.clampZoom)return t;let n=e.zoomLevel+t;return n=i(n,e.zoomBoundaries),t=n-e.zoomLevel}function k(t,e,o){return o.restrictZoom?e.zoomLevel:t}function D(t,e,o){return o.restrictZoom?0:t}function W(){return T(L,k)}function N(){return T(Z,D)}class U{constructor(){this.observers=[]}subscribe(t,e){if(this.observers.push(t),null==e?void 0:e.signal){if(e.signal.aborted)return this.observers=this.observers.filter((e=>e!==t)),()=>{};const o=()=>{var i;this.observers=this.observers.filter((e=>e!==t)),null===(i=e.signal)||void 0===i||i.removeEventListener("abort",o)};e.signal.addEventListener("abort",o)}return()=>{this.observers=this.observers.filter((e=>e!==t))}}notify(...t){this.observers.forEach((e=>queueMicrotask((()=>e(...t)))))}}class F{constructor(){this.pan=new U,this.zoom=new U,this.rotate=new U,this.all=new U}notifyPan(t,e){this.pan.notify(t,e),this.all.notify({type:"pan",diff:t.diff},e)}notifyZoom(t,e){this.zoom.notify(t,e),this.all.notify({type:"zoom",deltaZoomAmount:t.deltaZoomAmount},e)}notifyRotate(t,e){this.rotate.notify(t,e),this.all.notify({type:"rotate",deltaRotation:t.deltaRotation},e)}on(t,e,o){switch(t){case"pan":return this.pan.subscribe(e,o);case"zoom":return this.zoom.subscribe(e,o);case"rotate":return this.rotate.subscribe(e,o);case"all":return this.all.subscribe(e,o);default:throw new Error(`Invalid event name: ${t}`)}}}class Y{constructor(t=1e3,e=1e3,o={x:0,y:0},i=0,n=1,a={min:{x:-1e4,y:-1e4},max:{x:1e4,y:1e4}},r={min:.1,max:10},s=void 0){this._position=o,this._zoomLevel=n,this._rotation=i,this._viewPortHeight=e,this._viewPortWidth=t,this._zoomBoundaries=r,this._rotationBoundaries=s,this._boundaries=a}get boundaries(){return this._boundaries}set boundaries(t){this._boundaries=t}get viewPortWidth(){return this._viewPortWidth}set viewPortWidth(t){this._viewPortWidth=t}get viewPortHeight(){return this._viewPortHeight}set viewPortHeight(t){this._viewPortHeight=t}get position(){return this._position}setPosition(t){if(!u(t,this._boundaries))return!1;const e=a.subVector(t,this._position);return!(a.magnitude(e)<1e-9&&a.magnitude(e)<1/this._zoomLevel)&&(this._position=t,!0)}get zoomLevel(){return this._zoomLevel}get zoomBoundaries(){return this._zoomBoundaries}set zoomBoundaries(t){if(void 0!==t&&void 0!==t.min&&void 0!==t.max&&t.min>t.max){let e=t.max;t.max=t.min,t.min=e}this._zoomBoundaries=t}setMaxZoomLevel(t){return null==this._zoomBoundaries&&(this._zoomBoundaries={min:void 0,max:void 0}),!(null!=this._zoomBoundaries.min&&this._zoomBoundaries.min>t||this._zoomLevel>t)&&(this._zoomBoundaries.max=t,!0)}setMinZoomLevel(t){return null==this._zoomBoundaries&&(this._zoomBoundaries={min:void 0,max:void 0}),!(null!=this._zoomBoundaries.max&&this._zoomBoundaries.max<t)&&(this._zoomBoundaries.min=t,this._zoomLevel<t&&(this._zoomLevel=t),!0)}setZoomLevel(t){return!!n(t,this._zoomBoundaries)&&((void 0===this._zoomBoundaries||void 0===this._zoomBoundaries.max||i(t,this._zoomBoundaries)!=this._zoomBoundaries.max||this._zoomLevel!=this._zoomBoundaries.max)&&((void 0===this._zoomBoundaries||void 0===this._zoomBoundaries.min||i(t,this._zoomBoundaries)!=this._zoomBoundaries.min||this._zoomLevel!=this._zoomBoundaries.min)&&(this._zoomLevel=t,!0)))}get rotation(){return this._rotation}get rotationBoundaries(){return this._rotationBoundaries}set rotationBoundaries(t){if(void 0!==t&&void 0!==t.start&&void 0!==t.end&&t.start>t.end){let e=t.end;t.end=t.start,t.start=e}this._rotationBoundaries=t}getTransform(t,e){const o=t*this._viewPortWidth/2,i=t*this._viewPortHeight/2,n=-this._position.x,a=e?-this._position.y:this._position.y,r=t,s=this._zoomLevel,l=e?-this._rotation:this._rotation,c=Math.sin(l),h=Math.cos(l);return{a:s*r*h,b:s*r*c,c:-r*s*c,d:s*r*h,e:r*s*h*n-r*s*c*a+o,f:r*s*c*n+r*s*h*a+i}}setRotation(t){return!!P(t,this._rotationBoundaries)&&(t=g(t),(void 0===this._rotationBoundaries||void 0===this._rotationBoundaries.end||f(t,this._rotationBoundaries)!=this._rotationBoundaries.end||this._rotation!=this._rotationBoundaries.end)&&((void 0===this._rotationBoundaries||void 0===this.rotationBoundaries.start||f(t,this._rotationBoundaries)!=this._rotationBoundaries.start||this._rotation!=this._rotationBoundaries.start)&&(this._rotation=t,!0)))}getCameraOriginInWindow(t){return t}convertFromViewPort2WorldSpace(t){return s(t,this._position,this._zoomLevel,this._rotation)}convertFromWorld2ViewPort(t){return l(t,this._position,this._zoomLevel,this._rotation)}invertFromWorldSpace2ViewPort(t){let e={x:this.viewPortWidth/2,y:this._viewPortHeight/2},o=a.subVector(t,this._position);return o=a.rotatePoint(o,-this._rotation),o=a.multiplyVectorByScalar(o,this._zoomLevel),a.addVector(e,o)}setHorizontalBoundaries(t,e){if(t>e){let o=e;e=t,t=o}null==this._boundaries&&(this._boundaries={min:{x:void 0,y:void 0},max:{x:void 0,y:void 0}}),this._boundaries.min.x=t,this._boundaries.max.x=e}setVerticalBoundaries(t,e){if(t>e){let o=e;e=t,t=o}null==this._boundaries&&(this._boundaries={min:{x:void 0,y:void 0},max:{x:void 0,y:void 0}}),this._boundaries.min.y=t,this._boundaries.max.y=e}}class j{constructor(t=1e3,e=1e3,o={x:0,y:0},i=0,n=1,a={min:{x:-1e4,y:-1e4},max:{x:1e4,y:1e4}},r={min:.1,max:10},s=void 0){this._baseCamera=new Y(t,e,o,i,n,a,r,s),this._observer=new F}get boundaries(){return this._baseCamera.boundaries}set boundaries(t){this._baseCamera.boundaries=t}get viewPortWidth(){return this._baseCamera.viewPortWidth}set viewPortWidth(t){this._baseCamera.viewPortWidth=t}get viewPortHeight(){return this._baseCamera.viewPortHeight}set viewPortHeight(t){this._baseCamera.viewPortHeight=t}get position(){return this._baseCamera.position}setPosition(t){const e=Object.assign({},this._baseCamera.position);return!!this._baseCamera.setPosition(t)&&(this._observer.notifyPan({diff:a.subVector(t,e)},{position:this._baseCamera.position,rotation:this._baseCamera.rotation,zoomLevel:this._baseCamera.zoomLevel}),!0)}get zoomLevel(){return this._baseCamera.zoomLevel}get zoomBoundaries(){return this._baseCamera.zoomBoundaries}set zoomBoundaries(t){this._baseCamera.zoomBoundaries=t}setMaxZoomLevel(t){const e=this._baseCamera.zoomLevel;return!!this._baseCamera.setMaxZoomLevel(t)&&(this._observer.notifyZoom({deltaZoomAmount:t-e},{position:this._baseCamera.position,rotation:this._baseCamera.rotation,zoomLevel:this._baseCamera.zoomLevel}),!0)}setMinZoomLevel(t){return!!this._baseCamera.setMinZoomLevel(t)}setZoomLevel(t){return!!this._baseCamera.setZoomLevel(t)}get rotation(){return this._baseCamera.rotation}get rotationBoundaries(){return this._baseCamera.rotationBoundaries}set rotationBoundaries(t){this._baseCamera.rotationBoundaries=t}getTransform(t,e){const o=t*this._baseCamera.viewPortWidth/2,i=t*this._baseCamera.viewPortHeight/2,n=-this._baseCamera.position.x,a=e?-this._baseCamera.position.y:this._baseCamera.position.y,r=t,s=this._baseCamera.zoomLevel,l=e?-this._baseCamera.rotation:this._baseCamera.rotation,c=Math.sin(l),h=Math.cos(l);return{a:s*r*h,b:s*r*c,c:-r*s*c,d:s*r*h,e:r*s*h*n-r*s*c*a+o,f:r*s*c*n+r*s*h*a+i}}setRotation(t){const e=this._baseCamera.rotation;return!!this._baseCamera.setRotation(t)&&(this._observer.notifyRotate({deltaRotation:t-e},{position:this._baseCamera.position,rotation:this._baseCamera.rotation,zoomLevel:this._baseCamera.zoomLevel}),!0)}getCameraOriginInWindow(t){return t}convertFromViewPort2WorldSpace(t){return s(t,this._baseCamera.position,this._baseCamera.zoomLevel,this._baseCamera.rotation)}convertFromWorld2ViewPort(t){return l(t,this._baseCamera.position,this._baseCamera.zoomLevel,this._baseCamera.rotation)}invertFromWorldSpace2ViewPort(t){let e={x:this._baseCamera.viewPortWidth/2,y:this._baseCamera.viewPortHeight/2},o=a.subVector(t,this._baseCamera.position);return o=a.rotatePoint(o,-this._baseCamera.rotation),o=a.multiplyVectorByScalar(o,this._baseCamera.zoomLevel),a.addVector(e,o)}setHorizontalBoundaries(t,e){if(t>e){let o=e;e=t,t=o}null==this._baseCamera.boundaries&&(this._baseCamera.boundaries={min:{x:void 0,y:void 0},max:{x:void 0,y:void 0}}),this._baseCamera.boundaries.min.x=t,this._baseCamera.boundaries.max.x=e}setVerticalBoundaries(t,e){if(t>e){let o=e;e=t,t=o}null==this._baseCamera.boundaries&&(this._baseCamera.boundaries={min:{x:void 0,y:void 0},max:{x:void 0,y:void 0}}),this._baseCamera.boundaries.min.y=t,this._baseCamera.boundaries.max.y=e}on(t,e,o){return this._observer.on(t,e,o)}}class G{constructor(t,e=new j){this._panBy=B(),this._panTo=w(),this._zoomTo=W(),this._zoomBy=N(),this._rotateBy=E(),this._rotateTo=V(),this._config=Object.assign(Object.assign({},t),{restrictRotation:!1,clampRotation:!0}),this._camera=e}zoomToAt(t,e){let o=this._camera.convertFromViewPort2WorldSpace(e);const i=this._zoomTo(t,this._camera,this._config);this._camera.setZoomLevel(i);let n=this._camera.convertFromViewPort2WorldSpace(e);const r=a.subVector(o,n),s=this._panBy(r,this._camera,this._config);this._camera.setPosition(a.addVector(this._camera.position,s))}zoomByAt(t,e){let o=this._camera.convertFromViewPort2WorldSpace(e);const i=this._zoomBy(t,this._camera,this._config);this._camera.setZoomLevel(this._camera.zoomLevel+i);let n=this._camera.convertFromViewPort2WorldSpace(e);const r=a.subVector(o,n),s=this._panBy(r,this._camera,this._config);this._camera.setPosition(a.addVector(this._camera.position,s))}zoomTo(t){this._zoomTo(t,this._camera,this._config)}zoomBy(t){this._zoomBy(t,this._camera,this._config)}zoomToAtWorld(t,e){let o=this._camera.convertFromWorld2ViewPort(e);const i=this._zoomTo(t,this._camera,this._config);this._camera.setZoomLevel(i);let n=this._camera.convertFromWorld2ViewPort(e);const r=a.subVector(o,n),s=this._panBy(r,this._camera,this._config);this._camera.setPosition(a.addVector(this._camera.position,s))}zoomByAtWorld(t,e){let o=this._camera.convertFromWorld2ViewPort(e);const i=this._zoomBy(t,this._camera,this._config);this._camera.setZoomLevel(this._camera.zoomLevel+i);let n=this._camera.convertFromWorld2ViewPort(e);const r=h(a.subVector(o,n),this._camera.zoomLevel,this._camera.rotation),s=this._panBy(r,this._camera,this._config);this._camera.setPosition(a.addVector(this._camera.position,s))}panBy(t){const e=a.multiplyVectorByScalar(a.rotatePoint(t,this._camera.rotation),1/this._camera.zoomLevel),o=this._panBy(e,this._camera,this._config);this._camera.setPosition(a.addVector(this._camera.position,o))}panTo(t){const e=this._panTo(t,this._camera,this._config);this._camera.setPosition(e)}rotateBy(t){const e=this._rotateBy(t,this._camera,this._config);this._camera.setRotation(this._camera.rotation+e)}rotateTo(t){const e=this._rotateTo(t,this._camera,this._config);this._camera.setRotation(e)}set limitEntireViewPort(t){this._config.limitEntireViewPort=t}get limitEntireViewPort(){return this._config.limitEntireViewPort}get camera(){return this._camera}get config(){return this._config}set config(t){this._config=Object.assign({},t)}configure(t){this._config=Object.assign(Object.assign({},this._config),t)}cleanup(){}setup(){}}function X(t){return new G({limitEntireViewPort:!0,restrictRelativeXTranslation:!1,restrictRelativeYTranslation:!1,restrictXTranslation:!1,restrictYTranslation:!1,restrictZoom:!1,clampTranslation:!0,clampZoom:!0},t)}class ${constructor(t,e){this.bindFunctions(),this._stateMachine=e,this._keyfirstPressed=new Map,this._eventTarget=t}get disabled(){return this._disabled}set disabled(t){this._disabled=t}get stateMachine(){return this._stateMachine}setUp(){this.addEventListeners(this._eventTarget)}addEventListeners(t){t.addEventListener("pointerdown",this.pointerDownHandler),t.addEventListener("pointerup",this.pointerUpHandler),t.addEventListener("pointermove",this.pointerMoveHandler),t.addEventListener("wheel",this.scrollHandler),window.addEventListener("keydown",this.keypressHandler),window.addEventListener("keyup",this.keyupHandler)}tearDown(){this._eventTarget.removeEventListener("pointerdown",this.pointerDownHandler),this._eventTarget.removeEventListener("pointerup",this.pointerUpHandler),this._eventTarget.removeEventListener("pointermove",this.pointerMoveHandler),this._eventTarget.removeEventListener("wheel",this.scrollHandler),window.removeEventListener("keydown",this.keypressHandler),window.removeEventListener("keyup",this.keyupHandler)}bindFunctions(){this.pointerDownHandler=this.pointerDownHandler.bind(this),this.pointerUpHandler=this.pointerUpHandler.bind(this),this.pointerMoveHandler=this.pointerMoveHandler.bind(this),this.scrollHandler=this.scrollHandler.bind(this),this.keypressHandler=this.keypressHandler.bind(this),this.keyupHandler=this.keyupHandler.bind(this)}pointerDownHandler(t){this._disabled||(0!==t.button||"mouse"!==t.pointerType?1!==t.button||"mouse"!==t.pointerType||this.stateMachine.happens("middlePointerDown",{x:t.clientX,y:t.clientY}):this.stateMachine.happens("leftPointerDown",{x:t.clientX,y:t.clientY}))}pointerUpHandler(t){this._disabled||(0!==t.button||"mouse"!==t.pointerType?1!==t.button||"mouse"!==t.pointerType||this.stateMachine.happens("middlePointerUp",{x:t.clientX,y:t.clientY}):this.stateMachine.happens("leftPointerUp",{x:t.clientX,y:t.clientY}))}pointerMoveHandler(t){this._disabled||(1!==t.buttons||"mouse"!==t.pointerType?4!==t.buttons||"mouse"!==t.pointerType||this.stateMachine.happens("middlePointerMove",{x:t.clientX,y:t.clientY}):this.stateMachine.happens("leftPointerMove",{x:t.clientX,y:t.clientY}))}scrollHandler(t){this._disabled||(t.preventDefault(),t.ctrlKey?this.stateMachine.happens("scrollWithCtrl",{x:t.clientX,y:t.clientY,deltaX:t.deltaX,deltaY:t.deltaY}):this.stateMachine.happens("scroll",{deltaX:t.deltaX,deltaY:t.deltaY}))}keypressHandler(t){this._keyfirstPressed.has(t.key)||(this._keyfirstPressed.set(t.key,!0)," "===t.key&&(t.preventDefault(),this.stateMachine.happens("spacebarDown",{})))}keyupHandler(t){this._keyfirstPressed.has(t.key)&&this._keyfirstPressed.delete(t.key)," "===t.key&&this.stateMachine.happens("spacebarUp",{})}}class K{constructor(t,e){this._panDisabled=!1,this._zoomDisabled=!1,this._rotateDisabled=!1,this.touchPointsMap=new Map,this._canvas=t,this._disabled=!1,this.touchSM=e,this.bindListeners()}get touchStateMachine(){return this.touchSM}bindListeners(){this.touchstartHandler=this.touchstartHandler.bind(this),this.touchendHandler=this.touchendHandler.bind(this),this.touchcancelHandler=this.touchcancelHandler.bind(this),this.touchmoveHandler=this.touchmoveHandler.bind(this)}resetAttributes(){this.touchPointsMap.clear()}enableStrategy(){this._disabled=!1}disableStrategy(){this.resetAttributes(),this._disabled=!0}setUp(){this._canvas.addEventListener("touchstart",this.touchstartHandler),this._canvas.addEventListener("touchend",this.touchendHandler),this._canvas.addEventListener("touchcancel",this.touchcancelHandler),this._canvas.addEventListener("touchmove",this.touchmoveHandler)}tearDown(){this.resetAttributes(),this._canvas.removeEventListener("touchstart",this.touchstartHandler),this._canvas.removeEventListener("touchend",this.touchendHandler),this._canvas.removeEventListener("touchcancel",this.touchcancelHandler),this._canvas.removeEventListener("touchmove",this.touchmoveHandler)}get disabled(){return this._disabled}get alignCoordinateSystem(){return this._touchInputTracker.alignCoordinateSystem}set alignCoordinateSystem(t){this._touchInputTracker.alignCoordinateSystem=t}get panDisabled(){return this._panDisabled}set panDisabled(t){this._panDisabled=t}get zoomDisabled(){return this._zoomDisabled}set zoomDisabled(t){this._zoomDisabled=t}get rotateDisabled(){return this._rotateDisabled}set rotateDisabled(t){this._rotateDisabled=t}touchstartHandler(t){if(this._disabled)return;const e=[];for(let o=0;o<t.changedTouches.length;o++)e.push({ident:t.changedTouches[o].identifier,x:t.changedTouches[o].clientX,y:t.changedTouches[o].clientY});this.touchSM.happens("touchstart",{points:e}),t.preventDefault()}touchcancelHandler(t){if(this._disabled)return;const e=[];for(let o=0;o<t.changedTouches.length;o++)e.push({ident:t.changedTouches[o].identifier,x:t.changedTouches[o].clientX,y:t.changedTouches[o].clientY});this.touchSM.happens("touchend",{points:e})}touchendHandler(t){if(this._disabled)return;const e=[];for(let o=0;o<t.changedTouches.length;o++)e.push({ident:t.changedTouches[o].identifier,x:t.changedTouches[o].clientX,y:t.changedTouches[o].clientY});this.touchSM.happens("touchend",{points:e})}touchmoveHandler(t){if(this._disabled)return;t.preventDefault();const e=[];for(let o=0;o<t.targetTouches.length;o++)e.push({ident:t.targetTouches[o].identifier,x:t.targetTouches[o].clientX,y:t.targetTouches[o].clientY});this.touchSM.happens("touchmove",{points:e})}}function J(t,e,o,i){const n=p(t),a=y(t);if(null==n||null==a)return;let r=e/Math.abs(n*Math.cos(i)),s=e/Math.abs(a*Math.cos(i)),l=o/Math.abs(n*Math.sin(i)),c=o/Math.abs(a*Math.sin(i));r==1/0&&(r=0),s==1/0&&(s=0),l==1/0&&(l=0),c==1/0&&(c=0);const h=o/a,u=e/n;return Math.max(h,u,r,s,l,c)}function q(t,e){return null!=e&&(null==t||e!=1/0&&(void 0!==t&&(null==t.min||e>t.min)))}function Q(t,e,o,i){const n=p(t);if(null==n)return;const a=Math.abs(n*Math.cos(i)),r=Math.abs(n*Math.sin(i));if(e/a==1/0)return o/r;return Math.max(e/a,o/r)}function tt(t,e,o,i){const n=y(t);if(null==n)return;const a=e/Math.abs(n*Math.cos(i)),r=o/Math.abs(n*Math.sin(i));if(r==1/0)return a;return Math.max(a,r)}function et(t){if(t<=0)return 0;let e=0;if(t<1){let o=1;for(;o>t;)o/=10,e--}else{let o=1;for(;10*o<=t;)o*=10,e++}return e}function ot(t,e){return t.top!==e.top||t.left!==e.left||t.width!==e.width||t.height!==e.height}function it(t){console.log("Position updated:",t)}const nt={fillRect:[1],strokeRect:[1],fillText:[1],strokeText:[1],lineTo:[1],moveTo:[1],quadraticCurveTo:[1,3],bezierCurveTo:[1,3,5],arc:[1]};function at(t){return new Proxy(t,{get(t,e,o){const i=Reflect.get(t,e,t);return"string"==typeof e&&e in nt&&"function"==typeof i?function(...o){const n=[...o],a=nt[e];for(const t of a)t<n.length&&(n[t]=-n[t]);return i.apply(t,n)}:"function"==typeof i?function(...e){return i.apply(t,e)}:i},set:(t,e,o)=>Reflect.set(t,e,o)})}class rt extends e{constructor(t,e,o){super(t,e,o)}notifyPanInput(t){this.happens("userPanByInput",{diff:t})}notifyPanToAnimationInput(t){this.happens("transitionPanToInput",{target:t})}initateTransition(){this.happens("initateTransition",{})}set limitEntireViewPort(t){this._context.limitEntireViewPort=t}get limitEntireViewPort(){return this._context.limitEntireViewPort}}class st extends o{constructor(){super(),this.eventReactions={userPanByInput:{action:this.userPanByInputHandler,defaultTargetState:"ACCEPTING_USER_INPUT"},userPanToInput:{action:this.userPanToInputHandler,defaultTargetState:"ACCEPTING_USER_INPUT"},lockedOnObjectPanByInput:{action:this.lockedOnObjectPanByInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"},lockedOnObjectPanToInput:{action:this.lockedOnObjectPanToInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"},initateTransition:{action:t,defaultTargetState:"TRANSITION"}}}userPanByInputHandler(t,e){t.panBy(e.diff)}userPanToInputHandler(t,e){t.panTo(e.target)}lockedOnObjectPanByInputHandler(t,e){t.panBy(e.diff)}lockedOnObjectPanToInputHandler(t,e){t.panTo(e.target)}}class lt extends o{constructor(){super(),this.eventReactions={userPanByInput:{action:this.userPanByInputHandler,defaultTargetState:"ACCEPTING_USER_INPUT"},userPanToInput:{action:this.userPanToInputHandler,defaultTargetState:"ACCEPTING_USER_INPUT"},transitionPanByInput:{action:this.transitionPanByInputHandler,defaultTargetState:"TRANSITION"},transitionPanToInput:{action:this.transitionPanToInputHandler,defaultTargetState:"TRANSITION"},lockedOnObjectPanByInput:{action:this.lockedOnObjectPanByInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"},lockedOnObjectPanToInput:{action:this.lockedOnObjectPanToInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"}}}userPanByInputHandler(t,e){return t.panBy(e.diff),"ACCEPTING_USER_INPUT"}userPanToInputHandler(t,e){return t.panTo(e.target),"ACCEPTING_USER_INPUT"}transitionPanByInputHandler(t,e){return t.panBy(e.diff),"TRANSITION"}transitionPanToInputHandler(t,e){return t.panTo(e.target),"TRANSITION"}lockedOnObjectPanByInputHandler(t,e){return t.panBy(e.diff),"LOCKED_ON_OBJECT"}lockedOnObjectPanToInputHandler(t,e){return t.panTo(e.target),"LOCKED_ON_OBJECT"}}class ct extends o{constructor(){super(),this.eventReactions={unlock:{action:t,defaultTargetState:"ACCEPTING_USER_INPUT"},lockedOnObjectPanByInput:{action:this.lockedOnObjectPanByInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"},lockedOnObjectPanToInput:{action:this.lockedOnObjectPanToInputHandler,defaultTargetState:"LOCKED_ON_OBJECT"}}}lockedOnObjectPanByInputHandler(t,e){t.panBy(e.diff)}lockedOnObjectPanToInputHandler(t,e){t.panTo(e.target)}}function ht(){return{ACCEPTING_USER_INPUT:new st,TRANSITION:new lt,LOCKED_ON_OBJECT:new ct}}function ut(t){return new rt(ht(),"ACCEPTING_USER_INPUT",t)}class dt extends o{constructor(){super(...arguments),this._eventReactions={userZoomByAtInput:{action:this.userZoomByAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"},userZoomToAtInput:{action:this.userZoomToAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"},initiateTransition:{action:t,defaultTargetState:"TRANSITION"}}}get eventReactions(){return this._eventReactions}userZoomByAtInput(t,e){t.zoomByAt(e.deltaZoom,e.anchorPoint)}userZoomToAtInput(t,e){t.zoomToAt(e.targetZoom,e.anchorPoint)}}class mt extends o{constructor(){super(),this._eventReactions={lockedOnObjectZoomByAtInput:{action:this.lockedOnObjectZoomByAtInput,defaultTargetState:"LOCKED_ON_OBJECT"},lockedOnObjectZoomToAtInput:{action:this.lockedOnObjectZoomToAtInput,defaultTargetState:"LOCKED_ON_OBJECT"},transitionZoomByAtInput:{action:this.transitionZoomByAtInput,defaultTargetState:"TRANSITION"},transitionZoomToAtInput:{action:this.transitionZoomToAtInput,defaultTargetState:"TRANSITION"},transitionZoomToAtCenterInput:{action:this.transitionZoomToAtCenterInput,defaultTargetState:"TRANSITION"},transitionZoomToAtWorldInput:{action:this.transitionZoomToAtWorldInput,defaultTargetState:"TRANSITION"},userZoomByAtInput:{action:this.userZoomByAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"},userZoomToAtInput:{action:this.userZoomToAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"}}}get eventReactions(){return this._eventReactions}lockedOnObjectZoomByAtInput(t,e){t.zoomBy(e.deltaZoom)}lockedOnObjectZoomToAtInput(t,e){t.zoomTo(e.targetZoom)}userZoomByAtInput(t,e){t.zoomByAt(e.deltaZoom,e.anchorPoint)}userZoomToAtInput(t,e){t.zoomToAt(e.targetZoom,e.anchorPoint)}transitionZoomByAtInput(t,e){t.zoomByAt(e.deltaZoom,e.anchorPoint)}transitionZoomByAtCenterInput(t,e){t.zoomBy(e.deltaZoom)}transitionZoomToAtInput(t,e){t.zoomToAt(e.targetZoom,e.anchorPoint)}transitionZoomToAtCenterInput(t,e){t.zoomTo(e.targetZoom)}transitionZoomToAtWorldInput(t,e){t.zoomToAtWorld(e.targetZoom,e.anchorPoint)}}class pt extends o{constructor(){super(),this._eventReactions={lockedOnObjectZoomByAtInput:{action:this.lockedOnObjectZoomByAtInput,defaultTargetState:"LOCKED_ON_OBJECT"},lockedOnObjectZoomToAtInput:{action:this.lockedOnObjectZoomToAtInput,defaultTargetState:"LOCKED_ON_OBJECT"},userZoomByAtInput:{action:this.userZoomByAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"},userZoomToAtInput:{action:this.userZoomToAtInput,defaultTargetState:"ACCEPTING_USER_INPUT"}}}get eventReactions(){return this._eventReactions}lockedOnObjectZoomByAtInput(t,e){t.zoomByAt(e.deltaZoom,e.anchorPoint)}lockedOnObjectZoomToAtInput(t,e){t.zoomToAt(e.targetZoom,e.anchorPoint)}userZoomByAtInput(t,e){t.zoomByAt(e.deltaZoom,e.anchorPoint)}userZoomToAtInput(t,e){t.zoomToAt(e.targetZoom,e.anchorPoint)}}class _t extends e{constructor(t,e,o){super(t,e,o)}notifyZoomByAtInput(t,e){this.happens("userZoomByAtInput",{deltaZoom:t,anchorPoint:e})}notifyZoomByAtInputAnimation(t,e){this.happens("transitionZoomByAtInput",{deltaZoom:t,anchorPoint:e})}notifyZoomToAtCenterInput(t,e){this.happens("transitionZoomToAtCenterInput",{targetZoom:t,anchorPoint:e})}notifyZoomToAtWorldInput(t,e){this.happens("transitionZoomToAtWorldInput",{targetZoom:t,anchorPoint:e})}initateTransition(){this.happens("initiateTransition",{})}}function yt(){return{ACCEPTING_USER_INPUT:new dt,TRANSITION:new mt,LOCKED_ON_OBJECT:new pt}}function xt(t){return new _t(yt(),"ACCEPTING_USER_INPUT",t)}class vt{constructor(t,e){this._panStateMachine=t,this._zoomStateMachine=e}notifyPanToAnimationInput(t){this._panStateMachine.notifyPanToAnimationInput(t)}notifyPanInput(t){this._panStateMachine.notifyPanInput(t)}notifyZoomInput(t,e){this._zoomStateMachine.notifyZoomByAtInput(t,e)}notifyZoomInputAnimation(t,e={x:0,y:0}){this._zoomStateMachine.notifyZoomToAtCenterInput(t,e)}notifyZoomInputAnimationWorld(t,e={x:0,y:0}){this._zoomStateMachine.notifyZoomToAtWorldInput(t,e)}notifyRotationInput(t){console.error("Rotation input is not implemented")}initatePanTransition(){this._panStateMachine.initateTransition()}initateZoomTransition(){this._zoomStateMachine.initateTransition()}}class ft{constructor(t=X(new j)){this._cameraRig=t}notifyPanInput(t){this._cameraRig.panBy(t)}notifyZoomInput(t,e){this._cameraRig.zoomByAt(t,e)}notifyRotationInput(t){this._cameraRig.rotateBy(t)}}function Pt(t){const e=X(t);return new ft(e)}function gt(t){return new ft(t)}class bt{constructor(t){this.pan=new U,this.zoom=new U,this.rotate=new U,this.all=new U,this._flowControl=t}notifyPan(t){this._flowControl.notifyPanInput(t),this.pan.notify({diff:t}),this.all.notify({type:"pan",diff:t})}notifyZoom(t,e){this._flowControl.notifyZoomInput(t,e),this.zoom.notify({deltaZoomAmount:t,anchorPoint:e}),this.all.notify({type:"zoom",deltaZoomAmount:t,anchorPoint:e})}notifyRotate(t){this._flowControl.notifyRotationInput(t),this.rotate.notify({deltaRotation:t}),this.all.notify({type:"rotate",deltaRotation:t})}on(t,e){switch(t){case"pan":return this.pan.subscribe(e);case"zoom":return this.zoom.subscribe(e);case"rotate":return this.rotate.subscribe(e);case"all":return this.all.subscribe(e);default:throw new Error("Invalid raw user input event name")}}get flowControl(){return this._flowControl}set flowControl(t){this._flowControl=t}}function Tt(t,e){const o=e.getBoundingClientRect(),i={x:o.left+(o.right-o.left)/2,y:o.top+(o.bottom-o.top)/2};return a.subVector(t,i)}class wt extends o{constructor(){super(),this._guards={isIdle:()=>!0},this._eventGuards={},this._eventReactions={spacebarDown:{action:this.spacebarDownHandler,defaultTargetState:"READY_TO_PAN_VIA_SPACEBAR"},scroll:{action:this.scrollHandler,defaultTargetState:"IDLE"},scrollWithCtrl:{action:this.scrollWithCtrlHandler,defaultTargetState:"IDLE"},middlePointerDown:{action:this.middlePointerDownHandler,defaultTargetState:"READY_TO_PAN_VIA_SCROLL_WHEEL"}}}get eventReactions(){return this._eventReactions}scrollHandler(t,e){const o=Object.assign({},e);t.alignCoordinateSystem||(o.deltaY=-o.deltaY),t.notifyOnPan({x:o.deltaX,y:o.deltaY})}scrollWithCtrlHandler(t,e){let o=.005;Math.abs(e.deltaY)>100&&(o=5e-4);const i=e.deltaY*o,n=Tt({x:e.x,y:e.y},t.canvas);t.alignCoordinateSystem||(n.y=-n.y),t.notifyOnZoom(-5*i,n)}spacebarDownHandler(t,e){t.canvas.style.cursor="grab"}middlePointerDownHandler(t,e){t.setInitialCursorPosition({x:e.x,y:e.y}),t.canvas.style.cursor="grabbing"}}class Bt extends o{constructor(){super(),this._eventReactions={spacebarUp:{action:this.spacebarUpHandler,defaultTargetState:"IDLE"},leftPointerDown:{action:this.leftPointerDownHandler,defaultTargetState:"INITIAL_PAN"}}}get eventReactions(){return this._eventReactions}leftPointerDownHandler(t,e){t.setInitialCursorPosition({x:e.x,y:e.y}),t.canvas.style.cursor="grabbing"}spacebarUpHandler(t,e){t.canvas.style.cursor="default"}}class It extends o{constructor(){super(),this._eventReactions={leftPointerUp:{action:this.leftPointerUpHandler,defaultTargetState:"READY_TO_PAN_VIA_SPACEBAR"},leftPointerMove:{action:this.leftPointerMoveHandler,defaultTargetState:"PAN"},spacebarUp:{action:()=>"IDLE",defaultTargetState:"IDLE"},leftPointerDown:{action:()=>"PAN",defaultTargetState:"PAN"}}}get eventReactions(){return this._eventReactions}leftPointerMoveHandler(t,e){const o={x:t.initialCursorPosition.x-e.x,y:t.initialCursorPosition.y-e.y};t.alignCoordinateSystem||(o.y=-o.y),t.notifyOnPan(o),t.setInitialCursorPosition({x:e.x,y:e.y})}leftPointerUpHandler(t,e){t.canvas.style.cursor="grab"}}class St extends o{constructor(){super(),this._eventReactions={middlePointerUp:{action:this.middlePointerUpHandler,defaultTargetState:"IDLE"},middlePointerMove:{action:this.middlePointerMoveHandler,defaultTargetState:"PAN_VIA_SCROLL_WHEEL"}}}get eventReactions(){return this._eventReactions}middlePointerMoveHandler(t,e){t.canvas.style.cursor="grabbing"}middlePointerUpHandler(t,e){t.canvas.style.cursor="default"}}class Ct extends o{constructor(){super(),this._eventReactions={leftPointerUp:{action:this.leftPointerUpHandler,defaultTargetState:"READY_TO_PAN_VIA_SPACEBAR"},leftPointerMove:{action:this.leftPointerMoveHandler,defaultTargetState:"PAN"},spacebarUp:{action:this.spacebarUpHandler,defaultTargetState:"IDLE"}}}get eventReactions(){return this._eventReactions}leftPointerMoveHandler(t,e){const o={x:t.initialCursorPosition.x-e.x,y:t.initialCursorPosition.y-e.y};t.alignCoordinateSystem||(o.y=-o.y),t.notifyOnPan(o),t.setInitialCursorPosition({x:e.x,y:e.y})}spacebarUpHandler(t,e){t.canvas.style.cursor="default"}leftPointerUpHandler(t,e){t.canvas.style.cursor="grab"}}class zt extends o{constructor(){super(...arguments),this._eventReactions={middlePointerUp:{action:this.middlePointerUpHandler,defaultTargetState:"IDLE"},middlePointerMove:{action:this.middlePointerMoveHandler,defaultTargetState:"PAN_VIA_SCROLL_WHEEL"}}}get eventReactions(){return this._eventReactions}middlePointerMoveHandler(t,e){const o={x:t.initialCursorPosition.x-e.x,y:t.initialCursorPosition.y-e.y};t.alignCoordinateSystem||(o.y=-o.y),t.notifyOnPan(o),t.setInitialCursorPosition({x:e.x,y:e.y})}middlePointerUpHandler(t,e){t.canvas.style.cursor="default"}}function Rt(t){const o={IDLE:new wt,READY_TO_PAN_VIA_SPACEBAR:new Bt,INITIAL_PAN:new It,PAN:new Ct,READY_TO_PAN_VIA_SCROLL_WHEEL:new St,PAN_VIA_SCROLL_WHEEL:new zt};return new e(o,"IDLE",t)}class Mt extends o{constructor(){super(...arguments),this._eventReactions={touchstart:{action:this.touchstart,defaultTargetState:"IDLE"},touchend:{action:this.touchend,defaultTargetState:"IDLE"}},this._guards={touchPointsCount:(t=>2===t.getCurrentTouchPointsCount()).bind(this)},this._eventGuards={touchstart:[{guard:"touchPointsCount",target:"PENDING"}],touchend:[{guard:"touchPointsCount",target:"PENDING"}]}}get eventReactions(){return this._eventReactions}touchstart(t,e){t.addTouchPoints(e.points)}touchend(t,e){t.removeTouchPoints(e.points.map((t=>t.ident)))}}class At extends o{constructor(){super(...arguments),this._eventReactions={touchstart:{action:this.touchstart,defaultTargetState:"IDLE"},touchend:{action:this.touchend,defaultTargetState:"IDLE"},touchmove:{action:this.touchmove,defaultTargetState:"IN_PROGRESS"}}}get eventReactions(){return this._eventReactions}touchstart(t,e){t.addTouchPoints(e.points)}touchend(t,e){t.removeTouchPoints(e.points.map((t=>t.ident)))}touchmove(t,e){const o=e.points.map((t=>t.ident)),i=t.getInitialTouchPointsPositions(o),n=e.points,r=a.distanceBetweenPoints(i[0],i[1]),s=a.distanceBetweenPoints(n[0],n[1]),l=a.linearInterpolation(i[0],i[1],.5),c=a.linearInterpolation(n[0],n[1],.5),h=a.subVector(l,c),u=t.canvas.getBoundingClientRect(),d={x:u.left+u.width/2,y:u.top+u.height/2},m=a.subVector(l,d);let p=Math.abs(s-r)>a.distanceBetweenPoints(l,c)?"ZOOMING":"PANNING";switch(t.updateTouchPoints(n),p){case"ZOOMING":t.notifyOnZoom(.005*(s-r),m);break;case"PANNING":t.notifyOnPan(h);break;default:console.warn("Unknown panZoom state",p)}}}class Ot extends o{constructor(){super(...arguments),this._eventReactions={touchmove:{action:this.touchmove,defaultTargetState:"IN_PROGRESS"},touchend:{action:this.touchend,defaultTargetState:"IDLE"},touchstart:{action:()=>"IDLE",defaultTargetState:"IDLE"}}}get eventReactions(){return this._eventReactions}touchmove(t,e){const o=e.points.map((t=>t.ident)),i=t.getInitialTouchPointsPositions(o),n=e.points,r=a.distanceBetweenPoints(i[0],i[1]),s=a.distanceBetweenPoints(n[0],n[1]),l=a.linearInterpolation(i[0],i[1],.5),c=a.linearInterpolation(n[0],n[1],.5),h=a.subVector(l,c),u=t.canvas.getBoundingClientRect(),d={x:u.left+u.width/2,y:u.top+u.height/2},m=a.subVector(l,d);let p=Math.abs(s-r)>a.distanceBetweenPoints(l,c)?"ZOOMING":"PANNING";switch(t.updateTouchPoints(n),p){case"ZOOMING":t.alignCoordinateSystem||(m.y=-m.y),t.notifyOnZoom(.005*-(r-s),m);break;case"PANNING":t.alignCoordinateSystem||(h.y=-h.y),t.notifyOnPan(h);break;default:console.warn("Unknown panZoom state",p)}}touchend(t,e){t.removeTouchPoints(e.points.map((t=>t.ident)))}}function Ht(t){return new e({IDLE:new Mt,PENDING:new At,IN_PROGRESS:new Ot},"IDLE",t)}class Et{constructor(t,e){this._alignCoordinateSystem=!0,this._canvas=t,this._inputPublisher=e,this._initialCursorPosition={x:0,y:0}}get alignCoordinateSystem(){return this._alignCoordinateSystem}get canvas(){return this._canvas}get initialCursorPosition(){return this._initialCursorPosition}set alignCoordinateSystem(t){this._alignCoordinateSystem=t}notifyOnPan(t){this._inputPublisher.notifyPan(t)}notifyOnZoom(t,e){this._inputPublisher.notifyZoom(t,e)}notifyOnRotate(t){this._inputPublisher.notifyRotate(t)}setInitialCursorPosition(t){this._initialCursorPosition=t}cleanup(){}setup(){}}class Vt{constructor(t,e){this._touchPointsMap=new Map,this._canvas=t,this._inputPublisher=e,this._alignCoordinateSystem=!0}addTouchPoints(t){t.forEach((t=>{this._touchPointsMap.set(t.ident,Object.assign({},t))}))}removeTouchPoints(t){t.forEach((t=>{this._touchPointsMap.has(t)&&this._touchPointsMap.delete(t)}))}getCurrentTouchPointsCount(){return this._touchPointsMap.size}getInitialTouchPointsPositions(t){const e=[];return t.forEach((t=>{if(this._touchPointsMap.has(t)){const o=this._touchPointsMap.get(t);o&&e.push(o)}})),e}updateTouchPoints(t){t.forEach((t=>{this._touchPointsMap.has(t.ident)&&this._touchPointsMap.set(t.ident,Object.assign({},t))}))}notifyOnPan(t){this._inputPublisher.notifyPan(t)}notifyOnZoom(t,e){this._inputPublisher.notifyZoom(t,e)}get canvas(){return this._canvas}get alignCoordinateSystem(){return this._alignCoordinateSystem}set alignCoordinateSystem(t){this._alignCoordinateSystem=t}cleanup(){}setup(){}}exports.AcceptingUserInputState=st,exports.BaseCamera=Y,exports.Board=class{constructor(t,e=t){this._alignCoordinateSystem=!0,this._fullScreen=!1,this.lastUpdateTime=0,this._canvas=t;const o=new j;o.viewPortHeight=t.height,o.viewPortWidth=t.width,o.boundaries={min:{x:-5e3,y:-5e3},max:{x:5e3,y:5e3}};const i=t.getContext("2d");if(null==i)throw new Error("Canvas 2d context is null");this._context=i,this._reversedContext=at(i),this.bindFunctions(),this.attributeObserver=new MutationObserver(this.attributeCallBack),this.attributeObserver.observe(this._canvas,{attributes:!0}),this.windowResizeObserver=new ResizeObserver(this.windowResizeHandler),this.windowResizeObserver.observe(document.body),this.cameraRig=new G({limitEntireViewPort:!0,restrictRelativeXTranslation:!1,restrictRelativeYTranslation:!1,restrictXTranslation:!1,restrictYTranslation:!1,restrictZoom:!1,clampTranslation:!0,clampZoom:!0},o),this.boardInputPublisher=new bt(gt(this.cameraRig)),this._observableInputTracker=new Et(t,this.boardInputPublisher),this._touchInputTracker=new Vt(t,this.boardInputPublisher),this._kmtInputStateMachine=Rt(this._observableInputTracker),this._touchInputStateMachine=Ht(this._touchInputTracker),this._kmtParser=new $(e,this._kmtInputStateMachine),this._touchParser=new K(this._canvas,this._touchInputStateMachine),this._canvas.style.width=this._canvas.width+"px",this._canvas.style.height=this._canvas.height+"px",this._canvas.width=window.devicePixelRatio*this._canvas.width,this._canvas.height=window.devicePixelRatio*this._canvas.height,this.registerEventListeners()}registerEventListeners(){this._kmtParser.setUp(),this._touchParser.setUp()}removeEventListeners(){this._touchParser.tearDown(),this._kmtParser.tearDown()}setup(){this.registerEventListeners(),this.windowResizeObserver.observe(document.body),this.attributeObserver.observe(this._canvas,{attributes:!0})}tearDown(){this.removeEventListeners(),this.windowResizeObserver.disconnect(),this.attributeObserver.disconnect()}bindFunctions(){this.step=this.step.bind(this),this.attributeCallBack=this.attributeCallBack.bind(this),this.windowResizeHandler=this.windowResizeHandler.bind(this)}set width(t){if(this._canvas.width=t*window.devicePixelRatio,this._canvas.style.width=t+"px",this.camera.viewPortWidth=t,this.limitEntireViewPort){const t=Q(this.camera.boundaries,this._canvas.width/window.devicePixelRatio,this._canvas.height/window.devicePixelRatio,this.camera.rotation);null!=t&&q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}get width(){return this._canvas.width/window.devicePixelRatio}set height(t){if(this._canvas.height=t*window.devicePixelRatio,this._canvas.style.height=t+"px",this.camera.viewPortHeight=t,this.limitEntireViewPort){const t=tt(this.camera.boundaries,this._canvas.width/window.devicePixelRatio,this._canvas.height/window.devicePixelRatio,this.camera.rotation);null!=t&&q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}get height(){return this._canvas.height}set alignCoordinateSystem(t){this._alignCoordinateSystem=t,this._observableInputTracker.alignCoordinateSystem=t,this._touchInputTracker.alignCoordinateSystem=t}get alignCoordinateSystem(){return this._alignCoordinateSystem}get fullScreen(){return this._fullScreen}set fullScreen(t){this._fullScreen=t,this._fullScreen&&(this.width=window.innerWidth,this.height=window.innerHeight)}get context(){return this._alignCoordinateSystem?this._context:this._reversedContext}set limitEntireViewPort(t){if(this.cameraRig.limitEntireViewPort=t,t){const t=J(this.camera.boundaries,this._canvas.width,this._canvas.height,this.camera.rotation);null!=t&&q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}get limitEntireViewPort(){return this.cameraRig.limitEntireViewPort}set kmtParser(t){this._kmtParser.tearDown(),t.setUp(),this._kmtParser=t}get kmtParser(){return this._kmtParser}set touchParser(t){this._touchParser.tearDown(),t.setUp(),this._touchParser=t}get touchParser(){return this._touchParser}get camera(){return this.cameraRig.camera}set camera(t){t.viewPortHeight=this._canvas.height/window.devicePixelRatio,t.viewPortWidth=this._canvas.width/window.devicePixelRatio,this.camera=t}get flowControl(){return this.boardInputPublisher.flowControl}set flowControl(t){this.boardInputPublisher.flowControl=t}step(t){this.lastUpdateTime,this.lastUpdateTime=t,this._context.reset();const e=this.camera.boundaries;if(!d(e))throw new Error("Boundaries are not fully defined; not able to clear the canvas under the current implementation");this._context.clearRect(e.min.x,-e.min.y,e.max.x-e.min.x,-(e.max.y-e.min.y));const o=this.camera.getTransform(window.devicePixelRatio,this._alignCoordinateSystem);this._context.setTransform(o.a,o.b,o.c,o.d,o.e,o.f)}convertWindowPoint2WorldCoord(t){const e=this._canvas.getBoundingClientRect(),o={x:e.left+(e.right-e.left)/2,y:e.top+(e.bottom-e.top)/2},i=a.subVector(t,o);return this._alignCoordinateSystem||(i.y=-i.y),this.camera.convertFromViewPort2WorldSpace(i)}on(t,e){return this.camera.on(t,e)}onInput(t,e){return this.boardInputPublisher.on(t,e)}get maxHalfTransHeight(){return x(this.camera.boundaries)}get maxHalfTransWidth(){return _(this.camera.boundaries)}attributeCallBack(t,e){for(let e of t)if("attributes"===e.type)if("width"===e.attributeName){if(this.camera.viewPortWidth=parseFloat(this._canvas.style.width),this.limitEntireViewPort){const t=Q(this.camera.boundaries,this.camera.viewPortWidth,this.camera.viewPortHeight,this.camera.rotation);q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}else if("height"===e.attributeName&&(this.camera.viewPortHeight=parseFloat(this._canvas.style.height),this.limitEntireViewPort)){const t=tt(this.camera.boundaries,this.camera.viewPortWidth,this.camera.viewPortHeight,this.camera.rotation);q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}windowResizeHandler(){this._fullScreen&&(this.width=window.innerWidth,this.height=window.innerHeight)}setMaxTransWidthWithFixedMinBoundary(t){const e=this.camera.boundaries,o=null==e?void 0:e.min,i=null==o?void 0:o.x;if(null==i?this.camera.setHorizontalBoundaries(-t,t):this.camera.setHorizontalBoundaries(i,i+2*t),this.limitEntireViewPort){const t=Q(this.camera.boundaries,this.camera.viewPortWidth,this.camera.viewPortHeight,this.camera.rotation);q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}setMaxTransWidthWithFixedMaxBoundary(t){const e=this.camera.boundaries,o=null==e?void 0:e.max,i=null==o?void 0:o.x;if(null==i?this.camera.setHorizontalBoundaries(-t,t):this.camera.setHorizontalBoundaries(i-2*t,i),this.limitEntireViewPort){const t=Q(this.camera.boundaries,this.camera.viewPortWidth,this.camera.viewPortHeight,this.camera.rotation);q(this.camera.zoomBoundaries,t)&&this.camera.setMinZoomLevel(t)}}get restrictRelativeXTranslation(){return this.cameraRig.config.restrictRelativeXTranslation}get restrictRelativeYTranslation(){return this.cameraRig.config.restrictRelativeYTranslation}get restrictXTranslation(){return this.cameraRig.config.restrictXTranslation}get restrictYTranslation(){return this.cameraRig.config.restrictYTranslation}set restrictRelativeXTranslation(t){this.cameraRig.config.restrictRelativeXTranslation=t}set restrictRelativeYTranslation(t){this.cameraRig.configure({restrictRelativeYTranslation:t})}set restrictXTranslation(t){this.cameraRig.configure({restrictXTranslation:t})}set restrictYTranslation(t){this.cameraRig.configure({restrictYTranslation:t})}get restrictZoom(){return this.cameraRig.config.restrictZoom}set restrictZoom(t){this.cameraRig.configure({restrictZoom:t})}get restrictRotation(){return this.cameraRig.config.restrictRotation}set restrictRotation(t){this.cameraRig.configure({restrictRotation:t})}get clampTransl