@leafer-in/viewport 1.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-in/viewport
@@ -0,0 +1,353 @@
1
+ 'use strict';
2
+
3
+ var core = require('@leafer-ui/core');
4
+
5
+ function addViewport(leafer, mergeConfig, custom) {
6
+ addViewportConfig(leafer.parentApp ? leafer.parentApp : leafer, mergeConfig);
7
+ if (leafer.isApp || custom)
8
+ return;
9
+ leafer.__eventIds.push(leafer.on_(core.MoveEvent.BEFORE_MOVE, (e) => {
10
+ leafer.zoomLayer.move(leafer.getValidMove(e.moveX, e.moveY));
11
+ }), leafer.on_(core.ZoomEvent.BEFORE_ZOOM, (e) => {
12
+ const { zoomLayer } = leafer;
13
+ const changeScale = leafer.getValidScale(e.scale);
14
+ if (changeScale !== 1) {
15
+ core.PointHelper.scaleOf(zoomLayer, e, changeScale);
16
+ zoomLayer.scale = zoomLayer.__.scaleX * changeScale;
17
+ }
18
+ }));
19
+ }
20
+ function addViewportConfig(leafer, mergeConfig) {
21
+ if (mergeConfig)
22
+ core.DataHelper.assign(leafer.config, mergeConfig);
23
+ core.DataHelper.assign(leafer.config, {
24
+ wheel: { preventDefault: true },
25
+ touch: { preventDefault: true },
26
+ pointer: { preventDefaultMenu: true }
27
+ }, leafer.userConfig);
28
+ }
29
+
30
+ function custom(leafer) {
31
+ addViewport(leafer, null, true);
32
+ }
33
+
34
+ function design(leafer) {
35
+ addViewport(leafer, {
36
+ zoom: {
37
+ min: 0.01,
38
+ max: 256
39
+ },
40
+ move: {
41
+ holdSpaceKey: true,
42
+ holdMiddleKey: true,
43
+ }
44
+ });
45
+ }
46
+
47
+ function document(leafer) {
48
+ addViewport(leafer, {
49
+ zoom: { min: 1 },
50
+ move: { scroll: 'limit' }
51
+ });
52
+ }
53
+
54
+ const debug = core.Debug.get('LeaferTypeCreator');
55
+ const LeaferTypeCreator = {
56
+ list: {},
57
+ register(name, fn) {
58
+ list[name] && debug.repeat(name);
59
+ list[name] = fn;
60
+ },
61
+ run(name, leafer) {
62
+ const fn = list[name];
63
+ fn && fn(leafer);
64
+ }
65
+ };
66
+ const { list, register } = LeaferTypeCreator;
67
+ register('viewport', addViewport);
68
+ register('custom', custom);
69
+ register('design', design);
70
+ register('document', document);
71
+
72
+ const MultiTouchHelper = {
73
+ getData(list) {
74
+ const a = list[0], b = list[1];
75
+ const lastCenter = core.PointHelper.getCenter(a.from, b.from);
76
+ const center = core.PointHelper.getCenter(a.to, b.to);
77
+ const move = { x: center.x - lastCenter.x, y: center.y - lastCenter.y };
78
+ const lastDistance = core.PointHelper.getDistance(a.from, b.from);
79
+ const distance = core.PointHelper.getDistance(a.to, b.to);
80
+ const scale = distance / lastDistance;
81
+ const rotation = core.PointHelper.getRotation(a.from, b.from, a.to, b.to);
82
+ return { move, scale, rotation, center };
83
+ }
84
+ };
85
+
86
+ const WheelEventHelper = {
87
+ getMove(event, config) {
88
+ let { moveSpeed } = config;
89
+ let { deltaX, deltaY } = event;
90
+ if (event.shiftKey && !deltaX) {
91
+ deltaX = deltaY;
92
+ deltaY = 0;
93
+ }
94
+ if (deltaX > 50)
95
+ deltaX = Math.max(50, deltaX / 3);
96
+ if (deltaY > 50)
97
+ deltaY = Math.max(50, deltaY / 3);
98
+ return { x: -deltaX * moveSpeed * 2, y: -deltaY * moveSpeed * 2 };
99
+ },
100
+ getScale(event, config) {
101
+ let zoom;
102
+ let scale = 1;
103
+ let { zoomMode, zoomSpeed } = config;
104
+ const delta = event.deltaY || event.deltaX;
105
+ if (zoomMode) {
106
+ zoom = (zoomMode === 'mouse') ? true : (!event.deltaX && (core.Platform.intWheelDeltaY ? Math.abs(delta) > 17 : Math.ceil(delta) !== delta));
107
+ if (event.shiftKey || event.metaKey || event.ctrlKey)
108
+ zoom = true;
109
+ }
110
+ else {
111
+ zoom = !event.shiftKey && (event.metaKey || event.ctrlKey);
112
+ }
113
+ if (zoom) {
114
+ zoomSpeed = core.MathHelper.within(zoomSpeed, 0, 1);
115
+ const min = event.deltaY ? config.delta.y : config.delta.x;
116
+ scale = 1 - delta / (min * 4) * zoomSpeed;
117
+ if (scale < 0.5)
118
+ scale = 0.5;
119
+ if (scale >= 1.5)
120
+ scale = 1.5;
121
+ }
122
+ return scale;
123
+ }
124
+ };
125
+
126
+ class Transformer {
127
+ get transforming() { return !!(this.moveData || this.zoomData || this.rotateData); }
128
+ constructor(interaction) {
129
+ this.interaction = interaction;
130
+ }
131
+ move(data) {
132
+ const { interaction } = this;
133
+ if (!data.moveType)
134
+ data.moveType = 'move';
135
+ if (!this.moveData) {
136
+ this.setPath(data);
137
+ this.moveData = Object.assign(Object.assign({}, data), { moveX: 0, moveY: 0 });
138
+ interaction.emit(core.MoveEvent.START, this.moveData);
139
+ }
140
+ data.path = this.moveData.path;
141
+ interaction.emit(core.MoveEvent.BEFORE_MOVE, data);
142
+ interaction.emit(core.MoveEvent.MOVE, data);
143
+ this.transformEndWait();
144
+ }
145
+ zoom(data) {
146
+ const { interaction } = this;
147
+ if (!this.zoomData) {
148
+ this.setPath(data);
149
+ this.zoomData = Object.assign(Object.assign({}, data), { scale: 1 });
150
+ interaction.emit(core.ZoomEvent.START, this.zoomData);
151
+ }
152
+ data.path = this.zoomData.path;
153
+ interaction.emit(core.ZoomEvent.BEFORE_ZOOM, data);
154
+ interaction.emit(core.ZoomEvent.ZOOM, data);
155
+ this.transformEndWait();
156
+ }
157
+ rotate(data) {
158
+ const { interaction } = this;
159
+ if (!this.rotateData) {
160
+ this.setPath(data);
161
+ this.rotateData = Object.assign(Object.assign({}, data), { rotation: 0 });
162
+ interaction.emit(core.RotateEvent.START, this.rotateData);
163
+ }
164
+ data.path = this.rotateData.path;
165
+ interaction.emit(core.RotateEvent.BEFORE_ROTATE, data);
166
+ interaction.emit(core.RotateEvent.ROTATE, data);
167
+ this.transformEndWait();
168
+ }
169
+ setPath(data) {
170
+ const { interaction } = this;
171
+ const { path } = interaction.selector.getByPoint(data, interaction.hitRadius);
172
+ data.path = path;
173
+ interaction.cancelHover();
174
+ }
175
+ transformEndWait() {
176
+ clearTimeout(this.transformTimer);
177
+ this.transformTimer = setTimeout(() => {
178
+ this.transformEnd();
179
+ }, this.interaction.p.transformTime);
180
+ }
181
+ transformEnd() {
182
+ const { interaction, moveData, zoomData, rotateData } = this;
183
+ if (moveData)
184
+ interaction.emit(core.MoveEvent.END, moveData);
185
+ if (zoomData)
186
+ interaction.emit(core.ZoomEvent.END, zoomData);
187
+ if (rotateData)
188
+ interaction.emit(core.RotateEvent.END, rotateData);
189
+ this.reset();
190
+ }
191
+ reset() {
192
+ this.zoomData = this.moveData = this.rotateData = null;
193
+ }
194
+ destroy() {
195
+ this.reset();
196
+ }
197
+ }
198
+
199
+ const leafer = core.Leafer.prototype;
200
+ const bounds = new core.Bounds();
201
+ leafer.initType = function (type) {
202
+ LeaferTypeCreator.run(type, this);
203
+ };
204
+ leafer.getValidMove = function (moveX, moveY) {
205
+ const { scroll, disabled } = this.app.config.move;
206
+ if (scroll) {
207
+ const type = scroll === true ? '' : scroll;
208
+ if (type.includes('x'))
209
+ moveX = moveX || moveY, moveY = 0;
210
+ else if (type.includes('y'))
211
+ moveY = moveY || moveX, moveX = 0;
212
+ else
213
+ Math.abs(moveX) > Math.abs(moveY) ? moveY = 0 : moveX = 0;
214
+ if (type.includes('limit')) {
215
+ const { x, y, width, height } = bounds.set(this.__world).addPoint(this.zoomLayer);
216
+ const right = x + width - this.width, bottom = y + height - this.height;
217
+ if (x >= 0 && right <= 0)
218
+ moveX = 0;
219
+ else if (moveX > 0) {
220
+ if (x + moveX > 0)
221
+ moveX = -x;
222
+ }
223
+ else if (moveX < 0 && right + moveX < 0)
224
+ moveX = -right;
225
+ if (y >= 0 && bottom <= 0)
226
+ moveY = 0;
227
+ else if (moveY > 0) {
228
+ if (y + moveY > 0)
229
+ moveY = -y;
230
+ }
231
+ else if (moveY < 0 && bottom + moveY < 0)
232
+ moveY = -bottom;
233
+ }
234
+ }
235
+ return { x: disabled ? 0 : moveX, y: disabled ? 0 : moveY };
236
+ };
237
+ leafer.getValidScale = function (changeScale) {
238
+ const { scaleX } = this.zoomLayer.__, { min, max, disabled } = this.app.config.zoom, absScale = Math.abs(scaleX * changeScale);
239
+ if (min && absScale < min)
240
+ changeScale = min / scaleX;
241
+ else if (max && absScale > max)
242
+ changeScale = max / scaleX;
243
+ return disabled ? 1 : core.MathHelper.float(changeScale);
244
+ };
245
+
246
+ function getMoveEventData(move, event) {
247
+ return Object.assign(Object.assign({}, event), { moveX: move.x, moveY: move.y });
248
+ }
249
+ function getRotateEventData(rotation, event) {
250
+ return Object.assign(Object.assign({}, event), { rotation });
251
+ }
252
+ function getZoomEventData(scale, event) {
253
+ return Object.assign(Object.assign({}, event), { scale });
254
+ }
255
+ const interaction = core.InteractionBase.prototype;
256
+ interaction.createTransformer = function () {
257
+ this.transformer = new Transformer(this);
258
+ };
259
+ interaction.move = function (data) {
260
+ this.transformer.move(data);
261
+ };
262
+ interaction.zoom = function (data) {
263
+ this.transformer.zoom(data);
264
+ };
265
+ interaction.rotate = function (data) {
266
+ this.transformer.rotate(data);
267
+ };
268
+ interaction.transformEnd = function () {
269
+ this.transformer.transformEnd();
270
+ };
271
+ interaction.wheel = function (data) {
272
+ const { wheel } = this.config;
273
+ if (wheel.disabled)
274
+ return;
275
+ const scale = wheel.getScale ? wheel.getScale(data, wheel) : WheelEventHelper.getScale(data, wheel);
276
+ scale !== 1 ? this.zoom(getZoomEventData(scale, data)) : this.move(getMoveEventData(wheel.getMove ? wheel.getMove(data, wheel) : WheelEventHelper.getMove(data, wheel), data));
277
+ };
278
+ interaction.multiTouch = function (data, list) {
279
+ if (this.config.multiTouch.disabled)
280
+ return;
281
+ const { move, rotation, scale, center } = MultiTouchHelper.getData(list);
282
+ Object.assign(data, center);
283
+ this.rotate(getRotateEventData(rotation, data));
284
+ this.zoom(getZoomEventData(scale, data));
285
+ this.move(getMoveEventData(move, data));
286
+ };
287
+
288
+ const dragger = core.Dragger.prototype;
289
+ const { abs } = Math;
290
+ dragger.checkDragEndAnimate = function (data, speed) {
291
+ const { moveX, moveY } = this.dragData;
292
+ const absMoveX = abs(moveX), absMoveY = abs(moveY), minMove = speed ? 1 : 0.1;
293
+ const dragAnimate = this.interaction.m.dragAnimate && this.canAnimate && this.moving && (absMoveX > minMove || absMoveY > minMove);
294
+ if (dragAnimate) {
295
+ const inertia = data.pointerType === 'touch' ? 3 : 1, maxMove = 70;
296
+ speed = speed ? 0.95 : inertia;
297
+ if (absMoveX * speed > maxMove)
298
+ speed = maxMove / absMoveX;
299
+ else if (absMoveY * speed > maxMove)
300
+ speed = maxMove / absMoveY;
301
+ data = Object.assign({}, data);
302
+ core.PointHelper.move(data, moveX * speed, moveY * speed);
303
+ this.drag(data);
304
+ this.animate(() => { this.dragEnd(data, 1); });
305
+ }
306
+ return dragAnimate;
307
+ };
308
+ dragger.animate = function (func, off) {
309
+ const animateWait = func || this.animateWait;
310
+ if (animateWait)
311
+ this.interaction.target.nextRender(animateWait, null, off);
312
+ this.animateWait = func;
313
+ };
314
+ dragger.checkDragOut = function (data) {
315
+ const { interaction } = this;
316
+ this.autoMoveCancel();
317
+ if (this.dragging && !interaction.shrinkCanvasBounds.hitPoint(data))
318
+ this.autoMoveOnDragOut(data);
319
+ };
320
+ dragger.autoMoveOnDragOut = function (data) {
321
+ const { interaction, downData, canDragOut } = this;
322
+ const { autoDistance, dragOut } = interaction.m;
323
+ if (!dragOut || !canDragOut || !autoDistance)
324
+ return;
325
+ const bounds = interaction.shrinkCanvasBounds;
326
+ const { x, y } = bounds;
327
+ const right = core.BoundsHelper.maxX(bounds);
328
+ const bottom = core.BoundsHelper.maxY(bounds);
329
+ const moveX = data.x < x ? autoDistance : (right < data.x ? -autoDistance : 0);
330
+ const moveY = data.y < y ? autoDistance : (bottom < data.y ? -autoDistance : 0);
331
+ let totalX = 0, totalY = 0;
332
+ this.autoMoveTimer = setInterval(() => {
333
+ totalX += moveX;
334
+ totalY += moveY;
335
+ core.PointHelper.move(downData, moveX, moveY);
336
+ core.PointHelper.move(this.dragData, moveX, moveY);
337
+ interaction.move(Object.assign(Object.assign({}, data), { moveX, moveY, totalX, totalY, moveType: 'drag' }));
338
+ interaction.pointerMoveReal(data);
339
+ }, 10);
340
+ };
341
+ dragger.autoMoveCancel = function () {
342
+ if (this.autoMoveTimer) {
343
+ clearInterval(this.autoMoveTimer);
344
+ this.autoMoveTimer = 0;
345
+ }
346
+ };
347
+
348
+ exports.LeaferTypeCreator = LeaferTypeCreator;
349
+ exports.MultiTouchHelper = MultiTouchHelper;
350
+ exports.Transformer = Transformer;
351
+ exports.WheelEventHelper = WheelEventHelper;
352
+ exports.addViewport = addViewport;
353
+ exports.addViewportConfig = addViewportConfig;