@gradio/imageeditor 0.10.0 → 0.11.0-beta.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.
- package/CHANGELOG.md +35 -0
- package/Index.svelte +5 -2
- package/dist/Example.svelte +41 -0
- package/dist/Example.svelte.d.ts +19 -0
- package/dist/Index.svelte +184 -0
- package/dist/Index.svelte.d.ts +192 -0
- package/dist/shared/Controls.svelte +73 -0
- package/dist/shared/Controls.svelte.d.ts +24 -0
- package/dist/shared/ImageEditor.svelte +329 -0
- package/dist/shared/ImageEditor.svelte.d.ts +79 -0
- package/dist/shared/InteractiveImageEditor.svelte +273 -0
- package/dist/shared/InteractiveImageEditor.svelte.d.ts +58 -0
- package/dist/shared/layers/Layers.svelte +216 -0
- package/dist/shared/layers/Layers.svelte.d.ts +18 -0
- package/dist/shared/layers/utils.d.ts +69 -0
- package/dist/shared/layers/utils.js +131 -0
- package/dist/shared/tools/Brush.svelte +208 -0
- package/dist/shared/tools/Brush.svelte.d.ts +29 -0
- package/dist/shared/tools/BrushOptions.svelte +162 -0
- package/dist/shared/tools/BrushOptions.svelte.d.ts +29 -0
- package/dist/shared/tools/BrushSize.svelte +60 -0
- package/dist/shared/tools/BrushSize.svelte.d.ts +20 -0
- package/dist/shared/tools/ColorField.svelte +159 -0
- package/dist/shared/tools/ColorField.svelte.d.ts +20 -0
- package/dist/shared/tools/ColorPicker.svelte +179 -0
- package/dist/shared/tools/ColorPicker.svelte.d.ts +16 -0
- package/dist/shared/tools/ColorSwatch.svelte +183 -0
- package/dist/shared/tools/ColorSwatch.svelte.d.ts +30 -0
- package/dist/shared/tools/Crop.svelte +155 -0
- package/dist/shared/tools/Crop.svelte.d.ts +16 -0
- package/dist/shared/tools/Cropper.svelte +271 -0
- package/dist/shared/tools/Cropper.svelte.d.ts +41 -0
- package/dist/shared/tools/Handle.svelte +260 -0
- package/dist/shared/tools/Handle.svelte.d.ts +29 -0
- package/dist/shared/tools/Sources.svelte +250 -0
- package/dist/shared/tools/Sources.svelte.d.ts +32 -0
- package/dist/shared/tools/Tools.svelte +101 -0
- package/dist/shared/tools/Tools.svelte.d.ts +48 -0
- package/dist/shared/tools/brush.d.ts +62 -0
- package/dist/shared/tools/brush.js +133 -0
- package/dist/shared/tools/crop.d.ts +15 -0
- package/dist/shared/tools/crop.js +154 -0
- package/dist/shared/tools/index.d.ts +5 -0
- package/dist/shared/tools/index.js +5 -0
- package/dist/shared/tools/sources.d.ts +40 -0
- package/dist/shared/tools/sources.js +69 -0
- package/dist/shared/tools/types.d.ts +1 -0
- package/dist/shared/tools/types.js +1 -0
- package/dist/shared/utils/commands.d.ts +96 -0
- package/dist/shared/utils/commands.js +65 -0
- package/dist/shared/utils/events.d.ts +7 -0
- package/dist/shared/utils/events.js +22 -0
- package/dist/shared/utils/parse_placeholder.d.ts +1 -0
- package/dist/shared/utils/parse_placeholder.js +19 -0
- package/dist/shared/utils/pixi.d.ts +76 -0
- package/dist/shared/utils/pixi.js +152 -0
- package/package.json +23 -12
- package/shared/ImageEditor.svelte +8 -4
- package/shared/InteractiveImageEditor.svelte +9 -2
- package/shared/tools/Brush.svelte +4 -6
- package/shared/tools/BrushOptions.svelte +1 -1
- package/shared/tools/Crop.svelte +2 -1
- package/shared/utils/pixi.ts +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
|
+
export interface EditorData {
|
|
3
|
+
background: FileData | null;
|
|
4
|
+
layers: FileData[] | null;
|
|
5
|
+
composite: FileData | null;
|
|
6
|
+
}
|
|
7
|
+
export interface ImageBlobs {
|
|
8
|
+
background: FileData | null;
|
|
9
|
+
layers: FileData[];
|
|
10
|
+
composite: FileData | null;
|
|
11
|
+
}
|
|
12
|
+
import { type I18nFormatter } from "@gradio/utils";
|
|
13
|
+
import { type FileData, type Client } from "@gradio/client";
|
|
14
|
+
import { type Brush as IBrush } from "./tools/Brush.svelte";
|
|
15
|
+
import { type Eraser } from "./tools/Brush.svelte";
|
|
16
|
+
declare const __propDef: {
|
|
17
|
+
props: {
|
|
18
|
+
brush: IBrush | null;
|
|
19
|
+
eraser: Eraser | null;
|
|
20
|
+
sources: ("clipboard" | "webcam" | "upload")[];
|
|
21
|
+
crop_size?: ([number, number] | `${string}:${string}` | null) | undefined;
|
|
22
|
+
i18n: I18nFormatter;
|
|
23
|
+
root: string;
|
|
24
|
+
label?: string | undefined;
|
|
25
|
+
show_label: boolean;
|
|
26
|
+
changeable?: boolean | undefined;
|
|
27
|
+
value?: (EditorData | null) | undefined;
|
|
28
|
+
transforms?: "crop"[] | undefined;
|
|
29
|
+
layers: boolean;
|
|
30
|
+
accept_blobs: (a: any) => void;
|
|
31
|
+
status?: ("pending" | "complete" | "error" | "generating" | "streaming") | undefined;
|
|
32
|
+
canvas_size: [number, number] | undefined;
|
|
33
|
+
realtime: boolean;
|
|
34
|
+
upload: Client["upload"];
|
|
35
|
+
stream_handler: Client["stream"];
|
|
36
|
+
dragging: boolean;
|
|
37
|
+
placeholder?: string | undefined;
|
|
38
|
+
height?: number | undefined;
|
|
39
|
+
get_data?: (() => Promise<ImageBlobs>) | undefined;
|
|
40
|
+
image_id?: (null | string) | undefined;
|
|
41
|
+
};
|
|
42
|
+
events: {
|
|
43
|
+
save: CustomEvent<void>;
|
|
44
|
+
clear?: CustomEvent<undefined> | undefined;
|
|
45
|
+
upload?: CustomEvent<undefined> | undefined;
|
|
46
|
+
change?: CustomEvent<undefined> | undefined;
|
|
47
|
+
} & {
|
|
48
|
+
[evt: string]: CustomEvent<any>;
|
|
49
|
+
};
|
|
50
|
+
slots: {};
|
|
51
|
+
};
|
|
52
|
+
export type InteractiveImageEditorProps = typeof __propDef.props;
|
|
53
|
+
export type InteractiveImageEditorEvents = typeof __propDef.events;
|
|
54
|
+
export type InteractiveImageEditorSlots = typeof __propDef.slots;
|
|
55
|
+
export default class InteractiveImageEditor extends SvelteComponent<InteractiveImageEditorProps, InteractiveImageEditorEvents, InteractiveImageEditorSlots> {
|
|
56
|
+
get get_data(): () => Promise<ImageBlobs>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
<script>import { getContext, onMount, tick } from "svelte";
|
|
2
|
+
import { click_outside } from "../utils/events";
|
|
3
|
+
import { layer_manager } from "./utils";
|
|
4
|
+
import { EDITOR_KEY } from "../ImageEditor.svelte";
|
|
5
|
+
import { Layers } from "@gradio/icons";
|
|
6
|
+
let show_layers = false;
|
|
7
|
+
export let layer_files = [];
|
|
8
|
+
export let enable_layers = true;
|
|
9
|
+
const { pixi, current_layer, dimensions, register_context } = getContext(EDITOR_KEY);
|
|
10
|
+
const LayerManager = layer_manager();
|
|
11
|
+
let layers = [];
|
|
12
|
+
register_context("layers", {
|
|
13
|
+
init_fn: () => {
|
|
14
|
+
new_layer();
|
|
15
|
+
},
|
|
16
|
+
reset_fn: () => {
|
|
17
|
+
LayerManager.reset();
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
async function validate_layers() {
|
|
21
|
+
let invalid = layers.some(
|
|
22
|
+
(layer) => layer.composite.texture?.width != $dimensions[0] || layer.composite.texture?.height != $dimensions[1]
|
|
23
|
+
);
|
|
24
|
+
if (invalid) {
|
|
25
|
+
LayerManager.reset();
|
|
26
|
+
if (!layer_files || layer_files.length == 0)
|
|
27
|
+
new_layer();
|
|
28
|
+
else
|
|
29
|
+
render_layer_files(layer_files);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
$:
|
|
33
|
+
$dimensions, validate_layers();
|
|
34
|
+
async function new_layer() {
|
|
35
|
+
if (!$pixi)
|
|
36
|
+
return;
|
|
37
|
+
const [active_layer, all_layers] = LayerManager.add_layer(
|
|
38
|
+
$pixi.layer_container,
|
|
39
|
+
$pixi.renderer,
|
|
40
|
+
...$dimensions
|
|
41
|
+
);
|
|
42
|
+
$current_layer = active_layer;
|
|
43
|
+
layers = all_layers;
|
|
44
|
+
}
|
|
45
|
+
$:
|
|
46
|
+
render_layer_files(layer_files);
|
|
47
|
+
function is_not_null(x) {
|
|
48
|
+
return x !== null;
|
|
49
|
+
}
|
|
50
|
+
async function render_layer_files(_layer_files) {
|
|
51
|
+
await tick();
|
|
52
|
+
if (!_layer_files || _layer_files.length == 0) {
|
|
53
|
+
LayerManager.reset();
|
|
54
|
+
new_layer();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (!$pixi)
|
|
58
|
+
return;
|
|
59
|
+
const fetch_promises = await Promise.all(
|
|
60
|
+
_layer_files.map((f) => {
|
|
61
|
+
if (!f || !f.url)
|
|
62
|
+
return null;
|
|
63
|
+
return fetch(f.url);
|
|
64
|
+
})
|
|
65
|
+
);
|
|
66
|
+
const blobs = await Promise.all(
|
|
67
|
+
fetch_promises.map((p) => {
|
|
68
|
+
if (!p)
|
|
69
|
+
return null;
|
|
70
|
+
return p.blob();
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
LayerManager.reset();
|
|
74
|
+
let last_layer = null;
|
|
75
|
+
for (const blob of blobs.filter(is_not_null)) {
|
|
76
|
+
last_layer = await LayerManager.add_layer_from_blob(
|
|
77
|
+
$pixi.layer_container,
|
|
78
|
+
$pixi.renderer,
|
|
79
|
+
blob,
|
|
80
|
+
$pixi.view
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (!last_layer)
|
|
84
|
+
return;
|
|
85
|
+
$current_layer = last_layer[0];
|
|
86
|
+
layers = last_layer[1];
|
|
87
|
+
}
|
|
88
|
+
onMount(async () => {
|
|
89
|
+
await tick();
|
|
90
|
+
if (!$pixi)
|
|
91
|
+
return;
|
|
92
|
+
$pixi = { ...$pixi, get_layers: LayerManager.get_layers };
|
|
93
|
+
});
|
|
94
|
+
</script>
|
|
95
|
+
|
|
96
|
+
{#if enable_layers}
|
|
97
|
+
<div
|
|
98
|
+
class="layer-wrap"
|
|
99
|
+
class:closed={!show_layers}
|
|
100
|
+
use:click_outside={() => (show_layers = false)}
|
|
101
|
+
>
|
|
102
|
+
<button
|
|
103
|
+
aria-label="Show Layers"
|
|
104
|
+
on:click={() => (show_layers = !show_layers)}
|
|
105
|
+
><span class="icon"><Layers /></span> Layer {layers.findIndex(
|
|
106
|
+
(l) => l === $current_layer
|
|
107
|
+
) + 1}
|
|
108
|
+
</button>
|
|
109
|
+
{#if show_layers}
|
|
110
|
+
<ul>
|
|
111
|
+
{#each layers as layer, i (i)}
|
|
112
|
+
<li>
|
|
113
|
+
<button
|
|
114
|
+
class:selected_layer={$current_layer === layer}
|
|
115
|
+
on:click={() =>
|
|
116
|
+
($current_layer = LayerManager.change_active_layer(i))}
|
|
117
|
+
>Layer {i + 1}</button
|
|
118
|
+
>
|
|
119
|
+
</li>
|
|
120
|
+
{/each}
|
|
121
|
+
<li>
|
|
122
|
+
<button aria-label="Add Layer" on:click={new_layer}> +</button>
|
|
123
|
+
</li>
|
|
124
|
+
</ul>
|
|
125
|
+
{/if}
|
|
126
|
+
|
|
127
|
+
<span class="sep"></span>
|
|
128
|
+
</div>
|
|
129
|
+
{/if}
|
|
130
|
+
|
|
131
|
+
<style>
|
|
132
|
+
.icon {
|
|
133
|
+
width: 14px;
|
|
134
|
+
margin-right: var(--spacing-md);
|
|
135
|
+
color: var(--block-label-text-color);
|
|
136
|
+
margin-right: var(--spacing-lg);
|
|
137
|
+
margin-top: 1px;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.layer-wrap {
|
|
141
|
+
position: relative;
|
|
142
|
+
display: flex;
|
|
143
|
+
justify-content: center;
|
|
144
|
+
align-items: center;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.layer-wrap button {
|
|
148
|
+
justify-content: flex-start;
|
|
149
|
+
align-items: flex-start;
|
|
150
|
+
width: 100%;
|
|
151
|
+
border-bottom: 1px solid var(--block-border-color);
|
|
152
|
+
display: flex;
|
|
153
|
+
align-items: center;
|
|
154
|
+
justify-content: center;
|
|
155
|
+
font-size: var(--scale-000);
|
|
156
|
+
line-height: var(--line-sm);
|
|
157
|
+
padding-bottom: 1px;
|
|
158
|
+
margin-left: var(--spacing-xl);
|
|
159
|
+
padding: var(--spacing-sm) 0;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.layer-wrap li:last-child button {
|
|
163
|
+
border-bottom: none;
|
|
164
|
+
text-align: center;
|
|
165
|
+
font-size: var(--scale-0);
|
|
166
|
+
line-height: 1;
|
|
167
|
+
font-weight: var(--weight-bold);
|
|
168
|
+
padding: 5px 0 1px 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.closed > button {
|
|
172
|
+
border-bottom: none;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.layer-wrap button:hover {
|
|
176
|
+
background-color: none;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.layer-wrap button:hover .icon {
|
|
180
|
+
color: var(--color-accent);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.selected_layer {
|
|
184
|
+
background-color: var(--block-background-fill);
|
|
185
|
+
color: var(--color-accent);
|
|
186
|
+
font-weight: bold;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
ul {
|
|
190
|
+
position: absolute;
|
|
191
|
+
bottom: 0;
|
|
192
|
+
left: 0;
|
|
193
|
+
background: var(--block-background-fill);
|
|
194
|
+
width: calc(100% + 1px);
|
|
195
|
+
list-style: none;
|
|
196
|
+
z-index: var(--layer-top);
|
|
197
|
+
border: 1px solid var(--block-border-color);
|
|
198
|
+
padding: var(--spacing-sm) 0;
|
|
199
|
+
text-wrap: none;
|
|
200
|
+
transform: translate(-1px, 1px);
|
|
201
|
+
border-radius: var(--radius-sm);
|
|
202
|
+
border-bottom-right-radius: 0;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
.layer-wrap ul > li > button {
|
|
206
|
+
margin-left: 0;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.sep {
|
|
210
|
+
height: 12px;
|
|
211
|
+
background-color: var(--block-border-color);
|
|
212
|
+
width: 1px;
|
|
213
|
+
display: block;
|
|
214
|
+
margin-left: var(--spacing-xl);
|
|
215
|
+
}
|
|
216
|
+
</style>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
|
+
import type { FileData } from "@gradio/client";
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
layer_files?: ((FileData | null)[] | null) | undefined;
|
|
6
|
+
enable_layers?: boolean | undefined;
|
|
7
|
+
};
|
|
8
|
+
events: {
|
|
9
|
+
[evt: string]: CustomEvent<any>;
|
|
10
|
+
};
|
|
11
|
+
slots: {};
|
|
12
|
+
};
|
|
13
|
+
export type LayersProps = typeof __propDef.props;
|
|
14
|
+
export type LayersEvents = typeof __propDef.events;
|
|
15
|
+
export type LayersSlots = typeof __propDef.slots;
|
|
16
|
+
export default class Layers extends SvelteComponent<LayersProps, LayersEvents, LayersSlots> {
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Container, type IRenderer, RenderTexture, Sprite, Filter } from "pixi.js";
|
|
2
|
+
/**
|
|
3
|
+
* GLSL Shader that takes two textures and erases the second texture from the first.
|
|
4
|
+
*/
|
|
5
|
+
export declare const erase_shader = "\nprecision highp float;\n\nuniform sampler2D uDrawingTexture;\nuniform sampler2D uEraserTexture;\n\nvarying vec2 vTextureCoord;\n\nvoid main(void) {\n\tvec4 drawingColor = texture2D(uDrawingTexture,vTextureCoord);\n\tvec4 eraserColor = texture2D(uEraserTexture, vTextureCoord);\n\n\t// Use the alpha of the eraser to determine how much to \"erase\" from the drawing\n\tfloat alpha = 1.0 - eraserColor.a;\n\tgl_FragColor = vec4(drawingColor.rgb * alpha, drawingColor.a * alpha);\n}";
|
|
6
|
+
/**
|
|
7
|
+
* Interface holding data for a layer
|
|
8
|
+
*/
|
|
9
|
+
export interface LayerScene {
|
|
10
|
+
/**
|
|
11
|
+
* The texture used for tracking brush strokes.
|
|
12
|
+
*/
|
|
13
|
+
draw_texture: RenderTexture;
|
|
14
|
+
/**
|
|
15
|
+
* The texture used for tracking eraser strokes.
|
|
16
|
+
*/
|
|
17
|
+
erase_texture: RenderTexture;
|
|
18
|
+
/**
|
|
19
|
+
* The sprite used for displaying the composite of the draw and erase textures.
|
|
20
|
+
*/
|
|
21
|
+
composite: Sprite;
|
|
22
|
+
/**
|
|
23
|
+
* The filter used for combining the draw and erase textures into a composite texture.
|
|
24
|
+
*/
|
|
25
|
+
filter?: Filter;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Interface for managing layers.
|
|
29
|
+
*/
|
|
30
|
+
interface LayerManager {
|
|
31
|
+
/**
|
|
32
|
+
* Adds a layer to the container.
|
|
33
|
+
* @param layer The container to add the layer to.
|
|
34
|
+
* @param renderer The renderer to use for the layer.
|
|
35
|
+
* @param width the width of the layer
|
|
36
|
+
* @param height the height of the layer
|
|
37
|
+
*/
|
|
38
|
+
add_layer(container: Container, renderer: IRenderer, width: number, height: number): [LayerScene, LayerScene[]];
|
|
39
|
+
/**
|
|
40
|
+
* Swaps the layer with the layer above or below it.
|
|
41
|
+
* @param layer The index layer to swap.
|
|
42
|
+
* @param direction The direction to swap the layer.
|
|
43
|
+
*/
|
|
44
|
+
swap_layers(layer: number, direction: "up" | "down"): LayerScene;
|
|
45
|
+
/**
|
|
46
|
+
* Changes the active layer.
|
|
47
|
+
* @param layer The index of the layer to make active.
|
|
48
|
+
*/
|
|
49
|
+
change_active_layer(layer: number): LayerScene;
|
|
50
|
+
/**
|
|
51
|
+
* Resizes the layers.
|
|
52
|
+
* @param width The new width of the layers.
|
|
53
|
+
* @param height The new height of the layers.
|
|
54
|
+
*/
|
|
55
|
+
reset(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the layers.
|
|
58
|
+
* @returns The layers.
|
|
59
|
+
*/
|
|
60
|
+
get_layers(): LayerScene[];
|
|
61
|
+
add_layer_from_blob(container: Container, renderer: IRenderer, blob: Blob, view: HTMLCanvasElement): Promise<[LayerScene, LayerScene[]]>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates a layer manager.
|
|
65
|
+
* @param canvas_resize a function to resize the canvas
|
|
66
|
+
* @returns a layer manager
|
|
67
|
+
*/
|
|
68
|
+
export declare function layer_manager(): LayerManager;
|
|
69
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Container, RenderTexture, Texture, Sprite, Filter } from "pixi.js";
|
|
2
|
+
/**
|
|
3
|
+
* GLSL Shader that takes two textures and erases the second texture from the first.
|
|
4
|
+
*/
|
|
5
|
+
export const erase_shader = `
|
|
6
|
+
precision highp float;
|
|
7
|
+
|
|
8
|
+
uniform sampler2D uDrawingTexture;
|
|
9
|
+
uniform sampler2D uEraserTexture;
|
|
10
|
+
|
|
11
|
+
varying vec2 vTextureCoord;
|
|
12
|
+
|
|
13
|
+
void main(void) {
|
|
14
|
+
vec4 drawingColor = texture2D(uDrawingTexture,vTextureCoord);
|
|
15
|
+
vec4 eraserColor = texture2D(uEraserTexture, vTextureCoord);
|
|
16
|
+
|
|
17
|
+
// Use the alpha of the eraser to determine how much to "erase" from the drawing
|
|
18
|
+
float alpha = 1.0 - eraserColor.a;
|
|
19
|
+
gl_FragColor = vec4(drawingColor.rgb * alpha, drawingColor.a * alpha);
|
|
20
|
+
}`;
|
|
21
|
+
/**
|
|
22
|
+
* Swaps two adjacent elements in an array.
|
|
23
|
+
* @param array The array to swap elements in.
|
|
24
|
+
* @param index The index of the first element to swap.
|
|
25
|
+
*/
|
|
26
|
+
function swap_adjacent(array, index) {
|
|
27
|
+
if (index < 0 || index >= array.length - 1) {
|
|
28
|
+
throw new Error("Index out of bounds");
|
|
29
|
+
}
|
|
30
|
+
[array[index], array[index + 1]] = [array[index + 1], array[index]];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a layer manager.
|
|
34
|
+
* @param canvas_resize a function to resize the canvas
|
|
35
|
+
* @returns a layer manager
|
|
36
|
+
*/
|
|
37
|
+
export function layer_manager() {
|
|
38
|
+
let _layers = [];
|
|
39
|
+
let current_layer = 0;
|
|
40
|
+
let position = 0;
|
|
41
|
+
return {
|
|
42
|
+
add_layer: function (container, renderer, width, height) {
|
|
43
|
+
const layer_container = new Container();
|
|
44
|
+
position++;
|
|
45
|
+
layer_container.zIndex = position;
|
|
46
|
+
const composite_texture = RenderTexture.create({
|
|
47
|
+
width,
|
|
48
|
+
height
|
|
49
|
+
});
|
|
50
|
+
const composite = new Sprite(composite_texture);
|
|
51
|
+
layer_container.addChild(composite);
|
|
52
|
+
composite.zIndex = position;
|
|
53
|
+
const layer_scene = {
|
|
54
|
+
draw_texture: RenderTexture.create({
|
|
55
|
+
width,
|
|
56
|
+
height
|
|
57
|
+
}),
|
|
58
|
+
erase_texture: RenderTexture.create({
|
|
59
|
+
width,
|
|
60
|
+
height
|
|
61
|
+
}),
|
|
62
|
+
composite
|
|
63
|
+
};
|
|
64
|
+
const erase_filter = new Filter(undefined, erase_shader, {
|
|
65
|
+
uEraserTexture: layer_scene.erase_texture,
|
|
66
|
+
uDrawingTexture: layer_scene.draw_texture
|
|
67
|
+
});
|
|
68
|
+
composite.filters = [erase_filter];
|
|
69
|
+
container.addChild(layer_container);
|
|
70
|
+
_layers.push(layer_scene);
|
|
71
|
+
return [layer_scene, _layers];
|
|
72
|
+
},
|
|
73
|
+
swap_layers: function (layer, direction) {
|
|
74
|
+
if (direction === "up") {
|
|
75
|
+
swap_adjacent(_layers, layer);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
swap_adjacent(_layers, layer - 1);
|
|
79
|
+
}
|
|
80
|
+
return _layers[layer];
|
|
81
|
+
},
|
|
82
|
+
change_active_layer: function (layer) {
|
|
83
|
+
current_layer = layer;
|
|
84
|
+
return _layers[layer];
|
|
85
|
+
},
|
|
86
|
+
reset() {
|
|
87
|
+
_layers.forEach((layer) => {
|
|
88
|
+
layer.draw_texture.destroy(true);
|
|
89
|
+
layer.erase_texture.destroy(true);
|
|
90
|
+
layer.composite.destroy(true);
|
|
91
|
+
});
|
|
92
|
+
_layers = [];
|
|
93
|
+
current_layer = 0;
|
|
94
|
+
position = 0;
|
|
95
|
+
},
|
|
96
|
+
async add_layer_from_blob(container, renderer, blob, view) {
|
|
97
|
+
const img = await createImageBitmap(blob);
|
|
98
|
+
const bitmap_texture = Texture.from(img);
|
|
99
|
+
const [w, h] = resize_to_fit(bitmap_texture.width, bitmap_texture.height, view.width, view.height);
|
|
100
|
+
const sprite = new Sprite(bitmap_texture);
|
|
101
|
+
sprite.zIndex = 0;
|
|
102
|
+
sprite.width = w;
|
|
103
|
+
sprite.height = h;
|
|
104
|
+
const [layer, layers] = this.add_layer(container, renderer, view.width, view.height);
|
|
105
|
+
renderer.render(sprite, {
|
|
106
|
+
renderTexture: layer.draw_texture
|
|
107
|
+
});
|
|
108
|
+
return [layer, layers];
|
|
109
|
+
},
|
|
110
|
+
get_layers() {
|
|
111
|
+
return _layers;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function resize_to_fit(inner_width, inner_height, outer_width, outer_height) {
|
|
116
|
+
if (inner_width <= outer_width && inner_height <= outer_height) {
|
|
117
|
+
return [inner_width, inner_height];
|
|
118
|
+
}
|
|
119
|
+
const inner_aspect = inner_width / inner_height;
|
|
120
|
+
const outer_aspect = outer_width / outer_height;
|
|
121
|
+
let new_width, new_height;
|
|
122
|
+
if (inner_aspect > outer_aspect) {
|
|
123
|
+
new_width = outer_width;
|
|
124
|
+
new_height = outer_width / inner_aspect;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
new_height = outer_height;
|
|
128
|
+
new_width = outer_height * inner_aspect;
|
|
129
|
+
}
|
|
130
|
+
return [new_width, new_height];
|
|
131
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
<script context="module">import {} from "tinycolor2";
|
|
2
|
+
</script>
|
|
3
|
+
|
|
4
|
+
<script>import tinycolor from "tinycolor2";
|
|
5
|
+
import { clamp } from "../utils/pixi";
|
|
6
|
+
import { getContext, onMount, tick } from "svelte";
|
|
7
|
+
import { TOOL_KEY } from "./Tools.svelte";
|
|
8
|
+
import { EDITOR_KEY } from "../ImageEditor.svelte";
|
|
9
|
+
import { draw_path } from "./brush";
|
|
10
|
+
import BrushOptions from "./BrushOptions.svelte";
|
|
11
|
+
export let default_size;
|
|
12
|
+
export let default_color = void 0;
|
|
13
|
+
export let colors = void 0;
|
|
14
|
+
export let color_mode = void 0;
|
|
15
|
+
export let mode;
|
|
16
|
+
$:
|
|
17
|
+
processed_colors = colors ? colors.map(process_color).filter((_, i) => i < 4) : [];
|
|
18
|
+
$:
|
|
19
|
+
selected_color = default_color === "auto" ? processed_colors[0] : !default_color ? "black" : process_color(default_color);
|
|
20
|
+
let brush_options = false;
|
|
21
|
+
const {
|
|
22
|
+
pixi,
|
|
23
|
+
dimensions,
|
|
24
|
+
current_layer,
|
|
25
|
+
command_manager,
|
|
26
|
+
register_context,
|
|
27
|
+
editor_box,
|
|
28
|
+
crop,
|
|
29
|
+
toolbar_box
|
|
30
|
+
} = getContext(EDITOR_KEY);
|
|
31
|
+
const { active_tool, register_tool, current_color } = getContext(TOOL_KEY);
|
|
32
|
+
let drawing = false;
|
|
33
|
+
let draw;
|
|
34
|
+
function generate_sizes(x, y) {
|
|
35
|
+
const min = clamp(Math.min(x, y), 500, 1e3);
|
|
36
|
+
return Math.round(min * 2 / 100);
|
|
37
|
+
}
|
|
38
|
+
$:
|
|
39
|
+
mode === "draw" && current_color.set(selected_color);
|
|
40
|
+
let selected_size = default_size === "auto" ? generate_sizes(...$dimensions) : default_size;
|
|
41
|
+
function pointer_down_handler(event) {
|
|
42
|
+
if ($active_tool !== mode) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
drawing = true;
|
|
46
|
+
if (!$pixi || !$current_layer) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
draw = draw_path(
|
|
50
|
+
$pixi.renderer,
|
|
51
|
+
$pixi.layer_container,
|
|
52
|
+
$current_layer,
|
|
53
|
+
mode
|
|
54
|
+
);
|
|
55
|
+
draw.start({
|
|
56
|
+
x: event.screen.x,
|
|
57
|
+
y: event.screen.y,
|
|
58
|
+
color: selected_color || void 0,
|
|
59
|
+
size: selected_size,
|
|
60
|
+
opacity: 1
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function pointer_up_handler(event) {
|
|
64
|
+
if (!$pixi || !$current_layer) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if ($active_tool !== mode) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
draw.stop();
|
|
71
|
+
command_manager.execute(draw);
|
|
72
|
+
drawing = false;
|
|
73
|
+
}
|
|
74
|
+
function pointer_move_handler(event) {
|
|
75
|
+
if ($active_tool !== mode) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (drawing) {
|
|
79
|
+
draw.continue({
|
|
80
|
+
x: event.screen.x,
|
|
81
|
+
y: event.screen.y
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const x_bound = $crop[0] * $dimensions[0];
|
|
85
|
+
const y_bound = $crop[1] * $dimensions[1];
|
|
86
|
+
if (x_bound > event.screen.x || y_bound > event.screen.y || event.screen.x > x_bound + $crop[2] * $dimensions[0] || event.screen.y > y_bound + $crop[3] * $dimensions[1]) {
|
|
87
|
+
brush_cursor = false;
|
|
88
|
+
document.body.style.cursor = "auto";
|
|
89
|
+
} else {
|
|
90
|
+
brush_cursor = true;
|
|
91
|
+
document.body.style.cursor = "none";
|
|
92
|
+
}
|
|
93
|
+
if (brush_cursor) {
|
|
94
|
+
pos = {
|
|
95
|
+
x: event.clientX - $editor_box.child_left,
|
|
96
|
+
y: event.clientY - $editor_box.child_top
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let brush_cursor = false;
|
|
101
|
+
async function toggle_listeners(on_off) {
|
|
102
|
+
$pixi?.layer_container[on_off]("pointerdown", pointer_down_handler);
|
|
103
|
+
$pixi?.layer_container[on_off]("pointerup", pointer_up_handler);
|
|
104
|
+
$pixi?.layer_container[on_off]("pointermove", pointer_move_handler);
|
|
105
|
+
$pixi?.layer_container[on_off](
|
|
106
|
+
"pointerenter",
|
|
107
|
+
(event) => {
|
|
108
|
+
if ($active_tool === mode) {
|
|
109
|
+
brush_cursor = true;
|
|
110
|
+
document.body.style.cursor = "none";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
$pixi?.layer_container[on_off](
|
|
115
|
+
"pointerleave",
|
|
116
|
+
() => (brush_cursor = false, document.body.style.cursor = "auto")
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
register_context(mode, {
|
|
120
|
+
init_fn: () => {
|
|
121
|
+
toggle_listeners("on");
|
|
122
|
+
},
|
|
123
|
+
reset_fn: () => {
|
|
124
|
+
toggle_listeners("off");
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
const toggle_options = debounce_toggle();
|
|
128
|
+
const unregister = register_tool(mode, {
|
|
129
|
+
cb: toggle_options
|
|
130
|
+
});
|
|
131
|
+
onMount(() => {
|
|
132
|
+
return () => {
|
|
133
|
+
unregister();
|
|
134
|
+
toggle_listeners("off");
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
let recent_colors = [null, null, null];
|
|
138
|
+
function process_color(color) {
|
|
139
|
+
return tinycolor(color).toRgbString();
|
|
140
|
+
}
|
|
141
|
+
let pos = { x: 0, y: 0 };
|
|
142
|
+
$:
|
|
143
|
+
brush_size = selected_size / $dimensions[0] * $editor_box.child_width * 2;
|
|
144
|
+
function debounce_toggle() {
|
|
145
|
+
let timeout = null;
|
|
146
|
+
return function executedFunction(should_close) {
|
|
147
|
+
const later = () => {
|
|
148
|
+
if (timeout) {
|
|
149
|
+
clearTimeout(timeout);
|
|
150
|
+
}
|
|
151
|
+
if (should_close !== void 0) {
|
|
152
|
+
brush_options = should_close;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
brush_options = !brush_options;
|
|
156
|
+
};
|
|
157
|
+
if (timeout) {
|
|
158
|
+
clearTimeout(timeout);
|
|
159
|
+
}
|
|
160
|
+
timeout = setTimeout(later, 100);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
</script>
|
|
164
|
+
|
|
165
|
+
<svelte:window
|
|
166
|
+
on:keydown={({ key }) => key === "Escape" && toggle_options(false)}
|
|
167
|
+
/>
|
|
168
|
+
|
|
169
|
+
<span
|
|
170
|
+
style:transform="translate({pos.x}px, {pos.y}px)"
|
|
171
|
+
style:top="{$editor_box.child_top -
|
|
172
|
+
$editor_box.parent_top -
|
|
173
|
+
brush_size / 2}px"
|
|
174
|
+
style:left="{$editor_box.child_left -
|
|
175
|
+
$editor_box.parent_left -
|
|
176
|
+
brush_size / 2}px"
|
|
177
|
+
style:width="{brush_size}px"
|
|
178
|
+
style:height="{brush_size}px"
|
|
179
|
+
style:opacity={brush_cursor ? 1 : 0}
|
|
180
|
+
/>
|
|
181
|
+
|
|
182
|
+
{#if brush_options}
|
|
183
|
+
<div>
|
|
184
|
+
<BrushOptions
|
|
185
|
+
show_swatch={mode === "draw"}
|
|
186
|
+
on:click_outside={() => toggle_options()}
|
|
187
|
+
colors={processed_colors}
|
|
188
|
+
bind:selected_color
|
|
189
|
+
{color_mode}
|
|
190
|
+
bind:recent_colors
|
|
191
|
+
bind:selected_size
|
|
192
|
+
dimensions={$dimensions}
|
|
193
|
+
parent_width={$editor_box.parent_width}
|
|
194
|
+
parent_height={$editor_box.parent_height}
|
|
195
|
+
parent_left={$editor_box.parent_left}
|
|
196
|
+
toolbar_box={$toolbar_box}
|
|
197
|
+
/>
|
|
198
|
+
</div>
|
|
199
|
+
{/if}
|
|
200
|
+
|
|
201
|
+
<style>
|
|
202
|
+
span {
|
|
203
|
+
position: absolute;
|
|
204
|
+
background: rgba(0, 0, 0, 0.5);
|
|
205
|
+
pointer-events: none;
|
|
206
|
+
border-radius: 50%;
|
|
207
|
+
}
|
|
208
|
+
</style>
|