@gradio/imageeditor 0.16.2 → 0.16.3

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @gradio/imageeditor
2
2
 
3
+ ## 0.16.3
4
+
5
+ ### Fixes
6
+
7
+ - [#11667](https://github.com/gradio-app/gradio/pull/11667) [`07d19f7`](https://github.com/gradio-app/gradio/commit/07d19f7af3b60ff4a931d2c6740c9d61e6685dcb) - Fix the ImageEditor's auto-zoom, canvas resize, and zoom functionality. Thanks @pngwn!
8
+
9
+ ### Dependency updates
10
+
11
+ - @gradio/upload@0.16.13
12
+ - @gradio/client@1.16.0
13
+ - @gradio/image@0.22.15
14
+
3
15
  ## 0.16.2
4
16
 
5
17
  ### Dependency updates
@@ -118,6 +118,7 @@ export async function add_image_from_url(source) {
118
118
  crop.set_tool("image");
119
119
  crop.set_subtool("crop");
120
120
  background_image = true;
121
+ zoom.set_zoom("fit");
121
122
  dispatch("upload");
122
123
  dispatch("input");
123
124
  } catch (error) {
@@ -289,6 +290,8 @@ async function init_image_editor() {
289
290
  }
290
291
  $:
291
292
  if (background == null && layers.length == 0 && composite == null && editor && ready) {
293
+ editor.reset_canvas();
294
+ zoom.set_zoom("fit");
292
295
  handle_tool_change({ tool: "image" });
293
296
  background_image = false;
294
297
  has_drawn = false;
@@ -306,6 +309,7 @@ async function handle_files(files) {
306
309
  await crop.add_image({ image: _file });
307
310
  crop.reset();
308
311
  background_image = true;
312
+ zoom.set_zoom("fit");
309
313
  handle_tool_change({ tool: "draw" });
310
314
  dispatch("upload");
311
315
  dispatch("input");
@@ -73,6 +73,8 @@ export declare class ImageEditor {
73
73
  private target_element;
74
74
  private width;
75
75
  private height;
76
+ private original_width;
77
+ private original_height;
76
78
  dimensions: Spring<{
77
79
  width: number;
78
80
  height: number;
@@ -85,6 +85,8 @@ export class ImageEditor {
85
85
  target_element;
86
86
  width;
87
87
  height;
88
+ original_width;
89
+ original_height;
88
90
  dimensions;
89
91
  scale;
90
92
  position;
@@ -119,6 +121,8 @@ export class ImageEditor {
119
121
  this.target_element = options.target_element;
120
122
  this.width = options.width;
121
123
  this.height = options.height;
124
+ this.original_width = options.width;
125
+ this.original_height = options.height;
122
126
  this.command_manager = new CommandManager();
123
127
  this.ready = new Promise((resolve) => {
124
128
  this.ready_resolve = resolve;
@@ -438,6 +442,8 @@ export class ImageEditor {
438
442
  this.background_image = image;
439
443
  }
440
444
  async reset_canvas() {
445
+ this.width = this.original_width;
446
+ this.height = this.original_height;
441
447
  this.layer_manager.reset_layers(this.width, this.height);
442
448
  this.background_image = undefined;
443
449
  this.background_image_present.set(false);
@@ -369,34 +369,32 @@ export class ZoomTool {
369
369
  * @private
370
370
  */
371
371
  zoom_to_point(new_zoom, point, hard, is_fit_zoom) {
372
- const cursor_relative_to_image = {
373
- x: (point.x - this.local_position.x) / this.local_scale,
374
- y: (point.y - this.local_position.y) / this.local_scale
375
- };
376
- const image_percentages = {
377
- x: cursor_relative_to_image.x / this.local_dimensions.width,
378
- y: cursor_relative_to_image.y / this.local_dimensions.height
372
+ const container = this.image_editor_context.image_container;
373
+ const current_world_pos = container.getGlobalPosition();
374
+ const current_scale = container.scale.x;
375
+ const cursor_image_pixel = {
376
+ x: (point.x - current_world_pos.x) / current_scale,
377
+ y: (point.y - current_world_pos.y) / current_scale
379
378
  };
380
379
  const fit_zoom = this.calculate_min_zoom(this.local_dimensions.width, this.local_dimensions.height);
381
380
  const min_zoom = Math.min(fit_zoom, 1);
382
381
  new_zoom = Math.min(Math.max(new_zoom, min_zoom), this.max_zoom);
383
- const new_scaled_width = this.local_dimensions.width * new_zoom;
384
- const new_scaled_height = this.local_dimensions.height * new_zoom;
385
- let new_position = {
386
- x: point.x - new_scaled_width * image_percentages.x,
387
- y: point.y - new_scaled_height * image_percentages.y
382
+ const new_container_world_pos = {
383
+ x: point.x - cursor_image_pixel.x * new_zoom,
384
+ y: point.y - cursor_image_pixel.y * new_zoom
388
385
  };
389
386
  if (new_zoom === min_zoom || is_fit_zoom) {
390
387
  const canvas_width = this.image_editor_context.app.screen.width;
391
388
  const canvas_height = this.image_editor_context.app.screen.height;
392
- new_position = {
393
- x: (canvas_width - new_scaled_width) / 2,
394
- y: (canvas_height - this.pad_bottom - new_scaled_height) / 2
395
- };
389
+ const new_scaled_width = this.local_dimensions.width * new_zoom;
390
+ const new_scaled_height = this.local_dimensions.height * new_zoom;
391
+ new_container_world_pos.x = (canvas_width - new_scaled_width) / 2;
392
+ new_container_world_pos.y =
393
+ (canvas_height - this.pad_bottom - new_scaled_height) / 2;
396
394
  }
397
395
  this.image_editor_context.set_image_properties({
398
396
  scale: new_zoom,
399
- position: new_position,
397
+ position: new_container_world_pos,
400
398
  animate: typeof hard === "boolean" ? !hard : new_zoom === min_zoom
401
399
  });
402
400
  this.min_zoom.set(new_zoom === min_zoom);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/imageeditor",
3
- "version": "0.16.2",
3
+ "version": "0.16.3",
4
4
  "description": "Gradio UI packages",
5
5
  "type": "module",
6
6
  "author": "",
@@ -11,14 +11,14 @@
11
11
  "pixi-filters": "^6.1.0",
12
12
  "pixi.js": "^8.7.3",
13
13
  "tinycolor2": "^1.6.0",
14
- "@gradio/client": "^1.15.7",
15
14
  "@gradio/atoms": "^0.16.3",
15
+ "@gradio/client": "^1.16.0",
16
16
  "@gradio/icons": "^0.12.0",
17
- "@gradio/image": "^0.22.14",
17
+ "@gradio/image": "^0.22.15",
18
+ "@gradio/upload": "^0.16.13",
18
19
  "@gradio/statustracker": "^0.10.15",
19
- "@gradio/upload": "^0.16.12",
20
- "@gradio/wasm": "^0.18.1",
21
- "@gradio/utils": "^0.10.2"
20
+ "@gradio/utils": "^0.10.2",
21
+ "@gradio/wasm": "^0.18.1"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@gradio/preview": "^0.14.0"
@@ -175,6 +175,7 @@
175
175
  crop.set_tool("image");
176
176
  crop.set_subtool("crop");
177
177
  background_image = true;
178
+ zoom.set_zoom("fit");
178
179
  dispatch("upload");
179
180
  dispatch("input");
180
181
  } catch (error) {
@@ -405,7 +406,8 @@
405
406
  editor &&
406
407
  ready
407
408
  ) {
408
- // editor.reset_canvas();
409
+ editor.reset_canvas();
410
+ zoom.set_zoom("fit");
409
411
  handle_tool_change({ tool: "image" });
410
412
  background_image = false;
411
413
  has_drawn = false;
@@ -431,6 +433,7 @@
431
433
  crop.reset();
432
434
 
433
435
  background_image = true;
436
+ zoom.set_zoom("fit");
434
437
  handle_tool_change({ tool: "draw" });
435
438
  dispatch("upload");
436
439
  dispatch("input");
@@ -167,6 +167,8 @@ export class ImageEditor {
167
167
  private target_element: HTMLElement;
168
168
  private width: number;
169
169
  private height: number;
170
+ private original_width: number;
171
+ private original_height: number;
170
172
  dimensions: Spring<{ width: number; height: number }>;
171
173
  scale: Spring<number>;
172
174
  private position: Spring<{ x: number; y: number }>;
@@ -209,6 +211,8 @@ export class ImageEditor {
209
211
  this.target_element = options.target_element;
210
212
  this.width = options.width;
211
213
  this.height = options.height;
214
+ this.original_width = options.width;
215
+ this.original_height = options.height;
212
216
  this.command_manager = new CommandManager();
213
217
 
214
218
  this.ready = new Promise((resolve) => {
@@ -658,6 +662,9 @@ export class ImageEditor {
658
662
  }
659
663
 
660
664
  async reset_canvas(): Promise<void> {
665
+ this.width = this.original_width;
666
+ this.height = this.original_height;
667
+
661
668
  this.layer_manager.reset_layers(this.width, this.height);
662
669
  this.background_image = undefined;
663
670
  this.background_image_present.set(false);
@@ -521,14 +521,13 @@ export class ZoomTool implements Tool {
521
521
  hard?: boolean,
522
522
  is_fit_zoom?: boolean
523
523
  ): void {
524
- const cursor_relative_to_image = {
525
- x: (point.x - this.local_position.x) / this.local_scale,
526
- y: (point.y - this.local_position.y) / this.local_scale
527
- };
524
+ const container = this.image_editor_context.image_container;
525
+ const current_world_pos = container.getGlobalPosition();
526
+ const current_scale = container.scale.x;
528
527
 
529
- const image_percentages = {
530
- x: cursor_relative_to_image.x / this.local_dimensions.width,
531
- y: cursor_relative_to_image.y / this.local_dimensions.height
528
+ const cursor_image_pixel = {
529
+ x: (point.x - current_world_pos.x) / current_scale,
530
+ y: (point.y - current_world_pos.y) / current_scale
532
531
  };
533
532
 
534
533
  const fit_zoom = this.calculate_min_zoom(
@@ -538,27 +537,25 @@ export class ZoomTool implements Tool {
538
537
  const min_zoom = Math.min(fit_zoom, 1);
539
538
  new_zoom = Math.min(Math.max(new_zoom, min_zoom), this.max_zoom);
540
539
 
541
- const new_scaled_width = this.local_dimensions.width * new_zoom;
542
- const new_scaled_height = this.local_dimensions.height * new_zoom;
543
-
544
- let new_position = {
545
- x: point.x - new_scaled_width * image_percentages.x,
546
- y: point.y - new_scaled_height * image_percentages.y
540
+ const new_container_world_pos = {
541
+ x: point.x - cursor_image_pixel.x * new_zoom,
542
+ y: point.y - cursor_image_pixel.y * new_zoom
547
543
  };
548
544
 
549
545
  if (new_zoom === min_zoom || is_fit_zoom) {
550
546
  const canvas_width = this.image_editor_context.app.screen.width;
551
547
  const canvas_height = this.image_editor_context.app.screen.height;
548
+ const new_scaled_width = this.local_dimensions.width * new_zoom;
549
+ const new_scaled_height = this.local_dimensions.height * new_zoom;
552
550
 
553
- new_position = {
554
- x: (canvas_width - new_scaled_width) / 2,
555
- y: (canvas_height - this.pad_bottom - new_scaled_height) / 2
556
- };
551
+ new_container_world_pos.x = (canvas_width - new_scaled_width) / 2;
552
+ new_container_world_pos.y =
553
+ (canvas_height - this.pad_bottom - new_scaled_height) / 2;
557
554
  }
558
555
 
559
556
  this.image_editor_context.set_image_properties({
560
557
  scale: new_zoom,
561
- position: new_position,
558
+ position: new_container_world_pos,
562
559
  animate: typeof hard === "boolean" ? !hard : new_zoom === min_zoom
563
560
  });
564
561
  this.min_zoom.set(new_zoom === min_zoom);