@gradio/imageeditor 0.14.5 → 0.15.1

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/InteractiveImageEditor.svelte +4 -7
  3. package/dist/InteractiveImageEditor.svelte +4 -3
  4. package/dist/InteractiveImageEditor.svelte.d.ts +1 -1
  5. package/dist/shared/Controls.svelte +32 -1
  6. package/dist/shared/Controls.svelte.d.ts +2 -0
  7. package/dist/shared/ImageEditor.svelte +23 -1
  8. package/dist/shared/ImageEditor.svelte.d.ts +2 -1
  9. package/dist/shared/brush/brush-cursor.js +0 -42
  10. package/dist/shared/brush/brush-textures.d.ts +19 -2
  11. package/dist/shared/brush/brush-textures.js +120 -82
  12. package/dist/shared/brush/brush.js +0 -52
  13. package/dist/shared/{utils → core}/commands.d.ts +19 -58
  14. package/dist/shared/core/commands.js +70 -0
  15. package/dist/shared/core/editor.d.ts +4 -91
  16. package/dist/shared/core/editor.js +18 -708
  17. package/dist/shared/core/layers.d.ts +143 -0
  18. package/dist/shared/core/layers.js +774 -0
  19. package/dist/shared/image/image.d.ts +14 -4
  20. package/dist/shared/image/image.js +91 -18
  21. package/dist/shared/resize/resize.d.ts +31 -0
  22. package/dist/shared/resize/resize.js +127 -290
  23. package/dist/shared/zoom/zoom.js +0 -51
  24. package/package.json +7 -7
  25. package/shared/Controls.svelte +33 -1
  26. package/shared/ImageEditor.svelte +25 -9
  27. package/shared/Toolbar.svelte +1 -4
  28. package/shared/brush/ColorSwatch.svelte +0 -3
  29. package/shared/brush/brush-cursor.ts +0 -42
  30. package/shared/brush/brush-textures.ts +166 -83
  31. package/shared/brush/brush.ts +0 -53
  32. package/shared/core/commands.ts +118 -0
  33. package/shared/core/editor.ts +33 -957
  34. package/shared/core/layers.ts +1068 -0
  35. package/shared/image/image.ts +139 -21
  36. package/shared/resize/resize.ts +160 -299
  37. package/shared/zoom/zoom.ts +0 -51
  38. package/dist/shared/utils/commands.js +0 -73
  39. package/shared/utils/commands.ts +0 -174
@@ -13,7 +13,6 @@ import { clear_timeout } from "./brush-utils";
13
13
  * Class to handle all cursor preview-related functionality for the brush tool.
14
14
  */
