@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.
@@ -0,0 +1,323 @@
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
+ // Reference: https://github.com/flekschas/dom-2d-camera/blob/cd59ea035a0ea72c2c0535fa3721f8127946576c/src/index.js
22
+ // TODO: contribute modifications back upstream once things are working.
23
+ import { vec2 } from "gl-matrix";
24
+ import createCamera from "camera-2d-simple";
25
+ const MOUSE_DOWN_MOVE_ACTIONS = ["pan", "rotate"];
26
+ const KEY_MAP = {
27
+ alt: "altKey",
28
+ cmd: "metaKey",
29
+ ctrl: "ctrlKey",
30
+ meta: "metaKey",
31
+ shift: "shiftKey"
32
+ };
33
+ 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", } = {}) => {
34
+ let camera = createCamera(target, distance, rotation, viewCenter, scaleBounds, translationBounds);
35
+ let mouseX = 0;
36
+ let mouseY = 0;
37
+ let mouseRelX = 0;
38
+ let mouseRelY = 0;
39
+ let prevMouseX = 0;
40
+ let prevMouseY = 0;
41
+ let isLeftMousePressed = false;
42
+ let scrollDist = 0;
43
+ let width = 1;
44
+ let height = 1;
45
+ let aspectRatio = 1;
46
+ let isInteractivelyChanged = false;
47
+ let isProgrammaticallyChanged = false;
48
+ let isMouseDownMoveModActive = false;
49
+ let panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
50
+ let isPanX = isPan;
51
+ let isPanY = isPan;
52
+ let isPanXInverted = isPanInverted;
53
+ let isPanYInverted = isPanInverted;
54
+ let isZoomX = isZoom;
55
+ let isZoomY = isZoom;
56
+ const spreadXYSettings = () => {
57
+ isPanX = Array.isArray(isPan) ? Boolean(isPan[0]) : isPan;
58
+ isPanY = Array.isArray(isPan) ? Boolean(isPan[1]) : isPan;
59
+ isPanXInverted = Array.isArray(isPanInverted)
60
+ ? Boolean(isPanInverted[0])
61
+ : isPanInverted;
62
+ isPanYInverted = Array.isArray(isPanInverted)
63
+ ? Boolean(isPanInverted[1])
64
+ : isPanInverted;
65
+ isZoomX = Array.isArray(isZoom) ? Boolean(isZoom[0]) : isZoom;
66
+ isZoomY = Array.isArray(isZoom) ? Boolean(isZoom[1]) : isZoom;
67
+ };
68
+ spreadXYSettings();
69
+ let xAspectRatioModeFactor = 1.0;
70
+ let yAspectRatioModeFactor = 1.0;
71
+ let xAlignmentTranslation = 0.0;
72
+ let yAlignmentTranslation = 0.0;
73
+ /*
74
+ // Logic for aspect ratio handling in point_layer.wgsl
75
+ if (aspect_ratio_mode == 1u) {
76
+ // fit/contain
77
+ if (layer_aspect_ratio > 1.0) {
78
+ // Wide rectangle
79
+ // Show more than (0, 1) in x direction. Show exactly (0, 1) in y direction.
80
+ x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
81
+ } else if(layer_aspect_ratio < 1.0) {
82
+ // Tall layer
83
+ // Show exactly (0, 1) in x direction. Show more than (0, 1) in y direction.
84
+ y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
85
+ } else {
86
+ // Square layer; no change needed.
87
+ // Show exactly (0, 1) in both directions.
88
+ }
89
+ } else if (aspect_ratio_mode == 2u) {
90
+ // fill/cover
91
+ if(layer_aspect_ratio > 1.0) {
92
+ // Wide rectangle
93
+ // Show exactly (0, 1) in x direction. Show less than (0, 1) in y direction.
94
+ y_scale_for_aspect_ratio_mode = layer_aspect_ratio;
95
+ } else if(layer_aspect_ratio < 1.0) {
96
+ // Tall layer
97
+ // Show less than (0, 1) in x direction. Show exactly (0, 1) in y direction.
98
+ x_scale_for_aspect_ratio_mode = 1.0 / layer_aspect_ratio;
99
+ } else {
100
+ // Square layer; no change needed.
101
+ // Show exactly (0, 1) in both directions.
102
+ }
103
+ }
104
+ */
105
+ const updateAspectRatioModeFactors = () => {
106
+ xAspectRatioModeFactor = 1.0;
107
+ yAspectRatioModeFactor = 1.0;
108
+ if (aspectRatioMode === "Contain") {
109
+ if (aspectRatio > 1.0) {
110
+ xAspectRatioModeFactor = 1.0 / aspectRatio;
111
+ }
112
+ else if (aspectRatio < 1.0) {
113
+ yAspectRatioModeFactor = aspectRatio;
114
+ }
115
+ }
116
+ else if (aspectRatioMode === "Cover") {
117
+ if (aspectRatio > 1.0) {
118
+ yAspectRatioModeFactor = aspectRatio;
119
+ }
120
+ else if (aspectRatio < 1.0) {
121
+ xAspectRatioModeFactor = 1.0 / aspectRatio;
122
+ }
123
+ }
124
+ xAlignmentTranslation = 0.0;
125
+ yAlignmentTranslation = 0.0;
126
+ if (aspectRatioAlignmentMode === "Start") {
127
+ xAlignmentTranslation = xAspectRatioModeFactor - 1.0;
128
+ yAlignmentTranslation = yAspectRatioModeFactor - 1.0;
129
+ }
130
+ else if (aspectRatioAlignmentMode === "End") {
131
+ xAlignmentTranslation = 1.0 - xAspectRatioModeFactor;
132
+ yAlignmentTranslation = 1.0 - yAspectRatioModeFactor;
133
+ }
134
+ };
135
+ updateAspectRatioModeFactors();
136
+ const transformPanX = isNdc
137
+ ? dX => (dX / width) * 2 * (1.0 / xAspectRatioModeFactor) // to normalized device coords
138
+ : dX => dX;
139
+ const transformPanY = isNdc
140
+ ? dY => (dY / height) * 2 * (1.0 / yAspectRatioModeFactor) // to normalized device coords
141
+ : dY => -dY;
142
+ const transformScaleX = isNdc
143
+ ? x => ((-1 + (x / width) * 2) - xAlignmentTranslation) * (1.0 / xAspectRatioModeFactor)
144
+ : x => x;
145
+ const transformScaleY = isNdc
146
+ ? y => ((1 - (y / height) * 2) - yAlignmentTranslation) * (1.0 / yAspectRatioModeFactor)
147
+ : y => y;
148
+ const tick = () => {
149
+ if (isFixed) {
150
+ const isChanged = isProgrammaticallyChanged;
151
+ isProgrammaticallyChanged = false;
152
+ return isChanged;
153
+ }
154
+ isInteractivelyChanged = false;
155
+ const currentMouseX = mouseX;
156
+ const currentMouseY = mouseY;
157
+ if ((isPanX || isPanY) &&
158
+ isLeftMousePressed &&
159
+ ((panOnMouseDownMove && !isMouseDownMoveModActive) ||
160
+ (!panOnMouseDownMove && isMouseDownMoveModActive))) {
161
+ const dX = isPanXInverted
162
+ ? prevMouseX - currentMouseX
163
+ : currentMouseX - prevMouseX;
164
+ const transformedPanX = isPanX ? transformPanX(panSpeed * dX) : 0;
165
+ const dY = isPanYInverted
166
+ ? prevMouseY - currentMouseY
167
+ : currentMouseY - prevMouseY;
168
+ const transformedPanY = isPanY ? transformPanY(panSpeed * dY) : 0;
169
+ if (transformedPanX !== 0 || transformedPanY !== 0) {
170
+ camera.pan([transformedPanX, transformedPanY]);
171
+ isInteractivelyChanged = true;
172
+ }
173
+ }
174
+ if ((isZoomX || isZoomY) && scrollDist) {
175
+ const dZ = zoomSpeed * Math.exp(scrollDist / height);
176
+ const transformedX = transformScaleX(mouseRelX);
177
+ const transformedY = transformScaleY(mouseRelY);
178
+ camera.scale([isZoomX ? 1 / dZ : 1, isZoomY ? 1 / dZ : 1], [transformedX, transformedY]);
179
+ isInteractivelyChanged = true;
180
+ }
181
+ if (isRotate &&
182
+ isLeftMousePressed &&
183
+ ((panOnMouseDownMove && isMouseDownMoveModActive) ||
184
+ (!panOnMouseDownMove && !isMouseDownMoveModActive)) &&
185
+ Math.abs(prevMouseX - currentMouseX) +
186
+ Math.abs(prevMouseY - currentMouseY) >
187
+ 0) {
188
+ const wh = width / 2;
189
+ const hh = height / 2;
190
+ const x1 = prevMouseX - wh;
191
+ const y1 = hh - prevMouseY;
192
+ const x2 = currentMouseX - wh;
193
+ const y2 = hh - currentMouseY;
194
+ // Angle between the start and end mouse position with respect to the
195
+ // viewport center
196
+ const radians = vec2.angle([x1, y1], [x2, y2]);
197
+ // Determine the orientation
198
+ const cross = x1 * y2 - x2 * y1;
199
+ camera.rotate(rotateSpeed * radians * Math.sign(cross));
200
+ isInteractivelyChanged = true;
201
+ }
202
+ // Reset scroll delta and mouse position
203
+ scrollDist = 0;
204
+ prevMouseX = currentMouseX;
205
+ prevMouseY = currentMouseY;
206
+ const isChanged = isInteractivelyChanged || isProgrammaticallyChanged;
207
+ isProgrammaticallyChanged = false;
208
+ return isChanged;
209
+ };
210
+ 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 } = {}) => {
211
+ defaultMouseDownMoveAction =
212
+ newDefaultMouseDownMoveAction !== null &&
213
+ MOUSE_DOWN_MOVE_ACTIONS.includes(newDefaultMouseDownMoveAction)
214
+ ? newDefaultMouseDownMoveAction
215
+ : defaultMouseDownMoveAction;
216
+ panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
217
+ isFixed = newIsFixed !== null ? newIsFixed : isFixed;
218
+ isPan = newIsPan !== null ? newIsPan : isPan;
219
+ isPanInverted =
220
+ newIsPanInverted !== null ? newIsPanInverted : isPanInverted;
221
+ isRotate = newIsRotate !== null ? newIsRotate : isRotate;
222
+ isZoom = newIsZoom !== null ? newIsZoom : isZoom;
223
+ panSpeed = +newPanSpeed > 0 ? newPanSpeed : panSpeed;
224
+ rotateSpeed = +newRotateSpeed > 0 ? newRotateSpeed : rotateSpeed;
225
+ zoomSpeed = +newZoomSpeed > 0 ? newZoomSpeed : zoomSpeed;
226
+ spreadXYSettings();
227
+ mouseDownMoveModKey =
228
+ newMouseDownMoveModKey !== null &&
229
+ Object.keys(KEY_MAP).includes(newMouseDownMoveModKey)
230
+ ? newMouseDownMoveModKey
231
+ : mouseDownMoveModKey;
232
+ };
233
+ const refresh = () => {
234
+ const bBox = element.getBoundingClientRect();
235
+ width = bBox.width;
236
+ height = bBox.height;
237
+ aspectRatio = width / height;
238
+ updateAspectRatioModeFactors();
239
+ };
240
+ const keyUpHandler = event => {
241
+ isMouseDownMoveModActive = false;
242
+ onKeyUp(event);
243
+ };
244
+ const keyDownHandler = event => {
245
+ isMouseDownMoveModActive = event[KEY_MAP[mouseDownMoveModKey]];
246
+ onKeyDown(event);
247
+ };
248
+ const mouseUpHandler = event => {
249
+ isLeftMousePressed = false;
250
+ onMouseUp(event);
251
+ };
252
+ const mouseDownHandler = event => {
253
+ isLeftMousePressed = event.buttons === 1;
254
+ onMouseDown(event);
255
+ };
256
+ const offsetXSupport = document.createEvent("MouseEvent").offsetX !== undefined;
257
+ const updateMouseRelXY = offsetXSupport
258
+ ? event => {
259
+ mouseRelX = event.offsetX;
260
+ mouseRelY = event.offsetY;
261
+ }
262
+ : event => {
263
+ const bBox = element.getBoundingClientRect();
264
+ mouseRelX = event.clientX - bBox.left;
265
+ mouseRelY = event.clientY - bBox.top;
266
+ };
267
+ const updateMouseXY = event => {
268
+ mouseX = event.clientX;
269
+ mouseY = event.clientY;
270
+ };
271
+ const mouseMoveHandler = event => {
272
+ updateMouseXY(event);
273
+ onMouseMove(event);
274
+ };
275
+ const wheelHandler = event => {
276
+ if ((isZoomX || isZoomY) && !isFixed) {
277
+ event.preventDefault();
278
+ updateMouseXY(event);
279
+ updateMouseRelXY(event);
280
+ const scale = event.deltaMode === 1 ? 12 : 1;
281
+ scrollDist += scale * (event.deltaY || event.deltaX || 0);
282
+ }
283
+ onWheel(event);
284
+ };
285
+ const dispose = () => {
286
+ camera = undefined;
287
+ element.removeEventListener("keydown", keyDownHandler);
288
+ element.removeEventListener("keyup", keyUpHandler);
289
+ element.removeEventListener("mousedown", mouseDownHandler);
290
+ element.removeEventListener("mouseup", mouseUpHandler);
291
+ element.removeEventListener("mousemove", mouseMoveHandler);
292
+ element.removeEventListener("wheel", wheelHandler);
293
+ };
294
+ element.addEventListener("keydown", keyDownHandler, { passive: true });
295
+ element.addEventListener("keyup", keyUpHandler, { passive: true });
296
+ element.addEventListener("mousedown", mouseDownHandler, { passive: true });
297
+ element.addEventListener("mouseup", mouseUpHandler, { passive: true });
298
+ element.addEventListener("mousemove", mouseMoveHandler, { passive: true });
299
+ element.addEventListener("wheel", wheelHandler, { passive: false });
300
+ camera.config = config;
301
+ camera.dispose = dispose;
302
+ camera.refresh = refresh;
303
+ camera.tick = tick;
304
+ const withProgrammaticChange = fn => function () {
305
+ fn.apply(null, arguments);
306
+ isProgrammaticallyChanged = true;
307
+ };
308
+ camera.lookAt = withProgrammaticChange(camera.lookAt);
309
+ camera.translate = withProgrammaticChange(camera.translate);
310
+ camera.pan = withProgrammaticChange(camera.pan);
311
+ camera.rotate = withProgrammaticChange(camera.rotate);
312
+ camera.scale = withProgrammaticChange(camera.scale);
313
+ camera.zoom = withProgrammaticChange(camera.zoom);
314
+ camera.reset = withProgrammaticChange(camera.reset);
315
+ camera.set = withProgrammaticChange(camera.set);
316
+ camera.setScaleBounds = withProgrammaticChange(camera.setScaleBounds);
317
+ camera.setTranslationBounds = withProgrammaticChange(camera.setTranslationBounds);
318
+ camera.setView = withProgrammaticChange(camera.setView);
319
+ camera.setViewCenter = withProgrammaticChange(camera.setViewCenter);
320
+ refresh();
321
+ return camera;
322
+ };
323
+ export default dom2dCamera;
@@ -0,0 +1,2 @@
1
+ export declare function checkWebGpuFeatureDetection(): [boolean, string | null];
2
+ //# sourceMappingURL=feature-detection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature-detection.d.ts","sourceRoot":"","sources":["../src/feature-detection.ts"],"names":[],"mappings":"AAGA,wBAAgB,2BAA2B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAC,IAAI,CAAC,CAqDpE"}
@@ -0,0 +1,58 @@
1
+ import UserAgent from "lightua";
2
+ // Returns the tuple [supportsWebGpu, supportsWebGpuMessage]
3
+ export function checkWebGpuFeatureDetection() {
4
+ const isSupported = typeof navigator !== 'undefined' && navigator.gpu;
5
+ if (isSupported) {
6
+ return [true, null];
7
+ }
8
+ const ua = UserAgent.parse(navigator.userAgent);
9
+ // WebGPU is supported in the following cases:
10
+ // - Browser Engine: Chromium. Major: 113+. OS: mac, windows, chromeOS
11
+ // - Browser Engine: Chromium. Major: 121+. OS: android
12
+ // - Browser Engine: Chromium. Major: 144+. OS: linux
13
+ // - Browser: Firefox. Major: 141+. OS: windows
14
+ // - Browser: Firefox. Major: 145+. OS: macos
15
+ // - Browser: Safari. Major: 26+. OS: macOS. OS version: tahoe 26+.
16
+ // - Browser: Safari. Major: 26+. OS: iOS/iPadOS. OS version: tahoe 26+.
17
+ // Else: suggest upgrading browser version or OS version (if different version of same browser has support)
18
+ // Else: suggest using different browser version.
19
+ const browserName = ua.browser.name;
20
+ const browserMajor = ua.browser.major;
21
+ const osName = ua.os.name;
22
+ const osVersion = ua.os.version;
23
+ // Should be supported according to UA but support wasn't detected.
24
+ const shouldSupportResult = [false, `WebGPU support was not detected. Please ensure the WebGPU feature is enabled or try a different browser.`];
25
+ if (["Chrome", "Chromium"].includes(browserName)) {
26
+ if (["Windows", "macOS", "ChromeOS"].includes(osName)) {
27
+ if (browserMajor < 113) {
28
+ 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.`];
29
+ }
30
+ }
31
+ else if (osName === "Android") {
32
+ if (browserMajor < 121) {
33
+ 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.`];
34
+ }
35
+ }
36
+ else if (osName === "Linux") {
37
+ if (browserMajor < 144) {
38
+ 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.`];
39
+ }
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
+ }
49
+ else if (browserName === "Safari") {
50
+ if (["macOS", "iOS"].includes(osName)) {
51
+ const osVersionFloat = parseFloat(osVersion);
52
+ if (browserMajor < 26 || osVersionFloat < 26) {
53
+ 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.`];
54
+ }
55
+ }
56
+ }
57
+ return shouldSupportResult;
58
+ }
@@ -0,0 +1,9 @@
1
+ import { ViewportParams } from './viewport.js';
2
+ export type CameraMatrix = Float32Array;
3
+ export declare function onWheel(viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, event: WheelEvent): CameraMatrix;
4
+ export declare function onMouseMove(viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, event: MouseEvent): CameraMatrix;
5
+ export declare function onMouseDown(_viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, _event: MouseEvent): CameraMatrix;
6
+ export declare function onMouseUp(_viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, _event: MouseEvent): CameraMatrix;
7
+ export declare function onKeyDown(_viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, _event: KeyboardEvent): CameraMatrix;
8
+ export declare function onKeyUp(_viewportParams: ViewportParams, prevCameraMatrix: CameraMatrix, _event: KeyboardEvent): CameraMatrix;
9
+ //# sourceMappingURL=functional-2d-camera.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functional-2d-camera.d.ts","sourceRoot":"","sources":["../src/functional-2d-camera.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C,MAAM,MAAM,YAAY,GAAG,YAAY,CAAC;AAsGxC,wBAAgB,OAAO,CAAC,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,GAAG,YAAY,CA6BvH;AAED,wBAAgB,WAAW,CAAC,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,GAAG,YAAY,CA2D3H;AAMD,wBAAgB,WAAW,CAAC,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,CAE7H;AAED,wBAAgB,SAAS,CAAC,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,GAAG,YAAY,CAE3H;AAED,wBAAgB,SAAS,CAAC,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,GAAG,YAAY,CAE9H;AAED,wBAAgB,OAAO,CAAC,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,GAAG,YAAY,CAE5H"}
@@ -0,0 +1,178 @@
1
+ // Functional version of `dom-2d-camera`.
2
+ // Each event handler accepts viewportParams, the previous camera matrix, and the event object,
3
+ // then returns the next camera matrix. No global camera object or tick function required.
4
+ import { mat4, vec2 } from 'gl-matrix';
5
+ // Camera configuration constants (matching dom-2d-camera defaults)
6
+ const isFixed = false;
7
+ const isNdc = true;
8
+ const isPan = true;
9
+ const isPanInverted = [false, true];
10
+ const panSpeed = 1;
11
+ const isRotate = true;
12
+ const rotateSpeed = 1;
13
+ const defaultMouseDownMoveAction = "pan";
14
+ const mouseDownMoveModKey = "alt";
15
+ const isZoom = true;
16
+ const zoomSpeed = 1;
17
+ // Derived settings
18
+ const isPanX = Array.isArray(isPan) ? Boolean(isPan[0]) : Boolean(isPan);
19
+ const isPanY = Array.isArray(isPan) ? Boolean(isPan[1]) : Boolean(isPan);
20
+ const isPanXInverted = Array.isArray(isPanInverted) ? Boolean(isPanInverted[0]) : Boolean(isPanInverted);
21
+ const isPanYInverted = Array.isArray(isPanInverted) ? Boolean(isPanInverted[1]) : Boolean(isPanInverted);
22
+ const isZoomX = Array.isArray(isZoom) ? Boolean(isZoom[0]) : Boolean(isZoom);
23
+ const isZoomY = Array.isArray(isZoom) ? Boolean(isZoom[1]) : Boolean(isZoom);
24
+ const panOnMouseDownMove = defaultMouseDownMoveAction === "pan";
25
+ const KEY_MAP = {
26
+ alt: "altKey",
27
+ cmd: "metaKey",
28
+ ctrl: "ctrlKey",
29
+ meta: "metaKey",
30
+ shift: "shiftKey",
31
+ };
32
+ // --- Aspect ratio helpers ---
33
+ function computeAspectRatioFactors(vp) {
34
+ const aspectRatio = vp.width / vp.height;
35
+ let xFactor = 1.0;
36
+ let yFactor = 1.0;
37
+ if (vp.aspectRatioMode === "Contain") {
38
+ if (aspectRatio > 1.0)
39
+ xFactor = 1.0 / aspectRatio;
40
+ else if (aspectRatio < 1.0)
41
+ yFactor = aspectRatio;
42
+ }
43
+ else if (vp.aspectRatioMode === "Cover") {
44
+ if (aspectRatio > 1.0)
45
+ yFactor = aspectRatio;
46
+ else if (aspectRatio < 1.0)
47
+ xFactor = 1.0 / aspectRatio;
48
+ }
49
+ let xAlignTranslation = 0.0;
50
+ let yAlignTranslation = 0.0;
51
+ if (vp.aspectRatioAlignmentMode === "Start") {
52
+ xAlignTranslation = xFactor - 1.0;
53
+ yAlignTranslation = yFactor - 1.0;
54
+ }
55
+ else if (vp.aspectRatioAlignmentMode === "End") {
56
+ xAlignTranslation = 1.0 - xFactor;
57
+ yAlignTranslation = 1.0 - yFactor;
58
+ }
59
+ return { xFactor, yFactor, xAlignTranslation, yAlignTranslation };
60
+ }
61
+ // --- Matrix operation helpers (pure, no mutation of input) ---
62
+ function applyTranslate(view, dx, dy) {
63
+ const t = mat4.create();
64
+ mat4.fromTranslation(t, [dx, dy, 0]);
65
+ const result = mat4.create();
66
+ mat4.multiply(result, t, view);
67
+ return result;
68
+ }
69
+ function applyScale(view, sx, sy, cx, cy) {
70
+ // Replicate camera-2d-simple's scale: view = a * s * inv(a) * view
71
+ // where a = translate to scale center, s = scale matrix
72
+ const s = mat4.create();
73
+ mat4.fromScaling(s, [sx, sy, 1]);
74
+ const a = mat4.create();
75
+ mat4.fromTranslation(a, [cx, cy, 0]);
76
+ const aInv = mat4.create();
77
+ mat4.invert(aInv, a);
78
+ const temp1 = mat4.create();
79
+ mat4.multiply(temp1, aInv, view); // inv(a) * view
80
+ const temp2 = mat4.create();
81
+ mat4.multiply(temp2, s, temp1); // s * inv(a) * view
82
+ const result = mat4.create();
83
+ mat4.multiply(result, a, temp2); // a * s * inv(a) * view
84
+ return result;
85
+ }
86
+ function applyRotate(view, rad) {
87
+ const r = mat4.create();
88
+ mat4.fromRotation(r, rad, [0, 0, 1]);
89
+ const result = mat4.create();
90
+ mat4.multiply(result, r, view);
91
+ return result;
92
+ }
93
+ // --- Event handlers ---
94
+ export function onWheel(viewportParams, prevCameraMatrix, event) {
95
+ if (isFixed || (!isZoomX && !isZoomY))
96
+ return prevCameraMatrix;
97
+ const { width, height } = viewportParams;
98
+ const { xFactor, yFactor, xAlignTranslation, yAlignTranslation } = computeAspectRatioFactors(viewportParams);
99
+ const scaleFactor = event.deltaMode === 1 ? 12 : 1;
100
+ const scrollDist = scaleFactor * (event.deltaY || event.deltaX || 0);
101
+ if (scrollDist === 0)
102
+ return prevCameraMatrix;
103
+ const dZ = zoomSpeed * Math.exp(scrollDist / height);
104
+ // Transform mouse position to camera space
105
+ const mouseRelX = event.offsetX;
106
+ const mouseRelY = event.offsetY;
107
+ const transformedX = isNdc
108
+ ? ((-1 + (mouseRelX / width) * 2) - xAlignTranslation) * (1.0 / xFactor)
109
+ : mouseRelX;
110
+ const transformedY = isNdc
111
+ ? ((1 - (mouseRelY / height) * 2) - yAlignTranslation) * (1.0 / yFactor)
112
+ : mouseRelY;
113
+ return applyScale(prevCameraMatrix, isZoomX ? 1 / dZ : 1, isZoomY ? 1 / dZ : 1, transformedX, transformedY);
114
+ }
115
+ export function onMouseMove(viewportParams, prevCameraMatrix, event) {
116
+ if (isFixed)
117
+ return prevCameraMatrix;
118
+ const isLeftMousePressed = (event.buttons & 1) !== 0;
119
+ if (!isLeftMousePressed)
120
+ return prevCameraMatrix;
121
+ const modKey = KEY_MAP[mouseDownMoveModKey];
122
+ const isMouseDownMoveModActive = Boolean(modKey && event[modKey]);
123
+ const { width, height } = viewportParams;
124
+ const { xFactor, yFactor } = computeAspectRatioFactors(viewportParams);
125
+ // Pan (uses event.movementX/Y for the pixel delta since last mousemove)
126
+ if ((isPanX || isPanY) &&
127
+ ((panOnMouseDownMove && !isMouseDownMoveModActive) ||
128
+ (!panOnMouseDownMove && isMouseDownMoveModActive))) {
129
+ const rawDX = isPanXInverted ? -event.movementX : event.movementX;
130
+ const rawDY = isPanYInverted ? -event.movementY : event.movementY;
131
+ const transformedPanX = isPanX
132
+ ? (isNdc ? (rawDX / width) * 2 * (1.0 / xFactor) : rawDX) * panSpeed
133
+ : 0;
134
+ const transformedPanY = isPanY
135
+ ? (isNdc ? (rawDY / height) * 2 * (1.0 / yFactor) : -rawDY) * panSpeed
136
+ : 0;
137
+ if (transformedPanX !== 0 || transformedPanY !== 0) {
138
+ return applyTranslate(prevCameraMatrix, transformedPanX, transformedPanY);
139
+ }
140
+ }
141
+ // Rotate
142
+ if (isRotate &&
143
+ ((panOnMouseDownMove && isMouseDownMoveModActive) ||
144
+ (!panOnMouseDownMove && !isMouseDownMoveModActive))) {
145
+ const wh = width / 2;
146
+ const hh = height / 2;
147
+ // Derive previous position from current + movementX/Y
148
+ const currentX = event.offsetX;
149
+ const currentY = event.offsetY;
150
+ const prevX = currentX - event.movementX;
151
+ const prevY = currentY - event.movementY;
152
+ const x1 = prevX - wh;
153
+ const y1 = hh - prevY;
154
+ const x2 = currentX - wh;
155
+ const y2 = hh - currentY;
156
+ if (Math.abs(x1 - x2) + Math.abs(y1 - y2) > 0) {
157
+ const radians = vec2.angle([x1, y1], [x2, y2]);
158
+ const cross = x1 * y2 - x2 * y1;
159
+ return applyRotate(prevCameraMatrix, rotateSpeed * radians * Math.sign(cross));
160
+ }
161
+ }
162
+ return prevCameraMatrix;
163
+ }
164
+ // These handlers don't modify the camera matrix in the functional approach.
165
+ // Button state is read from event.buttons in onMouseMove; modifier key state
166
+ // is read from event.altKey/etc. in onMouseMove.
167
+ export function onMouseDown(_viewportParams, prevCameraMatrix, _event) {
168
+ return prevCameraMatrix;
169
+ }
170
+ export function onMouseUp(_viewportParams, prevCameraMatrix, _event) {
171
+ return prevCameraMatrix;
172
+ }
173
+ export function onKeyDown(_viewportParams, prevCameraMatrix, _event) {
174
+ return prevCameraMatrix;
175
+ }
176
+ export function onKeyUp(_viewportParams, prevCameraMatrix, _event) {
177
+ return prevCameraMatrix;
178
+ }
@@ -0,0 +1,6 @@
1
+ export { initialize, getIsWasmReady, render_wasm, pick_wasm, setStore, setStoreByName, getStore, } from './core.js';
2
+ export { default as create2dCamera } from "./dom-2d-camera.js";
3
+ export { default as create3dCamera } from "./3d-view-controls.js";
4
+ export { checkWebGpuFeatureDetection } from './feature-detection.js';
5
+ export { getBounds, getCameraMatrixFromBounds } from './viewport.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,WAAW,EACX,SAAS,EACT,QAAQ,EACR,cAAc,EACd,QAAQ,GACT,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { initialize, getIsWasmReady, render_wasm, pick_wasm, setStore, setStoreByName, getStore, } from './core.js';
2
+ export { default as create2dCamera } from "./dom-2d-camera.js";
3
+ export { default as create3dCamera } from "./3d-view-controls.js";
4
+ export { checkWebGpuFeatureDetection } from './feature-detection.js';
5
+ export { getBounds, getCameraMatrixFromBounds } from './viewport.js';
@@ -0,0 +1,13 @@
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 class LruStore<S extends AsyncReadable> implements AsyncReadable {
4
+ #private;
5
+ constructor(store: S, maxSize?: number);
6
+ get(...args: Parameters<S["get"]>): Promise<Uint8Array | undefined>;
7
+ getPeek(...args: Parameters<S["get"]>): 'pending' | 'fulfilled' | 'rejected' | undefined;
8
+ getRange(...args: Parameters<NonNullable<S["getRange"]>>): Promise<Uint8Array | undefined>;
9
+ getRangePeek(...args: Parameters<NonNullable<S["getRange"]>>): 'pending' | 'fulfilled' | 'rejected' | undefined;
10
+ clearCache(): void;
11
+ }
12
+ export declare function lru(inner_store: AsyncReadable, maxSize?: number): LruStore<AsyncReadable>;
13
+ //# 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;AAID,qBAAa,QAAQ,CAAC,CAAC,SAAS,aAAa,CAAE,YAAW,aAAa;;gBASzD,KAAK,EAAE,CAAC,EAAE,OAAO,SAAM;IAW7B,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IA8BzE,OAAO,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,SAAS,GAAG,WAAW,GAAG,UAAU,GAAG,SAAS;IAQlF,QAAQ,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAmChG,YAAY,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,WAAW,GAAG,UAAU,GAAG,SAAS;IAO/G,UAAU;CAUX;AAED,wBAAgB,GAAG,CAAC,WAAW,EAAE,aAAa,EAAE,OAAO,SAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,CAEtF"}