@gradio/imageeditor 0.12.14 → 0.14.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.
Files changed (131) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/Example.svelte +1 -1
  3. package/IMAGE_EDITOR_OVERVIEW.md +224 -0
  4. package/ImageEditor.stories.svelte +13 -5
  5. package/Index.svelte +46 -45
  6. package/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +65 -103
  7. package/README.md +2 -0
  8. package/dist/Example.svelte.d.ts +1 -1
  9. package/dist/Index.svelte +35 -33
  10. package/dist/Index.svelte.d.ts +25 -16
  11. package/dist/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +54 -88
  12. package/dist/{shared/InteractiveImageEditor.svelte.d.ts → InteractiveImageEditor.svelte.d.ts} +17 -15
  13. package/dist/shared/Anchor.svelte +311 -0
  14. package/dist/shared/Anchor.svelte.d.ts +18 -0
  15. package/dist/shared/Controls.svelte +169 -18
  16. package/dist/shared/Controls.svelte.d.ts +9 -2
  17. package/dist/shared/{tools/IconButton.svelte → IconButton.svelte} +19 -3
  18. package/dist/shared/{tools/IconButton.svelte.d.ts → IconButton.svelte.d.ts} +4 -1
  19. package/dist/shared/ImageEditor.svelte +572 -289
  20. package/dist/shared/ImageEditor.svelte.d.ts +54 -56
  21. package/dist/shared/Layers.svelte +260 -0
  22. package/dist/shared/Layers.svelte.d.ts +37 -0
  23. package/dist/shared/Resize.svelte +218 -0
  24. package/dist/shared/Resize.svelte.d.ts +29 -0
  25. package/dist/shared/SecondaryToolbar.svelte +55 -0
  26. package/dist/shared/SecondaryToolbar.svelte.d.ts +37 -0
  27. package/dist/shared/Toolbar.svelte +337 -0
  28. package/dist/shared/Toolbar.svelte.d.ts +42 -0
  29. package/dist/shared/{tools → brush}/BrushOptions.svelte +68 -64
  30. package/dist/shared/{tools → brush}/BrushOptions.svelte.d.ts +9 -9
  31. package/dist/shared/{tools → brush}/BrushSize.svelte +2 -27
  32. package/dist/shared/{tools → brush}/ColorField.svelte +2 -1
  33. package/dist/shared/{tools → brush}/ColorField.svelte.d.ts +1 -0
  34. package/dist/shared/{tools → brush}/ColorPicker.svelte +81 -9
  35. package/dist/shared/{tools → brush}/ColorPicker.svelte.d.ts +1 -0
  36. package/dist/shared/{tools → brush}/ColorSwatch.svelte +78 -38
  37. package/dist/shared/{tools → brush}/ColorSwatch.svelte.d.ts +6 -2
  38. package/dist/shared/brush/brush-cursor.d.ts +101 -0
  39. package/dist/shared/brush/brush-cursor.js +327 -0
  40. package/dist/shared/brush/brush-textures.d.ts +65 -0
  41. package/dist/shared/brush/brush-textures.js +385 -0
  42. package/dist/shared/brush/brush-utils.d.ts +32 -0
  43. package/dist/shared/brush/brush-utils.js +59 -0
  44. package/dist/shared/brush/brush.d.ts +152 -0
  45. package/dist/shared/brush/brush.js +397 -0
  46. package/dist/shared/brush/types.d.ts +37 -0
  47. package/dist/shared/brush/types.js +1 -0
  48. package/dist/shared/core/editor.d.ts +261 -0
  49. package/dist/shared/core/editor.js +1264 -0
  50. package/dist/shared/crop/crop.d.ts +201 -0
  51. package/dist/shared/crop/crop.js +727 -0
  52. package/dist/shared/image/image.d.ts +89 -0
  53. package/dist/shared/image/image.js +183 -0
  54. package/dist/shared/resize/resize.d.ts +270 -0
  55. package/dist/shared/resize/resize.js +1413 -0
  56. package/dist/shared/types.d.ts +16 -0
  57. package/dist/shared/utils/pixi.d.ts +16 -57
  58. package/dist/shared/utils/pixi.js +15 -113
  59. package/dist/shared/zoom/zoom.d.ts +136 -0
  60. package/dist/shared/zoom/zoom.js +548 -0
  61. package/package.json +8 -7
  62. package/shared/Anchor.svelte +384 -0
  63. package/shared/Controls.svelte +178 -37
  64. package/shared/{tools/IconButton.svelte → IconButton.svelte} +21 -4
  65. package/shared/ImageEditor.svelte +712 -350
  66. package/shared/Layers.svelte +282 -0
  67. package/shared/Resize.svelte +239 -0
  68. package/shared/SecondaryToolbar.svelte +67 -0
  69. package/shared/Toolbar.svelte +386 -0
  70. package/shared/brush/BRUSH_TOOL.md +134 -0
  71. package/shared/brush/BrushOptions.svelte +200 -0
  72. package/shared/{tools → brush}/BrushSize.svelte +2 -30
  73. package/shared/{tools → brush}/ColorField.svelte +3 -1
  74. package/shared/{tools → brush}/ColorPicker.svelte +86 -9
  75. package/shared/{tools → brush}/ColorSwatch.svelte +95 -42
  76. package/shared/brush/brush-cursor.ts +408 -0
  77. package/shared/brush/brush-textures.ts +485 -0
  78. package/shared/brush/brush-utils.ts +76 -0
  79. package/shared/brush/brush.ts +510 -0
  80. package/shared/brush/types.ts +41 -0
  81. package/shared/core/EDITOR.md +182 -0
  82. package/shared/core/editor.ts +1788 -0
  83. package/shared/crop/CROP.md +139 -0
  84. package/shared/crop/crop.ts +1027 -0
  85. package/shared/image/IMAGE.md +135 -0
  86. package/shared/image/image.ts +298 -0
  87. package/shared/resize/resize.ts +1894 -0
  88. package/shared/types.ts +19 -0
  89. package/shared/utils/pixi.ts +19 -246
  90. package/shared/zoom/zoom.ts +729 -0
  91. package/dist/shared/layers/Layers.svelte +0 -231
  92. package/dist/shared/layers/Layers.svelte.d.ts +0 -18
  93. package/dist/shared/layers/utils.d.ts +0 -73
  94. package/dist/shared/layers/utils.js +0 -156
  95. package/dist/shared/tools/Brush.svelte +0 -215
  96. package/dist/shared/tools/Brush.svelte.d.ts +0 -29
  97. package/dist/shared/tools/Crop.svelte +0 -155
  98. package/dist/shared/tools/Crop.svelte.d.ts +0 -16
  99. package/dist/shared/tools/Cropper.svelte +0 -271
  100. package/dist/shared/tools/Cropper.svelte.d.ts +0 -41
  101. package/dist/shared/tools/Handle.svelte +0 -260
  102. package/dist/shared/tools/Handle.svelte.d.ts +0 -29
  103. package/dist/shared/tools/Sources.svelte +0 -257
  104. package/dist/shared/tools/Sources.svelte.d.ts +0 -34
  105. package/dist/shared/tools/Tools.svelte +0 -102
  106. package/dist/shared/tools/Tools.svelte.d.ts +0 -48
  107. package/dist/shared/tools/brush.d.ts +0 -62
  108. package/dist/shared/tools/brush.js +0 -133
  109. package/dist/shared/tools/crop.d.ts +0 -15
  110. package/dist/shared/tools/crop.js +0 -154
  111. package/dist/shared/tools/index.d.ts +0 -5
  112. package/dist/shared/tools/index.js +0 -5
  113. package/dist/shared/tools/sources.d.ts +0 -46
  114. package/dist/shared/tools/sources.js +0 -121
  115. package/dist/shared/tools/types.d.ts +0 -1
  116. package/shared/layers/Layers.svelte +0 -250
  117. package/shared/layers/utils.ts +0 -294
  118. package/shared/tools/Brush.svelte +0 -280
  119. package/shared/tools/BrushOptions.svelte +0 -180
  120. package/shared/tools/Crop.svelte +0 -189
  121. package/shared/tools/Cropper.svelte +0 -346
  122. package/shared/tools/Handle.svelte +0 -289
  123. package/shared/tools/Sources.svelte +0 -283
  124. package/shared/tools/Tools.svelte +0 -162
  125. package/shared/tools/brush.ts +0 -247
  126. package/shared/tools/crop.ts +0 -213
  127. package/shared/tools/index.ts +0 -5
  128. package/shared/tools/sources.ts +0 -195
  129. package/shared/tools/types.ts +0 -1
  130. /package/dist/shared/{tools → brush}/BrushSize.svelte.d.ts +0 -0
  131. /package/dist/shared/{tools/types.js → types.js} +0 -0
