@pluot/react 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 +8244 -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 +223 -0
- package/dist-tsc/Pluot.d.ts +2 -0
- package/dist-tsc/Pluot.d.ts.map +1 -0
- package/dist-tsc/Pluot.js +417 -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 +303 -0
- package/dist-tsc/feature-detection.d.ts +5 -0
- package/dist-tsc/feature-detection.d.ts.map +1 -0
- package/dist-tsc/feature-detection.js +6 -0
- package/dist-tsc/index.d.ts +3 -0
- package/dist-tsc/index.d.ts.map +1 -0
- package/dist-tsc/index.js +2 -0
- package/dist-tsc/lru-store.d.ts +4 -0
- package/dist-tsc/lru-store.d.ts.map +1 -0
- package/dist-tsc/lru-store.js +137 -0
- package/package.json +59 -0
- package/src/Pluot.jsx +560 -0
- package/src/index.js +2 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default dom2dCamera;
|
|
2
|
+
declare function dom2dCamera(element: any, { distance, target, rotation, isNdc, isFixed, isPan, isPanInverted, panSpeed, isRotate, rotateSpeed, defaultMouseDownMoveAction, mouseDownMoveModKey, isZoom, zoomSpeed, viewCenter, scaleBounds, translationBounds, onKeyDown, onKeyUp, onMouseDown, onMouseUp, onMouseMove, onWheel, aspectRatioMode, aspectRatioAlignmentMode, }?: {
|
|
3
|
+
distance?: number | undefined;
|
|
4
|
+
target?: number[] | undefined;
|
|
5
|
+
rotation?: number | undefined;
|
|
6
|
+
isNdc?: boolean | undefined;
|
|
7
|
+
isFixed?: boolean | undefined;
|
|
8
|
+
isPan?: boolean | undefined;
|
|
9
|
+
isPanInverted?: boolean[] | undefined;
|
|
10
|
+
panSpeed?: number | undefined;
|
|
11
|
+
isRotate?: boolean | undefined;
|
|
12
|
+
rotateSpeed?: number | undefined;
|
|
13
|
+
defaultMouseDownMoveAction?: string | undefined;
|
|
14
|
+
mouseDownMoveModKey?: string | undefined;
|
|
15
|
+
isZoom?: boolean | undefined;
|
|
16
|
+
zoomSpeed?: number | undefined;
|
|
17
|
+
onKeyDown?: (() => void) | undefined;
|
|
18
|
+
onKeyUp?: (() => void) | undefined;
|
|
19
|
+
onMouseDown?: (() => void) | undefined;
|
|
20
|
+
onMouseUp?: (() => void) | undefined;
|
|
21
|
+
onMouseMove?: (() => void) | undefined;
|
|
22
|
+
onWheel?: (() => void) | undefined;
|
|
23
|
+
aspectRatioMode?: string | undefined;
|
|
24
|
+
aspectRatioAlignmentMode?: string | undefined;
|
|
25
|
+
}): any;
|
|
26
|
+
//# sourceMappingURL=dom-2d-camera.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dom-2d-camera.d.ts","sourceRoot":"","sources":["../src/dom-2d-camera.js"],"names":[],"mappings":";AAeA;;;;;;;;;;;;;;;;;;;;;;;QAkZC"}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// Copied from https://github.com/flekschas/dom-2d-camera/blob/cd59ea035a0ea72c2c0535fa3721f8127946576c/src/index.js
|
|
2
|
+
// TODO: fork and make PR if the changes are successful
|
|
3
|
+
import { vec2 } from "gl-matrix";
|
|
4
|
+
import createCamera from "camera-2d-simple";
|
|
5
|
+
const MOUSE_DOWN_MOVE_ACTIONS = ["pan", "rotate"];
|
|
6
|
+
const KEY_MAP = {
|
|
7
|
+
alt: "altKey",
|
|
8
|
+
cmd: "metaKey",
|
|
9
|
+
ctrl: "ctrlKey",
|
|
10
|
+
meta: "metaKey",
|
|
11
|
+
shift: "shiftKey"
|
|
12
|
+
};
|
|
13
|
+
const dom2dCamera = (element, { distance = 1.0, target = [0, 0], rotation = 0, isNdc = true, isFixed = false, isPan = true, isPanInverted = [false, true], panSpeed = 1, isRotate = true, rotateSpeed = 1, defaultMouseDownMoveAction = "pan", mouseDownMoveModKey = "alt", isZoom = true, zoomSpeed = 1, viewCenter, scaleBounds, translationBounds, onKeyDown = () => { }, onKeyUp = () => { }, onMouseDown = () => { }, onMouseUp = () => { }, onMouseMove = () => { }, onWheel = () => { }, aspectRatioMode = "Ignore", aspectRatioAlignmentMode = "Center", } = {}) => {
|
|
14
|
+
let camera = createCamera(target, distance, rotation, viewCenter, scaleBounds, translationBounds);
|
|
15
|
+
let mouseX = 0;
|
|
16
|
+
let mouseY = 0;
|
|
17
|
+
let mouseRelX = 0;
|
|
18
|
+
let mouseRelY = 0;
|
|
19
|
+
let prevMouseX = 0;
|
|
20
|
+
let prevMouseY = 0;
|
|
21
|
+
let isLeftMousePressed = false;
|
|
22
|
+
let scrollDist = 0;
|
|
23
|
+
let width = 1;
|
|
24
|
+
let height = 1;
|
|
25
|
+
let aspectRatio = 1;
|
|
26
|
+
let isInteractivelyChanged = false;
|
|
27
|
+
let isProgrammaticallyChanged = false;
|
|
28
|
+
let isMouseDownMoveModActive = false;
|
|
29
|
+
let panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
|
|
30
|
+
let isPanX = isPan;
|
|
31
|
+
let isPanY = isPan;
|
|
32
|
+
let isPanXInverted = isPanInverted;
|
|
33
|
+
let isPanYInverted = isPanInverted;
|
|
34
|
+
let isZoomX = isZoom;
|
|
35
|
+
let isZoomY = isZoom;
|
|
36
|
+
const spreadXYSettings = () => {
|
|
37
|
+
isPanX = Array.isArray(isPan) ? Boolean(isPan[0]) : isPan;
|
|
38
|
+
isPanY = Array.isArray(isPan) ? Boolean(isPan[1]) : isPan;
|
|
39
|
+
isPanXInverted = Array.isArray(isPanInverted)
|
|
40
|
+
? Boolean(isPanInverted[0])
|
|
41
|
+
: isPanInverted;
|
|
42
|
+
isPanYInverted = Array.isArray(isPanInverted)
|
|
43
|
+
? Boolean(isPanInverted[1])
|
|
44
|
+
: isPanInverted;
|
|
45
|
+
isZoomX = Array.isArray(isZoom) ? Boolean(isZoom[0]) : isZoom;
|
|
46
|
+
isZoomY = Array.isArray(isZoom) ? Boolean(isZoom[1]) : isZoom;
|
|
47
|
+
};
|
|
48
|
+
spreadXYSettings();
|
|
49
|
+
let xAspectRatioModeFactor = 1.0;
|
|
50
|
+
let yAspectRatioModeFactor = 1.0;
|
|
51
|
+
let xAlignmentTranslation = 0.0;
|
|
52
|
+
let yAlignmentTranslation = 0.0;
|
|
53
|
+
/*
|
|
54
|
+
// Logic for aspect ratio handling in point_layer.wgsl
|
|
55
|
+
if (aspect_ratio_mode == 1u) {
|
|
56
|
+
// fit/contain
|
|
57
|
+
if (layer_aspect_ratio > 1.0) {
|
|
58
|
+
// Wide rectangle
|
|
59
|
+
// Show more than (0, 1) in x direction. Show exactly (0, 1) in y direction.
|
|
60
|
+
x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
|
|
61
|
+
} else if(layer_aspect_ratio < 1.0) {
|
|
62
|
+
// Tall layer
|
|
63
|
+
// Show exactly (0, 1) in x direction. Show more than (0, 1) in y direction.
|
|
64
|
+
y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
|
|
65
|
+
} else {
|
|
66
|
+
// Square layer; no change needed.
|
|
67
|
+
// Show exactly (0, 1) in both directions.
|
|
68
|
+
}
|
|
69
|
+
} else if (aspect_ratio_mode == 2u) {
|
|
70
|
+
// fill/cover
|
|
71
|
+
if(layer_aspect_ratio > 1.0) {
|
|
72
|
+
// Wide rectangle
|
|
73
|
+
// Show exactly (0, 1) in x direction. Show less than (0, 1) in y direction.
|
|
74
|
+
y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
|
|
75
|
+
} else if(layer_aspect_ratio < 1.0) {
|
|
76
|
+
// Tall layer
|
|
77
|
+
// Show less than (0, 1) in x direction. Show exactly (0, 1) in y direction.
|
|
78
|
+
x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
|
|
79
|
+
} else {
|
|
80
|
+
// Square layer; no change needed.
|
|
81
|
+
// Show exactly (0, 1) in both directions.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
*/
|
|
85
|
+
const updateAspectRatioModeFactors = () => {
|
|
86
|
+
xAspectRatioModeFactor = 1.0;
|
|
87
|
+
yAspectRatioModeFactor = 1.0;
|
|
88
|
+
if (aspectRatioMode === "Contain") {
|
|
89
|
+
if (aspectRatio > 1.0) {
|
|
90
|
+
xAspectRatioModeFactor = 1.0 / aspectRatio;
|
|
91
|
+
}
|
|
92
|
+
else if (aspectRatio < 1.0) {
|
|
93
|
+
yAspectRatioModeFactor = aspectRatio;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else if (aspectRatioMode === "Cover") {
|
|
97
|
+
if (aspectRatio > 1.0) {
|
|
98
|
+
yAspectRatioModeFactor = aspectRatio;
|
|
99
|
+
}
|
|
100
|
+
else if (aspectRatio < 1.0) {
|
|
101
|
+
xAspectRatioModeFactor = 1.0 / aspectRatio;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
xAlignmentTranslation = 0.0;
|
|
105
|
+
yAlignmentTranslation = 0.0;
|
|
106
|
+
if (aspectRatioAlignmentMode === "Start") {
|
|
107
|
+
xAlignmentTranslation = xAspectRatioModeFactor - 1.0;
|
|
108
|
+
yAlignmentTranslation = yAspectRatioModeFactor - 1.0;
|
|
109
|
+
}
|
|
110
|
+
else if (aspectRatioAlignmentMode === "End") {
|
|
111
|
+
xAlignmentTranslation = 1.0 - xAspectRatioModeFactor;
|
|
112
|
+
yAlignmentTranslation = 1.0 - yAspectRatioModeFactor;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
updateAspectRatioModeFactors();
|
|
116
|
+
const transformPanX = isNdc
|
|
117
|
+
? dX => (dX / width) * 2 * (1.0 / xAspectRatioModeFactor) // to normalized device coords
|
|
118
|
+
: dX => dX;
|
|
119
|
+
const transformPanY = isNdc
|
|
120
|
+
? dY => (dY / height) * 2 * (1.0 / yAspectRatioModeFactor) // to normalized device coords
|
|
121
|
+
: dY => -dY;
|
|
122
|
+
const transformScaleX = isNdc
|
|
123
|
+
? x => ((-1 + (x / width) * 2) - xAlignmentTranslation) * (1.0 / xAspectRatioModeFactor)
|
|
124
|
+
: x => x;
|
|
125
|
+
const transformScaleY = isNdc
|
|
126
|
+
? y => ((1 - (y / height) * 2) - yAlignmentTranslation) * (1.0 / yAspectRatioModeFactor)
|
|
127
|
+
: y => y;
|
|
128
|
+
const tick = () => {
|
|
129
|
+
if (isFixed) {
|
|
130
|
+
const isChanged = isProgrammaticallyChanged;
|
|
131
|
+
isProgrammaticallyChanged = false;
|
|
132
|
+
return isChanged;
|
|
133
|
+
}
|
|
134
|
+
isInteractivelyChanged = false;
|
|
135
|
+
const currentMouseX = mouseX;
|
|
136
|
+
const currentMouseY = mouseY;
|
|
137
|
+
if ((isPanX || isPanY) &&
|
|
138
|
+
isLeftMousePressed &&
|
|
139
|
+
((panOnMouseDownMove && !isMouseDownMoveModActive) ||
|
|
140
|
+
(!panOnMouseDownMove && isMouseDownMoveModActive))) {
|
|
141
|
+
const dX = isPanXInverted
|
|
142
|
+
? prevMouseX - currentMouseX
|
|
143
|
+
: currentMouseX - prevMouseX;
|
|
144
|
+
const transformedPanX = isPanX ? transformPanX(panSpeed * dX) : 0;
|
|
145
|
+
const dY = isPanYInverted
|
|
146
|
+
? prevMouseY - currentMouseY
|
|
147
|
+
: currentMouseY - prevMouseY;
|
|
148
|
+
const transformedPanY = isPanY ? transformPanY(panSpeed * dY) : 0;
|
|
149
|
+
if (transformedPanX !== 0 || transformedPanY !== 0) {
|
|
150
|
+
camera.pan([transformedPanX, transformedPanY]);
|
|
151
|
+
isInteractivelyChanged = true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if ((isZoomX || isZoomY) && scrollDist) {
|
|
155
|
+
const dZ = zoomSpeed * Math.exp(scrollDist / height);
|
|
156
|
+
const transformedX = transformScaleX(mouseRelX);
|
|
157
|
+
const transformedY = transformScaleY(mouseRelY);
|
|
158
|
+
camera.scale([isZoomX ? 1 / dZ : 1, isZoomY ? 1 / dZ : 1], [transformedX, transformedY]);
|
|
159
|
+
isInteractivelyChanged = true;
|
|
160
|
+
}
|
|
161
|
+
if (isRotate &&
|
|
162
|
+
isLeftMousePressed &&
|
|
163
|
+
((panOnMouseDownMove && isMouseDownMoveModActive) ||
|
|
164
|
+
(!panOnMouseDownMove && !isMouseDownMoveModActive)) &&
|
|
165
|
+
Math.abs(prevMouseX - currentMouseX) +
|
|
166
|
+
Math.abs(prevMouseY - currentMouseY) >
|
|
167
|
+
0) {
|
|
168
|
+
const wh = width / 2;
|
|
169
|
+
const hh = height / 2;
|
|
170
|
+
const x1 = prevMouseX - wh;
|
|
171
|
+
const y1 = hh - prevMouseY;
|
|
172
|
+
const x2 = currentMouseX - wh;
|
|
173
|
+
const y2 = hh - currentMouseY;
|
|
174
|
+
// Angle between the start and end mouse position with respect to the
|
|
175
|
+
// viewport center
|
|
176
|
+
const radians = vec2.angle([x1, y1], [x2, y2]);
|
|
177
|
+
// Determine the orientation
|
|
178
|
+
const cross = x1 * y2 - x2 * y1;
|
|
179
|
+
camera.rotate(rotateSpeed * radians * Math.sign(cross));
|
|
180
|
+
isInteractivelyChanged = true;
|
|
181
|
+
}
|
|
182
|
+
// Reset scroll delta and mouse position
|
|
183
|
+
scrollDist = 0;
|
|
184
|
+
prevMouseX = currentMouseX;
|
|
185
|
+
prevMouseY = currentMouseY;
|
|
186
|
+
const isChanged = isInteractivelyChanged || isProgrammaticallyChanged;
|
|
187
|
+
isProgrammaticallyChanged = false;
|
|
188
|
+
return isChanged;
|
|
189
|
+
};
|
|
190
|
+
const config = ({ defaultMouseDownMoveAction: newDefaultMouseDownMoveAction = null, isFixed: newIsFixed = null, isPan: newIsPan = null, isPanInverted: newIsPanInverted = null, isRotate: newIsRotate = null, isZoom: newIsZoom = null, panSpeed: newPanSpeed = null, rotateSpeed: newRotateSpeed = null, zoomSpeed: newZoomSpeed = null, mouseDownMoveModKey: newMouseDownMoveModKey = null } = {}) => {
|
|
191
|
+
defaultMouseDownMoveAction =
|
|
192
|
+
newDefaultMouseDownMoveAction !== null &&
|
|
193
|
+
MOUSE_DOWN_MOVE_ACTIONS.includes(newDefaultMouseDownMoveAction)
|
|
194
|
+
? newDefaultMouseDownMoveAction
|
|
195
|
+
: defaultMouseDownMoveAction;
|
|
196
|
+
panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
|
|
197
|
+
isFixed = newIsFixed !== null ? newIsFixed : isFixed;
|
|
198
|
+
isPan = newIsPan !== null ? newIsPan : isPan;
|
|
199
|
+
isPanInverted =
|
|
200
|
+
newIsPanInverted !== null ? newIsPanInverted : isPanInverted;
|
|
201
|
+
isRotate = newIsRotate !== null ? newIsRotate : isRotate;
|
|
202
|
+
isZoom = newIsZoom !== null ? newIsZoom : isZoom;
|
|
203
|
+
panSpeed = +newPanSpeed > 0 ? newPanSpeed : panSpeed;
|
|
204
|
+
rotateSpeed = +newRotateSpeed > 0 ? newRotateSpeed : rotateSpeed;
|
|
205
|
+
zoomSpeed = +newZoomSpeed > 0 ? newZoomSpeed : zoomSpeed;
|
|
206
|
+
spreadXYSettings();
|
|
207
|
+
mouseDownMoveModKey =
|
|
208
|
+
newMouseDownMoveModKey !== null &&
|
|
209
|
+
Object.keys(KEY_MAP).includes(newMouseDownMoveModKey)
|
|
210
|
+
? newMouseDownMoveModKey
|
|
211
|
+
: mouseDownMoveModKey;
|
|
212
|
+
};
|
|
213
|
+
const refresh = () => {
|
|
214
|
+
const bBox = element.getBoundingClientRect();
|
|
215
|
+
width = bBox.width;
|
|
216
|
+
height = bBox.height;
|
|
217
|
+
aspectRatio = width / height;
|
|
218
|
+
updateAspectRatioModeFactors();
|
|
219
|
+
};
|
|
220
|
+
const keyUpHandler = event => {
|
|
221
|
+
isMouseDownMoveModActive = false;
|
|
222
|
+
onKeyUp(event);
|
|
223
|
+
};
|
|
224
|
+
const keyDownHandler = event => {
|
|
225
|
+
isMouseDownMoveModActive = event[KEY_MAP[mouseDownMoveModKey]];
|
|
226
|
+
onKeyDown(event);
|
|
227
|
+
};
|
|
228
|
+
const mouseUpHandler = event => {
|
|
229
|
+
isLeftMousePressed = false;
|
|
230
|
+
onMouseUp(event);
|
|
231
|
+
};
|
|
232
|
+
const mouseDownHandler = event => {
|
|
233
|
+
isLeftMousePressed = event.buttons === 1;
|
|
234
|
+
onMouseDown(event);
|
|
235
|
+
};
|
|
236
|
+
const offsetXSupport = document.createEvent("MouseEvent").offsetX !== undefined;
|
|
237
|
+
const updateMouseRelXY = offsetXSupport
|
|
238
|
+
? event => {
|
|
239
|
+
mouseRelX = event.offsetX;
|
|
240
|
+
mouseRelY = event.offsetY;
|
|
241
|
+
}
|
|
242
|
+
: event => {
|
|
243
|
+
const bBox = element.getBoundingClientRect();
|
|
244
|
+
mouseRelX = event.clientX - bBox.left;
|
|
245
|
+
mouseRelY = event.clientY - bBox.top;
|
|
246
|
+
};
|
|
247
|
+
const updateMouseXY = event => {
|
|
248
|
+
mouseX = event.clientX;
|
|
249
|
+
mouseY = event.clientY;
|
|
250
|
+
};
|
|
251
|
+
const mouseMoveHandler = event => {
|
|
252
|
+
updateMouseXY(event);
|
|
253
|
+
onMouseMove(event);
|
|
254
|
+
};
|
|
255
|
+
const wheelHandler = event => {
|
|
256
|
+
if ((isZoomX || isZoomY) && !isFixed) {
|
|
257
|
+
event.preventDefault();
|
|
258
|
+
updateMouseXY(event);
|
|
259
|
+
updateMouseRelXY(event);
|
|
260
|
+
const scale = event.deltaMode === 1 ? 12 : 1;
|
|
261
|
+
scrollDist += scale * (event.deltaY || event.deltaX || 0);
|
|
262
|
+
}
|
|
263
|
+
onWheel(event);
|
|
264
|
+
};
|
|
265
|
+
const dispose = () => {
|
|
266
|
+
camera = undefined;
|
|
267
|
+
element.removeEventListener("keydown", keyDownHandler);
|
|
268
|
+
element.removeEventListener("keyup", keyUpHandler);
|
|
269
|
+
element.removeEventListener("mousedown", mouseDownHandler);
|
|
270
|
+
element.removeEventListener("mouseup", mouseUpHandler);
|
|
271
|
+
element.removeEventListener("mousemove", mouseMoveHandler);
|
|
272
|
+
element.removeEventListener("wheel", wheelHandler);
|
|
273
|
+
};
|
|
274
|
+
element.addEventListener("keydown", keyDownHandler, { passive: true });
|
|
275
|
+
element.addEventListener("keyup", keyUpHandler, { passive: true });
|
|
276
|
+
element.addEventListener("mousedown", mouseDownHandler, { passive: true });
|
|
277
|
+
element.addEventListener("mouseup", mouseUpHandler, { passive: true });
|
|
278
|
+
element.addEventListener("mousemove", mouseMoveHandler, { passive: true });
|
|
279
|
+
element.addEventListener("wheel", wheelHandler, { passive: false });
|
|
280
|
+
camera.config = config;
|
|
281
|
+
camera.dispose = dispose;
|
|
282
|
+
camera.refresh = refresh;
|
|
283
|
+
camera.tick = tick;
|
|
284
|
+
const withProgrammaticChange = fn => function () {
|
|
285
|
+
fn.apply(null, arguments);
|
|
286
|
+
isProgrammaticallyChanged = true;
|
|
287
|
+
};
|
|
288
|
+
camera.lookAt = withProgrammaticChange(camera.lookAt);
|
|
289
|
+
camera.translate = withProgrammaticChange(camera.translate);
|
|
290
|
+
camera.pan = withProgrammaticChange(camera.pan);
|
|
291
|
+
camera.rotate = withProgrammaticChange(camera.rotate);
|
|
292
|
+
camera.scale = withProgrammaticChange(camera.scale);
|
|
293
|
+
camera.zoom = withProgrammaticChange(camera.zoom);
|
|
294
|
+
camera.reset = withProgrammaticChange(camera.reset);
|
|
295
|
+
camera.set = withProgrammaticChange(camera.set);
|
|
296
|
+
camera.setScaleBounds = withProgrammaticChange(camera.setScaleBounds);
|
|
297
|
+
camera.setTranslationBounds = withProgrammaticChange(camera.setTranslationBounds);
|
|
298
|
+
camera.setView = withProgrammaticChange(camera.setView);
|
|
299
|
+
camera.setViewCenter = withProgrammaticChange(camera.setViewCenter);
|
|
300
|
+
refresh();
|
|
301
|
+
return camera;
|
|
302
|
+
};
|
|
303
|
+
export default dom2dCamera;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-detection.d.ts","sourceRoot":"","sources":["../src/feature-detection.js"],"names":[],"mappings":"AAGA;;;EAGC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { checkWebGpuFeatureDetection } from "@pluot/core";
|
|
3
|
+
export function useWebGpuFeatureDetection() {
|
|
4
|
+
const [supportsWebGpu, supportsWebGpuMessage] = useMemo(checkWebGpuFeatureDetection, []);
|
|
5
|
+
return { supportsWebGpu, supportsWebGpuMessage };
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AsyncReadable } from 'zarrita';
|
|
2
|
+
export declare function createGetRange<S extends AsyncReadable>(store: S): (...args: Parameters<NonNullable<S["getRange"]>>) => Promise<Uint8Array | undefined>;
|
|
3
|
+
export declare function lru(inner_store: AsyncReadable, maxSize?: number): AsyncReadable;
|
|
4
|
+
//# sourceMappingURL=lru-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lru-store.d.ts","sourceRoot":"","sources":["../src/lru-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAc,aAAa,EAAgB,MAAM,SAAS,CAAC;AAavE,wBAAgB,cAAc,CAAC,CAAC,SAAS,aAAa,EAAE,KAAK,EAAE,CAAC,IAEhD,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAmBhG;AAoHD,wBAAgB,GAAG,CAAC,WAAW,EAAE,aAAa,EAAE,OAAO,SAAM,GAAG,aAAa,CAE5E"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import QuickLRU from "quick-lru";
|
|
2
|
+
function normalizeKey(key, range) {
|
|
3
|
+
if (!range)
|
|
4
|
+
return key;
|
|
5
|
+
if ("suffixLength" in range)
|
|
6
|
+
return `${key}:-${range.suffixLength}`;
|
|
7
|
+
return `${key}:${range.offset}:${range.offset + range.length - 1}`;
|
|
8
|
+
}
|
|
9
|
+
// Provides a blanket implementation of getRange that can be used with any AsyncReadable store,
|
|
10
|
+
// even if it doesn't define a getRange method.
|
|
11
|
+
// If the store does have a native getRange method, we use that instead.
|
|
12
|
+
// Reference: https://github.com/vitessce/vitessce/blob/main/packages/utils/zarr-utils/src/base-getrange.ts
|
|
13
|
+
export function createGetRange(store) {
|
|
14
|
+
// TODO: support options param for getRange?
|
|
15
|
+
return async (...args) => {
|
|
16
|
+
const [key, range, opts] = args;
|
|
17
|
+
if (typeof store.getRange === 'function') {
|
|
18
|
+
return store.getRange(key, range, opts);
|
|
19
|
+
}
|
|
20
|
+
// Store does not have a native getRange method; falling back to get. This may be inefficient for large data.
|
|
21
|
+
const arr = await store.get(key, opts);
|
|
22
|
+
if (!arr)
|
|
23
|
+
return undefined;
|
|
24
|
+
const { buffer } = arr;
|
|
25
|
+
if ('suffixLength' in range) {
|
|
26
|
+
const { suffixLength } = range;
|
|
27
|
+
return new Uint8Array(buffer, buffer.byteLength - suffixLength, suffixLength);
|
|
28
|
+
}
|
|
29
|
+
if ('offset' in range && 'length' in range) {
|
|
30
|
+
const { offset, length } = range;
|
|
31
|
+
return new Uint8Array(buffer, offset, length);
|
|
32
|
+
}
|
|
33
|
+
throw new Error('Invalid rangeQuery value.');
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// A class-based version of the proxy-based lru() function from vizarr.
|
|
37
|
+
// Reference: https://github.com/hms-dbmi/vizarr/blob/862745c1c7c095748bbe97475da61807d5b49189/src/lru-store.ts
|
|
38
|
+
class LruStore {
|
|
39
|
+
#inner_store;
|
|
40
|
+
#cache;
|
|
41
|
+
// We need a way to synchronously peek at the promise state (a-la Bun's peek or Effect's Deferred.poll).
|
|
42
|
+
// We can probably do something more sophisticated but will try this first.
|
|
43
|
+
#promise_states;
|
|
44
|
+
constructor(store, maxSize = 100) {
|
|
45
|
+
this.#inner_store = store;
|
|
46
|
+
this.#promise_states = new Map();
|
|
47
|
+
this.#cache = new QuickLRU({
|
|
48
|
+
maxSize,
|
|
49
|
+
onEviction: (key, _value) => {
|
|
50
|
+
this.#promise_states.delete(key);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async get(...args) {
|
|
55
|
+
const [key, opts] = args;
|
|
56
|
+
// console.log(`LRU get: ${key}`);
|
|
57
|
+
const cacheKey = normalizeKey(key);
|
|
58
|
+
const cached = this.#cache.get(cacheKey);
|
|
59
|
+
if (cached) {
|
|
60
|
+
return cached[0];
|
|
61
|
+
}
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
let getResult = this.#inner_store.get(key, {
|
|
64
|
+
signal: controller.signal,
|
|
65
|
+
...(opts ?? {})
|
|
66
|
+
});
|
|
67
|
+
const getResultPromise = Promise.resolve(getResult);
|
|
68
|
+
this.#promise_states.set(cacheKey, 'pending');
|
|
69
|
+
const result = getResultPromise.then((val) => {
|
|
70
|
+
this.#promise_states.set(cacheKey, 'fulfilled');
|
|
71
|
+
return val;
|
|
72
|
+
}).catch((err) => {
|
|
73
|
+
this.#promise_states.set(cacheKey, 'rejected');
|
|
74
|
+
this.#cache.delete(cacheKey);
|
|
75
|
+
throw err;
|
|
76
|
+
});
|
|
77
|
+
this.#cache.set(cacheKey, [result, controller]);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
// Synchronously peek at the promise state.
|
|
81
|
+
getPeek(...args) {
|
|
82
|
+
this.get(...args); // Kick off the promise but do not await. TODO: do we want to do this here?
|
|
83
|
+
const [key, opts] = args;
|
|
84
|
+
// console.log(`LRU getPeek: ${key}`);
|
|
85
|
+
const cacheKey = normalizeKey(key);
|
|
86
|
+
return this.#promise_states.get(cacheKey);
|
|
87
|
+
}
|
|
88
|
+
async getRange(...args) {
|
|
89
|
+
const [key, range, opts] = args;
|
|
90
|
+
const cacheKey = normalizeKey(key, range);
|
|
91
|
+
const cached = this.#cache.get(cacheKey);
|
|
92
|
+
if (cached) {
|
|
93
|
+
return cached[0];
|
|
94
|
+
}
|
|
95
|
+
const _getRange = typeof this.#inner_store.getRange === 'function'
|
|
96
|
+
? this.#inner_store.getRange.bind(this.#inner_store)
|
|
97
|
+
: createGetRange(this.#inner_store);
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
// @ts-expect-error
|
|
100
|
+
let getRangeResult = _getRange(key, range, {
|
|
101
|
+
signal: controller.signal,
|
|
102
|
+
...(opts ?? {})
|
|
103
|
+
});
|
|
104
|
+
const getRangeResultPromise = Promise.resolve(getRangeResult);
|
|
105
|
+
this.#promise_states.set(cacheKey, 'pending');
|
|
106
|
+
const result = getRangeResultPromise.then((val) => {
|
|
107
|
+
this.#promise_states.set(cacheKey, 'fulfilled');
|
|
108
|
+
return val;
|
|
109
|
+
}).catch((err) => {
|
|
110
|
+
this.#promise_states.set(cacheKey, 'rejected');
|
|
111
|
+
this.#cache.delete(cacheKey);
|
|
112
|
+
throw err;
|
|
113
|
+
});
|
|
114
|
+
this.#cache.set(cacheKey, [result, controller]);
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
// Synchronously peek at the promise state.
|
|
118
|
+
getRangePeek(...args) {
|
|
119
|
+
this.getRange(...args); // Kick off the promise but do not await. TODO: do we want to do this here?
|
|
120
|
+
const [key, range, opts] = args;
|
|
121
|
+
const cacheKey = normalizeKey(key, range);
|
|
122
|
+
return this.#promise_states.get(cacheKey);
|
|
123
|
+
}
|
|
124
|
+
clearCache() {
|
|
125
|
+
// Use AbortSignal in clearCache for promises that have not yet been resolved.
|
|
126
|
+
this.#cache.forEach(([promise, controller]) => {
|
|
127
|
+
// TODO: check if promise is still pending before aborting? Or just always abort?
|
|
128
|
+
// TODO: verify that this aborting is actually working
|
|
129
|
+
controller.abort();
|
|
130
|
+
});
|
|
131
|
+
this.#cache.clear();
|
|
132
|
+
this.#promise_states = new Map();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export function lru(inner_store, maxSize = 100) {
|
|
136
|
+
return new LruStore(inner_store, maxSize);
|
|
137
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluot/react",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"dist",
|
|
12
|
+
"dist-tsc"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"lightua": "0.1.0",
|
|
16
|
+
"zarrita": "^0.5.3",
|
|
17
|
+
"quick-lru": "^7.0.1",
|
|
18
|
+
"d3-zoom": "^3.0.0",
|
|
19
|
+
"d3-selection": "^3.0.0",
|
|
20
|
+
"d3-scale": "^4.0.2",
|
|
21
|
+
"d3-drag": "^3.0.0",
|
|
22
|
+
"dom-2d-camera": "^2.2.6",
|
|
23
|
+
"camera-2d-simple": "^3.0.0",
|
|
24
|
+
"gl-matrix": "^3.4.4",
|
|
25
|
+
"3d-view-controls": "^2.2.2",
|
|
26
|
+
"3d-view": "^2.0.1",
|
|
27
|
+
"has-passive-events": "^1.0.0",
|
|
28
|
+
"mouse-change": "^1.1.1",
|
|
29
|
+
"mouse-event-offset": "^3.0.2",
|
|
30
|
+
"mouse-wheel": "^1.0.2",
|
|
31
|
+
"lz-string": "^1.5.0",
|
|
32
|
+
"lodash-es": "^4.17.23",
|
|
33
|
+
"@pluot/core": "0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"vitest": "^3.2.4",
|
|
37
|
+
"vite": "^6.1.1",
|
|
38
|
+
"@vitejs/plugin-react": "^5.0.0",
|
|
39
|
+
"jsdom": "^26.1.0",
|
|
40
|
+
"vitest-canvas-mock": "^0.3.3",
|
|
41
|
+
"typescript": "^5.9"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": "^19.0.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"start": "pnpm -C ../../ run start-tsc",
|
|
48
|
+
"build": "pnpm -C ../../ run build-tsc",
|
|
49
|
+
"bundle": "pnpm exec vite build -c ./vite.config.js",
|
|
50
|
+
"test": "pnpm exec vitest --run -r ../../ --dir ."
|
|
51
|
+
},
|
|
52
|
+
"module": "dist/index.js",
|
|
53
|
+
"exports": {
|
|
54
|
+
".": {
|
|
55
|
+
"types": "./dist-tsc/index.d.ts",
|
|
56
|
+
"import": "./dist/index.js"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|