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