@@ -1,213 +0,0 @@
1
- import {
2
- Graphics,
3
- type IRenderer,
4
- type Container,
5
- Sprite,
6
- RenderTexture
7
- } from "pixi.js";
8
- import { type Command } from "../utils/commands";
9
- import { spring } from "svelte/motion";
10
- import type { Writable } from "svelte/store";
11
-
12
- export interface CropCommand extends Command {
13
- start: (
14
- width: number,
15
- height: number,
16
- previous_crop: [number, number, number, number],
17
- preview?: boolean,
18
- set_previous?: boolean
19
- ) => void;
20
- stop: () => number;
21
- continue: (
22
- crop_size: [number, number, number, number],
23
- preview?: boolean
24
- ) => void;
25
- }
26
-
27
- export function crop_canvas(
28
- renderer: IRenderer,
29
- background_container: Container,
30
- crop: Writable<[number, number, number, number]>,
31
- current_opacity = 0
32
- ): CropCommand {
33
- let text: RenderTexture;
34
- let sprite: Sprite;
35
- const mask_graphics = new Graphics();
36
- let previous_crop: [number, number, number, number];
37
- let final_crop: [number, number, number, number];
38
- let width: number;
39
- let height: number;
40
- let alpha_spring = spring(current_opacity, {
41
- stiffness: 0.1,
42
- damping: 0.5
43
- });
44
-
45
- let spring_value = current_opacity;
46
- alpha_spring.subscribe((value) => {
47
- if (!final_crop) return;
48
- spring_value = value;
49
- crop_mask(width, height, final_crop, true);
50
- });
51
-
52
- function crop_mask(
53
- width: number,
54
- height: number,
55
- crop_size: [number, number, number, number],
56
- preview: boolean
57
- ): void {
58
- mask_graphics.clear();
59
- if (preview) {
60
- mask_graphics.beginFill(0xffffff, spring_value);
61
- mask_graphics.drawRect(0, 0, width, height);
62
- mask_graphics.endFill();
63
- }
64
- mask_graphics.beginFill(0xffffff, 1);
65
- mask_graphics.drawRect(...crop_size);
66
- mask_graphics.endFill();
67
-
68
- renderer.render(mask_graphics, {
69
- renderTexture: text
70
- });
71
- }
72
-
73
- let clean = true;
74
- let stopped = false;
75
-
76
- return {
77
- start: (
78
- _width: number,
79
- _height: number,
80
- _previous_crop: [number, number, number, number],
81
- _preview = true,
82
- set_previous = true
83
- ) => {
84
- clean = false;
85
- text = RenderTexture.create({
86
- width: _width,
87
- height: _height
88
- });
89
-
90
- crop_mask(_width, _height, _previous_crop, _preview);
91
- sprite = new Sprite(text);
92
- background_container.mask = sprite;
93
- width = _width;
94
- height = _height;
95
- if (set_previous)
96
- previous_crop = JSON.parse(JSON.stringify(_previous_crop));
97
- },
98
- continue: (crop_size: [number, number, number, number], preview = true) => {
99
- final_crop = JSON.parse(JSON.stringify(crop_size));
100
- if (spring_value === 0.2) {
101
- crop_mask(width, height, final_crop, preview);
102
- } else {
103
- alpha_spring.set(0.2);
104
- }
105
- },
106
-
107
- undo() {
108
- this.start(width, height, previous_crop, false);
109
- crop.set([
110
- previous_crop[0] / width,
111
- previous_crop[1] / height,
112
- previous_crop[2] / width,
113
- previous_crop[3] / height
114
- ]);
115
- clean = true;
116
- },
117
- stop() {
118
- stopped = true;
119
- return spring_value;
120
- },
121
- execute() {
122
- if (clean) {
123
- this.start(width, height, final_crop, false);
124
-
125
- crop.set([
126
- final_crop[0] / width,
127
- final_crop[1] / height,
128
- final_crop[2] / width,
129
- final_crop[3] / height
130
- ]);
131
- clean = true;
132
- } else {
133
- if (!stopped) {
134
- alpha_spring.set(0, { hard: true });
135
- }
136
- crop.set([
137
- final_crop[0] / width,
138
- final_crop[1] / height,
139
- final_crop[2] / width,
140
- final_crop[3] / height
141
- ]);
142
- clean = true;
143
- }
144
- }
145
- };
146
- }
147
-
148
- export function resize_and_reposition(
149
- original_width: number,
150
- original_height: number,
151
- anchor: "t" | "r" | "l" | "b" | "tl" | "tr" | "bl" | "br" | "c",
152
- aspect_ratio: number,
153
- max_width: number,
154
- max_height: number
155
- ): {
156
- new_width: number;
157
- new_height: number;
158
- x_offset: number;
159
- y_offset: number;
160
- } {
161
- let new_width = original_width;
162
- let new_height = original_height;
163
-
164
- // Calculate new dimensions based on aspect ratio
165
- if (anchor.includes("t") || anchor.includes("b") || anchor == "c") {
166
- new_width = original_height * aspect_ratio;
167
- }
168
- if (anchor.includes("l") || anchor.includes("r") || anchor == "c") {
169
- new_height = original_width / aspect_ratio;
170
- }
171
-
172
- // Ensure aspect ratio is maintained
173
- new_height = new_height || new_width / aspect_ratio;
174
- new_width = new_width || new_height * aspect_ratio;
175
-
176
- // Apply max width/height constraints and adjust dimensions
177
- if (new_width > max_width) {
178
- new_width = max_width;
179
- new_height = new_width / aspect_ratio;
180
- }
181
- if (new_height > max_height) {
182
- new_height = max_height;
183
- new_width = new_height * aspect_ratio;
184
- }
185
-
186
- // Calculate offsets to ensure anchor position is maintained
187
- let x_offset = 0;
188
- let y_offset = 0;
189
- if (anchor.includes("r")) {
190
- x_offset = original_width - new_width;
191
- } else if (anchor.includes("l")) {
192
- x_offset = 0;
193
- } else {
194
- // Center or top/bottom center
195
- x_offset = (original_width - new_width) / 2;
196
- }
197
-
198
- if (anchor.includes("b")) {
199
- y_offset = original_height - new_height;
200
- } else if (anchor.includes("t")) {
201
- y_offset = 0;
202
- } else {
203
- // Center or left/right center
204
- y_offset = (original_height - new_height) / 2;
205
- }
206
-
207
- return {
208
- new_width: new_width,
209
- new_height: new_height,
210
- x_offset: x_offset,
211
- y_offset: y_offset
212
- };
213
- }
@@ -1,5 +0,0 @@
1
- export { default as Tools } from "./Tools.svelte";
2
- export { default as Brush } from "./Brush.svelte";
3
- export { default as Crop } from "./Crop.svelte";
4
- export { default as Sources } from "./Sources.svelte";
5
- export * from "./types";
@@ -1,195 +0,0 @@
1
- import {
2
- type Container,
3
- type IRenderer,
4
- Sprite,
5
- Texture,
6
- DisplayObject,
7
- type ColorSource,
8
- Color,
9
- RenderTexture
10
- } from "pixi.js";
11
-
12
- import { type Command } from "../utils/commands";
13
- import { make_graphics } from "../utils/pixi";
14
-
15
- interface BgImageCommand extends Command {
16
- /**
17
- * Initial setup for the bg command
18
- * @returns
19
- */
20
- start: () => Promise<[number, number]>;
21
- }
22
-
23
- /**
24
- * Calculates new dimensions and position for an image to fit within a canvas while maintaining aspect ratio
25
- * @param image_width Original width of the image
26
- * @param image_height Original height of the image
27
- * @param canvas_width Width of the canvas
28
- * @param canvas_height Height of the canvas
29
- * @returns Object containing new dimensions and position
30
- */
31
- export function fit_image_to_canvas(
32
- image_width: number,
33
- image_height: number,
34
- canvas_width: number,
35
- canvas_height: number
36
- ): {
37
- width: number;
38
- height: number;
39
- x: number;
40
- y: number;
41
- } {
42
- const image_aspect_ratio = image_width / image_height;
43
- const canvas_aspect_ratio = canvas_width / canvas_height;
44
-
45
- let new_width: number;
46
- let new_height: number;
47
-
48
- if (image_width <= canvas_width && image_height <= canvas_height) {
49
- new_width = image_width;
50
- new_height = image_height;
51
- } else {
52
- if (image_aspect_ratio > canvas_aspect_ratio) {
53
- // Width is the limiting factor
54
- new_width = canvas_width;
55
- new_height = canvas_width / image_aspect_ratio;
56
- } else {
57
- // Height is the limiting factor
58
- new_height = canvas_height;
59
- new_width = canvas_height * image_aspect_ratio;
60
- }
61
- }
62
-
63
- // Calculate position to center the image
64
- const x = Math.round((canvas_width - new_width) / 2);
65
- const y = Math.round((canvas_height - new_height) / 2);
66
-
67
- return {
68
- width: Math.round(new_width),
69
- height: Math.round(new_height),
70
- x,
71
- y
72
- };
73
- }
74
-
75
- export function add_bg_image(
76
- container: Container,
77
- renderer: IRenderer,
78
- background: Blob | File,
79
- resize: (width: number, height: number) => void,
80
- canvas_size: [number, number],
81
- fixed_canvas: boolean
82
- ): BgImageCommand {
83
- let sprite: Sprite & DisplayObject;
84
-
85
- return {
86
- async start() {
87
- container.removeChildren();
88
- const img = await createImageBitmap(background);
89
- const bitmap_texture = Texture.from(img);
90
- sprite = new Sprite(bitmap_texture) as Sprite & DisplayObject;
91
- if (!fixed_canvas) {
92
- canvas_size = [sprite.width, sprite.height];
93
- }
94
-
95
- if (fixed_canvas) {
96
- const [canvas_width, canvas_height] = canvas_size;
97
- const { width, height, x, y } = fit_image_to_canvas(
98
- sprite.width,
99
- sprite.height,
100
- canvas_width,
101
- canvas_height
102
- );
103
-
104
- sprite.width = width;
105
- sprite.height = height;
106
- sprite.x = x;
107
- sprite.y = y;
108
-
109
- return canvas_size;
110
- }
111
- // Use existing max_height based scaling if no canvas_size
112
- const x = fit_image_to_canvas(
113
- sprite.width,
114
- sprite.height,
115
- canvas_size[0],
116
- canvas_size[1]
117
- );
118
- sprite.width = x.width;
119
- sprite.height = x.height;
120
- sprite.x = 0;
121
- sprite.y = 0;
122
- return [x.width, x.height];
123
- },
124
- async execute() {
125
- resize(
126
- // @ts-ignore
127
- fixed_canvas ? canvas_size[0] : sprite.width,
128
- // @ts-ignore
129
- fixed_canvas ? canvas_size[1] : sprite.height
130
- );
131
-
132
- sprite.zIndex = 0;
133
- container.addChild(sprite);
134
- },
135
- undo() {
136
- container.removeChildAt(0);
137
- }
138
- };
139
- }
140
-
141
- /**
142
- * Command that sets a background
143
- */
144
- interface BgColorCommand extends Command {
145
- /**
146
- * Initial setup for the bg command
147
- * @returns
148
- */
149
- start: () => [number, number];
150
- }
151
-
152
- /**
153
- * Adds a background color to the canvas.
154
- * @param container The container to add the image to.
155
- * @param renderer The renderer to use for the image.
156
- * @param color The background color to add.
157
- * @param width The width of the background.
158
- * @param height The height of the background.
159
- * @param resize The function to resize the canvas.
160
- * @returns A command that can be used to undo the action.
161
- */
162
- export function add_bg_color(
163
- container: Container,
164
- renderer: IRenderer,
165
- color: ColorSource,
166
- width: number,
167
- height: number,
168
- resize: (width: number, height: number) => void
169
- ): BgColorCommand {
170
- let sprite: Sprite & DisplayObject;
171
- return {
172
- start() {
173
- container.removeChildren();
174
- const graphics = make_graphics(1);
175
- const texture = RenderTexture.create({
176
- width,
177
- height
178
- });
179
- graphics.beginFill(new Color(color));
180
- graphics.drawRect(0, 0, width, height);
181
- graphics.endFill();
182
- renderer.render(graphics, { renderTexture: texture });
183
- sprite = new Sprite(texture) as Sprite & DisplayObject;
184
- return [sprite.width, sprite.height];
185
- },
186
- async execute() {
187
- resize(sprite.width, sprite.height);
188
- sprite.zIndex = 1;
189
- container.addChild(sprite);
190
- },
191
- undo() {
192
- container.removeChildren();
193
- }
194
- };
195
- }
@@ -1 +0,0 @@
1
- export type tool = "draw" | "erase" | "crop" | "bg";
File without changes