@gradio/imageeditor 0.1.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 +57 -0
- package/Example.svelte +39 -0
- package/ImageEditor.stories.svelte +197 -0
- package/Index.svelte +181 -0
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/package.json +29 -0
- package/shared/Controls.svelte +104 -0
- package/shared/ImageEditor.svelte +375 -0
- package/shared/InteractiveImageEditor.svelte +211 -0
- package/shared/layers/Layers.svelte +178 -0
- package/shared/layers/utils.ts +223 -0
- package/shared/tools/Brush.svelte +301 -0
- package/shared/tools/BrushColor.svelte +139 -0
- package/shared/tools/BrushSize.svelte +75 -0
- package/shared/tools/ColorField.svelte +172 -0
- package/shared/tools/ColorPicker.svelte +205 -0
- package/shared/tools/ColorSwatch.svelte +170 -0
- package/shared/tools/Crop.svelte +193 -0
- package/shared/tools/Cropper.svelte +344 -0
- package/shared/tools/Handle.svelte +289 -0
- package/shared/tools/Sources.svelte +215 -0
- package/shared/tools/Tools.svelte +176 -0
- package/shared/tools/brush.ts +247 -0
- package/shared/tools/crop.ts +210 -0
- package/shared/tools/index.ts +5 -0
- package/shared/tools/sources.ts +114 -0
- package/shared/tools/types.ts +1 -0
- package/shared/utils/commands.ts +162 -0
- package/shared/utils/events.ts +28 -0
- package/shared/utils/pixi.ts +296 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { writable, type Writable } from "svelte/store";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base command interface that is added to the command_managers history
|
|
5
|
+
*/
|
|
6
|
+
export interface Command {
|
|
7
|
+
/**
|
|
8
|
+
* Optionally called when the command is first executed for multi-step commands
|
|
9
|
+
* @param args arguments to pass to the command
|
|
10
|
+
*/
|
|
11
|
+
start?: (...args: any) => any | Promise<any>;
|
|
12
|
+
/**
|
|
13
|
+
* Optionally called when the command is continued for multi-step commands
|
|
14
|
+
* @param args arguments to pass to the command
|
|
15
|
+
*/
|
|
16
|
+
continue?: (...args: any) => any | Promise<any>;
|
|
17
|
+
/**
|
|
18
|
+
* Optionally called when the command is stopped for multi-step commands
|
|
19
|
+
* @param args arguments to pass to the command
|
|
20
|
+
*/
|
|
21
|
+
stop?: (...args: any) => any | Promise<any>;
|
|
22
|
+
/**
|
|
23
|
+
* Called by the command manager to execute the command, can act as a no-op if the work has already been done
|
|
24
|
+
* This function must be able to recreate the command if the command is undone and redone (`stop`/`start`/`continue` will not be called again)
|
|
25
|
+
*/
|
|
26
|
+
execute(): any | Promise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Called by the command manager to undo the command
|
|
29
|
+
* This function must be able to undo the work done by the execute function
|
|
30
|
+
*/
|
|
31
|
+
undo(): any | Promise<any>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Command manager interface that handles the undo/redo history
|
|
36
|
+
*/
|
|
37
|
+
export interface CommandManager {
|
|
38
|
+
/**
|
|
39
|
+
* Undo the last command
|
|
40
|
+
*/
|
|
41
|
+
undo(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Redo the last undone command
|
|
44
|
+
*/
|
|
45
|
+
redo(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Execute a command and add it to the history
|
|
48
|
+
* @param command command to execute
|
|
49
|
+
*/
|
|
50
|
+
execute(command: Command): void;
|
|
51
|
+
/**
|
|
52
|
+
* Whether or not there are commands that can be undone
|
|
53
|
+
* Observable store that you can subscribe to for updates
|
|
54
|
+
*/
|
|
55
|
+
readonly can_undo: Writable<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Whether or not there are commands that can be redone
|
|
58
|
+
* Observable store that you can subscribe to for updates
|
|
59
|
+
*/
|
|
60
|
+
readonly can_redo: Writable<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* Resets the history
|
|
63
|
+
*/
|
|
64
|
+
reset(): void;
|
|
65
|
+
/**
|
|
66
|
+
* The current history node
|
|
67
|
+
* Observable store that you can subscribe to for updates
|
|
68
|
+
*/
|
|
69
|
+
readonly current_history: Writable<CommandNode>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Command node interface that is used to create the undo/redo history
|
|
74
|
+
*/
|
|
75
|
+
interface CommandNode {
|
|
76
|
+
/**
|
|
77
|
+
* Command that the node holds
|
|
78
|
+
*/
|
|
79
|
+
command: Command | null;
|
|
80
|
+
/**
|
|
81
|
+
* Next command in the history
|
|
82
|
+
*/
|
|
83
|
+
next: CommandNode | null;
|
|
84
|
+
/**
|
|
85
|
+
* Previous command in the history
|
|
86
|
+
*/
|
|
87
|
+
previous: CommandNode | null;
|
|
88
|
+
/**
|
|
89
|
+
* Push a command onto the history
|
|
90
|
+
* @param command command to push onto the history
|
|
91
|
+
*/
|
|
92
|
+
push(command: Command): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Creates a command node
|
|
97
|
+
* @param command command to add to the node
|
|
98
|
+
* @returns a command node
|
|
99
|
+
*/
|
|
100
|
+
function command_node(command?: Command): CommandNode {
|
|
101
|
+
return {
|
|
102
|
+
command: command || null,
|
|
103
|
+
next: null,
|
|
104
|
+
previous: null,
|
|
105
|
+
push: function (command: Command) {
|
|
106
|
+
const node = command_node(command);
|
|
107
|
+
node.previous = this;
|
|
108
|
+
this.next = node;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Creates a command manager
|
|
115
|
+
* @returns a command manager
|
|
116
|
+
*/
|
|
117
|
+
export function command_manager(): CommandManager {
|
|
118
|
+
let history: CommandNode = command_node();
|
|
119
|
+
const can_undo = writable(false);
|
|
120
|
+
const can_redo = writable(false);
|
|
121
|
+
const current_history = writable(history);
|
|
122
|
+
return {
|
|
123
|
+
undo: function () {
|
|
124
|
+
if (history.previous) {
|
|
125
|
+
history.command?.undo();
|
|
126
|
+
history = history.previous;
|
|
127
|
+
}
|
|
128
|
+
can_undo.set(!!history.previous);
|
|
129
|
+
can_redo.set(!!history.next);
|
|
130
|
+
current_history.set(history);
|
|
131
|
+
},
|
|
132
|
+
redo: function () {
|
|
133
|
+
if (history.next) {
|
|
134
|
+
history.next.command?.execute();
|
|
135
|
+
history = history.next;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
can_undo.set(!!history.previous);
|
|
139
|
+
can_redo.set(!!history.next);
|
|
140
|
+
current_history.set(history);
|
|
141
|
+
},
|
|
142
|
+
execute: function (command: Command) {
|
|
143
|
+
command.execute();
|
|
144
|
+
history.push(command);
|
|
145
|
+
history = history.next!;
|
|
146
|
+
can_undo.set(!!history.previous);
|
|
147
|
+
can_redo.set(!!history.next);
|
|
148
|
+
current_history.set(history);
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
can_undo,
|
|
152
|
+
can_redo,
|
|
153
|
+
current_history,
|
|
154
|
+
|
|
155
|
+
reset: function () {
|
|
156
|
+
history = command_node();
|
|
157
|
+
can_undo.set(false);
|
|
158
|
+
can_redo.set(false);
|
|
159
|
+
current_history.set(history);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte action to handle clicks outside of a DOM node
|
|
3
|
+
* @param node DOM node to check the click is outside of
|
|
4
|
+
* @param callback callback function to call if click is outside
|
|
5
|
+
* @returns svelte action return object with destroy method to remove event listener
|
|
6
|
+
*/
|
|
7
|
+
export function click_outside(
|
|
8
|
+
node: Node,
|
|
9
|
+
callback: (arg: MouseEvent) => void
|
|
10
|
+
): any {
|
|
11
|
+
const handle_click = (event: MouseEvent): void => {
|
|
12
|
+
if (
|
|
13
|
+
node &&
|
|
14
|
+
!node.contains(event.target as Node) &&
|
|
15
|
+
!event.defaultPrevented
|
|
16
|
+
) {
|
|
17
|
+
callback(event);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
document.addEventListener("mousedown", handle_click, true);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
destroy() {
|
|
25
|
+
document.removeEventListener("mousedown", handle_click, true);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Application,
|
|
3
|
+
Container,
|
|
4
|
+
Graphics,
|
|
5
|
+
Sprite,
|
|
6
|
+
Rectangle,
|
|
7
|
+
RenderTexture,
|
|
8
|
+
type IRenderer,
|
|
9
|
+
type DisplayObject,
|
|
10
|
+
type ICanvas
|
|
11
|
+
} from "pixi.js";
|
|
12
|
+
|
|
13
|
+
import { type LayerScene } from "../layers/utils";
|
|
14
|
+
import { background } from "@storybook/theming";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* interface holding references to pixi app components
|
|
18
|
+
*/
|
|
19
|
+
export interface PixiApp {
|
|
20
|
+
/**
|
|
21
|
+
* The pixi container for layers
|
|
22
|
+
*/
|
|
23
|
+
layer_container: Container;
|
|
24
|
+
/**
|
|
25
|
+
* The pixi container for background images and colors
|
|
26
|
+
*/
|
|
27
|
+
background_container: Container;
|
|
28
|
+
/**
|
|
29
|
+
* The pixi renderer
|
|
30
|
+
*/
|
|
31
|
+
renderer: IRenderer;
|
|
32
|
+
/**
|
|
33
|
+
* The pixi canvas
|
|
34
|
+
*/
|
|
35
|
+
view: HTMLCanvasElement & ICanvas;
|
|
36
|
+
/**
|
|
37
|
+
* The pixi container for masking
|
|
38
|
+
*/
|
|
39
|
+
mask_container: Container;
|
|
40
|
+
destroy(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Resizes the pixi app
|
|
43
|
+
* @param width the new width
|
|
44
|
+
* @param height the new height
|
|
45
|
+
*/
|
|
46
|
+
resize(width: number, height: number): void;
|
|
47
|
+
/**
|
|
48
|
+
* Gets the blobs for the background, layers, and composite
|
|
49
|
+
* @param bounds the bounds of the canvas
|
|
50
|
+
* @returns a promise with the blobs
|
|
51
|
+
*/
|
|
52
|
+
get_blobs(
|
|
53
|
+
layers: LayerScene[],
|
|
54
|
+
bounds: Rectangle,
|
|
55
|
+
dimensions: [number, number]
|
|
56
|
+
): Promise<ImageBlobs>;
|
|
57
|
+
// /**
|
|
58
|
+
// * Resets the mask
|
|
59
|
+
// */
|
|
60
|
+
// reset?: () => void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gets the layers
|
|
64
|
+
*/
|
|
65
|
+
get_layers?: () => LayerScene[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Creates a PIXI app and attaches it to a DOM element
|
|
70
|
+
* @param target DOM element to attach PIXI app to
|
|
71
|
+
* @param width Width of the PIXI app
|
|
72
|
+
* @param height Height of the PIXI app
|
|
73
|
+
* @param antialias Whether to use antialiasing
|
|
74
|
+
* @returns object with pixi container and renderer
|
|
75
|
+
*/
|
|
76
|
+
export function create_pixi_app(
|
|
77
|
+
target: HTMLElement,
|
|
78
|
+
width: number,
|
|
79
|
+
height: number,
|
|
80
|
+
antialias: boolean
|
|
81
|
+
): PixiApp {
|
|
82
|
+
const ratio = window.devicePixelRatio || 1;
|
|
83
|
+
const app = new Application({
|
|
84
|
+
width,
|
|
85
|
+
height,
|
|
86
|
+
antialias: antialias,
|
|
87
|
+
backgroundAlpha: 0,
|
|
88
|
+
eventMode: "static"
|
|
89
|
+
});
|
|
90
|
+
const view = app.view as HTMLCanvasElement;
|
|
91
|
+
// ensure that we can sort the background and layer containers
|
|
92
|
+
app.stage.sortableChildren = true;
|
|
93
|
+
view.style.maxWidth = `${width / ratio}px`;
|
|
94
|
+
view.style.maxHeight = `${height / ratio}px`;
|
|
95
|
+
view.style.width = "100%";
|
|
96
|
+
view.style.height = "100%";
|
|
97
|
+
|
|
98
|
+
target.appendChild(app.view as HTMLCanvasElement);
|
|
99
|
+
|
|
100
|
+
// we need a separate container for the background so that we can
|
|
101
|
+
// clear its content without knowing too much about its children
|
|
102
|
+
const background_container = new Container() as Container & DisplayObject;
|
|
103
|
+
background_container.zIndex = 0;
|
|
104
|
+
const layer_container = new Container() as Container & DisplayObject;
|
|
105
|
+
layer_container.zIndex = 1;
|
|
106
|
+
|
|
107
|
+
// ensure we can reorder layers via zIndex
|
|
108
|
+
layer_container.sortableChildren = true;
|
|
109
|
+
|
|
110
|
+
const mask_container = new Container() as Container & DisplayObject;
|
|
111
|
+
mask_container.zIndex = 1;
|
|
112
|
+
const composite_container = new Container() as Container & DisplayObject;
|
|
113
|
+
composite_container.zIndex = 0;
|
|
114
|
+
|
|
115
|
+
mask_container.addChild(background_container);
|
|
116
|
+
mask_container.addChild(layer_container);
|
|
117
|
+
|
|
118
|
+
app.stage.addChild(mask_container);
|
|
119
|
+
app.stage.addChild(composite_container);
|
|
120
|
+
const mask = new Graphics();
|
|
121
|
+
let text = RenderTexture.create({
|
|
122
|
+
width,
|
|
123
|
+
height
|
|
124
|
+
});
|
|
125
|
+
const sprite = new Sprite(text);
|
|
126
|
+
|
|
127
|
+
mask_container.mask = sprite;
|
|
128
|
+
|
|
129
|
+
app.render();
|
|
130
|
+
|
|
131
|
+
function reset_mask(width: number, height: number): void {
|
|
132
|
+
background_container.removeChildren();
|
|
133
|
+
mask.beginFill(0xffffff, 1);
|
|
134
|
+
mask.drawRect(0, 0, width, height);
|
|
135
|
+
mask.endFill();
|
|
136
|
+
text = RenderTexture.create({
|
|
137
|
+
width,
|
|
138
|
+
height
|
|
139
|
+
});
|
|
140
|
+
app.renderer.render(mask, {
|
|
141
|
+
renderTexture: text
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const sprite = new Sprite(text);
|
|
145
|
+
|
|
146
|
+
mask_container.mask = sprite;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function resize(width: number, height: number): void {
|
|
150
|
+
app.renderer.resize(width, height);
|
|
151
|
+
view.style.maxWidth = `${width / ratio}px`;
|
|
152
|
+
view.style.maxHeight = `${height / ratio}px`;
|
|
153
|
+
reset_mask(width, height);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function get_blobs(
|
|
157
|
+
_layers: LayerScene[],
|
|
158
|
+
bounds: Rectangle,
|
|
159
|
+
[w, h]: [number, number]
|
|
160
|
+
): Promise<ImageBlobs> {
|
|
161
|
+
const background = await get_canvas_blob(
|
|
162
|
+
app.renderer,
|
|
163
|
+
background_container,
|
|
164
|
+
bounds,
|
|
165
|
+
w,
|
|
166
|
+
h
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const layers = await Promise.all(
|
|
170
|
+
_layers.map((layer) =>
|
|
171
|
+
get_canvas_blob(
|
|
172
|
+
app.renderer,
|
|
173
|
+
layer.composite as DisplayObject,
|
|
174
|
+
bounds,
|
|
175
|
+
w,
|
|
176
|
+
h
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
const composite = await get_canvas_blob(
|
|
182
|
+
app.renderer,
|
|
183
|
+
mask_container,
|
|
184
|
+
bounds,
|
|
185
|
+
w,
|
|
186
|
+
h
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
background,
|
|
191
|
+
layers,
|
|
192
|
+
composite
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
layer_container,
|
|
198
|
+
renderer: app.renderer,
|
|
199
|
+
destroy: () => app.destroy(true),
|
|
200
|
+
view: app.view as HTMLCanvasElement & ICanvas,
|
|
201
|
+
background_container,
|
|
202
|
+
mask_container,
|
|
203
|
+
resize,
|
|
204
|
+
get_blobs
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Creates a pixi graphics object.
|
|
210
|
+
* @param z_index the z index of the graphics object
|
|
211
|
+
* @returns a graphics object
|
|
212
|
+
*/
|
|
213
|
+
export function make_graphics(z_index: number): Graphics {
|
|
214
|
+
const graphics = new Graphics();
|
|
215
|
+
graphics.eventMode = "none";
|
|
216
|
+
graphics.zIndex = z_index;
|
|
217
|
+
|
|
218
|
+
return graphics;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Clamps a number between a min and max value.
|
|
223
|
+
* @param n The number to clamp.
|
|
224
|
+
* @param min The minimum value.
|
|
225
|
+
* @param max The maximum value.
|
|
226
|
+
* @returns The clamped number.
|
|
227
|
+
*/
|
|
228
|
+
export function clamp(n: number, min: number, max: number): number {
|
|
229
|
+
return n < min ? min : n > max ? max : n;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Generates a blob from a pixi object.
|
|
234
|
+
* @param renderer The pixi renderer.
|
|
235
|
+
* @param obj The pixi object to generate a blob from.
|
|
236
|
+
* @param bounds The bounds of the canvas that we wish to extract
|
|
237
|
+
* @param width The full width of the canvas
|
|
238
|
+
* @param height The full height of the canvas
|
|
239
|
+
* @returns A promise with the blob.
|
|
240
|
+
*/
|
|
241
|
+
function get_canvas_blob(
|
|
242
|
+
renderer: IRenderer,
|
|
243
|
+
obj: DisplayObject,
|
|
244
|
+
bounds: Rectangle,
|
|
245
|
+
width: number,
|
|
246
|
+
height: number
|
|
247
|
+
): Promise<Blob | null> {
|
|
248
|
+
return new Promise((resolve) => {
|
|
249
|
+
// for some reason pixi won't extract a cropped canvas without distorting it
|
|
250
|
+
// so we have to extract the whole canvas and crop it manually
|
|
251
|
+
const src_canvas = renderer.extract.canvas(
|
|
252
|
+
obj,
|
|
253
|
+
new Rectangle(0, 0, width, height)
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
// Create a new canvas for the cropped area with the appropriate size
|
|
257
|
+
let dest_canvas = document.createElement("canvas");
|
|
258
|
+
dest_canvas.width = bounds.width;
|
|
259
|
+
dest_canvas.height = bounds.height;
|
|
260
|
+
let dest_ctx = dest_canvas.getContext("2d");
|
|
261
|
+
|
|
262
|
+
if (!dest_ctx) {
|
|
263
|
+
resolve(null);
|
|
264
|
+
throw new Error("Could not create canvas context");
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Draw the cropped area onto the destination canvas
|
|
268
|
+
dest_ctx.drawImage(
|
|
269
|
+
src_canvas as HTMLCanvasElement,
|
|
270
|
+
// this is the area of the source that we want to copy (the crop box)
|
|
271
|
+
bounds.x,
|
|
272
|
+
bounds.y,
|
|
273
|
+
bounds.width,
|
|
274
|
+
bounds.height,
|
|
275
|
+
// this is where we want to draw the crop box on the destination canvas
|
|
276
|
+
0,
|
|
277
|
+
0,
|
|
278
|
+
bounds.width,
|
|
279
|
+
bounds.height
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
// we grab a blob here so we can upload it
|
|
283
|
+
dest_canvas.toBlob?.((blob) => {
|
|
284
|
+
if (!blob) {
|
|
285
|
+
resolve(null);
|
|
286
|
+
}
|
|
287
|
+
resolve(blob);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export interface ImageBlobs {
|
|
293
|
+
background: Blob | null;
|
|
294
|
+
layers: (Blob | null)[];
|
|
295
|
+
composite: Blob | null;
|
|
296
|
+
}
|