15
15
  export class BrushCursor {
16
- // Cursor-related properties
17
16
  private cursor_graphics: Graphics | null = null;
18
17
  private cursor_container: Container | null = null;
19
18
  private brush_preview_container: Container | null = null;
@@ -23,7 +22,6 @@ export class BrushCursor {
23
22
  private cursor_position_check_timeout: number | null = null;
24
23
  private is_brush_or_erase_active = false;
25
24
 
26
- // Event handlers
27
25
  private _bound_update_cursor:
28
26
  | ((event: FederatedPointerEvent) => void)
29
27
  | null = null;
@@ -51,21 +49,16 @@ export class BrushCursor {
51
49
  * Sets up cursor-related event listeners.
52
50
  */
53
51
  setup_event_listeners(): void {
54
- // Clean up any existing listeners
55
52
  this.cleanup_event_listeners();
56
53
 
57
- // Create bound event handlers
58
54
  this._bound_update_cursor = this.update_cursor_position.bind(this);
59
55
  this._bound_check_cursor_over_image =
60
56
  this.check_cursor_over_image.bind(this);
61
57
 
62
- // Add event listeners using Pixi event system
63
- // Use app.stage for global events like pointermove
64
58
  const stage = this.image_editor_context.app.stage;
65
59
  stage.on("pointermove", this._bound_update_cursor);
66
60
  stage.on("pointermove", this._bound_check_cursor_over_image);
67
61
 
68
- // Add event listeners to the image container for enter/leave events
69
62
  this.image_editor_context.image_container.on(
70
63
  "pointerenter",
71
64
  this.on_image_container_pointer_enter.bind(this)
@@ -80,10 +73,8 @@ export class BrushCursor {
80
73
  * Removes all cursor-related event listeners.
81
74
  */
82
75
  cleanup_event_listeners(): void {
83
- // Use Pixi event system for removal
84
76
  const stage = this.image_editor_context.app.stage;
85
77
 
86
- // Remove event listeners if they exist
87
78
  if (this._bound_update_cursor) {
88
79
  stage.off("pointermove", this._bound_update_cursor);
89
80
  this._bound_update_cursor = null;
@@ -94,11 +85,9 @@ export class BrushCursor {
94
85
  this._bound_check_cursor_over_image = null;
95
86
  }
96
87
 
97
- // Remove image container event listeners
98
88
  this.image_editor_context.image_container.off("pointerenter");
99
89
  this.image_editor_context.image_container.off("pointerleave");
100
90
 
101
- // Clear timeout
102
91
  this.cursor_position_check_timeout = clear_timeout(
103
92
  this.cursor_position_check_timeout
104
93
  );
@@ -108,7 +97,6 @@ export class BrushCursor {
108
97
  * initializes the cursor for the brush tool.
109
98
  */
110
99
  initialize_cursor(): void {
111
- // clean up existing cursor if it exists
112
100
  if (this.cursor_container) {
113
101
  if (this.cursor_container.parent) {
114
102
  this.cursor_container.parent.removeChild(this.cursor_container);
@@ -117,15 +105,12 @@ export class BrushCursor {
117
105
  this.cursor_container = null;
118
106
  }
119
107
 
120
- // create new cursor container and add to ui container
121
108
  this.cursor_container = new Container();
122
109
  this.image_editor_context.ui_container.addChild(this.cursor_container);
123
110
 
124
- // create cursor graphics
125
111
  this.cursor_graphics = new Graphics();
126
112
  this.cursor_container.addChild(this.cursor_graphics);
127
113
 
128
- // draw initial cursor
129
114
  this.update_cursor_appearance();
130
115
  }
131
116
 
@@ -135,16 +120,13 @@ export class BrushCursor {
135
120
  update_cursor_appearance(): void {
136
121
  if (!this.cursor_graphics) return;
137
122
 
138
- // clear previous graphics
139
123
  this.cursor_graphics.clear();
140
124
 
141
- // set color based on mode
142
125
  const cursor_color =
143
126
  this.state.mode === "draw"
144
127
  ? tinycolor(this.state.color).toString()
145
128
  : 0xffffff;
146
129
 
147
- // draw circle with current brush size
148
130
  this.cursor_graphics
149
131
  .circle(0, 0, this.state.brush_size * this.scale)
150
132
  .stroke({
@@ -153,7 +135,6 @@ export class BrushCursor {
153
135
  alpha: 0.8
154
136
  });
155
137
 
156
- // add a small dot in the center for precision
157
138
  this.cursor_graphics.circle(0, 0, 1).fill({
158
139
  color: cursor_color,
159
140
  alpha: 0.8
@@ -167,17 +148,14 @@ export class BrushCursor {
167
148
  update_cursor_position(event: FederatedPointerEvent): void {
168
149
  if (!this.cursor_container) return;
169
150
 
170
- // get position in image coordinates
171
151
  const local_pos = this.image_editor_context.image_container.toLocal(
172
152
  event.global
173
153
  );
174
154
 
175
- // convert to ui coordinates
176
155
  const ui_pos = this.image_editor_context.ui_container.toLocal(
177
156
  this.image_editor_context.image_container.toGlobal(local_pos)
178
157
  );
179
158
 
180
- // update cursor position
181
159
  this.cursor_container.position.set(ui_pos.x, ui_pos.y);
182
160
  }
183
161
 
@@ -186,17 +164,14 @@ export class BrushCursor {
186
164
  * @param {FederatedPointerEvent} event - The pointer event.
187
165
  */
188
166
  check_cursor_over_image(event: FederatedPointerEvent): void {
189
- // cancel any pending timeout
190
167
  if (this.cursor_position_check_timeout !== null) {
191
168
  window.clearTimeout(this.cursor_position_check_timeout);
192
169
  this.cursor_position_check_timeout = null;
193
170
  }
194
171
 
195
- // get the image container bounds in global coordinates
196
172
  const image_container = this.image_editor_context.image_container;
197
173
  const bounds = image_container.getBounds();
198
174
 
199
- // check if cursor is over image
200
175
  const was_over_image = this.is_cursor_over_image;
201
176
  this.is_cursor_over_image =
202
177
  event.global.x >= bounds.x &&
@@ -204,7 +179,6 @@ export class BrushCursor {
204
179
  event.global.y >= bounds.y &&
205
180
  event.global.y <= bounds.y + bounds.height;
206
181
 
207
- // update visibility if state changed
208
182
  if (was_over_image !== this.is_cursor_over_image) {
209
183
  this.update_cursor_and_preview_visibility();
210
184
  }
@@ -248,7 +222,6 @@ export class BrushCursor {
248
222
  }
249
223
 
250
224
  if (show) {
251
- // update preview appearance and position
252
225
  this.update_brush_preview();
253
226
  this.update_brush_preview_position();
254
227
  }
@@ -258,7 +231,6 @@ export class BrushCursor {
258
231
  * initializes the brush preview.
259
232
  */
260
233
  initialize_brush_preview(): void {
261
- // clean up existing preview if it exists
262
234
  if (this.brush_preview_container) {
263
235
  if (this.brush_preview_container.parent) {
264
236
  this.brush_preview_container.parent.removeChild(
@@ -269,17 +241,14 @@ export class BrushCursor {
269
241
  this.brush_preview_container = null;
270
242
  }
271
243
 
272
- // create new preview container and add to ui container
273
244
  this.brush_preview_container = new Container();
274
245
  this.image_editor_context.ui_container.addChild(
275
246
  this.brush_preview_container
276
247
  );
277
248
 
278
- // create preview graphics
279
249
  this.brush_preview_graphics = new Graphics();
280
250
  this.brush_preview_container.addChild(this.brush_preview_graphics);
281
251
 
282
- // set initial visibility
283
252
  this.brush_preview_container.visible = false;
284
253
  }
285
254
 
@@ -289,23 +258,19 @@ export class BrushCursor {
289
258
  update_brush_preview(): void {
290
259
  if (!this.brush_preview_graphics) return;
291
260
 
292
- // clear previous graphics
293
261
  this.brush_preview_graphics.clear();
294
262
 
295
- // draw brush preview based on current settings
296
263
  const preview_size = this.state.brush_size * this.scale;
297
264
  const preview_color =
298
265
  this.state.mode === "draw"
299
266
  ? tinycolor(this.state.color).setAlpha(this.state.opacity).toString()
300
267
  : 0xffffff;
301
268
 
302
- // draw circle with current brush settings
303
269
  this.brush_preview_graphics.circle(0, 0, preview_size).fill({
304
270
  color: preview_color,
305
271
  alpha: this.state.mode === "draw" ? this.state.opacity : 0.3
306
272
  });
307
273
 
308
- // add outline
309
274
  this.brush_preview_graphics.circle(0, 0, preview_size + 1).stroke({
310
275
  width: 1,
311
276
  color: 0x000000,
@@ -319,17 +284,14 @@ export class BrushCursor {
319
284
  update_brush_preview_position(): void {
320
285
  if (!this.brush_preview_container) return;
321
286
 
322
- // get the center of the image in global coordinates
323
287
  const image_container = this.image_editor_context.image_container;
324
288
  const center_x = image_container.width / 2;
325
289
  const center_y = image_container.height / 2;
326
290
 
327
291
  const global_pos = image_container.toGlobal({ x: center_x, y: center_y });
328
292
 
329
- // convert to ui coordinates
330
293
  const ui_pos = this.image_editor_context.ui_container.toLocal(global_pos);
331
294
 
332
- // update preview position
333
295
  this.brush_preview_container.position.set(ui_pos.x, ui_pos.y);
334
296
  }
335
297
 
@@ -364,10 +326,8 @@ export class BrushCursor {
364
326
  * Cleans up all cursor resources.
365
327
  */
366
328
  cleanup(): void {
367
- // Reset active state
368
329
  this.is_brush_or_erase_active = false;
369
330
 
370
- // clean up cursor
371
331
  if (this.cursor_container) {
372
332
  if (this.cursor_container.parent) {
373
333
  this.cursor_container.parent.removeChild(this.cursor_container);
@@ -376,7 +336,6 @@ export class BrushCursor {
376
336
  this.cursor_container = null;
377
337
  }
378
338
 
379
- // clean up preview
380
339
  if (this.brush_preview_container) {
381
340
  if (this.brush_preview_container.parent) {
382
341
  this.brush_preview_container.parent.removeChild(
@@ -387,7 +346,6 @@ export class BrushCursor {
387
346
  this.brush_preview_container = null;
388
347
  }
389
348
 
390
- // clear timeout
391
349
  if (this.cursor_position_check_timeout !== null) {
392
350
  window.clearTimeout(this.cursor_position_check_timeout);
393
351
  this.cursor_position_check_timeout = null;