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