@zoompinch/core 0.0.1
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.
- package/dist/zoompinch-core.es.js +271 -0
- package/dist/zoompinch-core.umd.js +1 -0
- package/package.json +25 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
var x = Object.defineProperty;
|
|
2
|
+
var z = (r, o, t) => o in r ? x(r, o, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[o] = t;
|
|
3
|
+
var p = (r, o, t) => z(r, typeof o != "symbol" ? o + "" : o, t);
|
|
4
|
+
function W(r) {
|
|
5
|
+
return r * Math.PI / 180;
|
|
6
|
+
}
|
|
7
|
+
function R(r, o, t) {
|
|
8
|
+
return Math.min(Math.max(r, o), t);
|
|
9
|
+
}
|
|
10
|
+
function v(r, o, t) {
|
|
11
|
+
const [s, e] = r, [a, n] = o, i = Math.cos(t) * (s - a) - Math.sin(t) * (e - n) + a, h = Math.sin(t) * (s - a) + Math.cos(t) * (e - n) + n;
|
|
12
|
+
return [i, h];
|
|
13
|
+
}
|
|
14
|
+
function w(r, o) {
|
|
15
|
+
const t = Math.pow(10, o);
|
|
16
|
+
return Math.round(r * t) / t;
|
|
17
|
+
}
|
|
18
|
+
function I(r) {
|
|
19
|
+
var o = !1;
|
|
20
|
+
return r.wheelDeltaY ? r.wheelDeltaY === r.deltaY * -3 && (o = !0) : r.deltaMode === 0 && (o = !0), o;
|
|
21
|
+
}
|
|
22
|
+
function C(r, o) {
|
|
23
|
+
const t = o.find((s) => r % s === 0);
|
|
24
|
+
return t ? r / t : 1;
|
|
25
|
+
}
|
|
26
|
+
function y(r, o, t, s, e) {
|
|
27
|
+
let a = r.left - o, n = r.top - t;
|
|
28
|
+
const i = Math.cos(-e), h = Math.sin(-e);
|
|
29
|
+
let c = a * i - n * h, l = a * h + n * i;
|
|
30
|
+
const d = r.width / s, u = r.height / s;
|
|
31
|
+
return c /= s, l /= s, { x: w(c, 4), y: w(l, 4), width: w(d, 4), height: w(u, 4) };
|
|
32
|
+
}
|
|
33
|
+
class E extends EventTarget {
|
|
34
|
+
constructor(t, s, e, a, n, i, h = 0.1, c = 10) {
|
|
35
|
+
super();
|
|
36
|
+
p(this, "wrapperBounds");
|
|
37
|
+
p(this, "canvasBounds");
|
|
38
|
+
p(this, "gestureStartRadians", 0);
|
|
39
|
+
p(this, "dragStart", null);
|
|
40
|
+
p(this, "dragStartFrozenX", null);
|
|
41
|
+
p(this, "dragStartFrozenY", null);
|
|
42
|
+
p(this, "touchStarts", null);
|
|
43
|
+
p(this, "touchStartTranslateX", 0);
|
|
44
|
+
p(this, "touchStartTranslateY", 0);
|
|
45
|
+
p(this, "centeredTransform", {
|
|
46
|
+
translateX: 0,
|
|
47
|
+
translateY: 0,
|
|
48
|
+
scale: 1,
|
|
49
|
+
radians: 0
|
|
50
|
+
});
|
|
51
|
+
this.element = t, this.offset = s, this.translateX = e, this.translateY = a, this.scale = n, this.rotate = i, this.minScale = h, this.maxScale = c;
|
|
52
|
+
const l = new ResizeObserver(() => {
|
|
53
|
+
const { x: u, y: f, width: g, height: S } = this.element.getBoundingClientRect();
|
|
54
|
+
this.wrapperBounds = { x: u, y: f, width: g, height: S }, this.update();
|
|
55
|
+
}), d = new ResizeObserver(() => {
|
|
56
|
+
const { x: u, y: f, width: g, height: S } = y(
|
|
57
|
+
this.canvasElement.getBoundingClientRect(),
|
|
58
|
+
this.renderingTranslateX,
|
|
59
|
+
this.renderingTranslateY,
|
|
60
|
+
this.renderinScale,
|
|
61
|
+
this.renderingRotate
|
|
62
|
+
);
|
|
63
|
+
this.canvasBounds = { x: u, y: f, width: g, height: S }, this.update();
|
|
64
|
+
});
|
|
65
|
+
requestAnimationFrame(() => {
|
|
66
|
+
this.wrapperBounds = this.element.getBoundingClientRect(), this.canvasBounds = this.canvasElement.getBoundingClientRect(), this.update();
|
|
67
|
+
}), d.observe(this.canvasElement), l.observe(this.element);
|
|
68
|
+
}
|
|
69
|
+
get canvasElement() {
|
|
70
|
+
return this.element.querySelector(".canvas");
|
|
71
|
+
}
|
|
72
|
+
get wrapperInnerX() {
|
|
73
|
+
return this.wrapperBounds.x + this.offset.left;
|
|
74
|
+
}
|
|
75
|
+
get wrapperInnerY() {
|
|
76
|
+
return this.wrapperBounds.y + this.offset.top;
|
|
77
|
+
}
|
|
78
|
+
get wrapperInnerWidth() {
|
|
79
|
+
return this.wrapperBounds.width - this.offset.left - this.offset.right;
|
|
80
|
+
}
|
|
81
|
+
get wrapperInnerHeight() {
|
|
82
|
+
return this.wrapperBounds.height - this.offset.top - this.offset.bottom;
|
|
83
|
+
}
|
|
84
|
+
// Calculate the inner ratio
|
|
85
|
+
get wrapperInnerRatio() {
|
|
86
|
+
return this.wrapperInnerWidth / this.wrapperInnerHeight;
|
|
87
|
+
}
|
|
88
|
+
get canvasNaturalRatio() {
|
|
89
|
+
return this.canvasBounds.width / this.canvasBounds.height;
|
|
90
|
+
}
|
|
91
|
+
// Get natural scale
|
|
92
|
+
get naturalScale() {
|
|
93
|
+
return this.canvasNaturalRatio >= this.wrapperInnerRatio ? this.wrapperInnerWidth / this.canvasBounds.width : this.wrapperInnerHeight / this.canvasBounds.height;
|
|
94
|
+
}
|
|
95
|
+
handleGesturestart(t) {
|
|
96
|
+
this.gestureStartRadians = this.rotate;
|
|
97
|
+
}
|
|
98
|
+
handleGesturechange(t) {
|
|
99
|
+
const { clientX: s, clientY: e } = t, a = t.rotation;
|
|
100
|
+
if (a === 0)
|
|
101
|
+
return;
|
|
102
|
+
const n = this.normalizeMatrixCoordinates(s, e);
|
|
103
|
+
this.rotateCanvas(n[0], n[1], this.gestureStartRadians + W(a));
|
|
104
|
+
}
|
|
105
|
+
handleGestureend(t) {
|
|
106
|
+
}
|
|
107
|
+
handleMousedown(t) {
|
|
108
|
+
t.preventDefault(), this.dragStart = [t.clientX, t.clientY], this.dragStartFrozenX = this.translateX, this.dragStartFrozenY = this.translateY;
|
|
109
|
+
}
|
|
110
|
+
handleMouseup(t) {
|
|
111
|
+
t.preventDefault(), this.dragStart = null, this.dragStartFrozenX = null, this.dragStartFrozenY = null;
|
|
112
|
+
}
|
|
113
|
+
handleMousemove(t) {
|
|
114
|
+
if (t.preventDefault(), this.dragStart && this.dragStartFrozenX !== null && this.dragStartFrozenY !== null) {
|
|
115
|
+
const s = t.clientX - this.dragStart[0], e = t.clientY - this.dragStart[1], a = this.dragStartFrozenX - -s, n = this.dragStartFrozenY - -e;
|
|
116
|
+
this.translateX = a, this.translateY = n, this.update();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
handleWheel(t) {
|
|
120
|
+
let { deltaX: s, deltaY: e, ctrlKey: a } = t;
|
|
121
|
+
const n = [120, 100], i = 2;
|
|
122
|
+
I(t) || ((Math.abs(s) === 120 || Math.abs(s) === 200) && (s = s / (100 / i * C(s, n)) * Math.sign(s)), (Math.abs(e) === 120 || Math.abs(e) === 200) && (e = e / (100 / i * C(e, n)) * Math.sign(e)));
|
|
123
|
+
const h = this.scale;
|
|
124
|
+
if (a) {
|
|
125
|
+
const c = -e / 100 * h, l = R(h + c, this.minScale, this.maxScale), d = this.relativeWrapperCoordinatesFromClientCoords(t.clientX, t.clientY), [u, f] = this.calcProjectionTranslate(
|
|
126
|
+
l,
|
|
127
|
+
d,
|
|
128
|
+
this.normalizeMatrixCoordinates(t.clientX, t.clientY)
|
|
129
|
+
);
|
|
130
|
+
this.translateX = u, this.translateY = f, this.scale = l;
|
|
131
|
+
} else
|
|
132
|
+
this.translateX = this.translateX - s, this.translateY = this.translateY - e;
|
|
133
|
+
this.update(), t.preventDefault();
|
|
134
|
+
}
|
|
135
|
+
freezeTouches(t) {
|
|
136
|
+
return Array.from(t).map((s) => {
|
|
137
|
+
const e = this.clientCoordsToWrapperCoords(s.clientX, s.clientY);
|
|
138
|
+
return {
|
|
139
|
+
client: [s.clientX, s.clientY],
|
|
140
|
+
canvasRel: this.getCanvasCoordsRel(e[0], e[1])
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
handleTouchstart(t) {
|
|
145
|
+
this.touchStarts = this.freezeTouches(t.touches), this.touchStartTranslateX = this.translateX, this.touchStartTranslateY = this.translateY, t.preventDefault();
|
|
146
|
+
}
|
|
147
|
+
handleTouchmove(t) {
|
|
148
|
+
t.preventDefault();
|
|
149
|
+
const s = Array.from(t.touches).map(
|
|
150
|
+
(e) => this.clientCoordsToWrapperCoords(e.clientX, e.clientY)
|
|
151
|
+
);
|
|
152
|
+
if (this.touchStarts) {
|
|
153
|
+
if (s.length >= 2 && this.touchStarts.length >= 2) {
|
|
154
|
+
const e = [
|
|
155
|
+
this.touchStarts[0].canvasRel[0] * this.canvasBounds.width,
|
|
156
|
+
this.touchStarts[0].canvasRel[1] * this.canvasBounds.height
|
|
157
|
+
], a = [
|
|
158
|
+
this.touchStarts[1].canvasRel[0] * this.canvasBounds.width,
|
|
159
|
+
this.touchStarts[1].canvasRel[1] * this.canvasBounds.height
|
|
160
|
+
], n = Math.sqrt(
|
|
161
|
+
Math.pow(e[0] - a[0], 2) + Math.pow(e[1] - a[1], 2)
|
|
162
|
+
), i = Math.sqrt(
|
|
163
|
+
Math.pow(s[0][0] - s[1][0], 2) + Math.pow(s[0][1] - s[1][1], 2)
|
|
164
|
+
) / this.naturalScale, h = R(i / n, this.minScale, this.maxScale), c = [
|
|
165
|
+
s[0][0] / this.wrapperInnerWidth,
|
|
166
|
+
s[0][1] / this.wrapperInnerHeight
|
|
167
|
+
], l = this.touchStarts[0].canvasRel, [d, u] = this.calcProjectionTranslate(h, c, l, 0);
|
|
168
|
+
let f = 0, g = 0, S = 0;
|
|
169
|
+
const T = Math.atan2(
|
|
170
|
+
a[1] - e[1],
|
|
171
|
+
a[0] - e[0]
|
|
172
|
+
);
|
|
173
|
+
S = Math.atan2(s[1][1] - s[0][1], s[1][0] - s[0][0]) - T;
|
|
174
|
+
const m = (M, P) => [
|
|
175
|
+
this.offset.left + this.canvasBounds.width * M * this.naturalScale * h + d,
|
|
176
|
+
this.offset.top + this.canvasBounds.height * P * this.naturalScale * h + u
|
|
177
|
+
], Y = m(0, 0), B = m(
|
|
178
|
+
this.touchStarts[0].canvasRel[0],
|
|
179
|
+
this.touchStarts[0].canvasRel[1]
|
|
180
|
+
), X = v(
|
|
181
|
+
Y,
|
|
182
|
+
B,
|
|
183
|
+
S
|
|
184
|
+
);
|
|
185
|
+
f = X[0] - Y[0], g = X[1] - Y[1], this.scale = h, this.rotate = S, this.translateX = d + f, this.translateY = u + g;
|
|
186
|
+
} else {
|
|
187
|
+
const e = t.touches[0].clientX - this.touchStarts[0].client[0], a = t.touches[0].clientY - this.touchStarts[0].client[1], n = this.touchStartTranslateX + e, i = this.touchStartTranslateY + a;
|
|
188
|
+
this.translateX = n, this.translateY = i;
|
|
189
|
+
}
|
|
190
|
+
this.update();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
handleTouchend(t) {
|
|
194
|
+
t.touches.length === 0 ? this.touchStarts = null : (this.touchStarts = this.freezeTouches(t.touches), this.touchStartTranslateX = this.translateX, this.touchStartTranslateY = this.translateY);
|
|
195
|
+
}
|
|
196
|
+
calcProjectionTranslate(t, s, e, a) {
|
|
197
|
+
const n = this.canvasBounds.width * this.naturalScale, i = this.canvasBounds.height * this.naturalScale, h = e[0] * n * t, c = e[1] * i * t, l = v([h, c], [0, 0], a ?? this.rotate), d = s[0] * this.wrapperInnerWidth, u = s[1] * this.wrapperInnerHeight, f = d - l[0], g = u - l[1];
|
|
198
|
+
return [f, g];
|
|
199
|
+
}
|
|
200
|
+
applyTransform(t, s, e) {
|
|
201
|
+
const a = this.calcProjectionTranslate(t, s, e, 0);
|
|
202
|
+
this.scale = t, this.translateX = a[0], this.translateY = a[1], this.update();
|
|
203
|
+
}
|
|
204
|
+
composeRelPoint(t, s, e, a, n, i) {
|
|
205
|
+
e = e ?? this.scale, a = a ?? this.translateX, n = n ?? this.translateY, i = i ?? this.rotate;
|
|
206
|
+
const h = [this.offset.left, this.offset.top], c = [
|
|
207
|
+
this.offset.left + this.canvasBounds.width * (e * this.naturalScale) * t,
|
|
208
|
+
this.offset.top + this.canvasBounds.height * (e * this.naturalScale) * s
|
|
209
|
+
], l = v(c, h, i);
|
|
210
|
+
return [l[0] + a, l[1] + n];
|
|
211
|
+
}
|
|
212
|
+
composePoint(t, s) {
|
|
213
|
+
const e = t / this.canvasBounds.width, a = s / this.canvasBounds.height;
|
|
214
|
+
return this.composeRelPoint(e, a);
|
|
215
|
+
}
|
|
216
|
+
getAnchorOffset(t, s, e, a, n = [0.5, 0.5]) {
|
|
217
|
+
const i = this.calcProjectionTranslate(t, n, n, 0), h = [
|
|
218
|
+
this.offset.left + i[0] + this.canvasBounds.width * (t * this.naturalScale) * n[0],
|
|
219
|
+
this.offset.top + i[1] + this.canvasBounds.height * (t * this.naturalScale) * n[1]
|
|
220
|
+
], c = this.composeRelPoint(n[0], n[1], t, s, e, a), l = c[0] - h[0], d = c[1] - h[1];
|
|
221
|
+
return [l, d];
|
|
222
|
+
}
|
|
223
|
+
// Converts absolute inner wrapper coordinates to relative canvas coordinates (0-1, 0-1)
|
|
224
|
+
getCanvasCoordsRel(t, s) {
|
|
225
|
+
const e = [0, 0], a = [t - this.translateX, s - this.translateY], n = v(a, e, -this.rotate), i = [n[0] / this.renderinScale, n[1] / this.renderinScale];
|
|
226
|
+
return [i[0] / this.canvasBounds.width, i[1] / this.canvasBounds.height];
|
|
227
|
+
}
|
|
228
|
+
// Converts absolute client to coordinates to absolute inner-wrapper coorinates
|
|
229
|
+
clientCoordsToWrapperCoords(t, s) {
|
|
230
|
+
return [t - this.wrapperInnerX, s - this.wrapperInnerY];
|
|
231
|
+
}
|
|
232
|
+
// Converts absolute client coordinates to relative wrapper coordinates (0-1, 0-1)
|
|
233
|
+
relativeWrapperCoordinatesFromClientCoords(t, s) {
|
|
234
|
+
const [e, a] = this.clientCoordsToWrapperCoords(t, s);
|
|
235
|
+
return [e / this.wrapperInnerWidth, a / this.wrapperInnerHeight];
|
|
236
|
+
}
|
|
237
|
+
// Converts client coordinates to relative canvas coordinates (0-1, 0-1)
|
|
238
|
+
normalizeMatrixCoordinates(t, s) {
|
|
239
|
+
const e = this.clientCoordsToWrapperCoords(t, s);
|
|
240
|
+
return this.getCanvasCoordsRel(e[0], e[1]);
|
|
241
|
+
}
|
|
242
|
+
// Converts client coordinates to absolute canvas coordinates
|
|
243
|
+
normalizeClientCoords(t, s) {
|
|
244
|
+
const [e, a] = this.normalizeMatrixCoordinates(t, s);
|
|
245
|
+
return [e * this.canvasBounds.width, a * this.canvasBounds.height];
|
|
246
|
+
}
|
|
247
|
+
rotateCanvas(t, s, e) {
|
|
248
|
+
const a = this.composeRelPoint(t, s, this.scale, 0, 0, e), n = this.composeRelPoint(t, s);
|
|
249
|
+
this.translateX = n[0] - a[0], this.translateY = n[1] - a[1], this.rotate = e, this.update();
|
|
250
|
+
}
|
|
251
|
+
get renderinScale() {
|
|
252
|
+
return this.naturalScale * this.scale;
|
|
253
|
+
}
|
|
254
|
+
get renderingTranslateX() {
|
|
255
|
+
return this.offset.left + this.translateX;
|
|
256
|
+
}
|
|
257
|
+
get renderingTranslateY() {
|
|
258
|
+
return this.offset.top + this.translateY;
|
|
259
|
+
}
|
|
260
|
+
get renderingRotate() {
|
|
261
|
+
return this.rotate;
|
|
262
|
+
}
|
|
263
|
+
update() {
|
|
264
|
+
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"));
|
|
265
|
+
}
|
|
266
|
+
destroy() {
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
export {
|
|
270
|
+
E as Zoompinch
|
|
271
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(p,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(p=typeof globalThis<"u"?globalThis:p||self,d(p.ZoomPinch={}))})(this,(function(p){"use strict";var D=Object.defineProperty;var F=(p,d,m)=>d in p?D(p,d,{enumerable:!0,configurable:!0,writable:!0,value:m}):p[d]=m;var g=(p,d,m)=>F(p,typeof d!="symbol"?d+"":d,m);function d(o){return o*Math.PI/180}function m(o,c,t){return Math.min(Math.max(o,c),t)}function Y(o,c,t){const[e,s]=o,[a,n]=c,r=Math.cos(t)*(e-a)-Math.sin(t)*(s-n)+a,i=Math.sin(t)*(e-a)+Math.cos(t)*(s-n)+n;return[r,i]}function X(o,c){const t=Math.pow(10,c);return Math.round(o*t)/t}function M(o){var c=!1;return o.wheelDeltaY?o.wheelDeltaY===o.deltaY*-3&&(c=!0):o.deltaMode===0&&(c=!0),c}function C(o,c){const t=c.find(e=>o%e===0);return t?o/t:1}function P(o,c,t,e,s){let a=o.left-c,n=o.top-t;const r=Math.cos(-s),i=Math.sin(-s);let l=a*r-n*i,h=a*i+n*r;const u=o.width/e,f=o.height/e;return l/=e,h/=e,{x:X(l,4),y:X(h,4),width:X(u,4),height:X(f,4)}}class x extends EventTarget{constructor(t,e,s,a,n,r,i=.1,l=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=a,this.scale=n,this.rotate=r,this.minScale=i,this.maxScale=l;const h=new ResizeObserver(()=>{const{x:f,y:S,width:v,height:w}=this.element.getBoundingClientRect();this.wrapperBounds={x:f,y:S,width:v,height:w},this.update()}),u=new ResizeObserver(()=>{const{x:f,y:S,width:v,height:w}=P(this.canvasElement.getBoundingClientRect(),this.renderingTranslateX,this.renderingTranslateY,this.renderinScale,this.renderingRotate);this.canvasBounds={x:f,y:S,width:v,height:w},this.update()});requestAnimationFrame(()=>{this.wrapperBounds=this.element.getBoundingClientRect(),this.canvasBounds=this.canvasElement.getBoundingClientRect(),this.update()}),u.observe(this.canvasElement),h.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,a=t.rotation;if(a===0)return;const n=this.normalizeMatrixCoordinates(e,s);this.rotateCanvas(n[0],n[1],this.gestureStartRadians+d(a))}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],a=this.dragStartFrozenX- -e,n=this.dragStartFrozenY- -s;this.translateX=a,this.translateY=n,this.update()}}handleWheel(t){let{deltaX:e,deltaY:s,ctrlKey:a}=t;const n=[120,100],r=2;M(t)||((Math.abs(e)===120||Math.abs(e)===200)&&(e=e/(100/r*C(e,n))*Math.sign(e)),(Math.abs(s)===120||Math.abs(s)===200)&&(s=s/(100/r*C(s,n))*Math.sign(s)));const i=this.scale;if(a){const l=-s/100*i,h=m(i+l,this.minScale,this.maxScale),u=this.relativeWrapperCoordinatesFromClientCoords(t.clientX,t.clientY),[f,S]=this.calcProjectionTranslate(h,u,this.normalizeMatrixCoordinates(t.clientX,t.clientY));this.translateX=f,this.translateY=S,this.scale=h}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],a=[this.touchStarts[1].canvasRel[0]*this.canvasBounds.width,this.touchStarts[1].canvasRel[1]*this.canvasBounds.height],n=Math.sqrt(Math.pow(s[0]-a[0],2)+Math.pow(s[1]-a[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,i=m(r/n,this.minScale,this.maxScale),l=[e[0][0]/this.wrapperInnerWidth,e[0][1]/this.wrapperInnerHeight],h=this.touchStarts[0].canvasRel,[u,f]=this.calcProjectionTranslate(i,l,h,0);let S=0,v=0,w=0;const y=Math.atan2(a[1]-s[1],a[0]-s[0]);w=Math.atan2(e[1][1]-e[0][1],e[1][0]-e[0][0])-y;const T=(W,I)=>[this.offset.left+this.canvasBounds.width*W*this.naturalScale*i+u,this.offset.top+this.canvasBounds.height*I*this.naturalScale*i+f],R=T(0,0),z=T(this.touchStarts[0].canvasRel[0],this.touchStarts[0].canvasRel[1]),B=Y(R,z,w);S=B[0]-R[0],v=B[1]-R[1],this.scale=i,this.rotate=w,this.translateX=u+S,this.translateY=f+v}else{const s=t.touches[0].clientX-this.touchStarts[0].client[0],a=t.touches[0].clientY-this.touchStarts[0].client[1],n=this.touchStartTranslateX+s,r=this.touchStartTranslateY+a;this.translateX=n,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,a){const n=this.canvasBounds.width*this.naturalScale,r=this.canvasBounds.height*this.naturalScale,i=s[0]*n*t,l=s[1]*r*t,h=Y([i,l],[0,0],a??this.rotate),u=e[0]*this.wrapperInnerWidth,f=e[1]*this.wrapperInnerHeight,S=u-h[0],v=f-h[1];return[S,v]}applyTransform(t,e,s){const a=this.calcProjectionTranslate(t,e,s,0);this.scale=t,this.translateX=a[0],this.translateY=a[1],this.update()}composeRelPoint(t,e,s,a,n,r){s=s??this.scale,a=a??this.translateX,n=n??this.translateY,r=r??this.rotate;const i=[this.offset.left,this.offset.top],l=[this.offset.left+this.canvasBounds.width*(s*this.naturalScale)*t,this.offset.top+this.canvasBounds.height*(s*this.naturalScale)*e],h=Y(l,i,r);return[h[0]+a,h[1]+n]}composePoint(t,e){const s=t/this.canvasBounds.width,a=e/this.canvasBounds.height;return this.composeRelPoint(s,a)}getAnchorOffset(t,e,s,a,n=[.5,.5]){const r=this.calcProjectionTranslate(t,n,n,0),i=[this.offset.left+r[0]+this.canvasBounds.width*(t*this.naturalScale)*n[0],this.offset.top+r[1]+this.canvasBounds.height*(t*this.naturalScale)*n[1]],l=this.composeRelPoint(n[0],n[1],t,e,s,a),h=l[0]-i[0],u=l[1]-i[1];return[h,u]}getCanvasCoordsRel(t,e){const s=[0,0],a=[t-this.translateX,e-this.translateY],n=Y(a,s,-this.rotate),r=[n[0]/this.renderinScale,n[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,a]=this.clientCoordsToWrapperCoords(t,e);return[s/this.wrapperInnerWidth,a/this.wrapperInnerHeight]}normalizeMatrixCoordinates(t,e){const s=this.clientCoordsToWrapperCoords(t,e);return this.getCanvasCoordsRel(s[0],s[1])}normalizeClientCoords(t,e){const[s,a]=this.normalizeMatrixCoordinates(t,e);return[s*this.canvasBounds.width,a*this.canvasBounds.height]}rotateCanvas(t,e,s){const a=this.composeRelPoint(t,e,this.scale,0,0,s),n=this.composeRelPoint(t,e);this.translateX=n[0]-a[0],this.translateY=n[1]-a[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(){}}p.Zoompinch=x,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zoompinch/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/zoompinch-core.umd.js",
|
|
7
|
+
"module": "./dist/zoompinch-core.es.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/zoompinch-core.es.js",
|
|
12
|
+
"require": "./dist/zoompinch-core.umd.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": ["dist"],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "vite build",
|
|
19
|
+
"dev": "vite"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"vite": "^6.4.1"
|
|
24
|
+
}
|
|
25
|
+
}
|