@pooder/kit 4.1.0 → 4.3.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/CHANGELOG.md +12 -0
- package/dist/index.d.mts +62 -10
- package/dist/index.d.ts +62 -10
- package/dist/index.js +756 -348
- package/dist/index.mjs +753 -347
- package/package.json +3 -2
- package/src/CanvasService.ts +96 -89
- package/src/ViewportSystem.ts +92 -0
- package/src/background.ts +230 -230
- package/src/constraints.ts +191 -27
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +955 -871
- package/src/feature.ts +282 -195
- package/src/featureComplete.ts +46 -0
- package/src/film.ts +194 -194
- package/src/geometry.ts +161 -30
- package/src/image.ts +512 -512
- package/src/index.ts +10 -9
- package/src/mirror.ts +128 -128
- package/src/ruler.ts +508 -500
- package/src/tracer.ts +570 -570
- package/src/units.ts +27 -0
- package/src/white-ink.ts +373 -373
- package/tsconfig.test.json +15 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pooder/kit",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Standard plugins for Pooder editor",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
26
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
26
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
27
|
+
"test": "tsc -p tsconfig.test.json && node .test-dist/tests/run.js"
|
|
27
28
|
}
|
|
28
29
|
}
|
package/src/CanvasService.ts
CHANGED
|
@@ -1,89 +1,96 @@
|
|
|
1
|
-
import { Canvas, Group, FabricObject } from "fabric";
|
|
2
|
-
import { Service, EventBus } from "@pooder/core";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.canvas =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.canvas.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
}
|
|
1
|
+
import { Canvas, Group, FabricObject } from "fabric";
|
|
2
|
+
import { Service, EventBus } from "@pooder/core";
|
|
3
|
+
import { ViewportSystem } from "./ViewportSystem";
|
|
4
|
+
|
|
5
|
+
export default class CanvasService implements Service {
|
|
6
|
+
public canvas: Canvas;
|
|
7
|
+
public viewport: ViewportSystem;
|
|
8
|
+
private eventBus?: EventBus;
|
|
9
|
+
|
|
10
|
+
constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
|
|
11
|
+
if (el instanceof Canvas) {
|
|
12
|
+
this.canvas = el;
|
|
13
|
+
} else {
|
|
14
|
+
this.canvas = new Canvas(el, {
|
|
15
|
+
preserveObjectStacking: true,
|
|
16
|
+
...options,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.viewport = new ViewportSystem();
|
|
21
|
+
if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
|
|
22
|
+
this.viewport.updateContainer(this.canvas.width, this.canvas.height);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (options?.eventBus) {
|
|
26
|
+
this.setEventBus(options.eventBus);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setEventBus(eventBus: EventBus) {
|
|
31
|
+
this.eventBus = eventBus;
|
|
32
|
+
this.setupEvents();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private setupEvents() {
|
|
36
|
+
if (!this.eventBus) return;
|
|
37
|
+
const bus = this.eventBus;
|
|
38
|
+
|
|
39
|
+
const forward = (name: string) => (e: any) => bus.emit(name, e);
|
|
40
|
+
|
|
41
|
+
this.canvas.on("selection:created", forward("selection:created"));
|
|
42
|
+
this.canvas.on("selection:updated", forward("selection:updated"));
|
|
43
|
+
this.canvas.on("selection:cleared", forward("selection:cleared"));
|
|
44
|
+
this.canvas.on("object:modified", forward("object:modified"));
|
|
45
|
+
this.canvas.on("object:added", forward("object:added"));
|
|
46
|
+
this.canvas.on("object:removed", forward("object:removed"));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
dispose() {
|
|
50
|
+
this.canvas.dispose();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get a layer (Group) by its ID.
|
|
55
|
+
* We assume layers are Groups directly on the canvas with a data.id property.
|
|
56
|
+
*/
|
|
57
|
+
getLayer(id: string): Group | undefined {
|
|
58
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
|
|
59
|
+
| Group
|
|
60
|
+
| undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Create a layer (Group) with the given ID if it doesn't exist.
|
|
65
|
+
*/
|
|
66
|
+
createLayer(id: string, options: any = {}): Group {
|
|
67
|
+
let layer = this.getLayer(id);
|
|
68
|
+
if (!layer) {
|
|
69
|
+
const defaultOptions = {
|
|
70
|
+
selectable: false,
|
|
71
|
+
evented: false,
|
|
72
|
+
...options,
|
|
73
|
+
data: { ...options.data, id },
|
|
74
|
+
};
|
|
75
|
+
layer = new Group([], defaultOptions);
|
|
76
|
+
this.canvas.add(layer);
|
|
77
|
+
}
|
|
78
|
+
return layer;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Find an object by ID, optionally within a specific layer.
|
|
83
|
+
*/
|
|
84
|
+
getObject(id: string, layerId?: string): FabricObject | undefined {
|
|
85
|
+
if (layerId) {
|
|
86
|
+
const layer = this.getLayer(layerId);
|
|
87
|
+
if (!layer) return undefined;
|
|
88
|
+
return layer.getObjects().find((obj: any) => obj.data?.id === id);
|
|
89
|
+
}
|
|
90
|
+
return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
requestRenderAll() {
|
|
94
|
+
this.canvas.requestRenderAll();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +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
|
+
}
|