@pooder/kit 0.0.2 → 2.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/CHANGELOG.md +26 -9
- package/dist/index.d.mts +29 -6
- package/dist/index.d.ts +29 -6
- package/dist/index.js +405 -62
- package/dist/index.mjs +435 -68
- package/package.json +2 -2
- package/src/background.ts +183 -173
- package/src/dieline.ts +580 -424
- package/src/film.ts +163 -155
- package/src/geometry.ts +251 -244
- package/src/hole.ts +493 -413
- package/src/image.ts +304 -146
- package/src/index.ts +8 -7
- package/src/mirror.ts +91 -0
- package/src/ruler.ts +255 -238
- package/src/white-ink.ts +329 -302
- package/tsconfig.json +13 -13
package/src/white-ink.ts
CHANGED
|
@@ -1,302 +1,329 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from
|
|
13
|
-
|
|
14
|
-
interface WhiteInkToolOptions {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
export class WhiteInkTool implements Extension<WhiteInkToolOptions> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const userImage = editor.getObject("user-image", "user")
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
1
|
+
import {
|
|
2
|
+
Command,
|
|
3
|
+
Editor,
|
|
4
|
+
EditorState,
|
|
5
|
+
EventHandler,
|
|
6
|
+
Extension,
|
|
7
|
+
OptionSchema,
|
|
8
|
+
Image,
|
|
9
|
+
filters,
|
|
10
|
+
PooderObject,
|
|
11
|
+
PooderLayer,
|
|
12
|
+
} from "@pooder/core";
|
|
13
|
+
|
|
14
|
+
interface WhiteInkToolOptions {
|
|
15
|
+
customMask: string;
|
|
16
|
+
opacity: number;
|
|
17
|
+
enableClip: boolean;
|
|
18
|
+
}
|
|
19
|
+
export class WhiteInkTool implements Extension<WhiteInkToolOptions> {
|
|
20
|
+
public name = "WhiteInkTool";
|
|
21
|
+
public options: WhiteInkToolOptions = {
|
|
22
|
+
customMask: "",
|
|
23
|
+
opacity: 1,
|
|
24
|
+
enableClip: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
public schema: Record<keyof WhiteInkToolOptions, OptionSchema> = {
|
|
28
|
+
customMask: { type: "string", label: "Custom Mask URL" },
|
|
29
|
+
opacity: { type: "number", min: 0, max: 1, step: 0.01, label: "Opacity" },
|
|
30
|
+
enableClip: { type: "boolean", label: "Enable Clip" },
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
private syncHandler: EventHandler | undefined;
|
|
34
|
+
|
|
35
|
+
onMount(editor: Editor) {
|
|
36
|
+
this.setup(editor);
|
|
37
|
+
this.updateWhiteInk(editor, this.options);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
onUnmount(editor: Editor) {
|
|
41
|
+
this.teardown(editor);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
onDestroy(editor: Editor) {
|
|
45
|
+
this.teardown(editor);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private setup(editor: Editor) {
|
|
49
|
+
let userLayer = editor.getLayer("user");
|
|
50
|
+
if (!userLayer) {
|
|
51
|
+
userLayer = new PooderLayer([], {
|
|
52
|
+
width: editor.state.width,
|
|
53
|
+
height: editor.state.height,
|
|
54
|
+
left: 0,
|
|
55
|
+
top: 0,
|
|
56
|
+
originX: "left",
|
|
57
|
+
originY: "top",
|
|
58
|
+
selectable: false,
|
|
59
|
+
evented: true,
|
|
60
|
+
subTargetCheck: true,
|
|
61
|
+
interactive: true,
|
|
62
|
+
data: {
|
|
63
|
+
id: "user",
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
editor.canvas.add(userLayer);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!this.syncHandler) {
|
|
70
|
+
this.syncHandler = (e: any) => {
|
|
71
|
+
const target = e.target;
|
|
72
|
+
if (target && target.data?.id === "user-image") {
|
|
73
|
+
this.syncWithUserImage(editor);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
editor.canvas.on("object:moving", this.syncHandler);
|
|
78
|
+
editor.canvas.on("object:scaling", this.syncHandler);
|
|
79
|
+
editor.canvas.on("object:rotating", this.syncHandler);
|
|
80
|
+
editor.canvas.on("object:modified", this.syncHandler);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private teardown(editor: Editor) {
|
|
85
|
+
if (this.syncHandler) {
|
|
86
|
+
editor.canvas.off("object:moving", this.syncHandler);
|
|
87
|
+
editor.canvas.off("object:scaling", this.syncHandler);
|
|
88
|
+
editor.canvas.off("object:rotating", this.syncHandler);
|
|
89
|
+
editor.canvas.off("object:modified", this.syncHandler);
|
|
90
|
+
this.syncHandler = undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const layer = editor.getLayer("user");
|
|
94
|
+
if (layer) {
|
|
95
|
+
const whiteInk = editor.getObject("white-ink", "user");
|
|
96
|
+
if (whiteInk) {
|
|
97
|
+
layer.remove(whiteInk);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const userImage = editor.getObject("user-image", "user") as any;
|
|
102
|
+
if (userImage && userImage.clipPath) {
|
|
103
|
+
userImage.set({ clipPath: undefined });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
editor.canvas.requestRenderAll();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
onUpdate(editor: Editor, state: EditorState) {
|
|
110
|
+
this.updateWhiteInk(editor, this.options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
commands: Record<string, Command> = {
|
|
114
|
+
setWhiteInkImage: {
|
|
115
|
+
execute: (
|
|
116
|
+
editor: Editor,
|
|
117
|
+
customMask: string,
|
|
118
|
+
opacity: number,
|
|
119
|
+
enableClip: boolean = true,
|
|
120
|
+
) => {
|
|
121
|
+
if (
|
|
122
|
+
this.options.customMask === customMask &&
|
|
123
|
+
this.options.opacity === opacity &&
|
|
124
|
+
this.options.enableClip === enableClip
|
|
125
|
+
)
|
|
126
|
+
return true;
|
|
127
|
+
|
|
128
|
+
this.options.customMask = customMask;
|
|
129
|
+
this.options.opacity = opacity;
|
|
130
|
+
this.options.enableClip = enableClip;
|
|
131
|
+
|
|
132
|
+
this.updateWhiteInk(editor, this.options);
|
|
133
|
+
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
schema: {
|
|
137
|
+
customMask: {
|
|
138
|
+
type: "string",
|
|
139
|
+
label: "Custom Mask URL",
|
|
140
|
+
required: true,
|
|
141
|
+
},
|
|
142
|
+
opacity: {
|
|
143
|
+
type: "number",
|
|
144
|
+
label: "Opacity",
|
|
145
|
+
min: 0,
|
|
146
|
+
max: 1,
|
|
147
|
+
required: true,
|
|
148
|
+
},
|
|
149
|
+
enableClip: {
|
|
150
|
+
type: "boolean",
|
|
151
|
+
label: "Enable Clip",
|
|
152
|
+
default: true,
|
|
153
|
+
required: false,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
private updateWhiteInk(editor: Editor, opts: WhiteInkToolOptions) {
|
|
160
|
+
const { customMask, opacity, enableClip } = opts;
|
|
161
|
+
|
|
162
|
+
const layer = editor.getLayer("user");
|
|
163
|
+
if (!layer) {
|
|
164
|
+
console.warn("[WhiteInkTool] User layer not found");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const whiteInk = editor.getObject("white-ink", "user") as any;
|
|
169
|
+
const userImage = editor.getObject("user-image", "user") as any;
|
|
170
|
+
|
|
171
|
+
if (!customMask) {
|
|
172
|
+
if (whiteInk) {
|
|
173
|
+
layer.remove(whiteInk);
|
|
174
|
+
}
|
|
175
|
+
if (userImage && userImage.clipPath) {
|
|
176
|
+
userImage.set({ clipPath: undefined });
|
|
177
|
+
}
|
|
178
|
+
editor.canvas.requestRenderAll();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Check if we need to load/reload white ink backing
|
|
183
|
+
if (whiteInk) {
|
|
184
|
+
const currentSrc = whiteInk.getSrc?.() || whiteInk._element?.src;
|
|
185
|
+
if (currentSrc !== customMask) {
|
|
186
|
+
this.loadWhiteInk(
|
|
187
|
+
editor,
|
|
188
|
+
layer,
|
|
189
|
+
customMask,
|
|
190
|
+
opacity,
|
|
191
|
+
enableClip,
|
|
192
|
+
whiteInk,
|
|
193
|
+
);
|
|
194
|
+
} else {
|
|
195
|
+
if (whiteInk.opacity !== opacity) {
|
|
196
|
+
whiteInk.set({ opacity });
|
|
197
|
+
editor.canvas.requestRenderAll();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
this.loadWhiteInk(editor, layer, customMask, opacity, enableClip);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Handle Clip Path Toggle
|
|
205
|
+
if (userImage) {
|
|
206
|
+
if (enableClip) {
|
|
207
|
+
// If enabled but missing, or mask changed (handled by re-load above, but good to ensure), apply it
|
|
208
|
+
// We check if clipPath is present. Ideally we should check if it matches current mask,
|
|
209
|
+
// but re-applying is safe.
|
|
210
|
+
if (!userImage.clipPath) {
|
|
211
|
+
this.applyClipPath(editor, customMask);
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
// If disabled but present, remove it
|
|
215
|
+
if (userImage.clipPath) {
|
|
216
|
+
userImage.set({ clipPath: undefined });
|
|
217
|
+
editor.canvas.requestRenderAll();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private loadWhiteInk(
|
|
224
|
+
editor: Editor,
|
|
225
|
+
layer: PooderLayer,
|
|
226
|
+
url: string,
|
|
227
|
+
opacity: number,
|
|
228
|
+
enableClip: boolean,
|
|
229
|
+
oldImage?: any,
|
|
230
|
+
) {
|
|
231
|
+
Image.fromURL(url, { crossOrigin: "anonymous" })
|
|
232
|
+
.then((image) => {
|
|
233
|
+
if (oldImage) {
|
|
234
|
+
// Remove old image but don't copy properties yet, we'll sync with user-image
|
|
235
|
+
layer.remove(oldImage);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
image.filters?.push(
|
|
239
|
+
new filters.BlendColor({
|
|
240
|
+
color: "#FFFFFF",
|
|
241
|
+
mode: "add",
|
|
242
|
+
}),
|
|
243
|
+
);
|
|
244
|
+
image.applyFilters();
|
|
245
|
+
|
|
246
|
+
image.set({
|
|
247
|
+
opacity,
|
|
248
|
+
selectable: false,
|
|
249
|
+
evented: false,
|
|
250
|
+
data: {
|
|
251
|
+
id: "white-ink",
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// Add to layer
|
|
256
|
+
layer.add(image);
|
|
257
|
+
|
|
258
|
+
// Ensure white-ink is behind user-image
|
|
259
|
+
const userImage = editor.getObject("user-image", "user");
|
|
260
|
+
if (userImage) {
|
|
261
|
+
// Re-adding moves it to the top of the stack
|
|
262
|
+
layer.remove(userImage);
|
|
263
|
+
layer.add(userImage);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Apply clip path to user-image if enabled
|
|
267
|
+
if (enableClip) {
|
|
268
|
+
this.applyClipPath(editor, url);
|
|
269
|
+
} else if (userImage) {
|
|
270
|
+
userImage.set({ clipPath: undefined });
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Sync position immediately
|
|
274
|
+
this.syncWithUserImage(editor);
|
|
275
|
+
|
|
276
|
+
editor.canvas.requestRenderAll();
|
|
277
|
+
})
|
|
278
|
+
.catch((err) => {
|
|
279
|
+
console.error("Failed to load white ink mask", url, err);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private applyClipPath(editor: Editor, url: string) {
|
|
284
|
+
const userImage = editor.getObject("user-image", "user") as any;
|
|
285
|
+
if (!userImage) return;
|
|
286
|
+
|
|
287
|
+
Image.fromURL(url, { crossOrigin: "anonymous" })
|
|
288
|
+
.then((maskImage) => {
|
|
289
|
+
// Configure clipPath
|
|
290
|
+
// It needs to be relative to the object center
|
|
291
|
+
maskImage.set({
|
|
292
|
+
originX: "center",
|
|
293
|
+
originY: "center",
|
|
294
|
+
left: 0,
|
|
295
|
+
top: 0,
|
|
296
|
+
// Scale to fit userImage if dimensions differ
|
|
297
|
+
scaleX: userImage.width / maskImage.width,
|
|
298
|
+
scaleY: userImage.height / maskImage.height,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
userImage.set({ clipPath: maskImage });
|
|
302
|
+
editor.canvas.requestRenderAll();
|
|
303
|
+
})
|
|
304
|
+
.catch((err) => {
|
|
305
|
+
console.error("Failed to load clip path", url, err);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private syncWithUserImage(editor: Editor) {
|
|
310
|
+
const userImage = editor.getObject("user-image", "user");
|
|
311
|
+
const whiteInk = editor.getObject("white-ink", "user");
|
|
312
|
+
|
|
313
|
+
if (userImage && whiteInk) {
|
|
314
|
+
whiteInk.set({
|
|
315
|
+
left: userImage.left,
|
|
316
|
+
top: userImage.top,
|
|
317
|
+
scaleX: userImage.scaleX,
|
|
318
|
+
scaleY: userImage.scaleY,
|
|
319
|
+
angle: userImage.angle,
|
|
320
|
+
skewX: userImage.skewX,
|
|
321
|
+
skewY: userImage.skewY,
|
|
322
|
+
flipX: userImage.flipX,
|
|
323
|
+
flipY: userImage.flipY,
|
|
324
|
+
originX: userImage.originX,
|
|
325
|
+
originY: userImage.originY,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|