@pooder/kit 4.3.0 → 5.0.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/.test-dist/src/CanvasService.js +249 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/bridgeSelection.js +20 -0
- package/.test-dist/src/constraints.js +237 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +723 -0
- package/.test-dist/src/edgeScale.js +12 -0
- package/.test-dist/src/feature.js +752 -0
- package/.test-dist/src/featureComplete.js +32 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +506 -0
- package/.test-dist/src/image.js +1234 -0
- package/.test-dist/src/index.js +35 -0
- package/.test-dist/src/maskOps.js +270 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/renderSpec.js +2 -0
- package/.test-dist/src/ruler.js +343 -0
- package/.test-dist/src/sceneLayout.js +99 -0
- package/.test-dist/src/sceneLayoutModel.js +196 -0
- package/.test-dist/src/sceneView.js +40 -0
- package/.test-dist/src/sceneVisibility.js +42 -0
- package/.test-dist/src/size.js +332 -0
- package/.test-dist/src/tracer.js +544 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +829 -0
- package/.test-dist/src/wrappedOffsets.js +33 -0
- package/.test-dist/tests/run.js +94 -0
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +339 -36
- package/dist/index.d.ts +339 -36
- package/dist/index.js +3587 -854
- package/dist/index.mjs +3580 -856
- package/package.json +2 -2
- package/src/CanvasService.ts +300 -96
- package/src/ViewportSystem.ts +92 -92
- package/src/background.ts +230 -230
- package/src/bridgeSelection.ts +17 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +897 -955
- package/src/edgeScale.ts +19 -0
- package/src/feature.ts +83 -30
- package/src/film.ts +194 -194
- package/src/geometry.ts +234 -80
- package/src/image.ts +1582 -512
- package/src/index.ts +14 -10
- package/src/maskOps.ts +326 -0
- package/src/mirror.ts +128 -128
- package/src/renderSpec.ts +18 -0
- package/src/ruler.ts +449 -508
- package/src/sceneLayout.ts +121 -0
- package/src/sceneLayoutModel.ts +335 -0
- package/src/sceneVisibility.ts +49 -0
- package/src/size.ts +379 -0
- package/src/tracer.ts +719 -570
- package/src/units.ts +27 -27
- package/src/white-ink.ts +1018 -373
- package/src/wrappedOffsets.ts +33 -0
- package/tests/run.ts +118 -0
- package/tsconfig.test.json +15 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pooder/kit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Standard plugins for Pooder editor",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"paper": "^0.12.18",
|
|
21
21
|
"fabric": "^7.0.0",
|
|
22
|
-
"@pooder/core": "2.
|
|
22
|
+
"@pooder/core": "2.1.0"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
package/src/CanvasService.ts
CHANGED
|
@@ -1,96 +1,300 @@
|
|
|
1
|
-
import { Canvas, Group, FabricObject } from "fabric";
|
|
2
|
-
import { Service, EventBus } from "@pooder/core";
|
|
3
|
-
import { ViewportSystem } from "./ViewportSystem";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
public
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.canvas.on("selection:
|
|
43
|
-
this.canvas.on("selection:
|
|
44
|
-
this.canvas.on("
|
|
45
|
-
this.canvas.on("object:
|
|
46
|
-
this.canvas.on("object:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
1
|
+
import { Canvas, Group, FabricObject, Rect, Path, Image } from "fabric";
|
|
2
|
+
import { Service, EventBus } from "@pooder/core";
|
|
3
|
+
import { ViewportSystem } from "./ViewportSystem";
|
|
4
|
+
import type { RenderLayerSpec, RenderObjectSpec } from "./renderSpec";
|
|
5
|
+
|
|
6
|
+
export default class CanvasService implements Service {
|
|
7
|
+
public canvas: Canvas;
|
|
8
|
+
public viewport: ViewportSystem;
|
|
9
|
+
private eventBus?: EventBus;
|
|
10
|
+
|
|
11
|
+
constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
|
|
12
|
+
if (el instanceof Canvas) {
|
|
13
|
+
this.canvas = el;
|
|
14
|
+
} else {
|
|
15
|
+
this.canvas = new Canvas(el, {
|
|
16
|
+
preserveObjectStacking: true,
|
|
17
|
+
...options,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.viewport = new ViewportSystem();
|
|
22
|
+
if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
|
|
23
|
+
this.viewport.updateContainer(this.canvas.width, this.canvas.height);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (options?.eventBus) {
|
|
27
|
+
this.setEventBus(options.eventBus);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setEventBus(eventBus: EventBus) {
|
|
32
|
+
this.eventBus = eventBus;
|
|
33
|
+
this.setupEvents();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private setupEvents() {
|
|
37
|
+
if (!this.eventBus) return;
|
|
38
|
+
const bus = this.eventBus;
|
|
39
|
+
|
|
40
|
+
const forward = (name: string) => (e: any) => bus.emit(name, e);
|
|
41
|
+
|
|
42
|
+
this.canvas.on("selection:created", forward("selection:created"));
|
|
43
|
+
this.canvas.on("selection:updated", forward("selection:updated"));
|
|
44
|
+
this.canvas.on("selection:cleared", forward("selection:cleared"));
|
|
45
|
+
this.canvas.on("object:modified", forward("object:modified"));
|
|
46
|
+
this.canvas.on("object:added", forward("object:added"));
|
|
47
|
+
this.canvas.on("object:removed", forward("object:removed"));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
dispose() {
|
|
51
|
+
this.canvas.dispose();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get a layer (Group) by its ID.
|
|
56
|
+
* We assume layers are Groups directly on the canvas with a data.id property.
|
|
57
|
+
*/
|
|
58
|
+
getLayer(id: string): Group | undefined {
|
|
59
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
|
|
60
|
+
| Group
|
|
61
|
+
| undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Create a layer (Group) with the given ID if it doesn't exist.
|
|
66
|
+
*/
|
|
67
|
+
createLayer(id: string, options: any = {}): Group {
|
|
68
|
+
let layer = this.getLayer(id);
|
|
69
|
+
if (!layer) {
|
|
70
|
+
const defaultOptions = {
|
|
71
|
+
selectable: false,
|
|
72
|
+
evented: false,
|
|
73
|
+
...options,
|
|
74
|
+
data: { ...options.data, id },
|
|
75
|
+
};
|
|
76
|
+
layer = new Group([], defaultOptions);
|
|
77
|
+
this.canvas.add(layer);
|
|
78
|
+
}
|
|
79
|
+
return layer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Find an object by ID, optionally within a specific layer.
|
|
84
|
+
*/
|
|
85
|
+
getObject(id: string, layerId?: string): FabricObject | undefined {
|
|
86
|
+
if (layerId) {
|
|
87
|
+
const layer = this.getLayer(layerId);
|
|
88
|
+
if (!layer) return undefined;
|
|
89
|
+
return layer.getObjects().find((obj: any) => obj.data?.id === id);
|
|
90
|
+
}
|
|
91
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
requestRenderAll() {
|
|
95
|
+
this.canvas.requestRenderAll();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
resize(width: number, height: number) {
|
|
99
|
+
this.canvas.setDimensions({ width, height });
|
|
100
|
+
this.viewport.updateContainer(width, height);
|
|
101
|
+
this.eventBus?.emit("canvas:resized", { width, height });
|
|
102
|
+
this.requestRenderAll();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async applyLayerSpec(spec: RenderLayerSpec): Promise<void> {
|
|
106
|
+
const layer = this.createLayer(spec.id, spec.props || {});
|
|
107
|
+
await this.applyObjectSpecsToContainer(layer, spec.objects);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async applyObjectSpecsToLayer(
|
|
111
|
+
layerId: string,
|
|
112
|
+
objects: RenderObjectSpec[],
|
|
113
|
+
): Promise<void> {
|
|
114
|
+
const layer = this.createLayer(layerId, {});
|
|
115
|
+
await this.applyObjectSpecsToContainer(layer, objects);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getRootLayerObjects(layerId: string): FabricObject[] {
|
|
119
|
+
return this.canvas
|
|
120
|
+
.getObjects()
|
|
121
|
+
.filter((obj: any) => obj?.data?.layerId === layerId);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async applyObjectSpecsToRootLayer(
|
|
125
|
+
layerId: string,
|
|
126
|
+
specs: RenderObjectSpec[],
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
const desiredIds = new Set(specs.map((s) => s.id));
|
|
129
|
+
const existing = this.getRootLayerObjects(layerId) as any[];
|
|
130
|
+
existing.forEach((obj) => {
|
|
131
|
+
const id = obj?.data?.id;
|
|
132
|
+
if (typeof id === "string" && !desiredIds.has(id)) {
|
|
133
|
+
this.canvas.remove(obj);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const byId = new Map<string, any>();
|
|
138
|
+
this.getRootLayerObjects(layerId).forEach((obj: any) => {
|
|
139
|
+
const id = obj?.data?.id;
|
|
140
|
+
if (typeof id === "string") byId.set(id, obj);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
for (let index = 0; index < specs.length; index += 1) {
|
|
144
|
+
const spec = specs[index];
|
|
145
|
+
let current = byId.get(spec.id);
|
|
146
|
+
if (
|
|
147
|
+
current &&
|
|
148
|
+
spec.type === "image" &&
|
|
149
|
+
spec.src &&
|
|
150
|
+
current.getSrc &&
|
|
151
|
+
current.getSrc() !== spec.src
|
|
152
|
+
) {
|
|
153
|
+
this.canvas.remove(current);
|
|
154
|
+
byId.delete(spec.id);
|
|
155
|
+
current = undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!current) {
|
|
159
|
+
const created = await this.createFabricObject(spec);
|
|
160
|
+
if (!created) continue;
|
|
161
|
+
this.patchFabricObject(created as any, spec, { layerId });
|
|
162
|
+
this.canvas.add(created as any);
|
|
163
|
+
byId.set(spec.id, created);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.patchFabricObject(current, spec, { layerId });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this.requestRenderAll();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private async applyObjectSpecsToContainer(
|
|
174
|
+
container: Group,
|
|
175
|
+
specs: RenderObjectSpec[],
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
const desiredIds = new Set(specs.map((s) => s.id));
|
|
178
|
+
const existing = container.getObjects() as any[];
|
|
179
|
+
existing.forEach((obj) => {
|
|
180
|
+
const id = obj?.data?.id;
|
|
181
|
+
if (typeof id === "string" && !desiredIds.has(id)) {
|
|
182
|
+
container.remove(obj);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const byId = new Map<string, any>();
|
|
187
|
+
(container.getObjects() as any[]).forEach((obj) => {
|
|
188
|
+
const id = obj?.data?.id;
|
|
189
|
+
if (typeof id === "string") byId.set(id, obj);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
for (let index = 0; index < specs.length; index += 1) {
|
|
193
|
+
const spec = specs[index];
|
|
194
|
+
let current = byId.get(spec.id);
|
|
195
|
+
if (
|
|
196
|
+
current &&
|
|
197
|
+
spec.type === "image" &&
|
|
198
|
+
spec.src &&
|
|
199
|
+
current.getSrc &&
|
|
200
|
+
current.getSrc() !== spec.src
|
|
201
|
+
) {
|
|
202
|
+
container.remove(current);
|
|
203
|
+
byId.delete(spec.id);
|
|
204
|
+
current = undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!current) {
|
|
208
|
+
const created = await this.createFabricObject(spec);
|
|
209
|
+
if (!created) continue;
|
|
210
|
+
container.add(created as any);
|
|
211
|
+
current = created as any;
|
|
212
|
+
byId.set(spec.id, current);
|
|
213
|
+
} else {
|
|
214
|
+
this.patchFabricObject(current, spec);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
this.moveObjectInContainer(container, current, index);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
container.dirty = true;
|
|
221
|
+
this.requestRenderAll();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private patchFabricObject(
|
|
225
|
+
obj: any,
|
|
226
|
+
spec: RenderObjectSpec,
|
|
227
|
+
extraData?: Record<string, any>,
|
|
228
|
+
) {
|
|
229
|
+
const nextData = {
|
|
230
|
+
...(obj.data || {}),
|
|
231
|
+
...(spec.data || {}),
|
|
232
|
+
...(extraData || {}),
|
|
233
|
+
id: spec.id,
|
|
234
|
+
};
|
|
235
|
+
obj.set({ ...(spec.props || {}), data: nextData });
|
|
236
|
+
obj.setCoords();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private moveObjectInContainer(
|
|
240
|
+
container: Group | Canvas,
|
|
241
|
+
obj: any,
|
|
242
|
+
index: number,
|
|
243
|
+
) {
|
|
244
|
+
if (!obj) return;
|
|
245
|
+
|
|
246
|
+
const moveObjectTo = (container as any).moveObjectTo;
|
|
247
|
+
if (typeof moveObjectTo === "function") {
|
|
248
|
+
moveObjectTo.call(container, obj, index);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const list = (container as any)._objects as any[] | undefined;
|
|
253
|
+
if (!Array.isArray(list)) return;
|
|
254
|
+
const from = list.indexOf(obj);
|
|
255
|
+
if (from < 0 || from === index) return;
|
|
256
|
+
list.splice(from, 1);
|
|
257
|
+
const target = Math.max(0, Math.min(index, list.length));
|
|
258
|
+
list.splice(target, 0, obj);
|
|
259
|
+
if (typeof (container as any)._onStackOrderChanged === "function") {
|
|
260
|
+
(container as any)._onStackOrderChanged();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private async createFabricObject(
|
|
265
|
+
spec: RenderObjectSpec,
|
|
266
|
+
): Promise<FabricObject | undefined> {
|
|
267
|
+
if (spec.type === "rect") {
|
|
268
|
+
const rect = new Rect({
|
|
269
|
+
...(spec.props || {}),
|
|
270
|
+
data: { ...(spec.data || {}), id: spec.id },
|
|
271
|
+
} as any);
|
|
272
|
+
rect.setCoords();
|
|
273
|
+
return rect;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (spec.type === "path") {
|
|
277
|
+
const pathData = (spec.props as any)?.path || (spec.props as any)?.pathData;
|
|
278
|
+
if (!pathData) return undefined;
|
|
279
|
+
const path = new Path(pathData, {
|
|
280
|
+
...(spec.props || {}),
|
|
281
|
+
data: { ...(spec.data || {}), id: spec.id },
|
|
282
|
+
} as any);
|
|
283
|
+
path.setCoords();
|
|
284
|
+
return path;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (spec.type === "image") {
|
|
288
|
+
if (!spec.src) return undefined;
|
|
289
|
+
const image = await Image.fromURL(spec.src, { crossOrigin: "anonymous" });
|
|
290
|
+
image.set({
|
|
291
|
+
...(spec.props || {}),
|
|
292
|
+
data: { ...(spec.data || {}), id: spec.id },
|
|
293
|
+
} as any);
|
|
294
|
+
image.setCoords();
|
|
295
|
+
return image as any;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
}
|
package/src/ViewportSystem.ts
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
import { Coordinate, Layout, Point, Size } from "./coordinate";
|
|
2
|
-
|
|
3
|
-
export class ViewportSystem {
|
|
4
|
-
private _containerSize: Size = { width: 0, height: 0 };
|
|
5
|
-
private _physicalSize: Size = { width: 0, height: 0 };
|
|
6
|
-
private _padding: number = 0;
|
|
7
|
-
private _layout: Layout = {
|
|
8
|
-
scale: 1,
|
|
9
|
-
offsetX: 0,
|
|
10
|
-
offsetY: 0,
|
|
11
|
-
width: 0,
|
|
12
|
-
height: 0,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
containerSize: Size = { width: 0, height: 0 },
|
|
17
|
-
physicalSize: Size = { width: 0, height: 0 },
|
|
18
|
-
padding: number = 40,
|
|
19
|
-
) {
|
|
20
|
-
this._containerSize = containerSize;
|
|
21
|
-
this._physicalSize = physicalSize;
|
|
22
|
-
this._padding = padding;
|
|
23
|
-
this.updateLayout();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get layout(): Layout {
|
|
27
|
-
return this._layout;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get scale(): number {
|
|
31
|
-
return this._layout.scale;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get offset(): Point {
|
|
35
|
-
return { x: this._layout.offsetX, y: this._layout.offsetY };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
updateContainer(width: number, height: number) {
|
|
39
|
-
if (
|
|
40
|
-
this._containerSize.width === width &&
|
|
41
|
-
this._containerSize.height === height
|
|
42
|
-
)
|
|
43
|
-
return;
|
|
44
|
-
this._containerSize = { width, height };
|
|
45
|
-
this.updateLayout();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
updatePhysical(width: number, height: number) {
|
|
49
|
-
if (this._physicalSize.width === width && this._physicalSize.height === height)
|
|
50
|
-
return;
|
|
51
|
-
this._physicalSize = { width, height };
|
|
52
|
-
this.updateLayout();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
setPadding(padding: number) {
|
|
56
|
-
if (this._padding === padding) return;
|
|
57
|
-
this._padding = padding;
|
|
58
|
-
this.updateLayout();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private updateLayout() {
|
|
62
|
-
this._layout = Coordinate.calculateLayout(
|
|
63
|
-
this._containerSize,
|
|
64
|
-
this._physicalSize,
|
|
65
|
-
this._padding,
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
toPixel(value: number): number {
|
|
70
|
-
return value * this._layout.scale;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
toPhysical(value: number): number {
|
|
74
|
-
return this._layout.scale === 0 ? 0 : value / this._layout.scale;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
toPixelPoint(point: Point): Point {
|
|
78
|
-
return {
|
|
79
|
-
x: point.x * this._layout.scale + this._layout.offsetX,
|
|
80
|
-
y: point.y * this._layout.scale + this._layout.offsetY,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
|
|
85
|
-
toPhysicalPoint(point: Point): Point {
|
|
86
|
-
if (this._layout.scale === 0) return { x: 0, y: 0 };
|
|
87
|
-
return {
|
|
88
|
-
x: (point.x - this._layout.offsetX) / this._layout.scale,
|
|
89
|
-
y: (point.y - this._layout.offsetY) / this._layout.scale,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
}
|
|
1
|
+
import { Coordinate, Layout, Point, Size } from "./coordinate";
|
|
2
|
+
|
|
3
|
+
export class ViewportSystem {
|
|
4
|
+
private _containerSize: Size = { width: 0, height: 0 };
|
|
5
|
+
private _physicalSize: Size = { width: 0, height: 0 };
|
|
6
|
+
private _padding: number = 0;
|
|
7
|
+
private _layout: Layout = {
|
|
8
|
+
scale: 1,
|
|
9
|
+
offsetX: 0,
|
|
10
|
+
offsetY: 0,
|
|
11
|
+
width: 0,
|
|
12
|
+
height: 0,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
containerSize: Size = { width: 0, height: 0 },
|
|
17
|
+
physicalSize: Size = { width: 0, height: 0 },
|
|
18
|
+
padding: number = 40,
|
|
19
|
+
) {
|
|
20
|
+
this._containerSize = containerSize;
|
|
21
|
+
this._physicalSize = physicalSize;
|
|
22
|
+
this._padding = padding;
|
|
23
|
+
this.updateLayout();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get layout(): Layout {
|
|
27
|
+
return this._layout;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get scale(): number {
|
|
31
|
+
return this._layout.scale;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get offset(): Point {
|
|
35
|
+
return { x: this._layout.offsetX, y: this._layout.offsetY };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
updateContainer(width: number, height: number) {
|
|
39
|
+
if (
|
|
40
|
+
this._containerSize.width === width &&
|
|
41
|
+
this._containerSize.height === height
|
|
42
|
+
)
|
|
43
|
+
return;
|
|
44
|
+
this._containerSize = { width, height };
|
|
45
|
+
this.updateLayout();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
updatePhysical(width: number, height: number) {
|
|
49
|
+
if (this._physicalSize.width === width && this._physicalSize.height === height)
|
|
50
|
+
return;
|
|
51
|
+
this._physicalSize = { width, height };
|
|
52
|
+
this.updateLayout();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setPadding(padding: number) {
|
|
56
|
+
if (this._padding === padding) return;
|
|
57
|
+
this._padding = padding;
|
|
58
|
+
this.updateLayout();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private updateLayout() {
|
|
62
|
+
this._layout = Coordinate.calculateLayout(
|
|
63
|
+
this._containerSize,
|
|
64
|
+
this._physicalSize,
|
|
65
|
+
this._padding,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
toPixel(value: number): number {
|
|
70
|
+
return value * this._layout.scale;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
toPhysical(value: number): number {
|
|
74
|
+
return this._layout.scale === 0 ? 0 : value / this._layout.scale;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
toPixelPoint(point: Point): Point {
|
|
78
|
+
return {
|
|
79
|
+
x: point.x * this._layout.scale + this._layout.offsetX,
|
|
80
|
+
y: point.y * this._layout.scale + this._layout.offsetY,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
|
|
85
|
+
toPhysicalPoint(point: Point): Point {
|
|
86
|
+
if (this._layout.scale === 0) return { x: 0, y: 0 };
|
|
87
|
+
return {
|
|
88
|
+
x: (point.x - this._layout.offsetX) / this._layout.scale,
|
|
89
|
+
y: (point.y - this._layout.offsetY) / this._layout.scale,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|