@zoompinch/elements 0.0.5 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- var x = Object.defineProperty;
2
- var y = (r, t, e) => t in r ? x(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var b = (r, t, e) => y(r, typeof t != "symbol" ? t + "" : t, e);
4
- var E = Object.defineProperty, A = (r, t, e) => t in r ? E(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, p = (r, t, e) => A(r, typeof t != "symbol" ? t + "" : t, e);
1
+ var y = Object.defineProperty;
2
+ var E = (r, t, e) => t in r ? y(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var b = (r, t, e) => E(r, typeof t != "symbol" ? t + "" : t, e);
4
+ var x = Object.defineProperty, A = (r, t, e) => t in r ? x(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, f = (r, t, e) => A(r, typeof t != "symbol" ? t + "" : t, e);
5
5
  function z(r) {
6
6
  return r * Math.PI / 180;
7
7
  }
@@ -12,7 +12,7 @@ function v(r, t, e) {
12
12
  const [s, n] = r, [a, h] = t, o = Math.cos(e) * (s - a) - Math.sin(e) * (n - h) + a, c = Math.sin(e) * (s - a) + Math.cos(e) * (n - h) + h;
13
13
  return [o, c];
14
14
  }
15
- function S(r, t) {
15
+ function w(r, t) {
16
16
  const e = Math.pow(10, t);
17
17
  return Math.round(r * e) / e;
18
18
  }
@@ -29,32 +29,27 @@ function W(r, t, e, s, n) {
29
29
  const o = Math.cos(-n), c = Math.sin(-n);
30
30
  let l = a * o - h * c, u = a * c + h * o;
31
31
  const d = r.width / s, i = r.height / s;
32
- return l /= s, u /= s, { x: S(l, 4), y: S(u, 4), width: S(d, 4), height: S(i, 4) };
32
+ return l /= s, u /= s, { x: w(l, 4), y: w(u, 4), width: w(d, 4), height: w(i, 4) };
33
33
  }
34
34
  class L extends EventTarget {
35
35
  constructor(t, e, s, n, a, h, o = 0.1, c = 10) {
36
- super(), p(this, "wrapperBounds"), p(this, "canvasBounds"), p(this, "gestureStartRadians", 0), p(this, "dragStart", null), p(this, "dragStartFrozenX", null), p(this, "dragStartFrozenY", null), p(this, "touchStarts", null), p(this, "touchStartTranslateX", 0), p(this, "touchStartTranslateY", 0), p(this, "centeredTransform", {
37
- translateX: 0,
38
- translateY: 0,
39
- scale: 1,
40
- radians: 0
41
- }), this.element = t, this.offset = e, this.translateX = s, this.translateY = n, this.scale = a, this.rotate = h, this.minScale = o, this.maxScale = c;
36
+ super(), f(this, "wrapperBounds"), f(this, "canvasBounds"), f(this, "gestureStartRotate", 0), f(this, "dragStart", null), f(this, "dragStartFrozenX", null), f(this, "dragStartFrozenY", null), f(this, "touchStarts", null), f(this, "touchStartTranslateX", 0), f(this, "touchStartTranslateY", 0), this.element = t, this.offset = e, this.translateX = s, this.translateY = n, this.scale = a, this.rotate = h, this.minScale = o, this.maxScale = c;
42
37
  const l = new ResizeObserver(() => {
43
- const { x: d, y: i, width: g, height: f } = this.element.getBoundingClientRect();
44
- this.wrapperBounds = { x: d, y: i, width: g, height: f }, this.update();
38
+ const { x: d, y: i, width: g, height: p } = this.element.getBoundingClientRect();
39
+ this.wrapperBounds = { x: d, y: i, width: g, height: p }, this.update();
45
40
  }), u = new ResizeObserver(() => {
46
- const { x: d, y: i, width: g, height: f } = W(
41
+ const { x: d, y: i, width: g, height: p } = W(
47
42
  this.canvasElement.getBoundingClientRect(),
48
43
  this.renderingTranslateX,
49
44
  this.renderingTranslateY,
50
45
  this.renderinScale,
51
46
  this.renderingRotate
52
47
  );
53
- this.canvasBounds = { x: d, y: i, width: g, height: f }, this.update();
48
+ this.canvasBounds = { x: d, y: i, width: g, height: p }, this.update();
54
49
  });
55
50
  requestAnimationFrame(() => {
56
- this.wrapperBounds = this.element.getBoundingClientRect(), this.canvasBounds = this.canvasElement.getBoundingClientRect(), this.update();
57
- }), u.observe(this.canvasElement), l.observe(this.element);
51
+ this.wrapperBounds = this.element.getBoundingClientRect(), this.canvasBounds = this.canvasElement.getBoundingClientRect(), this.update(), this.dispatchEvent(new Event("init"));
52
+ }), u.observe(this.canvasElement), l.observe(this.element), console.log("INIT");
58
53
  }
59
54
  get canvasElement() {
60
55
  return this.element.querySelector(".canvas");
@@ -83,14 +78,14 @@ class L extends EventTarget {
83
78
  return this.canvasNaturalRatio >= this.wrapperInnerRatio ? this.wrapperInnerWidth / this.canvasBounds.width : this.wrapperInnerHeight / this.canvasBounds.height;
84
79
  }
85
80
  handleGesturestart(t) {
86
- this.gestureStartRadians = this.rotate;
81
+ this.gestureStartRotate = this.rotate;
87
82
  }
88
83
  handleGesturechange(t) {
89
84
  const { clientX: e, clientY: s } = t, n = t.rotation;
90
85
  if (n === 0)
91
86
  return;
92
87
  const a = this.normalizeMatrixCoordinates(e, s);
93
- this.rotateCanvas(a[0], a[1], this.gestureStartRadians + z(n));
88
+ this.rotateCanvas(a[0], a[1], this.gestureStartRotate + z(n));
94
89
  }
95
90
  handleGestureend(t) {
96
91
  }
@@ -155,24 +150,24 @@ class L extends EventTarget {
155
150
  e[0][0] / this.wrapperInnerWidth,
156
151
  e[0][1] / this.wrapperInnerHeight
157
152
  ], l = this.touchStarts[0].canvasRel, [u, d] = this.calcProjectionTranslate(o, c, l, 0);
158
- let i = 0, g = 0, f = 0;
153
+ let i = 0, g = 0, p = 0;
159
154
  const m = Math.atan2(
160
155
  n[1] - s[1],
161
156
  n[0] - s[0]
162
157
  );
163
- f = Math.atan2(e[1][1] - e[0][1], e[1][0] - e[0][0]) - m;
164
- const Y = (R, N) => [
165
- this.offset.left + this.canvasBounds.width * R * this.naturalScale * o + u,
166
- this.offset.top + this.canvasBounds.height * N * this.naturalScale * o + d
167
- ], w = Y(0, 0), M = Y(
158
+ p = Math.atan2(e[1][1] - e[0][1], e[1][0] - e[0][0]) - m;
159
+ const Y = (N, R) => [
160
+ this.offset.left + this.canvasBounds.width * N * this.naturalScale * o + u,
161
+ this.offset.top + this.canvasBounds.height * R * this.naturalScale * o + d
162
+ ], S = Y(0, 0), M = Y(
168
163
  this.touchStarts[0].canvasRel[0],
169
164
  this.touchStarts[0].canvasRel[1]
170
165
  ), X = v(
171
- w,
166
+ S,
172
167
  M,
173
- f
168
+ p
174
169
  );
175
- i = X[0] - w[0], g = X[1] - w[1], this.scale = o, this.rotate = f, this.translateX = u + i, this.translateY = d + g;
170
+ i = X[0] - S[0], g = X[1] - S[1], this.scale = o, this.rotate = p, this.translateX = u + i, this.translateY = d + g;
176
171
  } else {
177
172
  const s = t.touches[0].clientX - this.touchStarts[0].client[0], n = t.touches[0].clientY - this.touchStarts[0].client[1], a = this.touchStartTranslateX + s, h = this.touchStartTranslateY + n;
178
173
  this.translateX = a, this.translateY = h;
@@ -188,6 +183,7 @@ class L extends EventTarget {
188
183
  return [i, g];
189
184
  }
190
185
  applyTransform(t, e, s) {
186
+ console.log("....apply transform");
191
187
  const n = this.calcProjectionTranslate(t, e, s, 0);
192
188
  this.scale = t, this.translateX = n[0], this.translateY = n[1], this.update();
193
189
  }
@@ -335,8 +331,8 @@ class B extends HTMLElement {
335
331
  "touchstart",
336
332
  (i) => this.engine.handleTouchstart(i)
337
333
  ), window.addEventListener("touchmove", (i) => this.engine.handleTouchmove(i)), window.addEventListener("touchend", (i) => this.engine.handleTouchend(i)), this.engine.addEventListener("update", () => {
338
- const i = this.engine.translateX.toString(), g = this.engine.translateY.toString(), f = this.engine.scale.toString(), m = this.engine.rotate.toString();
339
- this.getAttribute("translate-x") !== i && this.setAttribute("translate-x", i), this.getAttribute("translate-y") !== g && this.setAttribute("translate-y", g), this.getAttribute("scale") !== f && this.setAttribute("scale", f), this.getAttribute("rotate") !== m && this.setAttribute("rotate", m), this.dispatchEvent(new Event("update"));
334
+ const i = this.engine.translateX.toString(), g = this.engine.translateY.toString(), p = this.engine.scale.toString(), m = this.engine.rotate.toString();
335
+ this.getAttribute("translate-x") !== i && this.setAttribute("translate-x", i), this.getAttribute("translate-y") !== g && this.setAttribute("translate-y", g), this.getAttribute("scale") !== p && this.setAttribute("scale", p), this.getAttribute("rotate") !== m && this.setAttribute("rotate", m), this.dispatchEvent(new Event("update"));
340
336
  });
341
337
  }
342
338
  disconnectedCallback() {
@@ -373,12 +369,12 @@ class B extends HTMLElement {
373
369
  case "offset-right":
374
370
  case "offset-bottom":
375
371
  case "offset-left":
376
- const d = Number(this.getAttribute("offset-top") || "0"), i = Number(this.getAttribute("offset-right") || "0"), g = Number(this.getAttribute("offset-bottom") || "0"), f = Number(this.getAttribute("offset-left") || "0");
372
+ const d = Number(this.getAttribute("offset-top") || "0"), i = Number(this.getAttribute("offset-right") || "0"), g = Number(this.getAttribute("offset-bottom") || "0"), p = Number(this.getAttribute("offset-left") || "0");
377
373
  this.engine.offset = {
378
374
  top: d,
379
375
  right: i,
380
376
  bottom: g,
381
- left: f
377
+ left: p
382
378
  }, this.engine.update();
383
379
  break;
384
380
  }
@@ -1,4 +1,4 @@
1
- (function(m){typeof define=="function"&&define.amd?define(m):m()})((function(){"use strict";var W=Object.defineProperty;var L=(m,v,g)=>v in m?W(m,v,{enumerable:!0,configurable:!0,writable:!0,value:g}):m[v]=g;var X=(m,v,g)=>L(m,typeof v!="symbol"?v+"":v,g);var m=Object.defineProperty,v=(h,t,e)=>t in h?m(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,g=(h,t,e)=>v(h,typeof t!="symbol"?t+"":t,e);function N(h){return h*Math.PI/180}function T(h,t,e){return Math.min(Math.max(h,t),e)}function S(h,t,e){const[s,n]=h,[a,r]=t,o=Math.cos(e)*(s-a)-Math.sin(e)*(n-r)+a,c=Math.sin(e)*(s-a)+Math.cos(e)*(n-r)+r;return[o,c]}function w(h,t){const e=Math.pow(10,t);return Math.round(h*e)/e}function x(h){var t=!1;return h.wheelDeltaY?h.wheelDeltaY===h.deltaY*-3&&(t=!0):h.deltaMode===0&&(t=!0),t}function C(h,t){const e=t.find(s=>h%s===0);return e?h/e:1}function y(h,t,e,s,n){let a=h.left-t,r=h.top-e;const o=Math.cos(-n),c=Math.sin(-n);let l=a*o-r*c,u=a*c+r*o;const d=h.width/s,i=h.height/s;return l/=s,u/=s,{x:w(l,4),y:w(u,4),width:w(d,4),height:w(i,4)}}class E extends EventTarget{constructor(t,e,s,n,a,r,o=.1,c=10){super(),g(this,"wrapperBounds"),g(this,"canvasBounds"),g(this,"gestureStartRadians",0),g(this,"dragStart",null),g(this,"dragStartFrozenX",null),g(this,"dragStartFrozenY",null),g(this,"touchStarts",null),g(this,"touchStartTranslateX",0),g(this,"touchStartTranslateY",0),g(this,"centeredTransform",{translateX:0,translateY:0,scale:1,radians:0}),this.element=t,this.offset=e,this.translateX=s,this.translateY=n,this.scale=a,this.rotate=r,this.minScale=o,this.maxScale=c;const l=new ResizeObserver(()=>{const{x:d,y:i,width:f,height:p}=this.element.getBoundingClientRect();this.wrapperBounds={x:d,y:i,width:f,height:p},this.update()}),u=new ResizeObserver(()=>{const{x:d,y:i,width:f,height:p}=y(this.canvasElement.getBoundingClientRect(),this.renderingTranslateX,this.renderingTranslateY,this.renderinScale,this.renderingRotate);this.canvasBounds={x:d,y:i,width:f,height:p},this.update()});requestAnimationFrame(()=>{this.wrapperBounds=this.element.getBoundingClientRect(),this.canvasBounds=this.canvasElement.getBoundingClientRect(),this.update()}),u.observe(this.canvasElement),l.observe(this.element)}get canvasElement(){return this.element.querySelector(".canvas")}get wrapperInnerX(){return this.wrapperBounds.x+this.offset.left}get wrapperInnerY(){return this.wrapperBounds.y+this.offset.top}get wrapperInnerWidth(){return this.wrapperBounds.width-this.offset.left-this.offset.right}get wrapperInnerHeight(){return this.wrapperBounds.height-this.offset.top-this.offset.bottom}get wrapperInnerRatio(){return this.wrapperInnerWidth/this.wrapperInnerHeight}get canvasNaturalRatio(){return this.canvasBounds.width/this.canvasBounds.height}get naturalScale(){return this.canvasNaturalRatio>=this.wrapperInnerRatio?this.wrapperInnerWidth/this.canvasBounds.width:this.wrapperInnerHeight/this.canvasBounds.height}handleGesturestart(t){this.gestureStartRadians=this.rotate}handleGesturechange(t){const{clientX:e,clientY:s}=t,n=t.rotation;if(n===0)return;const a=this.normalizeMatrixCoordinates(e,s);this.rotateCanvas(a[0],a[1],this.gestureStartRadians+N(n))}handleGestureend(t){}handleMousedown(t){t.preventDefault(),this.dragStart=[t.clientX,t.clientY],this.dragStartFrozenX=this.translateX,this.dragStartFrozenY=this.translateY}handleMouseup(t){t.preventDefault(),this.dragStart=null,this.dragStartFrozenX=null,this.dragStartFrozenY=null}handleMousemove(t){if(t.preventDefault(),this.dragStart&&this.dragStartFrozenX!==null&&this.dragStartFrozenY!==null){const e=t.clientX-this.dragStart[0],s=t.clientY-this.dragStart[1],n=this.dragStartFrozenX- -e,a=this.dragStartFrozenY- -s;this.translateX=n,this.translateY=a,this.update()}}handleWheel(t){let{deltaX:e,deltaY:s,ctrlKey:n}=t;const a=[120,100],r=2;x(t)||((Math.abs(e)===120||Math.abs(e)===200)&&(e=e/(100/r*C(e,a))*Math.sign(e)),(Math.abs(s)===120||Math.abs(s)===200)&&(s=s/(100/r*C(s,a))*Math.sign(s)));const o=this.scale;if(n){const c=-s/100*o,l=T(o+c,this.minScale,this.maxScale),u=this.relativeWrapperCoordinatesFromClientCoords(t.clientX,t.clientY),[d,i]=this.calcProjectionTranslate(l,u,this.normalizeMatrixCoordinates(t.clientX,t.clientY));this.translateX=d,this.translateY=i,this.scale=l}else this.translateX=this.translateX-e,this.translateY=this.translateY-s;this.update(),t.preventDefault()}freezeTouches(t){return Array.from(t).map(e=>{const s=this.clientCoordsToWrapperCoords(e.clientX,e.clientY);return{client:[e.clientX,e.clientY],canvasRel:this.getCanvasCoordsRel(s[0],s[1])}})}handleTouchstart(t){this.touchStarts=this.freezeTouches(t.touches),this.touchStartTranslateX=this.translateX,this.touchStartTranslateY=this.translateY,t.preventDefault()}handleTouchmove(t){t.preventDefault();const e=Array.from(t.touches).map(s=>this.clientCoordsToWrapperCoords(s.clientX,s.clientY));if(this.touchStarts){if(e.length>=2&&this.touchStarts.length>=2){const s=[this.touchStarts[0].canvasRel[0]*this.canvasBounds.width,this.touchStarts[0].canvasRel[1]*this.canvasBounds.height],n=[this.touchStarts[1].canvasRel[0]*this.canvasBounds.width,this.touchStarts[1].canvasRel[1]*this.canvasBounds.height],a=Math.sqrt(Math.pow(s[0]-n[0],2)+Math.pow(s[1]-n[1],2)),r=Math.sqrt(Math.pow(e[0][0]-e[1][0],2)+Math.pow(e[0][1]-e[1][1],2))/this.naturalScale,o=T(r/a,this.minScale,this.maxScale),c=[e[0][0]/this.wrapperInnerWidth,e[0][1]/this.wrapperInnerHeight],l=this.touchStarts[0].canvasRel,[u,d]=this.calcProjectionTranslate(o,c,l,0);let i=0,f=0,p=0;const b=Math.atan2(n[1]-s[1],n[0]-s[0]);p=Math.atan2(e[1][1]-e[0][1],e[1][0]-e[0][0])-b;const M=(z,I)=>[this.offset.left+this.canvasBounds.width*z*this.naturalScale*o+u,this.offset.top+this.canvasBounds.height*I*this.naturalScale*o+d],Y=M(0,0),A=M(this.touchStarts[0].canvasRel[0],this.touchStarts[0].canvasRel[1]),R=S(Y,A,p);i=R[0]-Y[0],f=R[1]-Y[1],this.scale=o,this.rotate=p,this.translateX=u+i,this.translateY=d+f}else{const s=t.touches[0].clientX-this.touchStarts[0].client[0],n=t.touches[0].clientY-this.touchStarts[0].client[1],a=this.touchStartTranslateX+s,r=this.touchStartTranslateY+n;this.translateX=a,this.translateY=r}this.update()}}handleTouchend(t){t.touches.length===0?this.touchStarts=null:(this.touchStarts=this.freezeTouches(t.touches),this.touchStartTranslateX=this.translateX,this.touchStartTranslateY=this.translateY)}calcProjectionTranslate(t,e,s,n){const a=this.canvasBounds.width*this.naturalScale,r=this.canvasBounds.height*this.naturalScale,o=s[0]*a*t,c=s[1]*r*t,l=S([o,c],[0,0],n??this.rotate),u=e[0]*this.wrapperInnerWidth,d=e[1]*this.wrapperInnerHeight,i=u-l[0],f=d-l[1];return[i,f]}applyTransform(t,e,s){const n=this.calcProjectionTranslate(t,e,s,0);this.scale=t,this.translateX=n[0],this.translateY=n[1],this.update()}composeRelPoint(t,e,s,n,a,r){s=s??this.scale,n=n??this.translateX,a=a??this.translateY,r=r??this.rotate;const o=[this.offset.left,this.offset.top],c=[this.offset.left+this.canvasBounds.width*(s*this.naturalScale)*t,this.offset.top+this.canvasBounds.height*(s*this.naturalScale)*e],l=S(c,o,r);return[l[0]+n,l[1]+a]}composePoint(t,e){const s=t/this.canvasBounds.width,n=e/this.canvasBounds.height;return this.composeRelPoint(s,n)}getAnchorOffset(t,e,s,n,a=[.5,.5]){const r=this.calcProjectionTranslate(t,a,a,0),o=[this.offset.left+r[0]+this.canvasBounds.width*(t*this.naturalScale)*a[0],this.offset.top+r[1]+this.canvasBounds.height*(t*this.naturalScale)*a[1]],c=this.composeRelPoint(a[0],a[1],t,e,s,n),l=c[0]-o[0],u=c[1]-o[1];return[l,u]}getCanvasCoordsRel(t,e){const s=[0,0],n=[t-this.translateX,e-this.translateY],a=S(n,s,-this.rotate),r=[a[0]/this.renderinScale,a[1]/this.renderinScale];return[r[0]/this.canvasBounds.width,r[1]/this.canvasBounds.height]}clientCoordsToWrapperCoords(t,e){return[t-this.wrapperInnerX,e-this.wrapperInnerY]}relativeWrapperCoordinatesFromClientCoords(t,e){const[s,n]=this.clientCoordsToWrapperCoords(t,e);return[s/this.wrapperInnerWidth,n/this.wrapperInnerHeight]}normalizeMatrixCoordinates(t,e){const s=this.clientCoordsToWrapperCoords(t,e);return this.getCanvasCoordsRel(s[0],s[1])}normalizeClientCoords(t,e){const[s,n]=this.normalizeMatrixCoordinates(t,e);return[s*this.canvasBounds.width,n*this.canvasBounds.height]}rotateCanvas(t,e,s){const n=this.composeRelPoint(t,e,this.scale,0,0,s),a=this.composeRelPoint(t,e);this.translateX=a[0]-n[0],this.translateY=a[1]-n[1],this.rotate=s,this.update()}get renderinScale(){return this.naturalScale*this.scale}get renderingTranslateX(){return this.offset.left+this.translateX}get renderingTranslateY(){return this.offset.top+this.translateY}get renderingRotate(){return this.rotate}update(){this.canvasElement.style.transformOrigin="top left",this.canvasElement.style.transform=`translateX(${this.renderingTranslateX}px) translateY(${this.renderingTranslateY}px) scale(${this.renderinScale}) rotate(${this.renderingRotate}rad)`,this.dispatchEvent(new Event("update"))}destroy(){}}class B extends HTMLElement{constructor(){super();X(this,"engine");this.attachShadow({mode:"open"})}get contentEl(){return this.shadowRoot.querySelector(".content")}get canvasElement(){return this.shadowRoot.querySelector(".canvas")}connectedCallback(){this.shadowRoot.innerHTML=`
1
+ (function(m){typeof define=="function"&&define.amd?define(m):m()})((function(){"use strict";var W=Object.defineProperty;var L=(m,v,g)=>v in m?W(m,v,{enumerable:!0,configurable:!0,writable:!0,value:g}):m[v]=g;var X=(m,v,g)=>L(m,typeof v!="symbol"?v+"":v,g);var m=Object.defineProperty,v=(h,t,e)=>t in h?m(h,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):h[t]=e,g=(h,t,e)=>v(h,typeof t!="symbol"?t+"":t,e);function R(h){return h*Math.PI/180}function T(h,t,e){return Math.min(Math.max(h,t),e)}function w(h,t,e){const[s,n]=h,[a,r]=t,o=Math.cos(e)*(s-a)-Math.sin(e)*(n-r)+a,c=Math.sin(e)*(s-a)+Math.cos(e)*(n-r)+r;return[o,c]}function S(h,t){const e=Math.pow(10,t);return Math.round(h*e)/e}function y(h){var t=!1;return h.wheelDeltaY?h.wheelDeltaY===h.deltaY*-3&&(t=!0):h.deltaMode===0&&(t=!0),t}function C(h,t){const e=t.find(s=>h%s===0);return e?h/e:1}function E(h,t,e,s,n){let a=h.left-t,r=h.top-e;const o=Math.cos(-n),c=Math.sin(-n);let l=a*o-r*c,u=a*c+r*o;const d=h.width/s,i=h.height/s;return l/=s,u/=s,{x:S(l,4),y:S(u,4),width:S(d,4),height:S(i,4)}}class x extends EventTarget{constructor(t,e,s,n,a,r,o=.1,c=10){super(),g(this,"wrapperBounds"),g(this,"canvasBounds"),g(this,"gestureStartRotate",0),g(this,"dragStart",null),g(this,"dragStartFrozenX",null),g(this,"dragStartFrozenY",null),g(this,"touchStarts",null),g(this,"touchStartTranslateX",0),g(this,"touchStartTranslateY",0),this.element=t,this.offset=e,this.translateX=s,this.translateY=n,this.scale=a,this.rotate=r,this.minScale=o,this.maxScale=c;const l=new ResizeObserver(()=>{const{x:d,y:i,width:f,height:p}=this.element.getBoundingClientRect();this.wrapperBounds={x:d,y:i,width:f,height:p},this.update()}),u=new ResizeObserver(()=>{const{x:d,y:i,width:f,height:p}=E(this.canvasElement.getBoundingClientRect(),this.renderingTranslateX,this.renderingTranslateY,this.renderinScale,this.renderingRotate);this.canvasBounds={x:d,y:i,width:f,height:p},this.update()});requestAnimationFrame(()=>{this.wrapperBounds=this.element.getBoundingClientRect(),this.canvasBounds=this.canvasElement.getBoundingClientRect(),this.update(),this.dispatchEvent(new Event("init"))}),u.observe(this.canvasElement),l.observe(this.element),console.log("INIT")}get canvasElement(){return this.element.querySelector(".canvas")}get wrapperInnerX(){return this.wrapperBounds.x+this.offset.left}get wrapperInnerY(){return this.wrapperBounds.y+this.offset.top}get wrapperInnerWidth(){return this.wrapperBounds.width-this.offset.left-this.offset.right}get wrapperInnerHeight(){return this.wrapperBounds.height-this.offset.top-this.offset.bottom}get wrapperInnerRatio(){return this.wrapperInnerWidth/this.wrapperInnerHeight}get canvasNaturalRatio(){return this.canvasBounds.width/this.canvasBounds.height}get naturalScale(){return this.canvasNaturalRatio>=this.wrapperInnerRatio?this.wrapperInnerWidth/this.canvasBounds.width:this.wrapperInnerHeight/this.canvasBounds.height}handleGesturestart(t){this.gestureStartRotate=this.rotate}handleGesturechange(t){const{clientX:e,clientY:s}=t,n=t.rotation;if(n===0)return;const a=this.normalizeMatrixCoordinates(e,s);this.rotateCanvas(a[0],a[1],this.gestureStartRotate+R(n))}handleGestureend(t){}handleMousedown(t){t.preventDefault(),this.dragStart=[t.clientX,t.clientY],this.dragStartFrozenX=this.translateX,this.dragStartFrozenY=this.translateY}handleMouseup(t){t.preventDefault(),this.dragStart=null,this.dragStartFrozenX=null,this.dragStartFrozenY=null}handleMousemove(t){if(t.preventDefault(),this.dragStart&&this.dragStartFrozenX!==null&&this.dragStartFrozenY!==null){const e=t.clientX-this.dragStart[0],s=t.clientY-this.dragStart[1],n=this.dragStartFrozenX- -e,a=this.dragStartFrozenY- -s;this.translateX=n,this.translateY=a,this.update()}}handleWheel(t){let{deltaX:e,deltaY:s,ctrlKey:n}=t;const a=[120,100],r=2;y(t)||((Math.abs(e)===120||Math.abs(e)===200)&&(e=e/(100/r*C(e,a))*Math.sign(e)),(Math.abs(s)===120||Math.abs(s)===200)&&(s=s/(100/r*C(s,a))*Math.sign(s)));const o=this.scale;if(n){const c=-s/100*o,l=T(o+c,this.minScale,this.maxScale),u=this.relativeWrapperCoordinatesFromClientCoords(t.clientX,t.clientY),[d,i]=this.calcProjectionTranslate(l,u,this.normalizeMatrixCoordinates(t.clientX,t.clientY));this.translateX=d,this.translateY=i,this.scale=l}else this.translateX=this.translateX-e,this.translateY=this.translateY-s;this.update(),t.preventDefault()}freezeTouches(t){return Array.from(t).map(e=>{const s=this.clientCoordsToWrapperCoords(e.clientX,e.clientY);return{client:[e.clientX,e.clientY],canvasRel:this.getCanvasCoordsRel(s[0],s[1])}})}handleTouchstart(t){this.touchStarts=this.freezeTouches(t.touches),this.touchStartTranslateX=this.translateX,this.touchStartTranslateY=this.translateY,t.preventDefault()}handleTouchmove(t){t.preventDefault();const e=Array.from(t.touches).map(s=>this.clientCoordsToWrapperCoords(s.clientX,s.clientY));if(this.touchStarts){if(e.length>=2&&this.touchStarts.length>=2){const s=[this.touchStarts[0].canvasRel[0]*this.canvasBounds.width,this.touchStarts[0].canvasRel[1]*this.canvasBounds.height],n=[this.touchStarts[1].canvasRel[0]*this.canvasBounds.width,this.touchStarts[1].canvasRel[1]*this.canvasBounds.height],a=Math.sqrt(Math.pow(s[0]-n[0],2)+Math.pow(s[1]-n[1],2)),r=Math.sqrt(Math.pow(e[0][0]-e[1][0],2)+Math.pow(e[0][1]-e[1][1],2))/this.naturalScale,o=T(r/a,this.minScale,this.maxScale),c=[e[0][0]/this.wrapperInnerWidth,e[0][1]/this.wrapperInnerHeight],l=this.touchStarts[0].canvasRel,[u,d]=this.calcProjectionTranslate(o,c,l,0);let i=0,f=0,p=0;const b=Math.atan2(n[1]-s[1],n[0]-s[0]);p=Math.atan2(e[1][1]-e[0][1],e[1][0]-e[0][0])-b;const M=(z,I)=>[this.offset.left+this.canvasBounds.width*z*this.naturalScale*o+u,this.offset.top+this.canvasBounds.height*I*this.naturalScale*o+d],Y=M(0,0),A=M(this.touchStarts[0].canvasRel[0],this.touchStarts[0].canvasRel[1]),N=w(Y,A,p);i=N[0]-Y[0],f=N[1]-Y[1],this.scale=o,this.rotate=p,this.translateX=u+i,this.translateY=d+f}else{const s=t.touches[0].clientX-this.touchStarts[0].client[0],n=t.touches[0].clientY-this.touchStarts[0].client[1],a=this.touchStartTranslateX+s,r=this.touchStartTranslateY+n;this.translateX=a,this.translateY=r}this.update()}}handleTouchend(t){t.touches.length===0?this.touchStarts=null:(this.touchStarts=this.freezeTouches(t.touches),this.touchStartTranslateX=this.translateX,this.touchStartTranslateY=this.translateY)}calcProjectionTranslate(t,e,s,n){const a=this.canvasBounds.width*this.naturalScale,r=this.canvasBounds.height*this.naturalScale,o=s[0]*a*t,c=s[1]*r*t,l=w([o,c],[0,0],n??this.rotate),u=e[0]*this.wrapperInnerWidth,d=e[1]*this.wrapperInnerHeight,i=u-l[0],f=d-l[1];return[i,f]}applyTransform(t,e,s){console.log("....apply transform");const n=this.calcProjectionTranslate(t,e,s,0);this.scale=t,this.translateX=n[0],this.translateY=n[1],this.update()}composeRelPoint(t,e,s,n,a,r){s=s??this.scale,n=n??this.translateX,a=a??this.translateY,r=r??this.rotate;const o=[this.offset.left,this.offset.top],c=[this.offset.left+this.canvasBounds.width*(s*this.naturalScale)*t,this.offset.top+this.canvasBounds.height*(s*this.naturalScale)*e],l=w(c,o,r);return[l[0]+n,l[1]+a]}composePoint(t,e){const s=t/this.canvasBounds.width,n=e/this.canvasBounds.height;return this.composeRelPoint(s,n)}getAnchorOffset(t,e,s,n,a=[.5,.5]){const r=this.calcProjectionTranslate(t,a,a,0),o=[this.offset.left+r[0]+this.canvasBounds.width*(t*this.naturalScale)*a[0],this.offset.top+r[1]+this.canvasBounds.height*(t*this.naturalScale)*a[1]],c=this.composeRelPoint(a[0],a[1],t,e,s,n),l=c[0]-o[0],u=c[1]-o[1];return[l,u]}getCanvasCoordsRel(t,e){const s=[0,0],n=[t-this.translateX,e-this.translateY],a=w(n,s,-this.rotate),r=[a[0]/this.renderinScale,a[1]/this.renderinScale];return[r[0]/this.canvasBounds.width,r[1]/this.canvasBounds.height]}clientCoordsToWrapperCoords(t,e){return[t-this.wrapperInnerX,e-this.wrapperInnerY]}relativeWrapperCoordinatesFromClientCoords(t,e){const[s,n]=this.clientCoordsToWrapperCoords(t,e);return[s/this.wrapperInnerWidth,n/this.wrapperInnerHeight]}normalizeMatrixCoordinates(t,e){const s=this.clientCoordsToWrapperCoords(t,e);return this.getCanvasCoordsRel(s[0],s[1])}normalizeClientCoords(t,e){const[s,n]=this.normalizeMatrixCoordinates(t,e);return[s*this.canvasBounds.width,n*this.canvasBounds.height]}rotateCanvas(t,e,s){const n=this.composeRelPoint(t,e,this.scale,0,0,s),a=this.composeRelPoint(t,e);this.translateX=a[0]-n[0],this.translateY=a[1]-n[1],this.rotate=s,this.update()}get renderinScale(){return this.naturalScale*this.scale}get renderingTranslateX(){return this.offset.left+this.translateX}get renderingTranslateY(){return this.offset.top+this.translateY}get renderingRotate(){return this.rotate}update(){this.canvasElement.style.transformOrigin="top left",this.canvasElement.style.transform=`translateX(${this.renderingTranslateX}px) translateY(${this.renderingTranslateY}px) scale(${this.renderinScale}) rotate(${this.renderingRotate}rad)`,this.dispatchEvent(new Event("update"))}destroy(){}}class B extends HTMLElement{constructor(){super();X(this,"engine");this.attachShadow({mode:"open"})}get contentEl(){return this.shadowRoot.querySelector(".content")}get canvasElement(){return this.shadowRoot.querySelector(".canvas")}connectedCallback(){this.shadowRoot.innerHTML=`
2
2
  <style>
3
3
  :host {
4
4
  display: block;
@@ -32,4 +32,4 @@
32
32
  <slot name="matrix"></slot>
33
33
  </div>
34
34
  </div>
35
- `;const e=Number(this.getAttribute("translate-x")||"0"),s=Number(this.getAttribute("translate-y")||"0"),n=Number(this.getAttribute("scale")||"1"),a=Number(this.getAttribute("rotate")||"0"),r=Number(this.getAttribute("min-scale")),o=Number(this.getAttribute("max-scale")),c=Number(this.getAttribute("offset-top")),l=Number(this.getAttribute("offset-right")),u=Number(this.getAttribute("offset-bottom")),d=Number(this.getAttribute("offset-left"));this.engine=new E(this.contentEl,{top:isNaN(c)?100:c,left:isNaN(d)?0:d,right:isNaN(l)?0:l,bottom:isNaN(u)?0:u},e,s,n,a,isNaN(r)?void 0:r,isNaN(o)?void 0:o),this.contentEl.addEventListener("wheel",i=>this.engine.handleWheel(i)),this.contentEl.addEventListener("gesturestart",i=>this.engine.handleGesturestart(i)),window.addEventListener("gesturechange",i=>this.engine.handleGesturechange(i)),window.addEventListener("gestureend",i=>this.engine.handleGestureend(i)),this.contentEl.addEventListener("mousedown",i=>this.engine.handleMousedown(i)),window.addEventListener("mousemove",i=>this.engine.handleMousemove(i)),window.addEventListener("mouseup",i=>this.engine.handleMouseup(i)),this.contentEl.addEventListener("touchstart",i=>this.engine.handleTouchstart(i)),window.addEventListener("touchmove",i=>this.engine.handleTouchmove(i)),window.addEventListener("touchend",i=>this.engine.handleTouchend(i)),this.engine.addEventListener("update",()=>{const i=this.engine.translateX.toString(),f=this.engine.translateY.toString(),p=this.engine.scale.toString(),b=this.engine.rotate.toString();this.getAttribute("translate-x")!==i&&this.setAttribute("translate-x",i),this.getAttribute("translate-y")!==f&&this.setAttribute("translate-y",f),this.getAttribute("scale")!==p&&this.setAttribute("scale",p),this.getAttribute("rotate")!==b&&this.setAttribute("rotate",b),this.dispatchEvent(new Event("update"))})}disconnectedCallback(){this.engine.destroy()}attributeChangedCallback(e,s,n){if(this.engine)switch(e){case"translate-x":const a=Number(n);this.engine.translateX!==a&&(this.engine.translateX=a,this.engine.update());break;case"translate-y":const r=Number(n);this.engine.translateY!==r&&(this.engine.translateY=r,this.engine.update());break;case"scale":const o=Number(n);this.engine.scale!==o&&(this.engine.scale=o,this.engine.update());break;case"rotate":const c=Number(n);this.engine.rotate!==c&&(this.engine.rotate=c,this.engine.update());break;case"min-scale":const l=Number(n);!isNaN(l)&&this.engine.minScale!==l&&(this.engine.minScale=l,this.engine.update());break;case"max-scale":const u=Number(n);!isNaN(u)&&this.engine.maxScale!==u&&(this.engine.maxScale=u,this.engine.update());break;case"offset-top":case"offset-right":case"offset-bottom":case"offset-left":const d=Number(this.getAttribute("offset-top")||"0"),i=Number(this.getAttribute("offset-right")||"0"),f=Number(this.getAttribute("offset-bottom")||"0"),p=Number(this.getAttribute("offset-left")||"0");this.engine.offset={top:d,right:i,bottom:f,left:p},this.engine.update();break}}get canvasWidth(){return this.engine.canvasBounds.width}get canvasHeight(){return this.engine.canvasBounds.height}applyTransform(e,s,n){this.engine.applyTransform(e,s,n)}normalizeClientCoords(e,s){return this.engine.normalizeClientCoords(e,s)}composePoint(e,s){return this.engine.composePoint(e,s)}}X(B,"observedAttributes",["translate-x","translate-y","scale","rotate","min-scale","max-scale","offset-top","offset-right","offset-bottom","offset-left"]),customElements.get("zoom-pinch")||customElements.define("zoom-pinch",B)}));
35
+ `;const e=Number(this.getAttribute("translate-x")||"0"),s=Number(this.getAttribute("translate-y")||"0"),n=Number(this.getAttribute("scale")||"1"),a=Number(this.getAttribute("rotate")||"0"),r=Number(this.getAttribute("min-scale")),o=Number(this.getAttribute("max-scale")),c=Number(this.getAttribute("offset-top")),l=Number(this.getAttribute("offset-right")),u=Number(this.getAttribute("offset-bottom")),d=Number(this.getAttribute("offset-left"));this.engine=new x(this.contentEl,{top:isNaN(c)?100:c,left:isNaN(d)?0:d,right:isNaN(l)?0:l,bottom:isNaN(u)?0:u},e,s,n,a,isNaN(r)?void 0:r,isNaN(o)?void 0:o),this.contentEl.addEventListener("wheel",i=>this.engine.handleWheel(i)),this.contentEl.addEventListener("gesturestart",i=>this.engine.handleGesturestart(i)),window.addEventListener("gesturechange",i=>this.engine.handleGesturechange(i)),window.addEventListener("gestureend",i=>this.engine.handleGestureend(i)),this.contentEl.addEventListener("mousedown",i=>this.engine.handleMousedown(i)),window.addEventListener("mousemove",i=>this.engine.handleMousemove(i)),window.addEventListener("mouseup",i=>this.engine.handleMouseup(i)),this.contentEl.addEventListener("touchstart",i=>this.engine.handleTouchstart(i)),window.addEventListener("touchmove",i=>this.engine.handleTouchmove(i)),window.addEventListener("touchend",i=>this.engine.handleTouchend(i)),this.engine.addEventListener("update",()=>{const i=this.engine.translateX.toString(),f=this.engine.translateY.toString(),p=this.engine.scale.toString(),b=this.engine.rotate.toString();this.getAttribute("translate-x")!==i&&this.setAttribute("translate-x",i),this.getAttribute("translate-y")!==f&&this.setAttribute("translate-y",f),this.getAttribute("scale")!==p&&this.setAttribute("scale",p),this.getAttribute("rotate")!==b&&this.setAttribute("rotate",b),this.dispatchEvent(new Event("update"))})}disconnectedCallback(){this.engine.destroy()}attributeChangedCallback(e,s,n){if(this.engine)switch(e){case"translate-x":const a=Number(n);this.engine.translateX!==a&&(this.engine.translateX=a,this.engine.update());break;case"translate-y":const r=Number(n);this.engine.translateY!==r&&(this.engine.translateY=r,this.engine.update());break;case"scale":const o=Number(n);this.engine.scale!==o&&(this.engine.scale=o,this.engine.update());break;case"rotate":const c=Number(n);this.engine.rotate!==c&&(this.engine.rotate=c,this.engine.update());break;case"min-scale":const l=Number(n);!isNaN(l)&&this.engine.minScale!==l&&(this.engine.minScale=l,this.engine.update());break;case"max-scale":const u=Number(n);!isNaN(u)&&this.engine.maxScale!==u&&(this.engine.maxScale=u,this.engine.update());break;case"offset-top":case"offset-right":case"offset-bottom":case"offset-left":const d=Number(this.getAttribute("offset-top")||"0"),i=Number(this.getAttribute("offset-right")||"0"),f=Number(this.getAttribute("offset-bottom")||"0"),p=Number(this.getAttribute("offset-left")||"0");this.engine.offset={top:d,right:i,bottom:f,left:p},this.engine.update();break}}get canvasWidth(){return this.engine.canvasBounds.width}get canvasHeight(){return this.engine.canvasBounds.height}applyTransform(e,s,n){this.engine.applyTransform(e,s,n)}normalizeClientCoords(e,s){return this.engine.normalizeClientCoords(e,s)}composePoint(e,s){return this.engine.composePoint(e,s)}}X(B,"observedAttributes",["translate-x","translate-y","scale","rotate","min-scale","max-scale","offset-top","offset-right","offset-bottom","offset-left"]),customElements.get("zoom-pinch")||customElements.define("zoom-pinch",B)}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zoompinch/elements",
3
3
  "description": "Custom elements wrapper ZoomPinch - reactive pinch & zoom component",
4
- "version": "0.0.5",
4
+ "version": "0.0.8",
5
5
  "type": "module",
6
6
  "main": "./dist/zoompinch-elements.umd.js",
7
7
  "module": "./dist/zoompinch-elements.es.js",
@@ -25,6 +25,6 @@
25
25
  "vite": "^6.4.1"
26
26
  },
27
27
  "dependencies": {
28
- "@zoompinch/core": "^0.0.3"
28
+ "@zoompinch/core": "^0.0.8"
29
29
  }
30
30
  }