@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,145 @@
1
+
2
+ // TODO: auto-generate these types from the Rust side: https://github.com/keller-mark/pluot/issues/133
3
+ export type AspectRatioMode = "Ignore" | "Contain" | "Cover";
4
+ export type AspectRatioAlignmentMode = "Center" | "Start" | "End";
5
+
6
+ export type Margins = {
7
+ marginTop?: number;
8
+ marginRight?: number;
9
+ marginBottom?: number;
10
+ marginLeft?: number;
11
+ };
12
+
13
+ export type ViewportParams = {
14
+ width: number;
15
+ height: number;
16
+ aspectRatioMode: AspectRatioMode;
17
+ aspectRatioAlignmentMode: AspectRatioAlignmentMode;
18
+ margins?: Margins;
19
+ };
20
+
21
+ export type Bounds = {
22
+ // Each value is optional.
23
+ // When an entire dimension is omitted (X or Y),
24
+ // use the current camera settings for that dimension.
25
+ // When a single value is omitted (e.g., xMin), ensure the resulting camera matrix keeps this boundary unchanged.
26
+ xMin?: number;
27
+ xMax?: number;
28
+ yMin?: number;
29
+ yMax?: number;
30
+ };
31
+
32
+ // Calculate the visible data range based on camera view and viewport parameters.
33
+ export function getBounds(cameraMatrix: Float32Array, viewportParams: ViewportParams): Required<Bounds> {
34
+ const zoomX = cameraMatrix[0];
35
+ const zoomY = cameraMatrix[5];
36
+ const translateX = cameraMatrix[12];
37
+ const translateY = cameraMatrix[13];
38
+
39
+ const marginTop = viewportParams.margins?.marginTop ?? 0;
40
+ const marginRight = viewportParams.margins?.marginRight ?? 0;
41
+ const marginBottom = viewportParams.margins?.marginBottom ?? 0;
42
+ const marginLeft = viewportParams.margins?.marginLeft ?? 0;
43
+
44
+ const layerW = viewportParams.width - marginLeft - marginRight;
45
+ const layerH = viewportParams.height - marginTop - marginBottom;
46
+ const layerAspectRatio = layerW / layerH;
47
+
48
+ let xScale = 1.0;
49
+ let yScale = 1.0;
50
+ if (viewportParams.aspectRatioMode === "Contain") {
51
+ if (layerAspectRatio > 1.0) xScale = layerAspectRatio;
52
+ else if (layerAspectRatio < 1.0) yScale = 1.0 / layerAspectRatio;
53
+ } else if (viewportParams.aspectRatioMode === "Cover") {
54
+ if (layerAspectRatio > 1.0) yScale = 1.0 / layerAspectRatio;
55
+ else if (layerAspectRatio < 1.0) xScale = layerAspectRatio;
56
+ }
57
+
58
+ let xAlignTranslation = 0.0;
59
+ let yAlignTranslation = 0.0;
60
+ if (viewportParams.aspectRatioAlignmentMode === "Start") {
61
+ xAlignTranslation = xScale - 1.0;
62
+ yAlignTranslation = yScale - 1.0;
63
+ } else if (viewportParams.aspectRatioAlignmentMode === "End") {
64
+ xAlignTranslation = 1.0 - xScale;
65
+ yAlignTranslation = 1.0 - yScale;
66
+ }
67
+
68
+ const xAdj = xScale - 1.0;
69
+ const yAdj = yScale - 1.0;
70
+
71
+ const xMin = ((-translateX - 1.0 - xAdj + xAlignTranslation) / zoomX + 1.0) / 2.0;
72
+ const xMax = ((-translateX + 1.0 + xAdj + xAlignTranslation) / zoomX + 1.0) / 2.0;
73
+ const yMin = ((-translateY - 1.0 - yAdj + yAlignTranslation) / zoomY + 1.0) / 2.0;
74
+ const yMax = ((-translateY + 1.0 + yAdj + yAlignTranslation) / zoomY + 1.0) / 2.0;
75
+
76
+ return { xMin, xMax, yMin, yMax };
77
+ }
78
+
79
+ // Given data bounds, compute the corresponding camera matrix.
80
+ // Missing bound values are filled in from prevCameraMatrix.
81
+ export function getCameraMatrixFromBounds(bounds: Bounds, prevCameraMatrix: Float32Array, viewportParams: ViewportParams): Float32Array {
82
+ // Fill in missing bounds from the previous camera matrix.
83
+ const currentBounds = getBounds(prevCameraMatrix, viewportParams);
84
+ const xMin = bounds.xMin ?? currentBounds.xMin;
85
+ const xMax = bounds.xMax ?? currentBounds.xMax;
86
+ const yMin = bounds.yMin ?? currentBounds.yMin;
87
+ const yMax = bounds.yMax ?? currentBounds.yMax;
88
+
89
+ const marginTop = viewportParams.margins?.marginTop ?? 0;
90
+ const marginRight = viewportParams.margins?.marginRight ?? 0;
91
+ const marginBottom = viewportParams.margins?.marginBottom ?? 0;
92
+ const marginLeft = viewportParams.margins?.marginLeft ?? 0;
93
+
94
+ const layerW = viewportParams.width - marginLeft - marginRight;
95
+ const layerH = viewportParams.height - marginTop - marginBottom;
96
+ const layerAspectRatio = layerW / layerH;
97
+
98
+ let xScale = 1.0;
99
+ let yScale = 1.0;
100
+ if (viewportParams.aspectRatioMode === "Contain") {
101
+ if (layerAspectRatio > 1.0) xScale = layerAspectRatio;
102
+ else if (layerAspectRatio < 1.0) yScale = 1.0 / layerAspectRatio;
103
+ } else if (viewportParams.aspectRatioMode === "Cover") {
104
+ if (layerAspectRatio > 1.0) yScale = 1.0 / layerAspectRatio;
105
+ else if (layerAspectRatio < 1.0) xScale = layerAspectRatio;
106
+ }
107
+
108
+ let xAlignTranslation = 0.0;
109
+ let yAlignTranslation = 0.0;
110
+ if (viewportParams.aspectRatioAlignmentMode === "Start") {
111
+ xAlignTranslation = xScale - 1.0;
112
+ yAlignTranslation = yScale - 1.0;
113
+ } else if (viewportParams.aspectRatioAlignmentMode === "End") {
114
+ xAlignTranslation = 1.0 - xScale;
115
+ yAlignTranslation = 1.0 - yScale;
116
+ }
117
+
118
+ const xAdj = xScale - 1.0;
119
+ const yAdj = yScale - 1.0;
120
+
121
+ const xRange = xMax - xMin;
122
+ const yRange = yMax - yMin;
123
+
124
+ let zoomX = (1.0 + xAdj) / xRange;
125
+ let zoomY = (1.0 + yAdj) / yRange;
126
+
127
+ // When aspect ratio is ignored, zoom each axis independently.
128
+ // Otherwise take the minimum so all requested data fits within the viewport.
129
+ if (viewportParams.aspectRatioMode !== "Ignore") {
130
+ zoomX = zoomY = Math.min(zoomX, zoomY);
131
+ }
132
+
133
+ // Invert the getBounds translation equations:
134
+ // min + max = (-translate + align) / zoom + 1.0
135
+ // So: translate = align - zoom * ((min + max) - 1.0)
136
+ const translateX = xAlignTranslation - zoomX * ((xMin + xMax) - 1.0);
137
+ const translateY = yAlignTranslation - zoomY * ((yMin + yMax) - 1.0);
138
+
139
+ return new Float32Array([
140
+ zoomX, 0.0, 0.0, 0.0,
141
+ 0.0, zoomY, 0.0, 0.0,
142
+ 0.0, 0.0, 1.0, 0.0,
143
+ translateX, translateY, 0.0, 1.0,
144
+ ]);
145
+ }