@pluot/core 0.1.0
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 +299 -0
- package/dist/index.js +6155 -0
- package/dist-tsc/3d-view-controls.d.ts +17 -0
- package/dist-tsc/3d-view-controls.d.ts.map +1 -0
- package/dist-tsc/3d-view-controls.js +247 -0
- package/dist-tsc/core.d.ts +10 -0
- package/dist-tsc/core.d.ts.map +1 -0
- package/dist-tsc/core.js +71 -0
- package/dist-tsc/dom-2d-camera.d.ts +26 -0
- package/dist-tsc/dom-2d-camera.d.ts.map +1 -0
- package/dist-tsc/dom-2d-camera.js +323 -0
- package/dist-tsc/feature-detection.d.ts +2 -0
- package/dist-tsc/feature-detection.d.ts.map +1 -0
- package/dist-tsc/feature-detection.js +58 -0
- package/dist-tsc/functional-2d-camera.d.ts +9 -0
- package/dist-tsc/functional-2d-camera.d.ts.map +1 -0
- package/dist-tsc/functional-2d-camera.js +178 -0
- package/dist-tsc/index.d.ts +6 -0
- package/dist-tsc/index.d.ts.map +1 -0
- package/dist-tsc/index.js +5 -0
- package/dist-tsc/lru-store.d.ts +13 -0
- package/dist-tsc/lru-store.d.ts.map +1 -0
- package/dist-tsc/lru-store.js +137 -0
- package/dist-tsc/viewport.d.ts +24 -0
- package/dist-tsc/viewport.d.ts.map +1 -0
- package/dist-tsc/viewport.js +108 -0
- package/package.json +55 -0
- package/src/3d-view-controls.js +271 -0
- package/src/core.ts +89 -0
- package/src/dom-2d-camera.js +441 -0
- package/src/feature-detection.ts +57 -0
- package/src/index.ts +13 -0
- package/src/lru-store.ts +155 -0
- package/src/viewport.ts +145 -0
|
@@ -0,0 +1,441 @@
|
|
|
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;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import UserAgent from "lightua";
|
|
2
|
+
|
|
3
|
+
// Returns the tuple [supportsWebGpu, supportsWebGpuMessage]
|
|
4
|
+
export function checkWebGpuFeatureDetection(): [boolean, string|null] {
|
|
5
|
+
const isSupported = typeof navigator !== 'undefined' && navigator.gpu;
|
|
6
|
+
if (isSupported) {
|
|
7
|
+
return [true, null];
|
|
8
|
+
}
|
|
9
|
+
const ua = UserAgent.parse(navigator.userAgent);
|
|
10
|
+
// WebGPU is supported in the following cases:
|
|
11
|
+
// - Browser Engine: Chromium. Major: 113+. OS: mac, windows, chromeOS
|
|
12
|
+
// - Browser Engine: Chromium. Major: 121+. OS: android
|
|
13
|
+
// - Browser Engine: Chromium. Major: 144+. OS: linux
|
|
14
|
+
// - Browser: Firefox. Major: 141+. OS: windows
|
|
15
|
+
// - Browser: Firefox. Major: 145+. OS: macos
|
|
16
|
+
// - Browser: Safari. Major: 26+. OS: macOS. OS version: tahoe 26+.
|
|
17
|
+
// - Browser: Safari. Major: 26+. OS: iOS/iPadOS. OS version: tahoe 26+.
|
|
18
|
+
// Else: suggest upgrading browser version or OS version (if different version of same browser has support)
|
|
19
|
+
// Else: suggest using different browser version.
|
|
20
|
+
const browserName = ua.browser.name;
|
|
21
|
+
const browserMajor = ua.browser.major;
|
|
22
|
+
const osName = ua.os.name;
|
|
23
|
+
const osVersion = ua.os.version;
|
|
24
|
+
|
|
25
|
+
// Should be supported according to UA but support wasn't detected.
|
|
26
|
+
const shouldSupportResult: [boolean, string|null] = [false, `WebGPU support was not detected. Please ensure the WebGPU feature is enabled or try a different browser.`];
|
|
27
|
+
|
|
28
|
+
if(["Chrome", "Chromium"].includes(browserName)) {
|
|
29
|
+
if(["Windows", "macOS", "ChromeOS"].includes(osName)) {
|
|
30
|
+
if(browserMajor < 113) {
|
|
31
|
+
return [false, `WebGPU is not supported in your web browser. If using Chrome or a Chromium-based browser on Windows/macOS/ChromeOS, WebGPU is supported in browser versions 113 and above.`];
|
|
32
|
+
}
|
|
33
|
+
} else if(osName === "Android") {
|
|
34
|
+
if(browserMajor < 121) {
|
|
35
|
+
return [false, `WebGPU is not supported in your web browser. If using Chrome or a Chromium-based browser on Android, WebGPU is supported in browser versions 121 and above.`];
|
|
36
|
+
}
|
|
37
|
+
} else if(osName === "Linux") {
|
|
38
|
+
if(browserMajor < 144) {
|
|
39
|
+
return [false, `WebGPU is not supported in your web browser. If using Chrome or a Chromium-based browser on Linux, WebGPU is supported in browser versions 144 and above.`];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else if(browserName === "Firefox") {
|
|
43
|
+
if(osName === "Windows") {
|
|
44
|
+
if(browserMajor < 141) {
|
|
45
|
+
return [false, `WebGPU is not supported in your web browser. If using Firefox on Windows, WebGPU is supported in browser versions 141 and above.`];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} else if(browserName === "Safari") {
|
|
49
|
+
if(["macOS", "iOS"].includes(osName)) {
|
|
50
|
+
const osVersionFloat = parseFloat(osVersion);
|
|
51
|
+
if(browserMajor < 26 || osVersionFloat < 26) {
|
|
52
|
+
return [false, `WebGPU is not supported in your web browser. If using Safari, WebGPU is supported in Safari version 26 and above on macOS 26 (Tahoe) and above.`];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return shouldSupportResult;
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
initialize,
|
|
3
|
+
getIsWasmReady,
|
|
4
|
+
render_wasm,
|
|
5
|
+
pick_wasm,
|
|
6
|
+
setStore,
|
|
7
|
+
setStoreByName,
|
|
8
|
+
getStore,
|
|
9
|
+
} from './core.js';
|
|
10
|
+
export { default as create2dCamera } from "./dom-2d-camera.js";
|
|
11
|
+
export { default as create3dCamera } from "./3d-view-controls.js";
|
|
12
|
+
export { checkWebGpuFeatureDetection } from './feature-detection.js';
|
|
13
|
+
export { getBounds, getCameraMatrixFromBounds } from './viewport.js';
|
package/src/lru-store.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import QuickLRU from "quick-lru";
|
|
2
|
+
|
|
3
|
+
import type { RangeQuery, AsyncReadable, AbsolutePath } from 'zarrita';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function normalizeKey(key: string, range?: RangeQuery) {
|
|
7
|
+
if (!range) return key;
|
|
8
|
+
if ("suffixLength" in range) return `${key}:-${range.suffixLength}`;
|
|
9
|
+
return `${key}:${range.offset}:${range.offset + range.length - 1}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Provides a blanket implementation of getRange that can be used with any AsyncReadable store,
|
|
13
|
+
// even if it doesn't define a getRange method.
|
|
14
|
+
// If the store does have a native getRange method, we use that instead.
|
|
15
|
+
// Reference: https://github.com/vitessce/vitessce/blob/main/packages/utils/zarr-utils/src/base-getrange.ts
|
|
16
|
+
export function createGetRange<S extends AsyncReadable>(store: S) {
|
|
17
|
+
// TODO: support options param for getRange?
|
|
18
|
+
return async (...args: Parameters<NonNullable<S["getRange"]>>): Promise<Uint8Array | undefined> => {
|
|
19
|
+
const [key, range, opts] = args;
|
|
20
|
+
if (typeof store.getRange === 'function') {
|
|
21
|
+
return store.getRange(key, range, opts);
|
|
22
|
+
}
|
|
23
|
+
// Store does not have a native getRange method; falling back to get. This may be inefficient for large data.
|
|
24
|
+
const arr = await store.get(key, opts);
|
|
25
|
+
if (!arr) return undefined;
|
|
26
|
+
const { buffer } = arr;
|
|
27
|
+
if ('suffixLength' in range) {
|
|
28
|
+
const { suffixLength } = range;
|
|
29
|
+
return new Uint8Array(buffer, buffer.byteLength - suffixLength, suffixLength);
|
|
30
|
+
}
|
|
31
|
+
if ('offset' in range && 'length' in range) {
|
|
32
|
+
const { offset, length } = range;
|
|
33
|
+
return new Uint8Array(buffer, offset, length);
|
|
34
|
+
}
|
|
35
|
+
throw new Error('Invalid rangeQuery value.');
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A class-based version of the proxy-based lru() function from vizarr.
|
|
40
|
+
// Reference: https://github.com/hms-dbmi/vizarr/blob/862745c1c7c095748bbe97475da61807d5b49189/src/lru-store.ts
|
|
41
|
+
export class LruStore<S extends AsyncReadable> implements AsyncReadable {
|
|
42
|
+
#inner_store: S;
|
|
43
|
+
|
|
44
|
+
#cache: QuickLRU<string, [Promise<Uint8Array | undefined>, AbortController]>;
|
|
45
|
+
|
|
46
|
+
// We need a way to synchronously peek at the promise state (a-la Bun's peek or Effect's Deferred.poll).
|
|
47
|
+
// We can probably do something more sophisticated but will try this first.
|
|
48
|
+
#promise_states: Map<string, 'pending' | 'fulfilled' | 'rejected'>;
|
|
49
|
+
|
|
50
|
+
constructor(store: S, maxSize = 100) {
|
|
51
|
+
this.#inner_store = store;
|
|
52
|
+
this.#promise_states = new Map();
|
|
53
|
+
this.#cache = new QuickLRU({
|
|
54
|
+
maxSize,
|
|
55
|
+
onEviction: (key, _value) => {
|
|
56
|
+
this.#promise_states.delete(key);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async get(...args: Parameters<S["get"]>): Promise<Uint8Array | undefined> {
|
|
62
|
+
const [key, opts] = args;
|
|
63
|
+
// console.log(`LRU get: ${key}`);
|
|
64
|
+
const cacheKey = normalizeKey(key);
|
|
65
|
+
const cached = this.#cache.get(cacheKey);
|
|
66
|
+
if (cached) {
|
|
67
|
+
return cached[0];
|
|
68
|
+
}
|
|
69
|
+
const controller = new AbortController();
|
|
70
|
+
let getResult = this.#inner_store.get(key, {
|
|
71
|
+
signal: controller.signal,
|
|
72
|
+
...(opts ?? {})
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const getResultPromise = Promise.resolve(getResult);
|
|
76
|
+
this.#promise_states.set(cacheKey, 'pending');
|
|
77
|
+
|
|
78
|
+
const result = getResultPromise.then((val) => {
|
|
79
|
+
this.#promise_states.set(cacheKey, 'fulfilled');
|
|
80
|
+
return val;
|
|
81
|
+
}).catch((err) => {
|
|
82
|
+
this.#promise_states.set(cacheKey, 'rejected');
|
|
83
|
+
this.#cache.delete(cacheKey);
|
|
84
|
+
throw err;
|
|
85
|
+
});
|
|
86
|
+
this.#cache.set(cacheKey, [result, controller]);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Synchronously peek at the promise state.
|
|
91
|
+
getPeek(...args: Parameters<S["get"]>): 'pending' | 'fulfilled' | 'rejected' | undefined {
|
|
92
|
+
this.get(...args); // Kick off the promise but do not await. TODO: do we want to do this here?
|
|
93
|
+
const [key, opts] = args;
|
|
94
|
+
// console.log(`LRU getPeek: ${key}`);
|
|
95
|
+
const cacheKey = normalizeKey(key);
|
|
96
|
+
return this.#promise_states.get(cacheKey);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getRange(...args: Parameters<NonNullable<S["getRange"]>>): Promise<Uint8Array | undefined> {
|
|
100
|
+
const [key, range, opts] = args;
|
|
101
|
+
const cacheKey = normalizeKey(key, range);
|
|
102
|
+
const cached = this.#cache.get(cacheKey);
|
|
103
|
+
if (cached) {
|
|
104
|
+
return cached[0];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const _getRange = typeof this.#inner_store.getRange === 'function'
|
|
108
|
+
? this.#inner_store.getRange.bind(this.#inner_store)
|
|
109
|
+
: createGetRange(this.#inner_store);
|
|
110
|
+
|
|
111
|
+
const controller = new AbortController();
|
|
112
|
+
// @ts-expect-error
|
|
113
|
+
let getRangeResult = _getRange(key, range, {
|
|
114
|
+
signal: controller.signal,
|
|
115
|
+
...(opts ?? {})
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const getRangeResultPromise = Promise.resolve(getRangeResult);
|
|
119
|
+
this.#promise_states.set(cacheKey, 'pending');
|
|
120
|
+
|
|
121
|
+
const result = getRangeResultPromise.then((val) => {
|
|
122
|
+
this.#promise_states.set(cacheKey, 'fulfilled');
|
|
123
|
+
return val;
|
|
124
|
+
}).catch((err) => {
|
|
125
|
+
this.#promise_states.set(cacheKey, 'rejected');
|
|
126
|
+
this.#cache.delete(cacheKey);
|
|
127
|
+
throw err;
|
|
128
|
+
});
|
|
129
|
+
this.#cache.set(cacheKey, [result, controller]);
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Synchronously peek at the promise state.
|
|
134
|
+
getRangePeek(...args: Parameters<NonNullable<S["getRange"]>>): 'pending' | 'fulfilled' | 'rejected' | undefined {
|
|
135
|
+
this.getRange(...args); // Kick off the promise but do not await. TODO: do we want to do this here?
|
|
136
|
+
const [key, range, opts] = args;
|
|
137
|
+
const cacheKey = normalizeKey(key, range);
|
|
138
|
+
return this.#promise_states.get(cacheKey);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
clearCache() {
|
|
142
|
+
// Use AbortSignal in clearCache for promises that have not yet been resolved.
|
|
143
|
+
this.#cache.forEach(([promise, controller]) => {
|
|
144
|
+
// TODO: check if promise is still pending before aborting? Or just always abort?
|
|
145
|
+
// TODO: verify that this aborting is actually working
|
|
146
|
+
controller.abort()
|
|
147
|
+
});
|
|
148
|
+
this.#cache.clear();
|
|
149
|
+
this.#promise_states = new Map();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function lru(inner_store: AsyncReadable, maxSize = 100): LruStore<AsyncReadable> {
|
|
154
|
+
return new LruStore(inner_store, maxSize);
|
|
155
|
+
}
|