@pluot/core 0.1.0 → 0.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.
Files changed (34) hide show
  1. package/README.md +1 -0
  2. package/dist/index.js +1109 -4101
  3. package/dist-tsc/functional-3d-view-controls.d.ts +11 -0
  4. package/dist-tsc/functional-3d-view-controls.d.ts.map +1 -0
  5. package/dist-tsc/functional-3d-view-controls.js +517 -0
  6. package/dist-tsc/functional-dom-2d-camera.d.ts +9 -0
  7. package/dist-tsc/functional-dom-2d-camera.d.ts.map +1 -0
  8. package/dist-tsc/functional-dom-2d-camera.js +178 -0
  9. package/dist-tsc/index.d.ts +2 -2
  10. package/dist-tsc/index.d.ts.map +1 -1
  11. package/dist-tsc/index.js +2 -2
  12. package/dist-tsc/lru-store.d.ts.map +1 -1
  13. package/dist-tsc/lru-store.js +3 -0
  14. package/dist-tsc/unrolled-3d-view-controls.d.ts +2 -0
  15. package/dist-tsc/unrolled-3d-view-controls.d.ts.map +1 -0
  16. package/dist-tsc/unrolled-3d-view-controls.js +637 -0
  17. package/dist-tsc/unrolled-dom-2d-camera.d.ts +2 -0
  18. package/dist-tsc/unrolled-dom-2d-camera.d.ts.map +1 -0
  19. package/dist-tsc/unrolled-dom-2d-camera.js +193 -0
  20. package/dist-tsc/viewport.d.ts +91 -0
  21. package/dist-tsc/viewport.d.ts.map +1 -1
  22. package/dist-tsc/viewport.js +91 -3
  23. package/dist-tsc/viewport.test.d.ts +2 -0
  24. package/dist-tsc/viewport.test.d.ts.map +1 -0
  25. package/dist-tsc/viewport.test.js +184 -0
  26. package/package.json +8 -2
  27. package/src/functional-3d-view-controls.ts +585 -0
  28. package/src/functional-dom-2d-camera.ts +214 -0
  29. package/src/index.ts +2 -2
  30. package/src/lru-store.ts +3 -0
  31. package/src/viewport.test.ts +262 -0
  32. package/src/viewport.ts +91 -3
  33. package/src/3d-view-controls.js +0 -271
  34. package/src/dom-2d-camera.js +0 -441
