@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,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
+ export 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
+ }
@@ -0,0 +1,24 @@
1
+ export type AspectRatioMode = "Ignore" | "Contain" | "Cover";
2
+ export type AspectRatioAlignmentMode = "Center" | "Start" | "End";
3
+ export type Margins = {
4
+ marginTop?: number;
5
+ marginRight?: number;
6
+ marginBottom?: number;
7
+ marginLeft?: number;
8
+ };
9
+ export type ViewportParams = {
10
+ width: number;
11
+ height: number;
12
+ aspectRatioMode: AspectRatioMode;
13
+ aspectRatioAlignmentMode: AspectRatioAlignmentMode;
14
+ margins?: Margins;
15
+ };
16
+ export type Bounds = {
17
+ xMin?: number;
18
+ xMax?: number;
19
+ yMin?: number;
20
+ yMax?: number;
21
+ };
22
+ export declare function getBounds(cameraMatrix: Float32Array, viewportParams: ViewportParams): Required<Bounds>;
23
+ export declare function getCameraMatrixFromBounds(bounds: Bounds, prevCameraMatrix: Float32Array, viewportParams: ViewportParams): Float32Array;
24
+ //# sourceMappingURL=viewport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"viewport.d.ts","sourceRoot":"","sources":["../src/viewport.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,SAAS,GAAG,OAAO,CAAC;AAC7D,MAAM,MAAM,wBAAwB,GAAG,QAAQ,GAAG,OAAO,GAAG,KAAK,CAAC;AAElE,MAAM,MAAM,OAAO,GAAG;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,eAAe,CAAC;IACjC,wBAAwB,EAAE,wBAAwB,CAAC;IACnD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IAKnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAGF,wBAAgB,SAAS,CAAC,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,GAAG,QAAQ,CAAC,MAAM,CAAC,CA4CtG;AAID,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,GAAG,YAAY,CAgEtI"}
@@ -0,0 +1,108 @@
1
+ // Calculate the visible data range based on camera view and viewport parameters.
2
+ export function getBounds(cameraMatrix, viewportParams) {
3
+ const zoomX = cameraMatrix[0];
4
+ const zoomY = cameraMatrix[5];
5
+ const translateX = cameraMatrix[12];
6
+ const translateY = cameraMatrix[13];
7
+ const marginTop = viewportParams.margins?.marginTop ?? 0;
8
+ const marginRight = viewportParams.margins?.marginRight ?? 0;
9
+ const marginBottom = viewportParams.margins?.marginBottom ?? 0;
10
+ const marginLeft = viewportParams.margins?.marginLeft ?? 0;
11
+ const layerW = viewportParams.width - marginLeft - marginRight;
12
+ const layerH = viewportParams.height - marginTop - marginBottom;
13
+ const layerAspectRatio = layerW / layerH;
14
+ let xScale = 1.0;
15
+ let yScale = 1.0;
16
+ if (viewportParams.aspectRatioMode === "Contain") {
17
+ if (layerAspectRatio > 1.0)
18
+ xScale = layerAspectRatio;
19
+ else if (layerAspectRatio < 1.0)
20
+ yScale = 1.0 / layerAspectRatio;
21
+ }
22
+ else if (viewportParams.aspectRatioMode === "Cover") {
23
+ if (layerAspectRatio > 1.0)
24
+ yScale = 1.0 / layerAspectRatio;
25
+ else if (layerAspectRatio < 1.0)
26
+ xScale = layerAspectRatio;
27
+ }
28
+ let xAlignTranslation = 0.0;
29
+ let yAlignTranslation = 0.0;
30
+ if (viewportParams.aspectRatioAlignmentMode === "Start") {
31
+ xAlignTranslation = xScale - 1.0;
32
+ yAlignTranslation = yScale - 1.0;
33
+ }
34
+ else if (viewportParams.aspectRatioAlignmentMode === "End") {
35
+ xAlignTranslation = 1.0 - xScale;
36
+ yAlignTranslation = 1.0 - yScale;
37
+ }
38
+ const xAdj = xScale - 1.0;
39
+ const yAdj = yScale - 1.0;
40
+ const xMin = ((-translateX - 1.0 - xAdj + xAlignTranslation) / zoomX + 1.0) / 2.0;
41
+ const xMax = ((-translateX + 1.0 + xAdj + xAlignTranslation) / zoomX + 1.0) / 2.0;
42
+ const yMin = ((-translateY - 1.0 - yAdj + yAlignTranslation) / zoomY + 1.0) / 2.0;
43
+ const yMax = ((-translateY + 1.0 + yAdj + yAlignTranslation) / zoomY + 1.0) / 2.0;
44
+ return { xMin, xMax, yMin, yMax };
45
+ }
46
+ // Given data bounds, compute the corresponding camera matrix.
47
+ // Missing bound values are filled in from prevCameraMatrix.
48
+ export function getCameraMatrixFromBounds(bounds, prevCameraMatrix, viewportParams) {
49
+ // Fill in missing bounds from the previous camera matrix.
50
+ const currentBounds = getBounds(prevCameraMatrix, viewportParams);
51
+ const xMin = bounds.xMin ?? currentBounds.xMin;
52
+ const xMax = bounds.xMax ?? currentBounds.xMax;
53
+ const yMin = bounds.yMin ?? currentBounds.yMin;
54
+ const yMax = bounds.yMax ?? currentBounds.yMax;
55
+ const marginTop = viewportParams.margins?.marginTop ?? 0;
56
+ const marginRight = viewportParams.margins?.marginRight ?? 0;
57
+ const marginBottom = viewportParams.margins?.marginBottom ?? 0;
58
+ const marginLeft = viewportParams.margins?.marginLeft ?? 0;
59
+ const layerW = viewportParams.width - marginLeft - marginRight;
60
+ const layerH = viewportParams.height - marginTop - marginBottom;
61
+ const layerAspectRatio = layerW / layerH;
62
+ let xScale = 1.0;
63
+ let yScale = 1.0;
64
+ if (viewportParams.aspectRatioMode === "Contain") {
65
+ if (layerAspectRatio > 1.0)
66
+ xScale = layerAspectRatio;
67
+ else if (layerAspectRatio < 1.0)
68
+ yScale = 1.0 / layerAspectRatio;
69
+ }
70
+ else if (viewportParams.aspectRatioMode === "Cover") {
71
+ if (layerAspectRatio > 1.0)
72
+ yScale = 1.0 / layerAspectRatio;
73
+ else if (layerAspectRatio < 1.0)
74
+ xScale = layerAspectRatio;
75
+ }
76
+ let xAlignTranslation = 0.0;
77
+ let yAlignTranslation = 0.0;
78
+ if (viewportParams.aspectRatioAlignmentMode === "Start") {
79
+ xAlignTranslation = xScale - 1.0;
80
+ yAlignTranslation = yScale - 1.0;
81
+ }
82
+ else if (viewportParams.aspectRatioAlignmentMode === "End") {
83
+ xAlignTranslation = 1.0 - xScale;
84
+ yAlignTranslation = 1.0 - yScale;
85
+ }
86
+ const xAdj = xScale - 1.0;
87
+ const yAdj = yScale - 1.0;
88
+ const xRange = xMax - xMin;
89
+ const yRange = yMax - yMin;
90
+ let zoomX = (1.0 + xAdj) / xRange;
91
+ let zoomY = (1.0 + yAdj) / yRange;
92
+ // When aspect ratio is ignored, zoom each axis independently.
93
+ // Otherwise take the minimum so all requested data fits within the viewport.
94
+ if (viewportParams.aspectRatioMode !== "Ignore") {
95
+ zoomX = zoomY = Math.min(zoomX, zoomY);
96
+ }
97
+ // Invert the getBounds translation equations:
98
+ // min + max = (-translate + align) / zoom + 1.0
99
+ // So: translate = align - zoom * ((min + max) - 1.0)
100
+ const translateX = xAlignTranslation - zoomX * ((xMin + xMax) - 1.0);
101
+ const translateY = yAlignTranslation - zoomY * ((yMin + yMax) - 1.0);
102
+ return new Float32Array([
103
+ zoomX, 0.0, 0.0, 0.0,
104
+ 0.0, zoomY, 0.0, 0.0,
105
+ 0.0, 0.0, 1.0, 0.0,
106
+ translateX, translateY, 0.0, 1.0,
107
+ ]);
108
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@pluot/core",
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": "0.1.0"
34
+ },
35
+ "devDependencies": {
36
+ "vitest": "^3.2.4",
37
+ "vite": "^6.1.1",
38
+ "jsdom": "^26.1.0",
39
+ "vitest-canvas-mock": "~0.3.3",
40
+ "typescript": "^5.9"
41
+ },
42
+ "scripts": {
43
+ "start": "pnpm -C ../../ run start-tsc",
44
+ "build": "pnpm -C ../../ run build-tsc",
45
+ "bundle": "pnpm exec vite build -c ./vite.config.js",
46
+ "test": "pnpm exec vitest --run -r ../../ --dir ."
47
+ },
48
+ "module": "dist/index.js",
49
+ "exports": {
50
+ ".": {
51
+ "types": "./dist-tsc/index.d.ts",
52
+ "import": "./dist/index.js"
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,271 @@
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
+ }
package/src/core.ts ADDED
@@ -0,0 +1,89 @@
1
+ import * as wasm from "pluot";
2
+ import { lru, type LruStore } from "./lru-store.js";
3
+ import type { AsyncReadable } from "zarrita";
4
+
5
+ export const { render_wasm, pick_wasm } = wasm;
6
+
7
+ // Global stores singleton.
8
+ const stores: Record<string, LruStore<AsyncReadable>> = {};
9
+
10
+ let isInitializedPromise: Promise<any> | undefined = undefined;
11
+ let isWasmReady = false;
12
+
13
+ async function _initialize() {
14
+ // IMPORTANT: This function should only be executed ONCE.
15
+ await wasm.default();
16
+
17
+ // This is a hack that allows avoiding putting these functions on `window` or `globalThis`.
18
+ // It is a workaround for https://github.com/wasm-bindgen/wasm-bindgen/issues/3041
19
+ // See corresponding code in `bindings.rs`.
20
+ wasm.set_zarr_imports({
21
+ zarr_get: async (store_name: string, key: string) => {
22
+ console.log(`zarr_get: store_name=${store_name}, key=${key}`);
23
+ return stores[store_name].get(`/${key}`);
24
+ },
25
+
26
+ zarr_get_status: (store_name: string, key: string) => {
27
+ return stores[store_name].getPeek(`/${key}`);
28
+ },
29
+
30
+ zarr_has: async (store_name: string, key: string) => {
31
+ // console.log(`zarr_has: store_name=${store_name}, key=${key}`);
32
+ return stores[store_name].get(`/${key}`) !== undefined;
33
+ },
34
+
35
+ zarr_has_status: (store_name: string, key: string) => {
36
+ return stores[store_name].getPeek(`/${key}`);
37
+ },
38
+
39
+ zarr_get_range_from_offset: async (store_name: string, key: string, offset: number, length: number) => {
40
+ // console.log(`zarr_get_range_from_offset: store_name=${store_name}, key=${key}, offset=${offset}, length=${length}`);
41
+ return stores[store_name].getRange(`/${key}`, { offset, length });
42
+ },
43
+
44
+ zarr_get_range_from_offset_status: (store_name: string, key: string, offset: number, length: number) => {
45
+ // console.log(`zarr_get_range_from_offset: store_name=${store_name}, key=${key}, offset=${offset}, length=${length}`);
46
+ return stores[store_name].getRangePeek(`/${key}`, { offset, length });
47
+ },
48
+
49
+ zarr_get_range_from_end: async (store_name: string, key: string, suffix_length: number) => {
50
+ // console.log(`zarr_get_range_from_end: store_name=${store_name}, key=${key}, suffix_length=${suffix_length}`);
51
+ return stores[store_name].getRange(`/${key}`, { suffixLength: suffix_length });
52
+ },
53
+
54
+ zarr_get_range_from_end_status: (store_name: string, key: string, suffix_length: number) => {
55
+ // console.log(`zarr_get_range_from_end: store_name=${store_name}, key=${key}, suffix_length=${suffix_length}`);
56
+ return stores[store_name].getRangePeek(`/${key}`, { suffixLength: suffix_length });
57
+ },
58
+ });
59
+
60
+ // Opt-in to better error messages.
61
+ wasm.set_panic_hook();
62
+ }
63
+
64
+ export async function initialize() {
65
+ // This function is safe to execute multiple times.
66
+ if(!isInitializedPromise) {
67
+ isInitializedPromise = _initialize().then(() => { isWasmReady = true; });
68
+ } else {
69
+ isInitializedPromise.then(() => { isWasmReady = true; });
70
+ }
71
+ return isInitializedPromise;
72
+ }
73
+
74
+ export function getIsWasmReady(): boolean {
75
+ return isWasmReady;
76
+ }
77
+
78
+ export function setStore(store: AsyncReadable, plotId: string): string {
79
+ stores[plotId + "_store"] = lru(store);
80
+ return plotId + "_store";
81
+ }
82
+
83
+ export function setStoreByName(storeName: string, store: AsyncReadable) {
84
+ stores[storeName] = lru(store);
85
+ }
86
+
87
+ export function getStore(storeName: string) {
88
+ return stores[storeName];
89
+ }