@@ -1,271 +0,0 @@
1
- // License copied from https://github.com/mikolalysenko/3d-view-controls/blob/master/LICENSE
2
- //
3
- // The MIT License (MIT)
4
- //
5
- // Copyright (c) 2013 Mikola Lysenko
6
- //
7
- // Permission is hereby granted, free of charge, to any person obtaining a copy
8
- // of this software and associated documentation files (the "Software"), to deal
9
- // in the Software without restriction, including without limitation the rights
10
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- // copies of the Software, and to permit persons to whom the Software is
12
- // furnished to do so, subject to the following conditions:
13
- //
14
- // The above copyright notice and this permission notice shall be included in
15
- // all copies or substantial portions of the Software.
16
- //
17
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- // THE SOFTWARE.
24
-
25
-
26
- // Reference: https://github.com/mikolalysenko/3d-view-controls/blob/0e59c2ae4a891ce3c7bb83aa4291f89d99366037/camera.js
27
- // TODO: contribute modifications back upstream once things are working.
28
-
29
- import createView from '3d-view';
30
- import mouseChange from 'mouse-change';
31
- import mouseWheel from 'mouse-wheel';
32
- import mouseOffset from 'mouse-event-offset';
33
- import hasPassive from 'has-passive-events';
34
-
35
- // Updated right-now implementation to avoid `global` usage.
36
- // Reference: https://github.com/hughsk/right-now/blob/master/browser.js
37
- const now =
38
- performance && performance.now
39
- ? function now() {
40
- return performance.now();
41
- }
42
- : Date.now ||
43
- function now() {
44
- return +new Date();
45
- };
46
-
47
- export default function createCamera(element, options) {
48
- element = element || document.body
49
- options = options || {}
50
-
51
- var limits = [ 0.01, Infinity ]
52
- if('distanceLimits' in options) {
53
- limits[0] = options.distanceLimits[0]
54
- limits[1] = options.distanceLimits[1]
55
- }
56
- if('zoomMin' in options) {
57
- limits[0] = options.zoomMin
58
- }
59
- if('zoomMax' in options) {
60
- limits[1] = options.zoomMax
61
- }
62
-
63
- var view = createView({
64
- center: options.center || [0,0,0],
65
- up: options.up || [0,1,0],
66
- eye: options.eye || [0,0,10],
67
- mode: options.mode || 'orbit',
68
- distanceLimits: limits
69
- })
70
-
71
- var pmatrix = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
72
- var distance = 0.0
73
- var width = element.clientWidth
74
- var height = element.clientHeight
75
-
76
- var camera = {
77
- view: view,
78
- element: element,
79
- delay: options.delay || 16,
80
- rotateSpeed: options.rotateSpeed || 1,
81
- zoomSpeed: options.zoomSpeed || 1,
82
- translateSpeed: options.translateSpeed || 1,
83
- flipX: !!options.flipX,
84
- flipY: !!options.flipY,
85
- modes: view.modes,
86
- tick: function() {
87
- var t = now()
88
- var delay = this.delay
89
- view.idle(t-delay)
90
- view.flush(t-(100+delay*2))
91
- var ctime = t - 2 * delay
92
- view.recalcMatrix(ctime)
93
- var allEqual = true
94
- var matrix = view.computedMatrix
95
- for(var i=0; i<16; ++i) {
96
- allEqual = allEqual && (pmatrix[i] === matrix[i])
97
- pmatrix[i] = matrix[i]
98
- }
99
- var sizeChanged =
100
- element.clientWidth === width &&
101
- element.clientHeight === height
102
- width = element.clientWidth
103
- height = element.clientHeight
104
- if(allEqual) {
105
- return !sizeChanged
106
- }
107
- distance = Math.exp(view.computedRadius[0])
108
- return true
109
- },
110
- lookAt: function(center, eye, up) {
111
- view.lookAt(view.lastT(), center, eye, up)
112
- },
113
- rotate: function(pitch, yaw, roll) {
114
- view.rotate(view.lastT(), pitch, yaw, roll)
115
- },
116
- pan: function(dx, dy, dz) {
117
- view.pan(view.lastT(), dx, dy, dz)
118
- },
119
- translate: function(dx, dy, dz) {
120
- view.translate(view.lastT(), dx, dy, dz)
121
- }
122
- }
123
-
124
- Object.defineProperties(camera, {
125
- matrix: {
126
- get: function() {
127
- return view.computedMatrix
128
- },
129
- set: function(mat) {
130
- view.setMatrix(view.lastT(), mat)
131
- return view.computedMatrix
132
- },
133
- enumerable: true
134
- },
135
- mode: {
136
- get: function() {
137
- return view.getMode()
138
- },
139
- set: function(mode) {
140
- view.setMode(mode)
141
- return view.getMode()
142
- },
143
- enumerable: true
144
- },
145
- center: {
146
- get: function() {
147
- return view.computedCenter
148
- },
149
- set: function(ncenter) {
150
- view.lookAt(view.lastT(), ncenter)
151
- return view.computedCenter
152
- },
153
- enumerable: true
154
- },
155
- eye: {
156
- get: function() {
157
- return view.computedEye
158
- },
159
- set: function(neye) {
160
- view.lookAt(view.lastT(), null, neye)
161
- return view.computedEye
162
- },
163
- enumerable: true
164
- },
165
- up: {
166
- get: function() {
167
- return view.computedUp
168
- },
169
- set: function(nup) {
170
- view.lookAt(view.lastT(), null, null, nup)
171
- return view.computedUp
172
- },
173
- enumerable: true
174
- },
175
- distance: {
176
- get: function() {
177
- return distance
178
- },
179
- set: function(d) {
180
- view.setDistance(view.lastT(), d)
181
- return d
182
- },
183
- enumerable: true
184
- },
185
- distanceLimits: {
186
- get: function() {
187
- return view.getDistanceLimits(limits)
188
- },
189
- set: function(v) {
190
- view.setDistanceLimits(v)
191
- return v
192
- },
193
- enumerable: true
194
- }
195
- })
196
-
197
- element.addEventListener('contextmenu', function(ev) {
198
- //ev.preventDefault()
199
- //return false
200
- })
201
-
202
- var lastX = 0, lastY = 0, lastMods = {shift: false, control: false, alt: false, meta: false}
203
- mouseChange(element, handleInteraction)
204
-
205
- //enable simple touch interactions
206
- element.addEventListener('touchstart', function (ev) {
207
- var xy = mouseOffset(ev.changedTouches[0], element)
208
- handleInteraction(0, xy[0], xy[1], lastMods)
209
- handleInteraction(1, xy[0], xy[1], lastMods)
210
-
211
- ev.preventDefault()
212
- }, hasPassive ? {passive: false} : false)
213
-
214
- element.addEventListener('touchmove', function (ev) {
215
- var xy = mouseOffset(ev.changedTouches[0], element)
216
- handleInteraction(1, xy[0], xy[1], lastMods)
217
-
218
- ev.preventDefault()
219
- }, hasPassive ? {passive: false} : false)
220
-
221
- element.addEventListener('touchend', function (ev) {
222
- var xy = mouseOffset(ev.changedTouches[0], element)
223
- handleInteraction(0, lastX, lastY, lastMods)
224
-
225
- ev.preventDefault()
226
- }, hasPassive ? {passive: false} : false)
227
-
228
- function handleInteraction (buttons, x, y, mods) {
229
- var scale = 1.0 / element.clientHeight
230
- var dx = scale * (x - lastX)
231
- var dy = scale * (y - lastY)
232
-
233
- var flipX = camera.flipX ? 1 : -1
234
- var flipY = camera.flipY ? 1 : -1
235
-
236
- var drot = Math.PI * camera.rotateSpeed
237
-
238
- var t = now()
239
-
240
- if(buttons & 1) {
241
- if(mods.shift) {
242
- view.rotate(t, 0, 0, -dx * drot)
243
- } else {
244
- view.rotate(t, flipX * drot * dx, -flipY * drot * dy, 0)
245
- }
246
- } else if(buttons & 2) {
247
- view.pan(t, -camera.translateSpeed * dx * distance, camera.translateSpeed * dy * distance, 0)
248
- } else if(buttons & 4) {
249
- var kzoom = camera.zoomSpeed * dy / window.innerHeight * (t - view.lastT()) * 50.0
250
- view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1))
251
- }
252
-
253
- lastX = x
254
- lastY = y
255
- lastMods = mods
256
- }
257
-
258
- mouseWheel(element, function(dx, dy, dz) {
259
- var flipX = camera.flipX ? 1 : -1
260
- var flipY = camera.flipY ? 1 : -1
261
- var t = now()
262
- if(Math.abs(dx) > Math.abs(dy)) {
263
- view.rotate(t, 0, 0, -dx * flipX * Math.PI * camera.rotateSpeed / window.innerWidth)
264
- } else {
265
- var kzoom = camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 100.0
266
- view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1))
267
- }
268
- }, true)
269
-
270
- return camera
271
- }
@@ -1,441 +0,0 @@
1
- // License copied from https://github.com/flekschas/dom-2d-camera/blob/master/LICENSE.md
2
- //
3
- // This software is released under the MIT license:
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining a copy of
6
- // this software and associated documentation files (the "Software"), to deal in
7
- // the Software without restriction, including without limitation the rights to
8
- // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
- // the Software, and to permit persons to whom the Software is furnished to do so,
10
- // subject to the following conditions:
11
- //
12
- // The above copyright notice and this permission notice shall be included in all
13
- // 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, FITNESS
17
- // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
- // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
- // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
- // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- // Reference: https://github.com/flekschas/dom-2d-camera/blob/cd59ea035a0ea72c2c0535fa3721f8127946576c/src/index.js
23
- // TODO: contribute modifications back upstream once things are working.
24
-
25
- import { vec2 } from "gl-matrix";
26
- import createCamera from "camera-2d-simple";
27
-
28
- const MOUSE_DOWN_MOVE_ACTIONS = ["pan", "rotate"];
29
- const KEY_MAP = {
30
- alt: "altKey",
31
- cmd: "metaKey",
32
- ctrl: "ctrlKey",
33
- meta: "metaKey",
34
- shift: "shiftKey"
35
- };
36
-
37
- const dom2dCamera = (
38
- element,
39
- {
40
- distance = 1.0,
41
- target = [0, 0],
42
- rotation = 0,
43
- isNdc = true,
44
- isFixed = false,
45
- isPan = true,
46
- isPanInverted = [false, true],
47
- panSpeed = 1,
48
- isRotate = true,
49
- rotateSpeed = 1,
50
- defaultMouseDownMoveAction = "pan",
51
- mouseDownMoveModKey = "alt",
52
- isZoom = true,
53
- zoomSpeed = 1,
54
- viewCenter,
55
- scaleBounds,
56
- translationBounds,
57
- onKeyDown = () => {},
58
- onKeyUp = () => {},
59
- onMouseDown = () => {},
60
- onMouseUp = () => {},
61
- onMouseMove = () => {},
62
- onWheel = () => {},
63
- aspectRatioMode = "Ignore",
64
- aspectRatioAlignmentMode = "Center",
65
- } = {}
66
- ) => {
67
- let camera = createCamera(
68
- target,
69
- distance,
70
- rotation,
71
- viewCenter,
72
- scaleBounds,
73
- translationBounds
74
- );
75
- let mouseX = 0;
76
- let mouseY = 0;
77
- let mouseRelX = 0;
78
- let mouseRelY = 0;
79
- let prevMouseX = 0;
80
- let prevMouseY = 0;
81
- let isLeftMousePressed = false;
82
- let scrollDist = 0;
83
-
84
- let width = 1;
85
- let height = 1;
86
- let aspectRatio = 1;
87
-
88
- let isInteractivelyChanged = false;
89
- let isProgrammaticallyChanged = false;
90
- let isMouseDownMoveModActive = false;
91
-
92
- let panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
93
-
94
- let isPanX = isPan;
95
- let isPanY = isPan;
96
- let isPanXInverted = isPanInverted;
97
- let isPanYInverted = isPanInverted;
98
- let isZoomX = isZoom;
99
- let isZoomY = isZoom;
100
-
101
- const spreadXYSettings = () => {
102
- isPanX = Array.isArray(isPan) ? Boolean(isPan[0]) : isPan;
103
- isPanY = Array.isArray(isPan) ? Boolean(isPan[1]) : isPan;
104
- isPanXInverted = Array.isArray(isPanInverted)
105
- ? Boolean(isPanInverted[0])
106
- : isPanInverted;
107
- isPanYInverted = Array.isArray(isPanInverted)
108
- ? Boolean(isPanInverted[1])
109
- : isPanInverted;
110
- isZoomX = Array.isArray(isZoom) ? Boolean(isZoom[0]) : isZoom;
111
- isZoomY = Array.isArray(isZoom) ? Boolean(isZoom[1]) : isZoom;
112
- };
113
-
114
- spreadXYSettings();
115
-
116
- let xAspectRatioModeFactor = 1.0;
117
- let yAspectRatioModeFactor = 1.0;
118
- let xAlignmentTranslation = 0.0;
119
- let yAlignmentTranslation = 0.0;
120
-
121
- /*
122
- // Logic for aspect ratio handling in point_layer.wgsl
123
- if (aspect_ratio_mode == 1u) {
124
- // fit/contain
125
- if (layer_aspect_ratio > 1.0) {
126
- // Wide rectangle
127
- // Show more than (0, 1) in x direction. Show exactly (0, 1) in y direction.
128
- x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
129
- } else if(layer_aspect_ratio < 1.0) {
130
- // Tall layer
131
- // Show exactly (0, 1) in x direction. Show more than (0, 1) in y direction.
132
- y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
133
- } else {
134
- // Square layer; no change needed.
135
- // Show exactly (0, 1) in both directions.
136
- }
137
- } else if (aspect_ratio_mode == 2u) {
138
- // fill/cover
139
- if(layer_aspect_ratio > 1.0) {
140
- // Wide rectangle
141
- // Show exactly (0, 1) in x direction. Show less than (0, 1) in y direction.
142
- y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
143
- } else if(layer_aspect_ratio < 1.0) {
144
- // Tall layer
145
- // Show less than (0, 1) in x direction. Show exactly (0, 1) in y direction.
146
- x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
147
- } else {
148
- // Square layer; no change needed.
149
- // Show exactly (0, 1) in both directions.
150
- }
151
- }
152
- */
153
- const updateAspectRatioModeFactors = () => {
154
- xAspectRatioModeFactor = 1.0;
155
- yAspectRatioModeFactor = 1.0;
156
- if(aspectRatioMode === "Contain") {
157
- if(aspectRatio > 1.0) {
158
- xAspectRatioModeFactor = 1.0 / aspectRatio;
159
- } else if(aspectRatio < 1.0) {
160
- yAspectRatioModeFactor = aspectRatio;
161
- }
162
- } else if(aspectRatioMode === "Cover") {
163
- if(aspectRatio > 1.0) {
164
- yAspectRatioModeFactor = aspectRatio;
165
- } else if(aspectRatio < 1.0) {
166
- xAspectRatioModeFactor = 1.0 / aspectRatio;
167
- }
168
- }
169
-
170
- xAlignmentTranslation = 0.0;
171
- yAlignmentTranslation = 0.0;
172
- if(aspectRatioAlignmentMode === "Start") {
173
- xAlignmentTranslation = xAspectRatioModeFactor - 1.0;
174
- yAlignmentTranslation = yAspectRatioModeFactor - 1.0;
175
- } else if(aspectRatioAlignmentMode === "End") {
176
- xAlignmentTranslation = 1.0 - xAspectRatioModeFactor;
177
- yAlignmentTranslation = 1.0 - yAspectRatioModeFactor;
178
- }
179
- };
180
- updateAspectRatioModeFactors();
181
-
182
- const transformPanX = isNdc
183
- ? dX => (dX / width) * 2 * (1.0 / xAspectRatioModeFactor) // to normalized device coords
184
- : dX => dX;
185
- const transformPanY = isNdc
186
- ? dY => (dY / height) * 2 * (1.0 / yAspectRatioModeFactor) // to normalized device coords
187
- : dY => -dY;
188
-
189
- const transformScaleX = isNdc
190
- ? x => ((-1 + (x / width) * 2) - xAlignmentTranslation) * (1.0 / xAspectRatioModeFactor)
191
- : x => x;
192
- const transformScaleY = isNdc
193
- ? y => ((1 - (y / height) * 2) - yAlignmentTranslation) * (1.0 / yAspectRatioModeFactor)
194
- : y => y;
195
-
196
- const tick = () => {
197
- if (isFixed) {
198
- const isChanged = isProgrammaticallyChanged;
199
- isProgrammaticallyChanged = false;
200
- return isChanged;
201
- }
202
-
203
- isInteractivelyChanged = false;
204
- const currentMouseX = mouseX;
205
- const currentMouseY = mouseY;
206
-
207
- if (
208
- (isPanX || isPanY) &&
209
- isLeftMousePressed &&
210
- ((panOnMouseDownMove && !isMouseDownMoveModActive) ||
211
- (!panOnMouseDownMove && isMouseDownMoveModActive))
212
- ) {
213
- const dX = isPanXInverted
214
- ? prevMouseX - currentMouseX
215
- : currentMouseX - prevMouseX;
216
-
217
- const transformedPanX = isPanX ? transformPanX(panSpeed * dX) : 0;
218
-
219
- const dY = isPanYInverted
220
- ? prevMouseY - currentMouseY
221
- : currentMouseY - prevMouseY;
222
-
223
- const transformedPanY = isPanY ? transformPanY(panSpeed * dY) : 0;
224
-
225
- if (transformedPanX !== 0 || transformedPanY !== 0) {
226
- camera.pan([transformedPanX, transformedPanY]);
227
- isInteractivelyChanged = true;
228
- }
229
- }
230
-
231
- if ((isZoomX || isZoomY) && scrollDist) {
232
- const dZ = zoomSpeed * Math.exp(scrollDist / height);
233
-
234
- const transformedX = transformScaleX(mouseRelX);
235
- const transformedY = transformScaleY(mouseRelY);
236
-
237
- camera.scale(
238
- [isZoomX ? 1 / dZ : 1, isZoomY ? 1 / dZ : 1],
239
- [transformedX, transformedY]
240
- );
241
-
242
- isInteractivelyChanged = true;
243
- }
244
-
245
- if (
246
- isRotate &&
247
- isLeftMousePressed &&
248
- ((panOnMouseDownMove && isMouseDownMoveModActive) ||
249
- (!panOnMouseDownMove && !isMouseDownMoveModActive)) &&
250
- Math.abs(prevMouseX - currentMouseX) +
251
- Math.abs(prevMouseY - currentMouseY) >
252
- 0
253
- ) {
254
- const wh = width / 2;
255
- const hh = height / 2;
256
- const x1 = prevMouseX - wh;
257
- const y1 = hh - prevMouseY;
258
- const x2 = currentMouseX - wh;
259
- const y2 = hh - currentMouseY;
260
- // Angle between the start and end mouse position with respect to the
261
- // viewport center
262
- const radians = vec2.angle([x1, y1], [x2, y2]);
263
- // Determine the orientation
264
- const cross = x1 * y2 - x2 * y1;
265
-
266
- camera.rotate(rotateSpeed * radians * Math.sign(cross));
267
-
268
- isInteractivelyChanged = true;
269
- }
270
-
271
- // Reset scroll delta and mouse position
272
- scrollDist = 0;
273
- prevMouseX = currentMouseX;
274
- prevMouseY = currentMouseY;
275
-
276
- const isChanged = isInteractivelyChanged || isProgrammaticallyChanged;
277
-
278
- isProgrammaticallyChanged = false;
279
-
280
- return isChanged;
281
- };
282
-
283
- const config = ({
284
- defaultMouseDownMoveAction: newDefaultMouseDownMoveAction = null,
285
- isFixed: newIsFixed = null,
286
- isPan: newIsPan = null,
287
- isPanInverted: newIsPanInverted = null,
288
- isRotate: newIsRotate = null,
289
- isZoom: newIsZoom = null,
290
- panSpeed: newPanSpeed = null,
291
- rotateSpeed: newRotateSpeed = null,
292
- zoomSpeed: newZoomSpeed = null,
293
- mouseDownMoveModKey: newMouseDownMoveModKey = null
294
- } = {}) => {
295
- defaultMouseDownMoveAction =
296
- newDefaultMouseDownMoveAction !== null &&
297
- MOUSE_DOWN_MOVE_ACTIONS.includes(newDefaultMouseDownMoveAction)
298
- ? newDefaultMouseDownMoveAction
299
- : defaultMouseDownMoveAction;
300
-
301
- panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
302
-
303
- isFixed = newIsFixed !== null ? newIsFixed : isFixed;
304
- isPan = newIsPan !== null ? newIsPan : isPan;
305
- isPanInverted =
306
- newIsPanInverted !== null ? newIsPanInverted : isPanInverted;
307
- isRotate = newIsRotate !== null ? newIsRotate : isRotate;
308
- isZoom = newIsZoom !== null ? newIsZoom : isZoom;
309
- panSpeed = +newPanSpeed > 0 ? newPanSpeed : panSpeed;
310
- rotateSpeed = +newRotateSpeed > 0 ? newRotateSpeed : rotateSpeed;
311
- zoomSpeed = +newZoomSpeed > 0 ? newZoomSpeed : zoomSpeed;
312
-
313
- spreadXYSettings();
314
-
315
- mouseDownMoveModKey =
316
- newMouseDownMoveModKey !== null &&
317
- Object.keys(KEY_MAP).includes(newMouseDownMoveModKey)
318
- ? newMouseDownMoveModKey
319
- : mouseDownMoveModKey;
320
- };
321
-
322
- const refresh = () => {
323
- const bBox = element.getBoundingClientRect();
324
- width = bBox.width;
325
- height = bBox.height;
326
- aspectRatio = width / height;
327
- updateAspectRatioModeFactors();
328
- };
329
-
330
- const keyUpHandler = event => {
331
- isMouseDownMoveModActive = false;
332
-
333
- onKeyUp(event);
334
- };
335
-
336
- const keyDownHandler = event => {
337
- isMouseDownMoveModActive = event[KEY_MAP[mouseDownMoveModKey]];
338
-
339
- onKeyDown(event);
340
- };
341
-
342
- const mouseUpHandler = event => {
343
- isLeftMousePressed = false;
344
-
345
- onMouseUp(event);
346
- };
347
-
348
- const mouseDownHandler = event => {
349
- isLeftMousePressed = event.buttons === 1;
350
-
351
- onMouseDown(event);
352
- };
353
-
354
- const offsetXSupport =
355
- document.createEvent("MouseEvent").offsetX !== undefined;
356
-
357
- const updateMouseRelXY = offsetXSupport
358
- ? event => {
359
- mouseRelX = event.offsetX;
360
- mouseRelY = event.offsetY;
361
- }
362
- : event => {
363
- const bBox = element.getBoundingClientRect();
364
- mouseRelX = event.clientX - bBox.left;
365
- mouseRelY = event.clientY - bBox.top;
366
- };
367
-
368
- const updateMouseXY = event => {
369
- mouseX = event.clientX;
370
- mouseY = event.clientY;
371
- };
372
-
373
- const mouseMoveHandler = event => {
374
- updateMouseXY(event);
375
- onMouseMove(event);
376
- };
377
-
378
- const wheelHandler = event => {
379
- if ((isZoomX || isZoomY) && !isFixed) {
380
- event.preventDefault();
381
-
382
- updateMouseXY(event);
383
- updateMouseRelXY(event);
384
-
385
- const scale = event.deltaMode === 1 ? 12 : 1;
386
-
387
- scrollDist += scale * (event.deltaY || event.deltaX || 0);
388
- }
389
-
390
- onWheel(event);
391
- };
392
-
393
- const dispose = () => {
394
- camera = undefined;
395
- element.removeEventListener("keydown", keyDownHandler);
396
- element.removeEventListener("keyup", keyUpHandler);
397
- element.removeEventListener("mousedown", mouseDownHandler);
398
- element.removeEventListener("mouseup", mouseUpHandler);
399
- element.removeEventListener("mousemove", mouseMoveHandler);
400
- element.removeEventListener("wheel", wheelHandler);
401
- };
402
-
403
- element.addEventListener("keydown", keyDownHandler, { passive: true });
404
- element.addEventListener("keyup", keyUpHandler, { passive: true });
405
- element.addEventListener("mousedown", mouseDownHandler, { passive: true });
406
- element.addEventListener("mouseup", mouseUpHandler, { passive: true });
407
- element.addEventListener("mousemove", mouseMoveHandler, { passive: true });
408
- element.addEventListener("wheel", wheelHandler, { passive: false });
409
-
410
- camera.config = config;
411
- camera.dispose = dispose;
412
- camera.refresh = refresh;
413
- camera.tick = tick;
414
-
415
- const withProgrammaticChange = fn =>
416
- function() {
417
- fn.apply(null, arguments);
418
- isProgrammaticallyChanged = true;
419
- };
420
-
421
- camera.lookAt = withProgrammaticChange(camera.lookAt);
422
- camera.translate = withProgrammaticChange(camera.translate);
423
- camera.pan = withProgrammaticChange(camera.pan);
424
- camera.rotate = withProgrammaticChange(camera.rotate);
425
- camera.scale = withProgrammaticChange(camera.scale);
426
- camera.zoom = withProgrammaticChange(camera.zoom);
427
- camera.reset = withProgrammaticChange(camera.reset);
428
- camera.set = withProgrammaticChange(camera.set);
429
- camera.setScaleBounds = withProgrammaticChange(camera.setScaleBounds);
430
- camera.setTranslationBounds = withProgrammaticChange(
431
- camera.setTranslationBounds
432
- );
433
- camera.setView = withProgrammaticChange(camera.setView);
434
- camera.setViewCenter = withProgrammaticChange(camera.setViewCenter);
435
-
436
- refresh();
437
-
438
- return camera;
439
- };
440
-
441
- export default dom2dCamera;