Upload folder using huggingface_hub
Browse files- 6.0.0-dev.5/imageeditor/Example.svelte +44 -0
- 6.0.0-dev.5/imageeditor/Index.svelte +213 -0
- 6.0.0-dev.5/imageeditor/InteractiveImageEditor.svelte +309 -0
- 6.0.0-dev.5/imageeditor/package.json +48 -0
- 6.0.0-dev.5/imageeditor/shared/Anchor.svelte +384 -0
- 6.0.0-dev.5/imageeditor/shared/Controls.svelte +251 -0
- 6.0.0-dev.5/imageeditor/shared/IconButton.svelte +150 -0
- 6.0.0-dev.5/imageeditor/shared/ImageEditor.svelte +904 -0
- 6.0.0-dev.5/imageeditor/shared/Layers.svelte +282 -0
- 6.0.0-dev.5/imageeditor/shared/Resize.svelte +239 -0
- 6.0.0-dev.5/imageeditor/shared/SecondaryToolbar.svelte +67 -0
- 6.0.0-dev.5/imageeditor/shared/Toolbar.svelte +383 -0
- 6.0.0-dev.5/imageeditor/shared/brush/BrushOptions.svelte +197 -0
- 6.0.0-dev.5/imageeditor/shared/brush/BrushSize.svelte +40 -0
- 6.0.0-dev.5/imageeditor/shared/brush/ColorField.svelte +174 -0
- 6.0.0-dev.5/imageeditor/shared/brush/ColorPicker.svelte +283 -0
- 6.0.0-dev.5/imageeditor/shared/brush/ColorSwatch.svelte +253 -0
- 6.0.0-dev.5/imageeditor/shared/brush/brush-cursor.ts +366 -0
- 6.0.0-dev.5/imageeditor/shared/brush/brush-textures.ts +757 -0
- 6.0.0-dev.5/imageeditor/shared/brush/brush-utils.ts +76 -0
- 6.0.0-dev.5/imageeditor/shared/brush/brush.ts +468 -0
- 6.0.0-dev.5/imageeditor/shared/brush/types.ts +41 -0
- 6.0.0-dev.5/imageeditor/shared/core/commands.ts +134 -0
- 6.0.0-dev.5/imageeditor/shared/core/editor.ts +885 -0
- 6.0.0-dev.5/imageeditor/shared/core/layers.ts +1092 -0
- 6.0.0-dev.5/imageeditor/shared/crop/crop.ts +1027 -0
- 6.0.0-dev.5/imageeditor/shared/image/image.ts +420 -0
- 6.0.0-dev.5/imageeditor/shared/resize/resize.ts +1759 -0
- 6.0.0-dev.5/imageeditor/shared/types.ts +29 -0
- 6.0.0-dev.5/imageeditor/shared/utils/events.ts +30 -0
- 6.0.0-dev.5/imageeditor/shared/utils/parse_placeholder.ts +27 -0
- 6.0.0-dev.5/imageeditor/shared/utils/pixi.ts +71 -0
- 6.0.0-dev.5/imageeditor/shared/zoom/zoom.ts +675 -0
- 6.0.0-dev.5/imageeditor/types.ts +74 -0
6.0.0-dev.5/imageeditor/Example.svelte
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { BaseImage as Image } from "@gradio/image";
|
| 3 |
+
import type { EditorData } from "./InteractiveImageEditor.svelte";
|
| 4 |
+
|
| 5 |
+
export let value: EditorData;
|
| 6 |
+
export let type: "gallery" | "table";
|
| 7 |
+
export let selected = false;
|
| 8 |
+
</script>
|
| 9 |
+
|
| 10 |
+
<div
|
| 11 |
+
class="container"
|
| 12 |
+
class:table={type === "table"}
|
| 13 |
+
class:gallery={type === "gallery"}
|
| 14 |
+
class:selected
|
| 15 |
+
>
|
| 16 |
+
<Image src={value.composite?.url || value.background?.url} alt="" />
|
| 17 |
+
</div>
|
| 18 |
+
|
| 19 |
+
<style>
|
| 20 |
+
.container :global(img) {
|
| 21 |
+
width: 100%;
|
| 22 |
+
height: 100%;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.container.selected {
|
| 26 |
+
border-color: var(--border-color-accent);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.container.table {
|
| 30 |
+
margin: 0 auto;
|
| 31 |
+
border: 2px solid var(--border-color-primary);
|
| 32 |
+
border-radius: var(--radius-lg);
|
| 33 |
+
width: var(--size-20);
|
| 34 |
+
height: var(--size-20);
|
| 35 |
+
object-fit: cover;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
.container.gallery {
|
| 39 |
+
border: 2px solid var(--border-color-primary);
|
| 40 |
+
height: var(--size-20);
|
| 41 |
+
max-height: var(--size-20);
|
| 42 |
+
object-fit: cover;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
6.0.0-dev.5/imageeditor/Index.svelte
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} immutable={true} />
|
| 2 |
+
|
| 3 |
+
<script lang="ts">
|
| 4 |
+
import type { ImageBlobs } from "./InteractiveImageEditor.svelte";
|
| 5 |
+
|
| 6 |
+
import { FileData } from "@gradio/client";
|
| 7 |
+
|
| 8 |
+
import { BaseStaticImage as StaticImage } from "@gradio/image";
|
| 9 |
+
import InteractiveImageEditor from "./InteractiveImageEditor.svelte";
|
| 10 |
+
import { Block } from "@gradio/atoms";
|
| 11 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 12 |
+
|
| 13 |
+
import { Gradio } from "@gradio/utils";
|
| 14 |
+
import type { ImageEditorEvents, ImageEditorProps } from "./types";
|
| 15 |
+
|
| 16 |
+
let props = $props();
|
| 17 |
+
|
| 18 |
+
class ImageEditorGradio extends Gradio<ImageEditorEvents, ImageEditorProps> {
|
| 19 |
+
async get_data() {
|
| 20 |
+
const data = await super.get_data();
|
| 21 |
+
const value = await _get_value();
|
| 22 |
+
return { ...data, value: value };
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
const gradio = new ImageEditorGradio(props, {
|
| 26 |
+
server: { accept_blobs: () => {} },
|
| 27 |
+
buttons: [],
|
| 28 |
+
height: 350,
|
| 29 |
+
border_region: 0
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
let editor_instance: InteractiveImageEditor;
|
| 33 |
+
let image_id: null | string = $state(null);
|
| 34 |
+
|
| 35 |
+
let has_run_change = false;
|
| 36 |
+
let has_run_input = false;
|
| 37 |
+
|
| 38 |
+
async function _get_value(): Promise<ImageBlobs | { id: string }> {
|
| 39 |
+
if (image_id) {
|
| 40 |
+
const val = { id: image_id };
|
| 41 |
+
image_id = null;
|
| 42 |
+
return val;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const blobs = await editor_instance.get_data();
|
| 46 |
+
|
| 47 |
+
return blobs;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
let is_dragging: boolean;
|
| 51 |
+
|
| 52 |
+
const is_browser = typeof window !== "undefined";
|
| 53 |
+
|
| 54 |
+
// function handle_change(): void {
|
| 55 |
+
// if (gradio.props.value) gradio.dispatch("change");
|
| 56 |
+
// }
|
| 57 |
+
|
| 58 |
+
function handle_save(): void {
|
| 59 |
+
gradio.dispatch("apply");
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// function handle_history_change(): void {
|
| 63 |
+
// gradio.dispatch("change");
|
| 64 |
+
|
| 65 |
+
// gradio.dispatch("input");
|
| 66 |
+
// }
|
| 67 |
+
|
| 68 |
+
let has_value = $derived(
|
| 69 |
+
gradio.props.value?.background ||
|
| 70 |
+
gradio.props.value?.layers?.length ||
|
| 71 |
+
gradio.props.value?.composite
|
| 72 |
+
);
|
| 73 |
+
|
| 74 |
+
let normalised_background = $derived(
|
| 75 |
+
gradio.props.value?.background
|
| 76 |
+
? new FileData(gradio.props.value.background)
|
| 77 |
+
: null
|
| 78 |
+
);
|
| 79 |
+
let normalised_composite = $derived(
|
| 80 |
+
gradio.props.value?.composite
|
| 81 |
+
? new FileData(gradio.props.value.composite)
|
| 82 |
+
: null
|
| 83 |
+
);
|
| 84 |
+
let normalised_layers = $derived(
|
| 85 |
+
gradio.props.value?.layers?.map((layer) => new FileData(layer)) || []
|
| 86 |
+
);
|
| 87 |
+
</script>
|
| 88 |
+
|
| 89 |
+
{#if !gradio.shared.interactive}
|
| 90 |
+
<Block
|
| 91 |
+
visible={gradio.shared.visible}
|
| 92 |
+
variant={"solid"}
|
| 93 |
+
border_mode={is_dragging ? "focus" : "base"}
|
| 94 |
+
padding={false}
|
| 95 |
+
elem_id={gradio.shared.elem_id}
|
| 96 |
+
elem_classes={gradio.shared.elem_classes}
|
| 97 |
+
height={gradio.props.height}
|
| 98 |
+
width={gradio.props.width}
|
| 99 |
+
allow_overflow={true}
|
| 100 |
+
overflow_behavior="visible"
|
| 101 |
+
container={gradio.shared.container}
|
| 102 |
+
scale={gradio.shared.scale}
|
| 103 |
+
min_width={gradio.shared.min_width}
|
| 104 |
+
>
|
| 105 |
+
<StatusTracker
|
| 106 |
+
autoscroll={gradio.shared.autoscroll}
|
| 107 |
+
i18n={gradio.i18n}
|
| 108 |
+
{...gradio.shared.loading_status}
|
| 109 |
+
on:clear_status={() =>
|
| 110 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 111 |
+
/>
|
| 112 |
+
<StaticImage
|
| 113 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
| 114 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 115 |
+
on:error={({ detail }) => gradio.dispatch("error", detail)}
|
| 116 |
+
value={gradio.props.value?.composite || null}
|
| 117 |
+
label={gradio.shared.label}
|
| 118 |
+
show_label={gradio.shared.show_label}
|
| 119 |
+
buttons={gradio.props.buttons}
|
| 120 |
+
selectable={gradio.props._selectable}
|
| 121 |
+
i18n={gradio.i18n}
|
| 122 |
+
/>
|
| 123 |
+
</Block>
|
| 124 |
+
{:else}
|
| 125 |
+
<Block
|
| 126 |
+
visible={gradio.shared.visible}
|
| 127 |
+
variant={has_value ? "solid" : "dashed"}
|
| 128 |
+
border_mode={is_dragging ? "focus" : "base"}
|
| 129 |
+
padding={false}
|
| 130 |
+
elem_id={gradio.shared.elem_id}
|
| 131 |
+
elem_classes={gradio.shared.elem_classes}
|
| 132 |
+
height={gradio.props.height}
|
| 133 |
+
width={gradio.props.width}
|
| 134 |
+
allow_overflow={true}
|
| 135 |
+
overflow_behavior="visible"
|
| 136 |
+
container={gradio.shared.container}
|
| 137 |
+
scale={gradio.shared.scale}
|
| 138 |
+
min_width={gradio.shared.min_width}
|
| 139 |
+
>
|
| 140 |
+
<StatusTracker
|
| 141 |
+
autoscroll={gradio.shared.autoscroll}
|
| 142 |
+
i18n={gradio.i18n}
|
| 143 |
+
{...gradio.shared.loading_status}
|
| 144 |
+
on:clear_status={() =>
|
| 145 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 146 |
+
/>
|
| 147 |
+
|
| 148 |
+
<InteractiveImageEditor
|
| 149 |
+
border_region={gradio.props.border_region}
|
| 150 |
+
bind:is_dragging
|
| 151 |
+
canvas_size={gradio.props.canvas_size}
|
| 152 |
+
bind:image_id
|
| 153 |
+
layers={normalised_layers}
|
| 154 |
+
composite={normalised_composite}
|
| 155 |
+
background={normalised_background}
|
| 156 |
+
bind:this={editor_instance}
|
| 157 |
+
root={gradio.shared.root}
|
| 158 |
+
sources={gradio.props.sources}
|
| 159 |
+
label={gradio.shared.label}
|
| 160 |
+
show_label={gradio.shared.show_label}
|
| 161 |
+
fixed_canvas={gradio.props.fixed_canvas}
|
| 162 |
+
on:input={() => {
|
| 163 |
+
if (!has_run_input) {
|
| 164 |
+
has_run_input = true;
|
| 165 |
+
} else {
|
| 166 |
+
gradio.dispatch("input");
|
| 167 |
+
}
|
| 168 |
+
}}
|
| 169 |
+
on:save={(e) => handle_save()}
|
| 170 |
+
on:edit={() => gradio.dispatch("edit")}
|
| 171 |
+
on:clear={() => gradio.dispatch("clear")}
|
| 172 |
+
on:drag={({ detail }) => (is_dragging = detail)}
|
| 173 |
+
on:upload={() => gradio.dispatch("upload")}
|
| 174 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 175 |
+
on:error={({ detail }) => {
|
| 176 |
+
gradio.shared.loading_status = gradio.shared.loading_status || {};
|
| 177 |
+
gradio.shared.loading_status.status = "error";
|
| 178 |
+
gradio.dispatch("error", detail);
|
| 179 |
+
}}
|
| 180 |
+
on:receive_null={() =>
|
| 181 |
+
(gradio.props.value = {
|
| 182 |
+
background: null,
|
| 183 |
+
layers: [],
|
| 184 |
+
composite: null
|
| 185 |
+
})}
|
| 186 |
+
on:change={() => {
|
| 187 |
+
if (!has_run_change) {
|
| 188 |
+
has_run_change = true;
|
| 189 |
+
} else {
|
| 190 |
+
gradio.dispatch("change");
|
| 191 |
+
}
|
| 192 |
+
}}
|
| 193 |
+
on:error
|
| 194 |
+
brush={gradio.props.brush}
|
| 195 |
+
eraser={gradio.props.eraser}
|
| 196 |
+
changeable={gradio.shared.attached_events.includes("apply")}
|
| 197 |
+
realtime={gradio.shared.attached_events.includes("change") ||
|
| 198 |
+
gradio.shared.attached_events.includes("input")}
|
| 199 |
+
i18n={gradio.i18n}
|
| 200 |
+
transforms={gradio.props.transforms}
|
| 201 |
+
accept_blobs={gradio.shared.server.accept_blobs}
|
| 202 |
+
layer_options={gradio.props.layers}
|
| 203 |
+
upload={(...args) => gradio.shared.client.upload(...args)}
|
| 204 |
+
placeholder={gradio.props.placeholder}
|
| 205 |
+
webcam_options={gradio.props.webcam_options}
|
| 206 |
+
show_download_button={gradio.props.buttons === null
|
| 207 |
+
? true
|
| 208 |
+
: gradio.props.buttons.includes("download")}
|
| 209 |
+
theme_mode={gradio.shared.theme_mode}
|
| 210 |
+
on:download_error={(e) => gradio.dispatch("error", e.detail)}
|
| 211 |
+
></InteractiveImageEditor>
|
| 212 |
+
</Block>
|
| 213 |
+
{/if}
|
6.0.0-dev.5/imageeditor/InteractiveImageEditor.svelte
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ts" context="module">
|
| 2 |
+
export interface EditorData {
|
| 3 |
+
background: FileData | null;
|
| 4 |
+
layers: FileData[] | null;
|
| 5 |
+
composite: FileData | null;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
export interface ImageBlobs {
|
| 9 |
+
background: FileData | null;
|
| 10 |
+
layers: FileData[];
|
| 11 |
+
composite: FileData | null;
|
| 12 |
+
}
|
| 13 |
+
</script>
|
| 14 |
+
|
| 15 |
+
<script lang="ts">
|
| 16 |
+
import { createEventDispatcher } from "svelte";
|
| 17 |
+
import { type I18nFormatter } from "@gradio/utils";
|
| 18 |
+
import { prepare_files, type FileData, type Client } from "@gradio/client";
|
| 19 |
+
import { type CommandNode } from "./shared/core/commands";
|
| 20 |
+
import ImageEditor from "./shared/ImageEditor.svelte";
|
| 21 |
+
import { type Brush as IBrush, type Eraser } from "./shared/brush/types";
|
| 22 |
+
import { type Tool as ToolbarTool } from "./shared/Toolbar.svelte";
|
| 23 |
+
|
| 24 |
+
import { BlockLabel } from "@gradio/atoms";
|
| 25 |
+
import { Image as ImageIcon } from "@gradio/icons";
|
| 26 |
+
import { inject } from "./shared/utils/parse_placeholder";
|
| 27 |
+
import {
|
| 28 |
+
type LayerOptions,
|
| 29 |
+
type Transform,
|
| 30 |
+
type Source,
|
| 31 |
+
type WebcamOptions
|
| 32 |
+
} from "./shared/types";
|
| 33 |
+
|
| 34 |
+
export let brush: IBrush;
|
| 35 |
+
export let eraser: Eraser;
|
| 36 |
+
export let sources: Source[];
|
| 37 |
+
export let i18n: I18nFormatter;
|
| 38 |
+
export let root: string;
|
| 39 |
+
export let label: string | undefined = undefined;
|
| 40 |
+
export let show_label: boolean;
|
| 41 |
+
export let changeable = false;
|
| 42 |
+
export let theme_mode: "dark" | "light";
|
| 43 |
+
|
| 44 |
+
export let layers: FileData[];
|
| 45 |
+
export let composite: FileData | null;
|
| 46 |
+
export let background: FileData | null;
|
| 47 |
+
|
| 48 |
+
export let layer_options: LayerOptions;
|
| 49 |
+
export let transforms: Transform[];
|
| 50 |
+
export let accept_blobs: (a: any) => void;
|
| 51 |
+
|
| 52 |
+
export let canvas_size: [number, number];
|
| 53 |
+
export let fixed_canvas = false;
|
| 54 |
+
export let realtime: boolean;
|
| 55 |
+
export let upload: Client["upload"];
|
| 56 |
+
export let is_dragging: boolean;
|
| 57 |
+
export let placeholder: string | undefined = undefined;
|
| 58 |
+
export let border_region: number;
|
| 59 |
+
export let full_history: CommandNode | null = null;
|
| 60 |
+
export let webcam_options: WebcamOptions;
|
| 61 |
+
export let show_download_button = false;
|
| 62 |
+
|
| 63 |
+
const dispatch = createEventDispatcher<{
|
| 64 |
+
clear?: never;
|
| 65 |
+
upload?: never;
|
| 66 |
+
change?: never;
|
| 67 |
+
receive_null?: never;
|
| 68 |
+
}>();
|
| 69 |
+
|
| 70 |
+
let editor: ImageEditor;
|
| 71 |
+
let has_drawn = false;
|
| 72 |
+
|
| 73 |
+
function is_not_null(o: Blob | null): o is Blob {
|
| 74 |
+
return !!o;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
function is_file_data(o: null | FileData): o is FileData {
|
| 78 |
+
return !!o;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
$: if (background_image) dispatch("upload");
|
| 82 |
+
|
| 83 |
+
export async function get_data(): Promise<ImageBlobs> {
|
| 84 |
+
let blobs;
|
| 85 |
+
try {
|
| 86 |
+
blobs = await editor.get_blobs();
|
| 87 |
+
} catch (e) {
|
| 88 |
+
return { background: null, layers: [], composite: null };
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
const bg = blobs.background
|
| 92 |
+
? upload(
|
| 93 |
+
await prepare_files([new File([blobs.background], "background.png")]),
|
| 94 |
+
root
|
| 95 |
+
)
|
| 96 |
+
: Promise.resolve(null);
|
| 97 |
+
|
| 98 |
+
const layers = blobs.layers
|
| 99 |
+
.filter(is_not_null)
|
| 100 |
+
.map(async (blob, i) =>
|
| 101 |
+
upload(await prepare_files([new File([blob], `layer_${i}.png`)]), root)
|
| 102 |
+
);
|
| 103 |
+
|
| 104 |
+
const composite = blobs.composite
|
| 105 |
+
? upload(
|
| 106 |
+
await prepare_files([new File([blobs.composite], "composite.png")]),
|
| 107 |
+
root
|
| 108 |
+
)
|
| 109 |
+
: Promise.resolve(null);
|
| 110 |
+
|
| 111 |
+
const [background, composite_, ...layers_] = await Promise.all([
|
| 112 |
+
bg,
|
| 113 |
+
composite,
|
| 114 |
+
...layers
|
| 115 |
+
]);
|
| 116 |
+
|
| 117 |
+
return {
|
| 118 |
+
background: Array.isArray(background) ? background[0] : background,
|
| 119 |
+
layers: layers_
|
| 120 |
+
.flatMap((layer) => (Array.isArray(layer) ? layer : [layer]))
|
| 121 |
+
.filter(is_file_data),
|
| 122 |
+
composite: Array.isArray(composite_) ? composite_[0] : composite_
|
| 123 |
+
};
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
function handle_value(value: EditorData | null): void {
|
| 127 |
+
if (!editor) return;
|
| 128 |
+
if (value == null) {
|
| 129 |
+
editor.handle_remove();
|
| 130 |
+
dispatch("receive_null");
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
$: handle_value({ layers, composite, background });
|
| 135 |
+
|
| 136 |
+
let background_image = false;
|
| 137 |
+
let can_undo: boolean;
|
| 138 |
+
|
| 139 |
+
export let image_id: null | string = null;
|
| 140 |
+
|
| 141 |
+
type BinaryImages = [string, string, File, number | null][];
|
| 142 |
+
|
| 143 |
+
function nextframe(): Promise<void> {
|
| 144 |
+
return new Promise((resolve) => setTimeout(() => resolve(), 30));
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
let uploading = false;
|
| 148 |
+
let pending = false;
|
| 149 |
+
async function handle_change(e: CustomEvent<Blob | any>): Promise<void> {
|
| 150 |
+
if (!realtime) return;
|
| 151 |
+
if (uploading) {
|
| 152 |
+
pending = true;
|
| 153 |
+
return;
|
| 154 |
+
}
|
| 155 |
+
uploading = true;
|
| 156 |
+
await nextframe();
|
| 157 |
+
const blobs = await editor.get_blobs();
|
| 158 |
+
const images: BinaryImages = [];
|
| 159 |
+
let id = Math.random().toString(36).substring(2);
|
| 160 |
+
if (blobs.background)
|
| 161 |
+
images.push([
|
| 162 |
+
id,
|
| 163 |
+
"background",
|
| 164 |
+
new File([blobs.background], "background.png"),
|
| 165 |
+
null
|
| 166 |
+
]);
|
| 167 |
+
if (blobs.composite)
|
| 168 |
+
images.push([
|
| 169 |
+
id,
|
| 170 |
+
"composite",
|
| 171 |
+
new File([blobs.composite], "composite.png"),
|
| 172 |
+
null
|
| 173 |
+
]);
|
| 174 |
+
blobs.layers.forEach((layer, i) => {
|
| 175 |
+
if (layer)
|
| 176 |
+
images.push([
|
| 177 |
+
id as string,
|
| 178 |
+
`layer`,
|
| 179 |
+
new File([layer], `layer_${i}.png`),
|
| 180 |
+
i
|
| 181 |
+
]);
|
| 182 |
+
});
|
| 183 |
+
await Promise.all(
|
| 184 |
+
images.map(async ([image_id, type, data, index]) => {
|
| 185 |
+
return accept_blobs({
|
| 186 |
+
binary: true,
|
| 187 |
+
data: { file: data, id: image_id, type, index }
|
| 188 |
+
});
|
| 189 |
+
})
|
| 190 |
+
);
|
| 191 |
+
image_id = id;
|
| 192 |
+
dispatch("change");
|
| 193 |
+
await nextframe();
|
| 194 |
+
uploading = false;
|
| 195 |
+
if (pending) {
|
| 196 |
+
pending = false;
|
| 197 |
+
uploading = false;
|
| 198 |
+
handle_change(e);
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
$: [heading, paragraph] = placeholder ? inject(placeholder) : [false, false];
|
| 203 |
+
|
| 204 |
+
let current_tool: ToolbarTool;
|
| 205 |
+
</script>
|
| 206 |
+
|
| 207 |
+
<BlockLabel
|
| 208 |
+
{show_label}
|
| 209 |
+
Icon={ImageIcon}
|
| 210 |
+
label={label || i18n("image.image")}
|
| 211 |
+
/>
|
| 212 |
+
<ImageEditor
|
| 213 |
+
{transforms}
|
| 214 |
+
{composite}
|
| 215 |
+
{layers}
|
| 216 |
+
{background}
|
| 217 |
+
on:history
|
| 218 |
+
{canvas_size}
|
| 219 |
+
bind:this={editor}
|
| 220 |
+
{changeable}
|
| 221 |
+
on:save
|
| 222 |
+
on:input
|
| 223 |
+
on:change={handle_change}
|
| 224 |
+
on:clear={() => dispatch("clear")}
|
| 225 |
+
on:download_error
|
| 226 |
+
{sources}
|
| 227 |
+
bind:background_image
|
| 228 |
+
bind:current_tool
|
| 229 |
+
brush_options={brush}
|
| 230 |
+
eraser_options={eraser}
|
| 231 |
+
{fixed_canvas}
|
| 232 |
+
{border_region}
|
| 233 |
+
{layer_options}
|
| 234 |
+
{i18n}
|
| 235 |
+
{root}
|
| 236 |
+
{upload}
|
| 237 |
+
bind:is_dragging
|
| 238 |
+
bind:has_drawn
|
| 239 |
+
{webcam_options}
|
| 240 |
+
{show_download_button}
|
| 241 |
+
{theme_mode}
|
| 242 |
+
bind:can_undo
|
| 243 |
+
bind:full_history
|
| 244 |
+
>
|
| 245 |
+
{#if current_tool === "image" && !can_undo}
|
| 246 |
+
<div class="empty wrap">
|
| 247 |
+
{#if sources && sources.length}
|
| 248 |
+
{#if heading || paragraph}
|
| 249 |
+
{#if heading}
|
| 250 |
+
<h2>{heading}</h2>
|
| 251 |
+
{/if}
|
| 252 |
+
{#if paragraph}
|
| 253 |
+
<p>{paragraph}</p>
|
| 254 |
+
{/if}
|
| 255 |
+
{:else}
|
| 256 |
+
<div>Upload an image</div>
|
| 257 |
+
{/if}
|
| 258 |
+
{/if}
|
| 259 |
+
|
| 260 |
+
{#if sources && sources.length && brush && !placeholder}
|
| 261 |
+
<div class="or">or</div>
|
| 262 |
+
{/if}
|
| 263 |
+
{#if brush && !placeholder}
|
| 264 |
+
<div>select the draw tool to start</div>
|
| 265 |
+
{/if}
|
| 266 |
+
</div>
|
| 267 |
+
{/if}
|
| 268 |
+
</ImageEditor>
|
| 269 |
+
|
| 270 |
+
<style>
|
| 271 |
+
h2 {
|
| 272 |
+
font-size: var(--text-xl);
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
p,
|
| 276 |
+
h2 {
|
| 277 |
+
white-space: pre-line;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
.empty {
|
| 281 |
+
display: flex;
|
| 282 |
+
flex-direction: column;
|
| 283 |
+
justify-content: center;
|
| 284 |
+
align-items: center;
|
| 285 |
+
position: absolute;
|
| 286 |
+
height: 100%;
|
| 287 |
+
width: 100%;
|
| 288 |
+
left: 0;
|
| 289 |
+
right: 0;
|
| 290 |
+
margin: auto;
|
| 291 |
+
z-index: var(--layer-1);
|
| 292 |
+
text-align: center;
|
| 293 |
+
color: var(--color-grey-500) !important;
|
| 294 |
+
cursor: pointer;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
.wrap {
|
| 298 |
+
display: flex;
|
| 299 |
+
flex-direction: column;
|
| 300 |
+
justify-content: center;
|
| 301 |
+
align-items: center;
|
| 302 |
+
line-height: var(--line-md);
|
| 303 |
+
font-size: var(--text-md);
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
.or {
|
| 307 |
+
color: var(--body-text-color-subdued);
|
| 308 |
+
}
|
| 309 |
+
</style>
|
6.0.0-dev.5/imageeditor/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/imageeditor",
|
| 3 |
+
"version": "0.18.2-dev.1",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"@gradio/atoms": "workspace:^",
|
| 11 |
+
"@gradio/client": "workspace:^",
|
| 12 |
+
"@gradio/icons": "workspace:^",
|
| 13 |
+
"@gradio/image": "workspace:^",
|
| 14 |
+
"@gradio/statustracker": "workspace:^",
|
| 15 |
+
"@gradio/upload": "workspace:^",
|
| 16 |
+
"@gradio/utils": "workspace:^",
|
| 17 |
+
"@types/tinycolor2": "^1.4.6",
|
| 18 |
+
"pixi-filters": "^6.1.4",
|
| 19 |
+
"pixi.js": "^8.14.0",
|
| 20 |
+
"tinycolor2": "^1.6.0"
|
| 21 |
+
},
|
| 22 |
+
"devDependencies": {
|
| 23 |
+
"@gradio/preview": "workspace:^"
|
| 24 |
+
},
|
| 25 |
+
"main_changeset": true,
|
| 26 |
+
"main": "./Index.svelte",
|
| 27 |
+
"exports": {
|
| 28 |
+
".": {
|
| 29 |
+
"gradio": "./Index.svelte",
|
| 30 |
+
"svelte": "./dist/Index.svelte",
|
| 31 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 32 |
+
},
|
| 33 |
+
"./example": {
|
| 34 |
+
"gradio": "./Example.svelte",
|
| 35 |
+
"svelte": "./dist/Example.svelte",
|
| 36 |
+
"types": "./dist/Example.svelte.d.ts"
|
| 37 |
+
},
|
| 38 |
+
"./package.json": "./package.json"
|
| 39 |
+
},
|
| 40 |
+
"peerDependencies": {
|
| 41 |
+
"svelte": "^5.43.4"
|
| 42 |
+
},
|
| 43 |
+
"repository": {
|
| 44 |
+
"type": "git",
|
| 45 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 46 |
+
"directory": "js/imageeditor"
|
| 47 |
+
}
|
| 48 |
+
}
|
6.0.0-dev.5/imageeditor/shared/Anchor.svelte
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { spring } from "svelte/motion";
|
| 3 |
+
import { Image } from "@gradio/icons";
|
| 4 |
+
import { onMount, createEventDispatcher } from "svelte";
|
| 5 |
+
|
| 6 |
+
const positions = [
|
| 7 |
+
"top-left",
|
| 8 |
+
"top",
|
| 9 |
+
"top-right",
|
| 10 |
+
"left",
|
| 11 |
+
"center",
|
| 12 |
+
"right",
|
| 13 |
+
"bottom-left",
|
| 14 |
+
"bottom",
|
| 15 |
+
"bottom-right"
|
| 16 |
+
] as const;
|
| 17 |
+
const dispatch = createEventDispatcher<{
|
| 18 |
+
position: (typeof positions)[number];
|
| 19 |
+
}>();
|
| 20 |
+
|
| 21 |
+
const spring_opt = {
|
| 22 |
+
stiffness: 0.1,
|
| 23 |
+
precision: 0.5
|
| 24 |
+
};
|
| 25 |
+
const pos = spring([5, 5], spring_opt);
|
| 26 |
+
const init = [(120 - 10) / 3, (120 - 10) / 3];
|
| 27 |
+
const dimensions = spring(init, spring_opt);
|
| 28 |
+
type Arrow = {
|
| 29 |
+
x: number;
|
| 30 |
+
y: number;
|
| 31 |
+
x_dir: number;
|
| 32 |
+
y_dir: number;
|
| 33 |
+
rotation: number;
|
| 34 |
+
type: number;
|
| 35 |
+
};
|
| 36 |
+
const arrow_spring = spring<Arrow[]>([], spring_opt);
|
| 37 |
+
|
| 38 |
+
let last_i = 0;
|
| 39 |
+
let expanded = true;
|
| 40 |
+
const box_size = (120 - 5 * 2) / 3;
|
| 41 |
+
|
| 42 |
+
async function handle_box_hover(i: number): Promise<void> {
|
| 43 |
+
expanded = false;
|
| 44 |
+
last_i = i;
|
| 45 |
+
const y = Math.floor(i / 3);
|
| 46 |
+
const x = i % 3;
|
| 47 |
+
pos.set([x * box_size + 5 * x + 5, y * box_size + 5 * y + 5]);
|
| 48 |
+
|
| 49 |
+
dimensions.set(init);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
let last_pos = 0;
|
| 53 |
+
async function handle_box_click(i: number, stagger = false): Promise<void> {
|
| 54 |
+
if (expanded && stagger) return;
|
| 55 |
+
|
| 56 |
+
const y = Math.floor(i / 3);
|
| 57 |
+
const x = i % 3;
|
| 58 |
+
|
| 59 |
+
const [initial_arrows, eventual_arrows] = get_valid_offsets({
|
| 60 |
+
x: x,
|
| 61 |
+
y: y
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
if (stagger && last_i !== i) {
|
| 65 |
+
await Promise.all([
|
| 66 |
+
pos.set([x * box_size + 5 * x + 5, y * box_size + 5 * y + 5]),
|
| 67 |
+
arrow_spring.set(initial_arrows, { hard: true }),
|
| 68 |
+
dimensions.set(init)
|
| 69 |
+
]);
|
| 70 |
+
} else {
|
| 71 |
+
await arrow_spring.set(initial_arrows, { hard: true });
|
| 72 |
+
}
|
| 73 |
+
last_pos = i;
|
| 74 |
+
expanded = true;
|
| 75 |
+
|
| 76 |
+
pos.set([0, 0]);
|
| 77 |
+
dimensions.set([120 + 10, 120 + 10]);
|
| 78 |
+
arrow_spring.set(eventual_arrows);
|
| 79 |
+
|
| 80 |
+
dispatch("position", positions[i]);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
function get_valid_offsets(anchorPoint: {
|
| 84 |
+
x: number;
|
| 85 |
+
y: number;
|
| 86 |
+
}): [Arrow[], Arrow[]] {
|
| 87 |
+
const destination_points: { x: number; y: number }[] = [
|
| 88 |
+
{ x: 0, y: 0 },
|
| 89 |
+
{ x: 1, y: 0 },
|
| 90 |
+
{ x: 2, y: 0 },
|
| 91 |
+
{ x: 0, y: 1 },
|
| 92 |
+
{ x: 1, y: 1 },
|
| 93 |
+
{ x: 2, y: 1 },
|
| 94 |
+
{ x: 0, y: 2 },
|
| 95 |
+
{ x: 1, y: 2 },
|
| 96 |
+
{ x: 2, y: 2 }
|
| 97 |
+
];
|
| 98 |
+
|
| 99 |
+
const offsets = destination_points
|
| 100 |
+
.map((dest) => ({
|
| 101 |
+
x: dest.x - anchorPoint.x,
|
| 102 |
+
y: dest.y - anchorPoint.y,
|
| 103 |
+
dest
|
| 104 |
+
}))
|
| 105 |
+
.filter((offset) => !(offset.x === 0 && offset.y === 0));
|
| 106 |
+
|
| 107 |
+
const normalized_offsets = offsets
|
| 108 |
+
.map((offset) => {
|
| 109 |
+
// cardinal
|
| 110 |
+
if (offset.x === 0 || offset.y === 0) {
|
| 111 |
+
const magnitude = Math.max(Math.abs(offset.x), Math.abs(offset.y));
|
| 112 |
+
return {
|
| 113 |
+
magnitude,
|
| 114 |
+
x: offset.x / magnitude,
|
| 115 |
+
y: offset.y / magnitude,
|
| 116 |
+
dest: offset.dest,
|
| 117 |
+
original: offset,
|
| 118 |
+
type: "cardinal" as const
|
| 119 |
+
};
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// ordinal
|
| 123 |
+
if (Math.abs(offset.x) === Math.abs(offset.y)) {
|
| 124 |
+
const magnitude = Math.abs(offset.x);
|
| 125 |
+
return {
|
| 126 |
+
magnitude,
|
| 127 |
+
x: offset.x / magnitude,
|
| 128 |
+
y: offset.y / magnitude,
|
| 129 |
+
dest: offset.dest,
|
| 130 |
+
original: offset,
|
| 131 |
+
type: "ordinal" as const
|
| 132 |
+
};
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// skip the rest
|
| 136 |
+
return null;
|
| 137 |
+
})
|
| 138 |
+
.filter((offset) => offset !== null);
|
| 139 |
+
|
| 140 |
+
const directions = new Map<
|
| 141 |
+
string,
|
| 142 |
+
{
|
| 143 |
+
x: number;
|
| 144 |
+
y: number;
|
| 145 |
+
dest: { x: number; y: number };
|
| 146 |
+
original: { x: number; y: number };
|
| 147 |
+
magnitude: number;
|
| 148 |
+
type: "cardinal" | "ordinal";
|
| 149 |
+
}[]
|
| 150 |
+
>();
|
| 151 |
+
|
| 152 |
+
normalized_offsets.forEach((offset) => {
|
| 153 |
+
if (directions.has(`${offset.x}-${offset.y}`)) {
|
| 154 |
+
directions.get(`${offset.x}-${offset.y}`)?.push(offset);
|
| 155 |
+
} else {
|
| 156 |
+
directions.set(`${offset.x}-${offset.y}`, [offset]);
|
| 157 |
+
}
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
const unique_directions = Array.from(directions.values()).map(
|
| 161 |
+
(direction) => {
|
| 162 |
+
return direction.sort((a, b) => b.magnitude - a.magnitude)[0];
|
| 163 |
+
}
|
| 164 |
+
);
|
| 165 |
+
|
| 166 |
+
const eventual_arrows = unique_directions.map((arrow) => ({
|
| 167 |
+
x:
|
| 168 |
+
arrow.dest.x * box_size +
|
| 169 |
+
14 * arrow.dest.x +
|
| 170 |
+
14 +
|
| 171 |
+
10 +
|
| 172 |
+
(arrow.type === "ordinal" ? 0 : 12 * arrow.x),
|
| 173 |
+
y:
|
| 174 |
+
arrow.dest.y * box_size +
|
| 175 |
+
14 * arrow.dest.y +
|
| 176 |
+
14 +
|
| 177 |
+
10 +
|
| 178 |
+
(arrow.type === "ordinal" ? 0 : 12 * arrow.y),
|
| 179 |
+
x_dir: arrow.x,
|
| 180 |
+
y_dir: arrow.y,
|
| 181 |
+
rotation:
|
| 182 |
+
Math.atan2(arrow.original.y, arrow.original.x) * (180 / Math.PI) + 180,
|
| 183 |
+
type: arrow.type === "ordinal" ? 1 : 2
|
| 184 |
+
}));
|
| 185 |
+
|
| 186 |
+
const initial_arrows = eventual_arrows.map((arrow) => ({
|
| 187 |
+
...arrow,
|
| 188 |
+
x:
|
| 189 |
+
anchorPoint.x * box_size +
|
| 190 |
+
14 * anchorPoint.x + // gaps
|
| 191 |
+
14 + // padding
|
| 192 |
+
10 + // arrow head
|
| 193 |
+
(arrow.x_dir * box_size) / 2 +
|
| 194 |
+
arrow.x_dir * (arrow.type === 1 ? 0 : 10), // offset based on direction
|
| 195 |
+
|
| 196 |
+
y:
|
| 197 |
+
anchorPoint.y * box_size +
|
| 198 |
+
14 * anchorPoint.y +
|
| 199 |
+
14 +
|
| 200 |
+
10 +
|
| 201 |
+
(arrow.y_dir * box_size) / 2 +
|
| 202 |
+
arrow.y_dir * (arrow.type === 1 ? 0 : 10)
|
| 203 |
+
}));
|
| 204 |
+
|
| 205 |
+
return [initial_arrows, eventual_arrows];
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
onMount(() => {
|
| 209 |
+
handle_box_click(0);
|
| 210 |
+
});
|
| 211 |
+
</script>
|
| 212 |
+
|
| 213 |
+
<div class="wrap" class:expanded>
|
| 214 |
+
<div
|
| 215 |
+
class="box-wrap"
|
| 216 |
+
role="grid"
|
| 217 |
+
tabindex="0"
|
| 218 |
+
on:mouseleave={() => handle_box_click(last_pos, true)}
|
| 219 |
+
>
|
| 220 |
+
{#each { length: 9 } as _, i}
|
| 221 |
+
<button
|
| 222 |
+
class="box"
|
| 223 |
+
class:active-box={last_pos === i}
|
| 224 |
+
on:mouseenter={() => handle_box_hover(i)}
|
| 225 |
+
on:click={() => handle_box_click(i)}
|
| 226 |
+
>
|
| 227 |
+
<span class:active={last_pos === i} class="icon-wrap">
|
| 228 |
+
<Image />
|
| 229 |
+
</span>
|
| 230 |
+
</button>
|
| 231 |
+
{/each}
|
| 232 |
+
</div>
|
| 233 |
+
<div
|
| 234 |
+
class="expanding-box"
|
| 235 |
+
style:width="{$dimensions[0]}px"
|
| 236 |
+
style:height="{$dimensions[1]}px"
|
| 237 |
+
style:transform="translate({$pos[0]}px, {$pos[1]}px)"
|
| 238 |
+
></div>
|
| 239 |
+
<svg viewBox="0 0 150 150">
|
| 240 |
+
{#each $arrow_spring as arrow}
|
| 241 |
+
<line
|
| 242 |
+
x1={(last_pos % 3) * box_size +
|
| 243 |
+
14 * (last_pos % 3) +
|
| 244 |
+
14 +
|
| 245 |
+
10 +
|
| 246 |
+
(arrow.x_dir * box_size) / 2 +
|
| 247 |
+
arrow.x_dir * (arrow.type === 1 ? 0 : 10)}
|
| 248 |
+
x2={arrow.x}
|
| 249 |
+
y1={Math.floor(last_pos / 3) * box_size +
|
| 250 |
+
14 * Math.floor(last_pos / 3) +
|
| 251 |
+
14 +
|
| 252 |
+
10 +
|
| 253 |
+
(arrow.y_dir * box_size) / 2 +
|
| 254 |
+
arrow.y_dir * (arrow.type === 1 ? 0 : 10)}
|
| 255 |
+
y2={arrow.y}
|
| 256 |
+
/>
|
| 257 |
+
{/each}
|
| 258 |
+
{#each $arrow_spring as arrow}
|
| 259 |
+
<g style:transform="translate(-75px, -75px)">
|
| 260 |
+
<g style:transform="translate({arrow.x}px, {arrow.y}px)">
|
| 261 |
+
<path
|
| 262 |
+
d="M 70,75 80,69 80,81 Z"
|
| 263 |
+
style:transform="rotate({arrow.rotation}deg)"
|
| 264 |
+
style:transform-origin="75 75"
|
| 265 |
+
/>
|
| 266 |
+
</g>
|
| 267 |
+
</g>
|
| 268 |
+
{/each}
|
| 269 |
+
</svg>
|
| 270 |
+
</div>
|
| 271 |
+
|
| 272 |
+
<style>
|
| 273 |
+
.wrap {
|
| 274 |
+
position: relative;
|
| 275 |
+
margin: 10px 0;
|
| 276 |
+
height: 130px;
|
| 277 |
+
width: 130px;
|
| 278 |
+
padding: 5px;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
.box-wrap {
|
| 282 |
+
width: 100%;
|
| 283 |
+
height: 100%;
|
| 284 |
+
display: grid;
|
| 285 |
+
grid-template-columns: repeat(3, 1fr);
|
| 286 |
+
grid-template-rows: repeat(3, 1fr);
|
| 287 |
+
gap: 5px;
|
| 288 |
+
|
| 289 |
+
background-color: var(--block-background-fill);
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
.box {
|
| 293 |
+
cursor: pointer;
|
| 294 |
+
border: none;
|
| 295 |
+
border: #ccc dashed 1px;
|
| 296 |
+
border-radius: 4px;
|
| 297 |
+
transition: 0.1s;
|
| 298 |
+
display: flex;
|
| 299 |
+
justify-content: center;
|
| 300 |
+
align-items: center;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
.box::after {
|
| 304 |
+
background: none !important;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
.expanding-box {
|
| 308 |
+
--mod: 3;
|
| 309 |
+
--border: 2;
|
| 310 |
+
border: orange solid 2px;
|
| 311 |
+
border-radius: 4px;
|
| 312 |
+
position: absolute;
|
| 313 |
+
top: 0px;
|
| 314 |
+
left: 0px;
|
| 315 |
+
width: 100%;
|
| 316 |
+
height: 100%;
|
| 317 |
+
pointer-events: none;
|
| 318 |
+
|
| 319 |
+
width: calc(100% / var(--mod));
|
| 320 |
+
height: calc(100% / var(--mod));
|
| 321 |
+
transform-origin: 50% 0;
|
| 322 |
+
box-sizing: border-box;
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
svg {
|
| 326 |
+
position: absolute;
|
| 327 |
+
top: 0px;
|
| 328 |
+
left: 0px;
|
| 329 |
+
width: 100%;
|
| 330 |
+
height: 100%;
|
| 331 |
+
|
| 332 |
+
pointer-events: none;
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
.icon-wrap {
|
| 336 |
+
opacity: 0.2;
|
| 337 |
+
height: 50%;
|
| 338 |
+
width: 50%;
|
| 339 |
+
|
| 340 |
+
display: flex;
|
| 341 |
+
justify-content: center;
|
| 342 |
+
align-items: center;
|
| 343 |
+
border-color: #ccc;
|
| 344 |
+
transition: 0.1s;
|
| 345 |
+
padding-top: 1px;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
.wrap.expanded .icon-wrap {
|
| 349 |
+
opacity: 0;
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
.wrap.expanded .box {
|
| 353 |
+
border-color: #fff;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
.icon-wrap.active {
|
| 357 |
+
opacity: 1 !important;
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
svg {
|
| 361 |
+
opacity: 0;
|
| 362 |
+
padding: 5px;
|
| 363 |
+
transition: 0.1s;
|
| 364 |
+
}
|
| 365 |
+
.expanded svg {
|
| 366 |
+
opacity: 1;
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
svg line {
|
| 370 |
+
stroke: #999;
|
| 371 |
+
stroke-width: 3px;
|
| 372 |
+
stroke-linecap: round;
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
svg path {
|
| 376 |
+
stroke-width: 3;
|
| 377 |
+
stroke: #999;
|
| 378 |
+
stroke-linejoin: round;
|
| 379 |
+
stroke-linecap: round;
|
| 380 |
+
fill: #999;
|
| 381 |
+
/* transition: 0.5s; */
|
| 382 |
+
transform-origin: 75px 75px;
|
| 383 |
+
}
|
| 384 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/Controls.svelte
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher } from "svelte";
|
| 3 |
+
import { IconButton, IconButtonWrapper } from "@gradio/atoms";
|
| 4 |
+
import {
|
| 5 |
+
Check,
|
| 6 |
+
Trash,
|
| 7 |
+
ZoomIn,
|
| 8 |
+
ZoomOut,
|
| 9 |
+
Pan,
|
| 10 |
+
Download,
|
| 11 |
+
Undo,
|
| 12 |
+
Redo
|
| 13 |
+
} from "@gradio/icons";
|
| 14 |
+
import type { Writable } from "svelte/store";
|
| 15 |
+
|
| 16 |
+
export let can_save = false;
|
| 17 |
+
export let changeable = false;
|
| 18 |
+
export let current_zoom = 1;
|
| 19 |
+
export let tool: string;
|
| 20 |
+
export let min_zoom = true;
|
| 21 |
+
export let enable_download = false;
|
| 22 |
+
export let can_undo: boolean;
|
| 23 |
+
export let can_redo: boolean;
|
| 24 |
+
|
| 25 |
+
const dispatch = createEventDispatcher<{
|
| 26 |
+
remove_image: void;
|
| 27 |
+
undo: void;
|
| 28 |
+
redo: void;
|
| 29 |
+
save: void;
|
| 30 |
+
zoom_in: void;
|
| 31 |
+
zoom_out: void;
|
| 32 |
+
set_zoom: number | "fit";
|
| 33 |
+
pan: void;
|
| 34 |
+
download: void;
|
| 35 |
+
}>();
|
| 36 |
+
|
| 37 |
+
let show_zoom_popup = false;
|
| 38 |
+
|
| 39 |
+
function handle_zoom_click(e: MouseEvent): void {
|
| 40 |
+
e.stopPropagation();
|
| 41 |
+
show_zoom_popup = !show_zoom_popup;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
function handle_zoom_change(zoom: number | "fit"): void {
|
| 45 |
+
dispatch("set_zoom", zoom);
|
| 46 |
+
show_zoom_popup = false;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function handle_zoom_keydown(e: KeyboardEvent): void {
|
| 50 |
+
if (e.key === "Enter") {
|
| 51 |
+
handle_zoom_change(current_zoom);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
$: formatted_zoom = Math.round(current_zoom * 100);
|
| 56 |
+
</script>
|
| 57 |
+
|
| 58 |
+
<IconButtonWrapper>
|
| 59 |
+
{#if enable_download}
|
| 60 |
+
<IconButton
|
| 61 |
+
Icon={Download}
|
| 62 |
+
label="Download"
|
| 63 |
+
on:click={(event) => {
|
| 64 |
+
dispatch("download");
|
| 65 |
+
event.stopPropagation();
|
| 66 |
+
}}
|
| 67 |
+
/>
|
| 68 |
+
{/if}
|
| 69 |
+
|
| 70 |
+
<IconButton
|
| 71 |
+
Icon={Pan}
|
| 72 |
+
label="Pan"
|
| 73 |
+
on:click={(e) => {
|
| 74 |
+
e.stopPropagation();
|
| 75 |
+
dispatch("pan");
|
| 76 |
+
}}
|
| 77 |
+
highlight={tool === "pan"}
|
| 78 |
+
size="small"
|
| 79 |
+
padded={false}
|
| 80 |
+
transparent={true}
|
| 81 |
+
disabled={min_zoom}
|
| 82 |
+
/>
|
| 83 |
+
|
| 84 |
+
<IconButton
|
| 85 |
+
Icon={ZoomOut}
|
| 86 |
+
label="Zoom out"
|
| 87 |
+
on:click={(event) => {
|
| 88 |
+
dispatch("zoom_out");
|
| 89 |
+
event.stopPropagation();
|
| 90 |
+
}}
|
| 91 |
+
/>
|
| 92 |
+
<IconButton
|
| 93 |
+
Icon={ZoomIn}
|
| 94 |
+
label="Zoom in"
|
| 95 |
+
on:click={(event) => {
|
| 96 |
+
dispatch("zoom_in");
|
| 97 |
+
event.stopPropagation();
|
| 98 |
+
}}
|
| 99 |
+
/>
|
| 100 |
+
|
| 101 |
+
<div class="zoom-number">
|
| 102 |
+
<span
|
| 103 |
+
role="button"
|
| 104 |
+
tabindex="0"
|
| 105 |
+
on:click={handle_zoom_click}
|
| 106 |
+
on:keydown={handle_zoom_keydown}>{formatted_zoom}%</span
|
| 107 |
+
>
|
| 108 |
+
{#if show_zoom_popup}
|
| 109 |
+
<div class="zoom-controls">
|
| 110 |
+
<ul>
|
| 111 |
+
<li>
|
| 112 |
+
<button on:click|stopPropagation={() => handle_zoom_change("fit")}>
|
| 113 |
+
Fit to screen
|
| 114 |
+
</button>
|
| 115 |
+
</li>
|
| 116 |
+
{#each [0.25, 0.5, 1, 2, 4] as zoom}
|
| 117 |
+
<li>
|
| 118 |
+
<button on:click|stopPropagation={() => handle_zoom_change(zoom)}>
|
| 119 |
+
{zoom * 100}%
|
| 120 |
+
</button>
|
| 121 |
+
</li>
|
| 122 |
+
{/each}
|
| 123 |
+
</ul>
|
| 124 |
+
</div>
|
| 125 |
+
{/if}
|
| 126 |
+
</div>
|
| 127 |
+
<div class="separator"></div>
|
| 128 |
+
|
| 129 |
+
<IconButton
|
| 130 |
+
Icon={Undo}
|
| 131 |
+
label="Undo"
|
| 132 |
+
on:click={(event) => {
|
| 133 |
+
dispatch("undo");
|
| 134 |
+
event.stopPropagation();
|
| 135 |
+
}}
|
| 136 |
+
disabled={!can_undo}
|
| 137 |
+
/>
|
| 138 |
+
|
| 139 |
+
<IconButton
|
| 140 |
+
Icon={Redo}
|
| 141 |
+
label="Redo"
|
| 142 |
+
on:click={(event) => {
|
| 143 |
+
dispatch("redo");
|
| 144 |
+
event.stopPropagation();
|
| 145 |
+
}}
|
| 146 |
+
disabled={!can_redo}
|
| 147 |
+
/>
|
| 148 |
+
|
| 149 |
+
{#if changeable}
|
| 150 |
+
<IconButton
|
| 151 |
+
disabled={!can_save}
|
| 152 |
+
Icon={Check}
|
| 153 |
+
label="Save changes"
|
| 154 |
+
on:click={(event) => {
|
| 155 |
+
dispatch("save");
|
| 156 |
+
event.stopPropagation();
|
| 157 |
+
}}
|
| 158 |
+
color="var(--color-accent)"
|
| 159 |
+
/>
|
| 160 |
+
{/if}
|
| 161 |
+
|
| 162 |
+
<IconButton
|
| 163 |
+
Icon={Trash}
|
| 164 |
+
label="Clear canvas"
|
| 165 |
+
on:click={(event) => {
|
| 166 |
+
dispatch("remove_image");
|
| 167 |
+
event.stopPropagation();
|
| 168 |
+
}}
|
| 169 |
+
/>
|
| 170 |
+
</IconButtonWrapper>
|
| 171 |
+
|
| 172 |
+
<style>
|
| 173 |
+
.separator {
|
| 174 |
+
width: 1px;
|
| 175 |
+
height: 10px;
|
| 176 |
+
background-color: var(--border-color-primary);
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.zoom-number {
|
| 180 |
+
position: relative;
|
| 181 |
+
width: 30px;
|
| 182 |
+
padding-left: 4px;
|
| 183 |
+
display: flex;
|
| 184 |
+
align-items: center;
|
| 185 |
+
justify-content: center;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.zoom-number span {
|
| 189 |
+
cursor: pointer;
|
| 190 |
+
font-size: var(--text-sm);
|
| 191 |
+
transition: color 0.15s ease;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.zoom-number span:hover {
|
| 195 |
+
color: var(--color-accent);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.zoom-controls {
|
| 199 |
+
position: absolute;
|
| 200 |
+
top: calc(100% + var(--spacing-xxs) + 3px);
|
| 201 |
+
left: -2px;
|
| 202 |
+
background: var(--block-background-fill);
|
| 203 |
+
border: 1px solid var(--color-gray-200);
|
| 204 |
+
border-radius: var(--radius-sm);
|
| 205 |
+
padding: var(--spacing-xxs);
|
| 206 |
+
box-shadow: var(--shadow-drop);
|
| 207 |
+
font-size: 12px;
|
| 208 |
+
z-index: var(--layer-2);
|
| 209 |
+
width: max-content;
|
| 210 |
+
color: var(--block-label-text-color);
|
| 211 |
+
border: 1px solid var(--block-border-color);
|
| 212 |
+
border-top-left-radius: 0;
|
| 213 |
+
border-top-right-radius: 0;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.zoom-controls ul {
|
| 217 |
+
list-style: none;
|
| 218 |
+
margin: 0;
|
| 219 |
+
padding: 0;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
.zoom-controls li {
|
| 223 |
+
display: flex;
|
| 224 |
+
justify-content: space-between;
|
| 225 |
+
align-items: center;
|
| 226 |
+
transition: background-color 0.15s ease;
|
| 227 |
+
border-bottom: 1px solid var(--border-color-primary);
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.zoom-controls li:last-child {
|
| 231 |
+
border-bottom: none;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
.zoom-controls li:hover {
|
| 235 |
+
background-color: var(--background-fill-secondary);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
.zoom-controls button {
|
| 239 |
+
width: 100%;
|
| 240 |
+
text-align: left;
|
| 241 |
+
padding: var(--spacing-sm) var(--spacing-md);
|
| 242 |
+
font-size: var(--text-sm);
|
| 243 |
+
line-height: var(--line-sm);
|
| 244 |
+
transition: all 0.15s ease;
|
| 245 |
+
border-radius: var(--radius-sm);
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
.zoom-controls button:hover {
|
| 249 |
+
color: var(--color-accent);
|
| 250 |
+
}
|
| 251 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/IconButton.svelte
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { type ComponentType } from "svelte";
|
| 3 |
+
import type { ColorInput } from "tinycolor2";
|
| 4 |
+
export let Icon: ComponentType;
|
| 5 |
+
export let label = "";
|
| 6 |
+
export let show_label = false;
|
| 7 |
+
export let pending = false;
|
| 8 |
+
export let size: "small" | "large" | "medium" = "small";
|
| 9 |
+
export let padded = true;
|
| 10 |
+
export let highlight = false;
|
| 11 |
+
export let disabled = false;
|
| 12 |
+
export let hasPopup = false;
|
| 13 |
+
export let color: string | ColorInput = "var(--block-label-text-color)";
|
| 14 |
+
export let transparent = false;
|
| 15 |
+
export let background = "var(--background-fill-primary)";
|
| 16 |
+
export let offset = 0;
|
| 17 |
+
export let label_position: "left" | "right" = "left";
|
| 18 |
+
export let roundedness: "quite" | "very" = "quite";
|
| 19 |
+
$: _color = highlight ? "var(--color-accent)" : color.toString();
|
| 20 |
+
</script>
|
| 21 |
+
|
| 22 |
+
<button
|
| 23 |
+
{disabled}
|
| 24 |
+
on:click
|
| 25 |
+
aria-label={label}
|
| 26 |
+
aria-haspopup={hasPopup}
|
| 27 |
+
title={label}
|
| 28 |
+
class:pending
|
| 29 |
+
class:padded
|
| 30 |
+
class:highlight
|
| 31 |
+
class:transparent
|
| 32 |
+
style:color={!disabled && _color ? _color : "var(--block-label-text-color)"}
|
| 33 |
+
style:--bg-color={!disabled ? background : "auto"}
|
| 34 |
+
style:margin-left={offset + "px"}
|
| 35 |
+
class="{roundedness}-round"
|
| 36 |
+
>
|
| 37 |
+
{#if show_label && label_position === "left"}
|
| 38 |
+
<span style="margin-left: 4px;">{label}</span>
|
| 39 |
+
{/if}
|
| 40 |
+
<div
|
| 41 |
+
class:small={size === "small"}
|
| 42 |
+
class:large={size === "large"}
|
| 43 |
+
class:medium={size === "medium"}
|
| 44 |
+
>
|
| 45 |
+
<Icon />
|
| 46 |
+
</div>
|
| 47 |
+
{#if show_label && label_position === "right"}
|
| 48 |
+
<span style="margin-right: 4px;">{label}</span>
|
| 49 |
+
{/if}
|
| 50 |
+
</button>
|
| 51 |
+
|
| 52 |
+
<style>
|
| 53 |
+
button {
|
| 54 |
+
display: flex;
|
| 55 |
+
justify-content: center;
|
| 56 |
+
align-items: center;
|
| 57 |
+
gap: 1px;
|
| 58 |
+
z-index: var(--layer-2);
|
| 59 |
+
/* background: var(--background-fill-primary); */
|
| 60 |
+
/* border-radius: var(--radius-sm); */
|
| 61 |
+
color: var(--block-label-text-color);
|
| 62 |
+
border: 1px solid transparent;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.quite-round {
|
| 66 |
+
border-radius: var(--radius-sm);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.very-round {
|
| 70 |
+
border-radius: var(--radius-md);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
button[disabled] {
|
| 74 |
+
opacity: 0.5;
|
| 75 |
+
box-shadow: none;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
button[disabled]:hover {
|
| 79 |
+
cursor: not-allowed;
|
| 80 |
+
/* border: 1px solid var(--button-secondary-border-color); */
|
| 81 |
+
/* padding: 2px; */
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.padded {
|
| 85 |
+
padding: 2px;
|
| 86 |
+
background: var(--bg-color);
|
| 87 |
+
box-shadow: var(--shadow-drop);
|
| 88 |
+
border: 1px solid var(--button-secondary-border-color);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
button:hover,
|
| 92 |
+
button.highlight {
|
| 93 |
+
cursor: pointer;
|
| 94 |
+
color: var(--color-accent);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.padded:hover {
|
| 98 |
+
border: 2px solid var(--button-secondary-border-color-hover);
|
| 99 |
+
padding: 1px;
|
| 100 |
+
color: var(--block-label-text-color);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
span {
|
| 104 |
+
padding: 0px 1px;
|
| 105 |
+
font-size: 10px;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
div {
|
| 109 |
+
padding: 2px;
|
| 110 |
+
display: flex;
|
| 111 |
+
align-items: flex-end;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.small {
|
| 115 |
+
width: 14px;
|
| 116 |
+
height: 14px;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.medium {
|
| 120 |
+
width: 20px;
|
| 121 |
+
height: 20px;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.large {
|
| 125 |
+
width: 22px;
|
| 126 |
+
height: 22px;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.pending {
|
| 130 |
+
animation: flash 0.5s infinite;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
@keyframes flash {
|
| 134 |
+
0% {
|
| 135 |
+
opacity: 0.5;
|
| 136 |
+
}
|
| 137 |
+
50% {
|
| 138 |
+
opacity: 1;
|
| 139 |
+
}
|
| 140 |
+
100% {
|
| 141 |
+
opacity: 0.5;
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.transparent {
|
| 146 |
+
background: transparent;
|
| 147 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts" context="module">
|
| 2 |
+
import type { Tool, Subtool } from "./Toolbar.svelte";
|
| 3 |
+
|
| 4 |
+
export const EDITOR_KEY = Symbol("editor");
|
| 5 |
+
export type context_type = "bg" | "layers" | "crop" | "draw" | "erase";
|
| 6 |
+
</script>
|
| 7 |
+
|
| 8 |
+
<script lang="ts">
|
| 9 |
+
import { onMount, createEventDispatcher, tick } from "svelte";
|
| 10 |
+
import { get } from "svelte/store";
|
| 11 |
+
import Toolbar, { type Tool as ToolbarTool } from "./Toolbar.svelte";
|
| 12 |
+
import { CropTool } from "./crop/crop";
|
| 13 |
+
import { ResizeTool } from "./resize/resize";
|
| 14 |
+
import { Webcam } from "@gradio/image";
|
| 15 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 16 |
+
import type { Client, FileData } from "@gradio/client";
|
| 17 |
+
import tinycolor, { type ColorInput } from "tinycolor2";
|
| 18 |
+
import { ZoomTool } from "./zoom/zoom";
|
| 19 |
+
import { type CommandManager, type CommandNode } from "./core/commands";
|
| 20 |
+
import { ImageEditor } from "./core/editor";
|
| 21 |
+
import { type Brush, type Eraser } from "./brush/types";
|
| 22 |
+
import { BrushTool } from "./brush/brush";
|
| 23 |
+
import { create_drag } from "@gradio/upload";
|
| 24 |
+
import SecondaryToolbar from "./SecondaryToolbar.svelte";
|
| 25 |
+
import { Check } from "@gradio/icons";
|
| 26 |
+
import type { LayerOptions, Source, Transform, WebcamOptions } from "./types";
|
| 27 |
+
|
| 28 |
+
import { type ImageBlobs } from "./types";
|
| 29 |
+
import Controls from "./Controls.svelte";
|
| 30 |
+
import IconButton from "./IconButton.svelte";
|
| 31 |
+
|
| 32 |
+
const { drag, open_file_upload } = create_drag();
|
| 33 |
+
const dispatch = createEventDispatcher<{
|
| 34 |
+
clear?: never;
|
| 35 |
+
save: void;
|
| 36 |
+
change: void;
|
| 37 |
+
upload: void;
|
| 38 |
+
input: void;
|
| 39 |
+
download_error: string;
|
| 40 |
+
}>();
|
| 41 |
+
|
| 42 |
+
export const antialias = true;
|
| 43 |
+
|
| 44 |
+
export let changeable = false;
|
| 45 |
+
export let sources: Source[] = ["upload", "webcam", "clipboard"];
|
| 46 |
+
export let transforms: Transform[] = ["crop", "resize"];
|
| 47 |
+
export let canvas_size: [number, number];
|
| 48 |
+
export let is_dragging = false;
|
| 49 |
+
export let background_image = false;
|
| 50 |
+
export let brush_options: Brush;
|
| 51 |
+
export let eraser_options: Eraser;
|
| 52 |
+
export let fixed_canvas = false;
|
| 53 |
+
export let root: string;
|
| 54 |
+
export let i18n: I18nFormatter;
|
| 55 |
+
export let upload: Client["upload"];
|
| 56 |
+
export let composite: FileData | null;
|
| 57 |
+
export let layers: FileData[];
|
| 58 |
+
export let background: FileData | null;
|
| 59 |
+
export let border_region = 0;
|
| 60 |
+
export let layer_options: LayerOptions;
|
| 61 |
+
export let current_tool: ToolbarTool;
|
| 62 |
+
export let webcam_options: WebcamOptions;
|
| 63 |
+
export let show_download_button = false;
|
| 64 |
+
export let theme_mode: "dark" | "light";
|
| 65 |
+
export let full_history: CommandNode | null = null;
|
| 66 |
+
|
| 67 |
+
let pixi_target: HTMLDivElement;
|
| 68 |
+
let pixi_target_crop: HTMLDivElement;
|
| 69 |
+
|
| 70 |
+
$: if (layer_options) {
|
| 71 |
+
if (check_if_should_init()) {
|
| 72 |
+
editor.set_layer_options(layer_options);
|
| 73 |
+
refresh_tools();
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
function refresh_tools(): void {
|
| 78 |
+
if (!editor || !ready) return;
|
| 79 |
+
editor.set_tool(current_tool);
|
| 80 |
+
editor.set_subtool(current_subtool);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
$: if (editor && ready && editor.layers) {
|
| 84 |
+
const current_layers = get(editor.layers);
|
| 85 |
+
|
| 86 |
+
if (current_layers.layers.length > 0 && !current_layers.active_layer) {
|
| 87 |
+
refresh_tools_for_layer_changes(current_layers);
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/**
|
| 92 |
+
* Refreshes tools when layer state changes
|
| 93 |
+
*/
|
| 94 |
+
function refresh_tools_for_layer_changes(current_layers: any): void {
|
| 95 |
+
if (!editor || !ready) return;
|
| 96 |
+
|
| 97 |
+
if (current_layers.layers.length > 0 && !current_layers.active_layer) {
|
| 98 |
+
editor.set_layer(current_layers.layers[0].id);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// reapply current tool to ensure it targets the correct layer
|
| 102 |
+
if (current_tool) {
|
| 103 |
+
editor.set_tool(current_tool);
|
| 104 |
+
if (current_subtool) {
|
| 105 |
+
editor.set_subtool(current_subtool);
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
if (brush && (current_tool === "draw" || current_tool === "erase")) {
|
| 110 |
+
brush.set_tool(current_tool, current_subtool);
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
function check_if_should_init(): boolean {
|
| 115 |
+
return layer_options && editor && ready;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
export let has_drawn = false;
|
| 119 |
+
|
| 120 |
+
/**
|
| 121 |
+
* Gets the image blobs from the editor
|
| 122 |
+
* @returns {Promise<ImageBlobs>} Object containing background, layers, and composite image blobs
|
| 123 |
+
*/
|
| 124 |
+
export async function get_blobs(): Promise<ImageBlobs> {
|
| 125 |
+
if (!editor) return { background: null, layers: [], composite: null };
|
| 126 |
+
if (!background_image && !has_drawn && !layers.length)
|
| 127 |
+
return { background: null, layers: [], composite: null };
|
| 128 |
+
const blobs = await editor.get_blobs();
|
| 129 |
+
return blobs;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
let editor: ImageEditor;
|
| 133 |
+
|
| 134 |
+
/**
|
| 135 |
+
* Adds an image to the editor
|
| 136 |
+
* @param {Blob | File} image - The image to add
|
| 137 |
+
*/
|
| 138 |
+
export function add_image(image: Blob | File): void {
|
| 139 |
+
editor.add_image({ image });
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
let pending_bg: Promise<void>;
|
| 143 |
+
/**
|
| 144 |
+
* Adds an image to the editor from a URL
|
| 145 |
+
* @param {string | FileData} source - The URL of the image or a FileData object
|
| 146 |
+
* @returns {Promise<void>}
|
| 147 |
+
*/
|
| 148 |
+
export async function add_image_from_url(
|
| 149 |
+
source:
|
| 150 |
+
| string
|
| 151 |
+
| {
|
| 152 |
+
url: string;
|
| 153 |
+
meta: {
|
| 154 |
+
_type: string;
|
| 155 |
+
};
|
| 156 |
+
}
|
| 157 |
+
| any
|
| 158 |
+
): Promise<void> {
|
| 159 |
+
if (!editor || !source || !check_if_should_init()) return;
|
| 160 |
+
let url: string;
|
| 161 |
+
|
| 162 |
+
if (typeof source === "string") {
|
| 163 |
+
url = source;
|
| 164 |
+
} else if (source?.meta?._type === "gradio.FileData" && source?.url) {
|
| 165 |
+
url = source.url;
|
| 166 |
+
} else {
|
| 167 |
+
console.warn("Invalid source provided to add_image_from_url:", source);
|
| 168 |
+
return;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
try {
|
| 172 |
+
pending_bg = editor.add_image_from_url(url);
|
| 173 |
+
let pending_crop = crop.add_image_from_url(url);
|
| 174 |
+
await Promise.all([pending_bg, pending_crop]);
|
| 175 |
+
crop.set_tool("image");
|
| 176 |
+
crop.set_subtool("crop");
|
| 177 |
+
background_image = true;
|
| 178 |
+
zoom.set_zoom("fit");
|
| 179 |
+
dispatch("upload");
|
| 180 |
+
dispatch("input");
|
| 181 |
+
} catch (error) {
|
| 182 |
+
console.error("Error adding image from URL:", error);
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
/**
|
| 187 |
+
* Adds a new layer with an image loaded from a URL
|
| 188 |
+
* @param {string | FileData} source - The URL of the image or a FileData object
|
| 189 |
+
* @returns {Promise<string | null>} - The ID of the created layer, or null if failed
|
| 190 |
+
*/
|
| 191 |
+
export async function add_layers_from_url(
|
| 192 |
+
source: FileData[] | any
|
| 193 |
+
): Promise<void> {
|
| 194 |
+
if (!editor || !source.length || !check_if_should_init()) return;
|
| 195 |
+
|
| 196 |
+
if (
|
| 197 |
+
Array.isArray(source) &&
|
| 198 |
+
source.every((item) => item?.meta?._type === "gradio.FileData")
|
| 199 |
+
) {
|
| 200 |
+
try {
|
| 201 |
+
await pending_bg;
|
| 202 |
+
await editor.add_layers_from_url(source.map((item) => item.url));
|
| 203 |
+
dispatch("change");
|
| 204 |
+
dispatch("input");
|
| 205 |
+
} catch (error) {
|
| 206 |
+
console.error("Error adding layer from URL:", error);
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
let brush: BrushTool;
|
| 212 |
+
let zoom: ZoomTool;
|
| 213 |
+
let zoom_level = 1;
|
| 214 |
+
let ready = false;
|
| 215 |
+
let mounted = false;
|
| 216 |
+
let min_zoom = true;
|
| 217 |
+
|
| 218 |
+
let last_dimensions = { width: 0, height: 0 };
|
| 219 |
+
|
| 220 |
+
/**
|
| 221 |
+
* Handles visibility changes and resets zoom if dimensions have changed
|
| 222 |
+
*/
|
| 223 |
+
async function handle_visibility_change(): Promise<void> {
|
| 224 |
+
if (!editor || !ready || !zoom) return;
|
| 225 |
+
await tick();
|
| 226 |
+
|
| 227 |
+
const is_visible = pixi_target.offsetParent !== null;
|
| 228 |
+
|
| 229 |
+
if (is_visible) {
|
| 230 |
+
const current_dimensions = pixi_target.getBoundingClientRect();
|
| 231 |
+
|
| 232 |
+
if (
|
| 233 |
+
current_dimensions.width !== last_dimensions.width ||
|
| 234 |
+
current_dimensions.height !== last_dimensions.height
|
| 235 |
+
) {
|
| 236 |
+
zoom.set_zoom("fit");
|
| 237 |
+
|
| 238 |
+
last_dimensions = {
|
| 239 |
+
width: current_dimensions.width,
|
| 240 |
+
height: current_dimensions.height
|
| 241 |
+
};
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
onMount(() => {
|
| 247 |
+
let intersection_observer: IntersectionObserver;
|
| 248 |
+
let resize_observer: ResizeObserver;
|
| 249 |
+
|
| 250 |
+
init_image_editor().then(() => {
|
| 251 |
+
mounted = true;
|
| 252 |
+
intersection_observer = new IntersectionObserver(() => {
|
| 253 |
+
handle_visibility_change();
|
| 254 |
+
});
|
| 255 |
+
|
| 256 |
+
resize_observer = new ResizeObserver(() => {
|
| 257 |
+
handle_visibility_change();
|
| 258 |
+
});
|
| 259 |
+
|
| 260 |
+
intersection_observer.observe(pixi_target);
|
| 261 |
+
resize_observer.observe(pixi_target);
|
| 262 |
+
|
| 263 |
+
setTimeout(() => {
|
| 264 |
+
if (full_history && editor) {
|
| 265 |
+
editor.command_manager
|
| 266 |
+
.replay(full_history, editor.context)
|
| 267 |
+
.then(() => {
|
| 268 |
+
refresh_tools_after_history();
|
| 269 |
+
});
|
| 270 |
+
}
|
| 271 |
+
}, 0);
|
| 272 |
+
});
|
| 273 |
+
|
| 274 |
+
if (typeof window !== "undefined") {
|
| 275 |
+
(window as any).editor = editor;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
return () => {
|
| 279 |
+
if (intersection_observer) {
|
| 280 |
+
intersection_observer.disconnect();
|
| 281 |
+
}
|
| 282 |
+
if (resize_observer) {
|
| 283 |
+
resize_observer.disconnect();
|
| 284 |
+
}
|
| 285 |
+
if (editor) {
|
| 286 |
+
editor.destroy();
|
| 287 |
+
}
|
| 288 |
+
};
|
| 289 |
+
});
|
| 290 |
+
|
| 291 |
+
/**
|
| 292 |
+
* Refreshes tool state after history replay to ensure proper layer targeting
|
| 293 |
+
*/
|
| 294 |
+
function refresh_tools_after_history(): void {
|
| 295 |
+
if (!editor || !ready) return;
|
| 296 |
+
|
| 297 |
+
const current_layers = get(editor.layers);
|
| 298 |
+
|
| 299 |
+
if (current_layers.layers.length > 0 && !current_layers.active_layer) {
|
| 300 |
+
editor.set_layer(current_layers.layers[0].id);
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
if (current_tool) {
|
| 304 |
+
editor.set_tool(current_tool);
|
| 305 |
+
if (current_subtool) {
|
| 306 |
+
editor.set_subtool(current_subtool);
|
| 307 |
+
}
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
if (brush && (current_tool === "draw" || current_tool === "erase")) {
|
| 311 |
+
brush.set_tool(current_tool, current_subtool);
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
full_history = editor.command_manager.history;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
let crop: ImageEditor;
|
| 318 |
+
let crop_zoom: ZoomTool;
|
| 319 |
+
export let can_undo = false;
|
| 320 |
+
let can_redo = false;
|
| 321 |
+
async function init_image_editor(): Promise<void> {
|
| 322 |
+
brush = new BrushTool();
|
| 323 |
+
zoom = new ZoomTool();
|
| 324 |
+
editor = new ImageEditor({
|
| 325 |
+
target_element: pixi_target,
|
| 326 |
+
width: canvas_size[0],
|
| 327 |
+
height: canvas_size[1],
|
| 328 |
+
tools: ["image", zoom, new ResizeTool(), brush],
|
| 329 |
+
fixed_canvas,
|
| 330 |
+
border_region,
|
| 331 |
+
layer_options,
|
| 332 |
+
theme_mode
|
| 333 |
+
});
|
| 334 |
+
|
| 335 |
+
brush.on("change", () => {
|
| 336 |
+
has_drawn = true;
|
| 337 |
+
});
|
| 338 |
+
|
| 339 |
+
crop_zoom = new ZoomTool();
|
| 340 |
+
|
| 341 |
+
crop = new ImageEditor({
|
| 342 |
+
target_element: pixi_target_crop,
|
| 343 |
+
width: canvas_size[0],
|
| 344 |
+
height: canvas_size[1],
|
| 345 |
+
tools: ["image", crop_zoom, new CropTool()],
|
| 346 |
+
dark: true,
|
| 347 |
+
fixed_canvas: false,
|
| 348 |
+
border_region: 0,
|
| 349 |
+
pad_bottom: 40
|
| 350 |
+
});
|
| 351 |
+
|
| 352 |
+
editor.scale.subscribe((_scale) => {
|
| 353 |
+
zoom_level = _scale;
|
| 354 |
+
});
|
| 355 |
+
|
| 356 |
+
editor.min_zoom.subscribe((is_min_zoom) => {
|
| 357 |
+
min_zoom = is_min_zoom;
|
| 358 |
+
});
|
| 359 |
+
|
| 360 |
+
editor.dimensions.subscribe((dimensions) => {
|
| 361 |
+
last_dimensions = { ...dimensions };
|
| 362 |
+
});
|
| 363 |
+
|
| 364 |
+
editor.command_manager.current_history.subscribe((history) => {
|
| 365 |
+
can_undo = history.previous !== null;
|
| 366 |
+
can_redo = history.next !== null;
|
| 367 |
+
});
|
| 368 |
+
|
| 369 |
+
await Promise.all([editor.ready, crop.ready]).then(() => {
|
| 370 |
+
handle_tool_change({ tool: "image" });
|
| 371 |
+
ready = true;
|
| 372 |
+
if (sources.length > 0) {
|
| 373 |
+
handle_tool_change({ tool: "image" });
|
| 374 |
+
} else {
|
| 375 |
+
handle_tool_change({ tool: "draw" });
|
| 376 |
+
}
|
| 377 |
+
crop.set_subtool("crop");
|
| 378 |
+
});
|
| 379 |
+
|
| 380 |
+
editor.on("change", () => {
|
| 381 |
+
dispatch("change");
|
| 382 |
+
dispatch("input");
|
| 383 |
+
full_history = editor.command_manager.history;
|
| 384 |
+
});
|
| 385 |
+
|
| 386 |
+
if (background || layers.length > 0) {
|
| 387 |
+
if (background) {
|
| 388 |
+
await add_image_from_url(background);
|
| 389 |
+
}
|
| 390 |
+
if (layers.length > 0) {
|
| 391 |
+
await add_layers_from_url(layers);
|
| 392 |
+
}
|
| 393 |
+
handle_tool_change({ tool: "draw" });
|
| 394 |
+
} else if (composite) {
|
| 395 |
+
await add_image_from_url(composite);
|
| 396 |
+
handle_tool_change({ tool: "draw" });
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
refresh_tools();
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
$: if (
|
| 403 |
+
background == null &&
|
| 404 |
+
layers.length == 0 &&
|
| 405 |
+
composite == null &&
|
| 406 |
+
editor &&
|
| 407 |
+
ready
|
| 408 |
+
) {
|
| 409 |
+
editor.reset_canvas();
|
| 410 |
+
zoom.set_zoom("fit");
|
| 411 |
+
handle_tool_change({ tool: "image" });
|
| 412 |
+
background_image = false;
|
| 413 |
+
has_drawn = false;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
$: current_tool === "image" &&
|
| 417 |
+
current_subtool === "crop" &&
|
| 418 |
+
crop_zoom.set_zoom("fit");
|
| 419 |
+
|
| 420 |
+
/**
|
| 421 |
+
* Handles file uploads
|
| 422 |
+
* @param {File[]} files - The uploaded files
|
| 423 |
+
*/
|
| 424 |
+
async function handle_files(
|
| 425 |
+
files: File[] | Blob[] | File | Blob | null
|
| 426 |
+
): Promise<void> {
|
| 427 |
+
if (files == null) return;
|
| 428 |
+
if (!sources.includes("upload")) return;
|
| 429 |
+
editor.reset_canvas();
|
| 430 |
+
const _file = Array.isArray(files) ? files[0] : files;
|
| 431 |
+
await editor.add_image({ image: _file });
|
| 432 |
+
await crop.add_image({ image: _file });
|
| 433 |
+
crop.reset();
|
| 434 |
+
|
| 435 |
+
background_image = true;
|
| 436 |
+
zoom.set_zoom("fit");
|
| 437 |
+
handle_tool_change({ tool: "draw" });
|
| 438 |
+
dispatch("upload");
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
$: background_image = can_undo && editor.command_manager.contains("AddImage");
|
| 442 |
+
|
| 443 |
+
/**
|
| 444 |
+
* Handles tool change events
|
| 445 |
+
* @param {{ tool: ToolbarTool }} param0 - Object containing the selected tool
|
| 446 |
+
*/
|
| 447 |
+
function handle_tool_change({ tool }: { tool: ToolbarTool }): void {
|
| 448 |
+
editor.set_tool(tool);
|
| 449 |
+
current_tool = tool;
|
| 450 |
+
|
| 451 |
+
if (tool === "image") {
|
| 452 |
+
crop.set_tool("image");
|
| 453 |
+
crop.set_subtool("crop");
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
/**
|
| 458 |
+
* Handles subtool change events
|
| 459 |
+
* @param {{ tool: ToolbarTool, subtool: Subtool }} param0 - Object containing the selected tool and subtool
|
| 460 |
+
*/
|
| 461 |
+
function handle_subtool_change({
|
| 462 |
+
tool,
|
| 463 |
+
subtool
|
| 464 |
+
}: {
|
| 465 |
+
tool: ToolbarTool;
|
| 466 |
+
subtool: Subtool | null;
|
| 467 |
+
}): void {
|
| 468 |
+
editor.set_subtool(subtool);
|
| 469 |
+
current_subtool = subtool;
|
| 470 |
+
|
| 471 |
+
if (subtool === null) {
|
| 472 |
+
return;
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
if (tool === "draw") {
|
| 476 |
+
if (subtool === "size") {
|
| 477 |
+
brush_size_visible = true;
|
| 478 |
+
} else if (subtool === "color") {
|
| 479 |
+
brush_color_visible = true;
|
| 480 |
+
}
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
if (tool === "erase" && subtool === "size") {
|
| 484 |
+
eraser_size_visible = true;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
if (tool === "image" && subtool === "paste") {
|
| 488 |
+
process_clipboard();
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
if (tool === "image" && subtool === "upload") {
|
| 492 |
+
tick().then(() => {
|
| 493 |
+
disable_click = false;
|
| 494 |
+
open_file_upload();
|
| 495 |
+
});
|
| 496 |
+
}
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
let eraser_size_visible = false;
|
| 500 |
+
let selected_color: ColorInput | string;
|
| 501 |
+
let selected_size: number;
|
| 502 |
+
let selected_opacity = 1;
|
| 503 |
+
let selected_eraser_size: number;
|
| 504 |
+
|
| 505 |
+
$: {
|
| 506 |
+
if (brush_options) {
|
| 507 |
+
update_brush_options();
|
| 508 |
+
}
|
| 509 |
+
|
| 510 |
+
if (eraser_options) {
|
| 511 |
+
update_eraser_options();
|
| 512 |
+
}
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
function update_brush_options(): void {
|
| 516 |
+
const default_color =
|
| 517 |
+
brush_options.default_color === "auto"
|
| 518 |
+
? brush_options.colors[0]
|
| 519 |
+
: brush_options.default_color;
|
| 520 |
+
|
| 521 |
+
// color is already a tuple [color, opacity]
|
| 522 |
+
if (Array.isArray(default_color)) {
|
| 523 |
+
selected_color = default_color[0];
|
| 524 |
+
selected_opacity = default_color[1];
|
| 525 |
+
} else {
|
| 526 |
+
selected_color = default_color;
|
| 527 |
+
|
| 528 |
+
// color is a string, check if it has opacity info
|
| 529 |
+
const color = tinycolor(default_color);
|
| 530 |
+
if (color.getAlpha() < 1) {
|
| 531 |
+
selected_opacity = color.getAlpha();
|
| 532 |
+
} else {
|
| 533 |
+
selected_opacity = 1;
|
| 534 |
+
}
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
selected_size =
|
| 538 |
+
typeof brush_options.default_size === "number"
|
| 539 |
+
? brush_options.default_size
|
| 540 |
+
: 25;
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
function update_eraser_options(): void {
|
| 544 |
+
selected_eraser_size =
|
| 545 |
+
eraser_options.default_size === "auto" ? 25 : eraser_options.default_size;
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
let brush_size_visible = false;
|
| 549 |
+
|
| 550 |
+
let brush_color_visible = false;
|
| 551 |
+
|
| 552 |
+
$: brush?.set_brush_color(
|
| 553 |
+
(() => {
|
| 554 |
+
let color_value;
|
| 555 |
+
if (selected_color === "auto") {
|
| 556 |
+
const default_color =
|
| 557 |
+
brush_options.colors.find((color) =>
|
| 558 |
+
Array.isArray(color)
|
| 559 |
+
? color[0] === brush_options.default_color
|
| 560 |
+
: color === brush_options.default_color
|
| 561 |
+
) || brush_options.colors[0];
|
| 562 |
+
|
| 563 |
+
color_value = Array.isArray(default_color)
|
| 564 |
+
? default_color[0]
|
| 565 |
+
: default_color;
|
| 566 |
+
} else {
|
| 567 |
+
color_value = selected_color;
|
| 568 |
+
}
|
| 569 |
+
return color_value;
|
| 570 |
+
})()
|
| 571 |
+
);
|
| 572 |
+
|
| 573 |
+
$: brush?.set_brush_size(
|
| 574 |
+
typeof selected_size === "number" ? selected_size : 25
|
| 575 |
+
);
|
| 576 |
+
|
| 577 |
+
$: brush?.set_eraser_size(
|
| 578 |
+
typeof selected_eraser_size === "number" ? selected_eraser_size : 25
|
| 579 |
+
);
|
| 580 |
+
|
| 581 |
+
$: disable_click =
|
| 582 |
+
current_tool !== "image" ||
|
| 583 |
+
(current_tool === "image" && background_image) ||
|
| 584 |
+
(current_tool === "image" && current_subtool === "webcam") ||
|
| 585 |
+
!sources.includes("upload");
|
| 586 |
+
|
| 587 |
+
let current_subtool: Subtool | null = null;
|
| 588 |
+
let preview = false;
|
| 589 |
+
$: brush?.preview_brush(preview);
|
| 590 |
+
$: brush?.set_brush_opacity(selected_opacity);
|
| 591 |
+
|
| 592 |
+
function handle_zoom_change(zoom_level: number | "fit"): void {
|
| 593 |
+
zoom.set_zoom(zoom_level);
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
function zoom_in_out(direction: "in" | "out"): void {
|
| 597 |
+
zoom.set_zoom(
|
| 598 |
+
direction === "in"
|
| 599 |
+
? zoom_level + (zoom_level < 1 ? 0.1 : zoom_level * 0.1)
|
| 600 |
+
: zoom_level - (zoom_level < 1 ? 0.1 : zoom_level * 0.1)
|
| 601 |
+
);
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
async function process_clipboard(): Promise<void> {
|
| 605 |
+
const items = await navigator.clipboard.read();
|
| 606 |
+
|
| 607 |
+
for (let i = 0; i < items.length; i++) {
|
| 608 |
+
const type = items[i].types.find((t) => t.startsWith("image/"));
|
| 609 |
+
if (type) {
|
| 610 |
+
const blob = await items[i].getType(type);
|
| 611 |
+
|
| 612 |
+
handle_files(blob);
|
| 613 |
+
}
|
| 614 |
+
}
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
function handle_capture(e: CustomEvent): void {
|
| 618 |
+
if (e.detail !== null) {
|
| 619 |
+
handle_files(e.detail as Blob);
|
| 620 |
+
}
|
| 621 |
+
handle_subtool_change({ tool: current_tool, subtool: null });
|
| 622 |
+
}
|
| 623 |
+
|
| 624 |
+
function handle_save(): void {
|
| 625 |
+
dispatch("save");
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
$: add_image_from_url(composite || background);
|
| 629 |
+
$: add_layers_from_url(layers);
|
| 630 |
+
|
| 631 |
+
async function handle_crop_confirm(): Promise<void> {
|
| 632 |
+
const { image } = await crop.get_crop_bounds();
|
| 633 |
+
if (!image) return;
|
| 634 |
+
|
| 635 |
+
await editor.add_image({
|
| 636 |
+
image,
|
| 637 |
+
resize: false
|
| 638 |
+
});
|
| 639 |
+
handle_subtool_change({ tool: "image", subtool: null });
|
| 640 |
+
dispatch("change");
|
| 641 |
+
dispatch("input");
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
async function handle_download(): Promise<void> {
|
| 645 |
+
const blobs = await editor.get_blobs();
|
| 646 |
+
|
| 647 |
+
const blob = blobs.composite;
|
| 648 |
+
if (!blob) {
|
| 649 |
+
dispatch("download_error", "Unable to generate image to download.");
|
| 650 |
+
return;
|
| 651 |
+
}
|
| 652 |
+
const url = URL.createObjectURL(blob);
|
| 653 |
+
const link = document.createElement("a");
|
| 654 |
+
link.href = url;
|
| 655 |
+
link.download = "image.png";
|
| 656 |
+
link.click();
|
| 657 |
+
URL.revokeObjectURL(url);
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
function handle_undo(): void {
|
| 661 |
+
editor.undo();
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
function handle_redo(): void {
|
| 665 |
+
editor.redo();
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
let old_background = background;
|
| 669 |
+
|
| 670 |
+
$: if (old_background !== background) {
|
| 671 |
+
handle_tool_change({ tool: "draw" });
|
| 672 |
+
handle_subtool_change({ tool: "draw", subtool: null });
|
| 673 |
+
current_tool = "draw";
|
| 674 |
+
old_background = background;
|
| 675 |
+
}
|
| 676 |
+
</script>
|
| 677 |
+
|
| 678 |
+
<div
|
| 679 |
+
data-testid="image"
|
| 680 |
+
class="image-container"
|
| 681 |
+
class:dark-bg={current_subtool === "crop"}
|
| 682 |
+
use:drag={{
|
| 683 |
+
on_drag_change: (dragging) => (is_dragging = dragging),
|
| 684 |
+
on_files: handle_files,
|
| 685 |
+
accepted_types: "image/*",
|
| 686 |
+
disable_click: disable_click
|
| 687 |
+
}}
|
| 688 |
+
aria-label={"Click to upload or drop files"}
|
| 689 |
+
aria-dropeffect="copy"
|
| 690 |
+
>
|
| 691 |
+
{#if ready}
|
| 692 |
+
{#if current_subtool !== "crop"}
|
| 693 |
+
<Controls
|
| 694 |
+
{changeable}
|
| 695 |
+
on:set_zoom={(e) => handle_zoom_change(e.detail)}
|
| 696 |
+
on:zoom_in={() => zoom_in_out("in")}
|
| 697 |
+
on:zoom_out={() => zoom_in_out("out")}
|
| 698 |
+
{min_zoom}
|
| 699 |
+
current_zoom={zoom_level}
|
| 700 |
+
on:remove_image={() => {
|
| 701 |
+
dispatch("clear");
|
| 702 |
+
editor.reset_canvas();
|
| 703 |
+
handle_tool_change({ tool: "image" });
|
| 704 |
+
background_image = false;
|
| 705 |
+
has_drawn = false;
|
| 706 |
+
}}
|
| 707 |
+
tool={current_tool}
|
| 708 |
+
can_save={true}
|
| 709 |
+
on:save={handle_save}
|
| 710 |
+
on:pan={(e) => {
|
| 711 |
+
handle_tool_change({ tool: "pan" });
|
| 712 |
+
}}
|
| 713 |
+
enable_download={show_download_button}
|
| 714 |
+
on:download={() => handle_download()}
|
| 715 |
+
{can_undo}
|
| 716 |
+
{can_redo}
|
| 717 |
+
on:undo={handle_undo}
|
| 718 |
+
on:redo={handle_redo}
|
| 719 |
+
/>
|
| 720 |
+
{/if}
|
| 721 |
+
|
| 722 |
+
{#if current_subtool !== "crop"}
|
| 723 |
+
<Toolbar
|
| 724 |
+
{sources}
|
| 725 |
+
{transforms}
|
| 726 |
+
background={background_image}
|
| 727 |
+
on:tool_change={(e) => handle_tool_change(e.detail)}
|
| 728 |
+
on:subtool_change={(e) => handle_subtool_change(e.detail)}
|
| 729 |
+
show_brush_size={brush_size_visible}
|
| 730 |
+
show_brush_color={brush_color_visible}
|
| 731 |
+
show_eraser_size={eraser_size_visible}
|
| 732 |
+
{brush_options}
|
| 733 |
+
{eraser_options}
|
| 734 |
+
bind:selected_color
|
| 735 |
+
bind:selected_size
|
| 736 |
+
bind:selected_eraser_size
|
| 737 |
+
bind:selected_opacity
|
| 738 |
+
bind:preview
|
| 739 |
+
tool={current_tool}
|
| 740 |
+
subtool={current_subtool}
|
| 741 |
+
/>
|
| 742 |
+
{/if}
|
| 743 |
+
|
| 744 |
+
{#if current_tool === "image" && current_subtool === "webcam"}
|
| 745 |
+
<div class="modal">
|
| 746 |
+
<div class="modal-inner">
|
| 747 |
+
<Webcam
|
| 748 |
+
{upload}
|
| 749 |
+
{root}
|
| 750 |
+
on:capture={handle_capture}
|
| 751 |
+
on:error
|
| 752 |
+
on:drag
|
| 753 |
+
streaming={false}
|
| 754 |
+
mode="image"
|
| 755 |
+
include_audio={false}
|
| 756 |
+
{i18n}
|
| 757 |
+
mirror_webcam={webcam_options.mirror}
|
| 758 |
+
webcam_constraints={webcam_options.constraints}
|
| 759 |
+
/>
|
| 760 |
+
</div>
|
| 761 |
+
</div>
|
| 762 |
+
{/if}
|
| 763 |
+
|
| 764 |
+
{#if current_subtool !== "crop" && !layer_options.disabled}
|
| 765 |
+
<SecondaryToolbar
|
| 766 |
+
enable_additional_layers={layer_options.allow_additional_layers}
|
| 767 |
+
layers={editor.layers}
|
| 768 |
+
on:new_layer={() => {
|
| 769 |
+
editor.add_layer();
|
| 770 |
+
}}
|
| 771 |
+
on:change_layer={(e) => {
|
| 772 |
+
editor.set_layer(e.detail);
|
| 773 |
+
if (current_tool === "draw") {
|
| 774 |
+
handle_tool_change({ tool: "draw" });
|
| 775 |
+
}
|
| 776 |
+
}}
|
| 777 |
+
on:move_layer={(e) => {
|
| 778 |
+
editor.move_layer(e.detail.id, e.detail.direction);
|
| 779 |
+
}}
|
| 780 |
+
on:delete_layer={(e) => {
|
| 781 |
+
editor.delete_layer(e.detail);
|
| 782 |
+
}}
|
| 783 |
+
on:toggle_layer_visibility={(e) => {
|
| 784 |
+
editor.toggle_layer_visibility(e.detail);
|
| 785 |
+
}}
|
| 786 |
+
/>
|
| 787 |
+
{/if}
|
| 788 |
+
{/if}
|
| 789 |
+
<div
|
| 790 |
+
class="pixi-target"
|
| 791 |
+
class:visible={current_subtool !== "crop"}
|
| 792 |
+
bind:this={pixi_target}
|
| 793 |
+
></div>
|
| 794 |
+
<div
|
| 795 |
+
class="pixi-target-crop"
|
| 796 |
+
class:visible={current_subtool === "crop"}
|
| 797 |
+
bind:this={pixi_target_crop}
|
| 798 |
+
></div>
|
| 799 |
+
|
| 800 |
+
{#if current_subtool === "crop"}
|
| 801 |
+
<div class="crop-confirm-button">
|
| 802 |
+
<IconButton
|
| 803 |
+
Icon={Check}
|
| 804 |
+
label="Confirm crop"
|
| 805 |
+
show_label={true}
|
| 806 |
+
size="large"
|
| 807 |
+
padded={true}
|
| 808 |
+
color="white"
|
| 809 |
+
background="var(--color-green-500)"
|
| 810 |
+
label_position="right"
|
| 811 |
+
on:click={handle_crop_confirm}
|
| 812 |
+
/>
|
| 813 |
+
</div>
|
| 814 |
+
{/if}
|
| 815 |
+
<slot></slot>
|
| 816 |
+
</div>
|
| 817 |
+
|
| 818 |
+
<style>
|
| 819 |
+
.image-container {
|
| 820 |
+
display: flex;
|
| 821 |
+
height: 100%;
|
| 822 |
+
flex-direction: column;
|
| 823 |
+
justify-content: center;
|
| 824 |
+
align-items: center;
|
| 825 |
+
max-height: 100%;
|
| 826 |
+
border-radius: var(--radius-sm);
|
| 827 |
+
}
|
| 828 |
+
|
| 829 |
+
.pixi-target {
|
| 830 |
+
width: 100%;
|
| 831 |
+
height: 100%;
|
| 832 |
+
position: absolute;
|
| 833 |
+
top: 0;
|
| 834 |
+
left: 0;
|
| 835 |
+
z-index: 1;
|
| 836 |
+
display: block;
|
| 837 |
+
opacity: 0;
|
| 838 |
+
pointer-events: none;
|
| 839 |
+
border-radius: var(--radius-sm);
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
+
.pixi-target-crop {
|
| 843 |
+
width: 100%;
|
| 844 |
+
height: 100%;
|
| 845 |
+
position: absolute;
|
| 846 |
+
top: 0;
|
| 847 |
+
left: 0;
|
| 848 |
+
z-index: 2;
|
| 849 |
+
display: block;
|
| 850 |
+
opacity: 0;
|
| 851 |
+
pointer-events: none;
|
| 852 |
+
border-radius: var(--radius-sm);
|
| 853 |
+
}
|
| 854 |
+
|
| 855 |
+
.visible {
|
| 856 |
+
opacity: 1;
|
| 857 |
+
pointer-events: auto;
|
| 858 |
+
}
|
| 859 |
+
|
| 860 |
+
.pixi-target {
|
| 861 |
+
width: 100%;
|
| 862 |
+
height: 100%;
|
| 863 |
+
position: absolute;
|
| 864 |
+
top: 0;
|
| 865 |
+
left: 0;
|
| 866 |
+
right: 0;
|
| 867 |
+
bottom: 0;
|
| 868 |
+
overflow: hidden;
|
| 869 |
+
}
|
| 870 |
+
|
| 871 |
+
.modal {
|
| 872 |
+
position: absolute;
|
| 873 |
+
height: 100%;
|
| 874 |
+
width: 100%;
|
| 875 |
+
left: 0;
|
| 876 |
+
right: 0;
|
| 877 |
+
margin: auto;
|
| 878 |
+
z-index: var(--layer-top);
|
| 879 |
+
display: flex;
|
| 880 |
+
align-items: center;
|
| 881 |
+
}
|
| 882 |
+
|
| 883 |
+
.modal-inner {
|
| 884 |
+
height: 100%;
|
| 885 |
+
width: 100%;
|
| 886 |
+
background: var(--block-background-fill);
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
+
.dark-bg {
|
| 890 |
+
background: #333;
|
| 891 |
+
}
|
| 892 |
+
|
| 893 |
+
.crop-confirm-button {
|
| 894 |
+
position: absolute;
|
| 895 |
+
bottom: 8px;
|
| 896 |
+
left: 0;
|
| 897 |
+
right: 0;
|
| 898 |
+
margin: auto;
|
| 899 |
+
z-index: var(--layer-top);
|
| 900 |
+
display: flex;
|
| 901 |
+
align-items: center;
|
| 902 |
+
justify-content: center;
|
| 903 |
+
}
|
| 904 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/Layers.svelte
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { click_outside } from "./utils/events";
|
| 3 |
+
import {
|
| 4 |
+
Layers,
|
| 5 |
+
Clear,
|
| 6 |
+
ArrowUp,
|
| 7 |
+
ArrowDown,
|
| 8 |
+
Plus,
|
| 9 |
+
Visibility,
|
| 10 |
+
VisibilityOff
|
| 11 |
+
} from "@gradio/icons";
|
| 12 |
+
import { IconButton } from "@gradio/atoms";
|
| 13 |
+
import { createEventDispatcher } from "svelte";
|
| 14 |
+
import type { Writable } from "svelte/store";
|
| 15 |
+
|
| 16 |
+
const dispatch = createEventDispatcher<{
|
| 17 |
+
new_layer: void;
|
| 18 |
+
change_layer: string;
|
| 19 |
+
move_layer: { id: string; direction: "up" | "down" };
|
| 20 |
+
delete_layer: string;
|
| 21 |
+
toggle_layer_visibility: string;
|
| 22 |
+
}>();
|
| 23 |
+
|
| 24 |
+
export let layers: Writable<{
|
| 25 |
+
active_layer: string;
|
| 26 |
+
layers: {
|
| 27 |
+
name: string;
|
| 28 |
+
id: string;
|
| 29 |
+
user_created: boolean;
|
| 30 |
+
visible: boolean;
|
| 31 |
+
}[];
|
| 32 |
+
}>;
|
| 33 |
+
export let enable_additional_layers = true;
|
| 34 |
+
export let enable_layers = true;
|
| 35 |
+
export let show_layers = false;
|
| 36 |
+
|
| 37 |
+
function new_layer(): void {
|
| 38 |
+
dispatch("new_layer");
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
function change_layer(id: string): void {
|
| 42 |
+
dispatch("change_layer", id);
|
| 43 |
+
show_layers = false;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
function move_layer(id: string, direction: "up" | "down"): void {
|
| 47 |
+
dispatch("move_layer", { id, direction });
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
function delete_layer(id: string): void {
|
| 51 |
+
dispatch("delete_layer", id);
|
| 52 |
+
}
|
| 53 |
+
</script>
|
| 54 |
+
|
| 55 |
+
{#if enable_layers}
|
| 56 |
+
<div
|
| 57 |
+
class="layer-wrap"
|
| 58 |
+
class:closed={!show_layers}
|
| 59 |
+
use:click_outside={() => (show_layers = false)}
|
| 60 |
+
>
|
| 61 |
+
<button
|
| 62 |
+
class="layer-title-button"
|
| 63 |
+
aria-label="Show Layers"
|
| 64 |
+
on:click|stopPropagation={() => (show_layers = !show_layers)}
|
| 65 |
+
>
|
| 66 |
+
{show_layers
|
| 67 |
+
? "Layers"
|
| 68 |
+
: $layers.layers.find((l) => l.id === $layers.active_layer)?.name}
|
| 69 |
+
<span class="icon"><Layers /></span>
|
| 70 |
+
</button>
|
| 71 |
+
{#if show_layers}
|
| 72 |
+
<ul>
|
| 73 |
+
{#each $layers.layers as { id, name, user_created, visible }, i (i)}
|
| 74 |
+
<li>
|
| 75 |
+
{#if !visible}
|
| 76 |
+
<IconButton
|
| 77 |
+
Icon={VisibilityOff}
|
| 78 |
+
size="small"
|
| 79 |
+
on:click={(e) => {
|
| 80 |
+
e.stopPropagation();
|
| 81 |
+
dispatch("toggle_layer_visibility", id);
|
| 82 |
+
}}
|
| 83 |
+
/>
|
| 84 |
+
{:else}
|
| 85 |
+
<IconButton
|
| 86 |
+
Icon={Visibility}
|
| 87 |
+
size="small"
|
| 88 |
+
on:click={(e) => {
|
| 89 |
+
e.stopPropagation();
|
| 90 |
+
dispatch("toggle_layer_visibility", id);
|
| 91 |
+
}}
|
| 92 |
+
/>
|
| 93 |
+
{/if}
|
| 94 |
+
<button
|
| 95 |
+
class:selected_layer={$layers.active_layer === id}
|
| 96 |
+
aria-label={`layer-${i + 1}`}
|
| 97 |
+
on:click|stopPropagation={() => change_layer(id)}>{name}</button
|
| 98 |
+
>
|
| 99 |
+
{#if $layers.layers.length > 1}
|
| 100 |
+
<div>
|
| 101 |
+
{#if i > 0}
|
| 102 |
+
<IconButton
|
| 103 |
+
on:click={(e) => {
|
| 104 |
+
e.stopPropagation();
|
| 105 |
+
move_layer(id, "up");
|
| 106 |
+
}}
|
| 107 |
+
Icon={ArrowUp}
|
| 108 |
+
size="x-small"
|
| 109 |
+
/>
|
| 110 |
+
{/if}
|
| 111 |
+
{#if i < $layers.layers.length - 1}
|
| 112 |
+
<IconButton
|
| 113 |
+
on:click={(e) => {
|
| 114 |
+
e.stopPropagation();
|
| 115 |
+
move_layer(id, "down");
|
| 116 |
+
}}
|
| 117 |
+
Icon={ArrowDown}
|
| 118 |
+
size="x-small"
|
| 119 |
+
/>
|
| 120 |
+
{/if}
|
| 121 |
+
{#if $layers.layers.length > 1 && user_created}
|
| 122 |
+
<IconButton
|
| 123 |
+
on:click={(e) => {
|
| 124 |
+
e.stopPropagation();
|
| 125 |
+
delete_layer(id);
|
| 126 |
+
}}
|
| 127 |
+
Icon={Clear}
|
| 128 |
+
size="x-small"
|
| 129 |
+
/>
|
| 130 |
+
{/if}
|
| 131 |
+
</div>
|
| 132 |
+
{/if}
|
| 133 |
+
</li>
|
| 134 |
+
{/each}
|
| 135 |
+
{#if enable_additional_layers}
|
| 136 |
+
<li class="add-layer">
|
| 137 |
+
<IconButton
|
| 138 |
+
Icon={Plus}
|
| 139 |
+
label="Add Layer"
|
| 140 |
+
on:click={(e) => {
|
| 141 |
+
e.stopPropagation();
|
| 142 |
+
new_layer();
|
| 143 |
+
}}
|
| 144 |
+
size="x-small"
|
| 145 |
+
/>
|
| 146 |
+
</li>
|
| 147 |
+
{/if}
|
| 148 |
+
</ul>
|
| 149 |
+
{/if}
|
| 150 |
+
</div>
|
| 151 |
+
{/if}
|
| 152 |
+
|
| 153 |
+
<style>
|
| 154 |
+
.add-layer {
|
| 155 |
+
display: flex;
|
| 156 |
+
justify-content: center;
|
| 157 |
+
align-items: center;
|
| 158 |
+
margin: var(--spacing-sm) 0 0 0;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.add-layer :global(button) {
|
| 162 |
+
width: 100%;
|
| 163 |
+
}
|
| 164 |
+
.icon {
|
| 165 |
+
width: 14px;
|
| 166 |
+
color: var(--body-text-color);
|
| 167 |
+
transition: color 0.15s ease;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
.layer-wrap {
|
| 171 |
+
/* position: absolute; */
|
| 172 |
+
display: flex;
|
| 173 |
+
justify-content: center;
|
| 174 |
+
align-items: center;
|
| 175 |
+
/* border: 1px solid var(--border-color-primary); */
|
| 176 |
+
/* z-index: var(--layer-1); */
|
| 177 |
+
/* border-radius: var(--radius-sm); */
|
| 178 |
+
/* background-color: var(--block-background-fill); */
|
| 179 |
+
width: 100%;
|
| 180 |
+
flex-direction: column;
|
| 181 |
+
/* bottom: 5px; */
|
| 182 |
+
/* right: 5px; */
|
| 183 |
+
/* border-bottom: 0;
|
| 184 |
+
border-right: 0; */
|
| 185 |
+
/* box-shadow: var(--shadow-drop); */
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.layer-wrap button {
|
| 189 |
+
justify-content: space-between;
|
| 190 |
+
align-items: center;
|
| 191 |
+
width: 100%;
|
| 192 |
+
display: flex;
|
| 193 |
+
font-size: var(--text-sm);
|
| 194 |
+
line-height: var(--line-sm);
|
| 195 |
+
transition: all 0.15s ease;
|
| 196 |
+
padding: var(--spacing-md) var(--spacing-lg);
|
| 197 |
+
/* padding-right: var(--spacing-lg); */
|
| 198 |
+
border-radius: var(--radius-sm);
|
| 199 |
+
/* padding-left: 30px; */
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
button.layer-title-button {
|
| 203 |
+
width: 100%;
|
| 204 |
+
/* padding-left: 30px; */
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
.layer-wrap button:hover .icon {
|
| 208 |
+
color: var(--color-accent);
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.selected_layer {
|
| 212 |
+
color: var(--color-accent);
|
| 213 |
+
font-weight: var(--weight-semibold);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
ul {
|
| 217 |
+
/* position: absolute; */
|
| 218 |
+
bottom: 0;
|
| 219 |
+
right: 0;
|
| 220 |
+
/* background: var(--block-background-fill); */
|
| 221 |
+
width: 100%;
|
| 222 |
+
/* min-width: 100%; */
|
| 223 |
+
list-style: none;
|
| 224 |
+
z-index: var(--layer-top);
|
| 225 |
+
/* border: 1px solid var(--border-color-primary); */
|
| 226 |
+
/* border-radius: var(--radius-sm); */
|
| 227 |
+
/* box-shadow: var(--shadow-drop-lg); */
|
| 228 |
+
padding: var(--spacing-sm);
|
| 229 |
+
/* transform: translate(-1px, 1px); */
|
| 230 |
+
border-top: 1px solid var(--border-color-primary);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/* .sep {
|
| 234 |
+
height: 12px;
|
| 235 |
+
background-color: var(--block-border-color);
|
| 236 |
+
width: 1px;
|
| 237 |
+
display: block;
|
| 238 |
+
margin-left: var(--spacing-xl);
|
| 239 |
+
} */
|
| 240 |
+
|
| 241 |
+
li {
|
| 242 |
+
display: flex;
|
| 243 |
+
justify-content: space-between;
|
| 244 |
+
align-items: center;
|
| 245 |
+
/* border-radius: var(--radius-sm); */
|
| 246 |
+
transition: background-color 0.15s ease;
|
| 247 |
+
border-bottom: 1px solid var(--border-color-primary);
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
.layer-wrap ul > li > button {
|
| 251 |
+
padding-left: calc(var(--spacing-md));
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
li:hover {
|
| 255 |
+
background-color: var(--background-fill-secondary);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
li:last-child {
|
| 259 |
+
border-bottom: none;
|
| 260 |
+
}
|
| 261 |
+
li:last-child:hover {
|
| 262 |
+
background-color: unset;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
li div {
|
| 266 |
+
display: flex;
|
| 267 |
+
gap: var(--spacing-sm);
|
| 268 |
+
padding: var(--spacing-sm);
|
| 269 |
+
}
|
| 270 |
+
li > button {
|
| 271 |
+
padding: var(--spacing-sm) var(--spacing-md);
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
/* li > div > button {
|
| 275 |
+
opacity: 0.7;
|
| 276 |
+
transition: opacity 0.15s ease;
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
li > div > button:hover {
|
| 280 |
+
opacity: 1;
|
| 281 |
+
} */
|
| 282 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/Resize.svelte
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts" context="module">
|
| 2 |
+
export type Position =
|
| 3 |
+
| "top-left"
|
| 4 |
+
| "top"
|
| 5 |
+
| "top-right"
|
| 6 |
+
| "left"
|
| 7 |
+
| "center"
|
| 8 |
+
| "right"
|
| 9 |
+
| "bottom-left"
|
| 10 |
+
| "bottom"
|
| 11 |
+
| "bottom-right";
|
| 12 |
+
</script>
|
| 13 |
+
|
| 14 |
+
<script lang="ts">
|
| 15 |
+
import { createEventDispatcher } from "svelte";
|
| 16 |
+
import type { Spring } from "svelte/motion";
|
| 17 |
+
import Anchor from "./Anchor.svelte";
|
| 18 |
+
|
| 19 |
+
export let dimensions: Spring<{ width: number; height: number }>;
|
| 20 |
+
|
| 21 |
+
const dispatch = createEventDispatcher<{
|
| 22 |
+
change: { anchor: Position; scale: boolean; width: number; height: number };
|
| 23 |
+
}>();
|
| 24 |
+
|
| 25 |
+
let selected_anchor: Position = "center";
|
| 26 |
+
let scale = false;
|
| 27 |
+
|
| 28 |
+
let new_width = $dimensions?.width;
|
| 29 |
+
let new_height = $dimensions?.height;
|
| 30 |
+
|
| 31 |
+
function handle_click(): void {
|
| 32 |
+
dispatch("change", {
|
| 33 |
+
anchor: selected_anchor,
|
| 34 |
+
scale,
|
| 35 |
+
width: new_width,
|
| 36 |
+
height: new_height
|
| 37 |
+
});
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function set_anchor(position: Position): void {
|
| 41 |
+
selected_anchor = position;
|
| 42 |
+
}
|
| 43 |
+
</script>
|
| 44 |
+
|
| 45 |
+
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
| 46 |
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
| 47 |
+
<div class="wrap" on:click|stopPropagation>
|
| 48 |
+
<div class="size-wrap">
|
| 49 |
+
<div class="size-input">
|
| 50 |
+
<label for="width">Width</label><input
|
| 51 |
+
type="number"
|
| 52 |
+
id="width"
|
| 53 |
+
bind:value={new_width}
|
| 54 |
+
/>
|
| 55 |
+
</div>
|
| 56 |
+
<div class="size-input">
|
| 57 |
+
<label for="height">Height </label><input
|
| 58 |
+
type="number"
|
| 59 |
+
id="height"
|
| 60 |
+
bind:value={new_height}
|
| 61 |
+
/>
|
| 62 |
+
</div>
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
+
<div class="scale-wrap">
|
| 66 |
+
<h2 class="image-will-label">Image will:</h2>
|
| 67 |
+
<div class="toggle-container">
|
| 68 |
+
<label class="radio-label">
|
| 69 |
+
<input
|
| 70 |
+
type="radio"
|
| 71 |
+
name="resize_mode"
|
| 72 |
+
value={true}
|
| 73 |
+
bind:group={scale}
|
| 74 |
+
/>
|
| 75 |
+
<span class="radio-button">rescale</span>
|
| 76 |
+
</label>
|
| 77 |
+
<label class="radio-label">
|
| 78 |
+
<input
|
| 79 |
+
type="radio"
|
| 80 |
+
name="resize_mode"
|
| 81 |
+
value={false}
|
| 82 |
+
bind:group={scale}
|
| 83 |
+
/>
|
| 84 |
+
<span class="radio-button">anchor</span>
|
| 85 |
+
</label>
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
<div class="anchor-wrap">
|
| 89 |
+
<Anchor on:position={(e) => set_anchor(e.detail)} />
|
| 90 |
+
</div>
|
| 91 |
+
<button class="apply-button" on:click={() => handle_click()}
|
| 92 |
+
>Resize Canvas</button
|
| 93 |
+
>
|
| 94 |
+
</div>
|
| 95 |
+
|
| 96 |
+
<!-- </div> -->
|
| 97 |
+
|
| 98 |
+
<style>
|
| 99 |
+
.wrap {
|
| 100 |
+
padding: var(--spacing-xl) var(--spacing-lg);
|
| 101 |
+
color: var(--block-label-text-color);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
.size-wrap {
|
| 105 |
+
display: flex;
|
| 106 |
+
flex-direction: row;
|
| 107 |
+
gap: 10px;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
.size-input {
|
| 111 |
+
display: flex;
|
| 112 |
+
flex-direction: column;
|
| 113 |
+
gap: var(--spacing-xxs);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
label,
|
| 117 |
+
h2 {
|
| 118 |
+
display: flex;
|
| 119 |
+
align-items: center;
|
| 120 |
+
gap: 15px;
|
| 121 |
+
|
| 122 |
+
cursor: pointer;
|
| 123 |
+
color: var(--checkbox-label-text-color);
|
| 124 |
+
font-weight: var(--checkbox-label-text-weight);
|
| 125 |
+
font-size: var(--text-sm);
|
| 126 |
+
line-height: var(--line-md);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.scale-wrap {
|
| 130 |
+
display: flex;
|
| 131 |
+
flex-direction: column;
|
| 132 |
+
gap: var(--spacing-sm);
|
| 133 |
+
margin-top: var(--spacing-md);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
input[type="number"] {
|
| 137 |
+
border: 1px solid var(--border-color-primary);
|
| 138 |
+
width: 60px;
|
| 139 |
+
border-radius: var(--radius-sm);
|
| 140 |
+
background-color: var(--checkbox-background-color-focus);
|
| 141 |
+
color: var(--body-text-color);
|
| 142 |
+
font-size: var(--text-sm);
|
| 143 |
+
transition: border-color 0.15s ease;
|
| 144 |
+
padding: var(--spacing-xs);
|
| 145 |
+
line-height: 1rem;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
input[type="number"]:hover {
|
| 149 |
+
border-color: var(--color-accent-soft);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
input[type="number"]:focus {
|
| 153 |
+
border-color: var(--color-accent);
|
| 154 |
+
outline: none;
|
| 155 |
+
box-shadow: 0 0 0 1px var(--color-accent-soft);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.image-will-label {
|
| 159 |
+
grid-column: 1;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.toggle-container {
|
| 163 |
+
display: flex;
|
| 164 |
+
grid-column: 2;
|
| 165 |
+
border-radius: var(--radius-sm);
|
| 166 |
+
border: 1px solid var(--border-color-primary);
|
| 167 |
+
overflow: hidden;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
.toggle-container {
|
| 171 |
+
display: flex;
|
| 172 |
+
grid-column: 2;
|
| 173 |
+
border-radius: var(--radius-sm);
|
| 174 |
+
border: 1px solid var(--border-color-primary);
|
| 175 |
+
overflow: hidden;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
.radio-label {
|
| 179 |
+
flex: 1;
|
| 180 |
+
margin: 0;
|
| 181 |
+
padding: 0;
|
| 182 |
+
position: relative;
|
| 183 |
+
cursor: pointer;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.radio-button {
|
| 187 |
+
display: flex;
|
| 188 |
+
justify-content: center;
|
| 189 |
+
align-items: center;
|
| 190 |
+
padding: var(--spacing-sm);
|
| 191 |
+
background: var(--checkbox-background-color);
|
| 192 |
+
color: var(--body-text-color);
|
| 193 |
+
font-size: var(--text-sm);
|
| 194 |
+
transition: background-color 0.15s ease;
|
| 195 |
+
width: 100%;
|
| 196 |
+
height: 100%;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
input[type="radio"] {
|
| 200 |
+
position: absolute;
|
| 201 |
+
opacity: 0;
|
| 202 |
+
width: 0;
|
| 203 |
+
height: 0;
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
input[type="radio"]:not(:checked) + .radio-button:hover {
|
| 207 |
+
background: var(--checkbox-background-color-hover);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
input[type="radio"]:checked + .radio-button {
|
| 211 |
+
background: var(--color-accent);
|
| 212 |
+
color: white;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
.apply-button {
|
| 216 |
+
width: 100%;
|
| 217 |
+
margin-top: var(--spacing-md);
|
| 218 |
+
padding: var(--spacing-sm) var(--spacing-md);
|
| 219 |
+
background: var(--color-accent);
|
| 220 |
+
color: white;
|
| 221 |
+
border: none;
|
| 222 |
+
border-radius: var(--radius-sm);
|
| 223 |
+
font-size: var(--text-sm);
|
| 224 |
+
font-weight: var(--weight-semibold);
|
| 225 |
+
transition: all 0.15s ease;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.apply-button:hover {
|
| 229 |
+
background: var(--button-primary-background-fill-hover);
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
.anchor-wrap {
|
| 233 |
+
display: flex;
|
| 234 |
+
justify-content: center;
|
| 235 |
+
align-items: center;
|
| 236 |
+
flex-direction: column;
|
| 237 |
+
gap: 10px;
|
| 238 |
+
}
|
| 239 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/SecondaryToolbar.svelte
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import Layers from "./Layers.svelte";
|
| 3 |
+
import type { Writable } from "svelte/store";
|
| 4 |
+
|
| 5 |
+
export let layers: Writable<{
|
| 6 |
+
active_layer: string;
|
| 7 |
+
layers: {
|
| 8 |
+
name: string;
|
| 9 |
+
id: string;
|
| 10 |
+
user_created: boolean;
|
| 11 |
+
visible: boolean;
|
| 12 |
+
}[];
|
| 13 |
+
}>;
|
| 14 |
+
|
| 15 |
+
export let enable_additional_layers = true;
|
| 16 |
+
export let enable_layers = true;
|
| 17 |
+
export let show_layers = false;
|
| 18 |
+
</script>
|
| 19 |
+
|
| 20 |
+
<div class="toolbar-wrap-wrap">
|
| 21 |
+
<div class="toolbar-wrap">
|
| 22 |
+
{#if enable_layers}
|
| 23 |
+
<Layers
|
| 24 |
+
{layers}
|
| 25 |
+
{enable_additional_layers}
|
| 26 |
+
{enable_layers}
|
| 27 |
+
{show_layers}
|
| 28 |
+
on:new_layer
|
| 29 |
+
on:change_layer
|
| 30 |
+
on:move_layer
|
| 31 |
+
on:delete_layer
|
| 32 |
+
on:toggle_layer_visibility
|
| 33 |
+
/>
|
| 34 |
+
{/if}
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
|
| 38 |
+
<style>
|
| 39 |
+
.toolbar-wrap-wrap {
|
| 40 |
+
position: absolute;
|
| 41 |
+
right: 0;
|
| 42 |
+
bottom: 0;
|
| 43 |
+
width: fit-content;
|
| 44 |
+
max-height: 100%;
|
| 45 |
+
overflow-y: auto;
|
| 46 |
+
display: flex;
|
| 47 |
+
flex-direction: column-reverse;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.toolbar-wrap {
|
| 51 |
+
min-width: 110px;
|
| 52 |
+
display: flex;
|
| 53 |
+
flex-direction: column;
|
| 54 |
+
justify-content: center;
|
| 55 |
+
align-items: center;
|
| 56 |
+
position: relative;
|
| 57 |
+
right: 0;
|
| 58 |
+
bottom: 0;
|
| 59 |
+
border: 1px solid var(--block-border-color);
|
| 60 |
+
border-radius: 0;
|
| 61 |
+
border-top-left-radius: var(--radius-md);
|
| 62 |
+
border-right: none;
|
| 63 |
+
border-bottom: none;
|
| 64 |
+
z-index: 1000;
|
| 65 |
+
background-color: var(--block-background-fill);
|
| 66 |
+
}
|
| 67 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/Toolbar.svelte
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
| 5 |
+
| "paste"
|
| 6 |
+
| "webcam"
|
| 7 |
+
| "color"
|
| 8 |
+
| "size"
|
| 9 |
+
| "crop"
|
| 10 |
+
| "remove_background"
|
| 11 |
+
| null;
|
| 12 |
+
</script>
|
| 13 |
+
|
| 14 |
+
<script lang="ts">
|
| 15 |
+
import { createEventDispatcher } from "svelte";
|
| 16 |
+
import IconButton from "./IconButton.svelte";
|
| 17 |
+
import {
|
| 18 |
+
Image,
|
| 19 |
+
Brush,
|
| 20 |
+
Erase,
|
| 21 |
+
Crop,
|
| 22 |
+
Upload,
|
| 23 |
+
ImagePaste,
|
| 24 |
+
Webcam,
|
| 25 |
+
Circle,
|
| 26 |
+
Resize,
|
| 27 |
+
ColorPickerSolid
|
| 28 |
+
} from "@gradio/icons";
|
| 29 |
+
import BrushOptions from "./brush/BrushOptions.svelte";
|
| 30 |
+
import type { Source, Transform } from "./types";
|
| 31 |
+
import {
|
| 32 |
+
type Brush as BrushType,
|
| 33 |
+
type Eraser as EraserType
|
| 34 |
+
} from "./brush/types";
|
| 35 |
+
import tinycolor from "tinycolor2";
|
| 36 |
+
|
| 37 |
+
export let tool: Tool = "image";
|
| 38 |
+
export let subtool: Subtool = null;
|
| 39 |
+
|
| 40 |
+
export let background = false;
|
| 41 |
+
export let brush_options: BrushType | false;
|
| 42 |
+
export let selected_size =
|
| 43 |
+
brush_options && typeof brush_options.default_size === "number"
|
| 44 |
+
? brush_options.default_size
|
| 45 |
+
: 25;
|
| 46 |
+
export let eraser_options: EraserType;
|
| 47 |
+
export let selected_eraser_size =
|
| 48 |
+
eraser_options && typeof eraser_options.default_size === "number"
|
| 49 |
+
? eraser_options.default_size
|
| 50 |
+
: 25;
|
| 51 |
+
|
| 52 |
+
// Handle default_color including potential color-opacity tuple
|
| 53 |
+
export let selected_color =
|
| 54 |
+
brush_options &&
|
| 55 |
+
(() => {
|
| 56 |
+
const default_color = brush_options.default_color;
|
| 57 |
+
if (Array.isArray(default_color)) {
|
| 58 |
+
return default_color[0];
|
| 59 |
+
}
|
| 60 |
+
return default_color;
|
| 61 |
+
})();
|
| 62 |
+
|
| 63 |
+
// Set default opacity based on default_color if it's a tuple
|
| 64 |
+
export let selected_opacity =
|
| 65 |
+
brush_options &&
|
| 66 |
+
(() => {
|
| 67 |
+
const default_color = brush_options.default_color;
|
| 68 |
+
if (Array.isArray(default_color)) {
|
| 69 |
+
return default_color[1];
|
| 70 |
+
}
|
| 71 |
+
// Check if color string has opacity
|
| 72 |
+
const color = tinycolor(default_color);
|
| 73 |
+
if (color.getAlpha() < 1) {
|
| 74 |
+
return color.getAlpha();
|
| 75 |
+
}
|
| 76 |
+
return 1;
|
| 77 |
+
})();
|
| 78 |
+
|
| 79 |
+
export let preview = false;
|
| 80 |
+
export let show_brush_color = false;
|
| 81 |
+
export let show_brush_size = false;
|
| 82 |
+
export let show_eraser_size = false;
|
| 83 |
+
export let sources: Source[];
|
| 84 |
+
export let transforms: Transform[];
|
| 85 |
+
let recent_colors: string[] = [];
|
| 86 |
+
|
| 87 |
+
let enable_layers = true;
|
| 88 |
+
const dispatch = createEventDispatcher<{
|
| 89 |
+
tool_change: { tool: Tool };
|
| 90 |
+
subtool_change: {
|
| 91 |
+
tool: Tool;
|
| 92 |
+
subtool: Subtool;
|
| 93 |
+
};
|
| 94 |
+
}>();
|
| 95 |
+
|
| 96 |
+
/**
|
| 97 |
+
* Handles tool click events
|
| 98 |
+
* @param {Tool} _tool - The selected tool
|
| 99 |
+
*/
|
| 100 |
+
function handle_tool_click(e: Event, _tool: Tool): void {
|
| 101 |
+
e.stopPropagation();
|
| 102 |
+
dispatch("tool_change", { tool: _tool });
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/**
|
| 106 |
+
* Handles subtool click events
|
| 107 |
+
* @param {Event} e - The click event
|
| 108 |
+
* @param {Subtool} _subtool - The selected subtool
|
| 109 |
+
*/
|
| 110 |
+
function handle_subtool_click(e: Event, _subtool: typeof subtool): void {
|
| 111 |
+
e.stopPropagation();
|
| 112 |
+
|
| 113 |
+
dispatch("subtool_change", { tool, subtool: _subtool });
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
$: show_brush_size = tool === "draw" && subtool === "size";
|
| 117 |
+
$: show_brush_color = tool === "draw" && subtool === "color";
|
| 118 |
+
$: show_eraser_size = tool === "erase" && subtool === "size";
|
| 119 |
+
|
| 120 |
+
$: can_crop = transforms.includes("crop");
|
| 121 |
+
$: can_resize = transforms.includes("resize");
|
| 122 |
+
$: can_upload = sources.includes("upload");
|
| 123 |
+
$: can_webcam = sources.includes("webcam");
|
| 124 |
+
$: can_paste = sources.includes("clipboard");
|
| 125 |
+
</script>
|
| 126 |
+
|
| 127 |
+
<div class="toolbar-wrap">
|
| 128 |
+
<div class="half-container">
|
| 129 |
+
{#if sources.length > 0}
|
| 130 |
+
<IconButton
|
| 131 |
+
Icon={Image}
|
| 132 |
+
label="Image"
|
| 133 |
+
highlight={tool === "image"}
|
| 134 |
+
on:click={(e) => handle_tool_click(e, "image")}
|
| 135 |
+
size="medium"
|
| 136 |
+
padded={false}
|
| 137 |
+
transparent={true}
|
| 138 |
+
/>
|
| 139 |
+
{/if}
|
| 140 |
+
{#if brush_options}
|
| 141 |
+
<IconButton
|
| 142 |
+
Icon={Brush}
|
| 143 |
+
label="Brush"
|
| 144 |
+
on:click={(e) => handle_tool_click(e, "draw")}
|
| 145 |
+
highlight={tool === "draw"}
|
| 146 |
+
size="medium"
|
| 147 |
+
padded={false}
|
| 148 |
+
transparent={true}
|
| 149 |
+
/>
|
| 150 |
+
{/if}
|
| 151 |
+
{#if eraser_options}
|
| 152 |
+
<IconButton
|
| 153 |
+
Icon={Erase}
|
| 154 |
+
label="Erase"
|
| 155 |
+
on:click={(e) => handle_tool_click(e, "erase")}
|
| 156 |
+
highlight={tool === "erase"}
|
| 157 |
+
size="medium"
|
| 158 |
+
padded={false}
|
| 159 |
+
transparent={true}
|
| 160 |
+
/>
|
| 161 |
+
{/if}
|
| 162 |
+
</div>
|
| 163 |
+
<div
|
| 164 |
+
class="half-container right"
|
| 165 |
+
class:hide={tool === "pan" ||
|
| 166 |
+
(tool === "image" && !background && sources.length === 0) ||
|
| 167 |
+
(tool === "image" && background && transforms.length === 0)}
|
| 168 |
+
>
|
| 169 |
+
{#if tool === "image"}
|
| 170 |
+
{#if background}
|
| 171 |
+
{#if can_crop}
|
| 172 |
+
<IconButton
|
| 173 |
+
Icon={Crop}
|
| 174 |
+
label="Crop"
|
| 175 |
+
on:click={(e) => handle_subtool_click(e, "crop")}
|
| 176 |
+
highlight={subtool === "crop"}
|
| 177 |
+
size="medium"
|
| 178 |
+
padded={false}
|
| 179 |
+
transparent={true}
|
| 180 |
+
offset={0}
|
| 181 |
+
/>
|
| 182 |
+
{/if}
|
| 183 |
+
{#if can_resize}
|
| 184 |
+
<IconButton
|
| 185 |
+
Icon={Resize}
|
| 186 |
+
label="Resize"
|
| 187 |
+
on:click={(e) => handle_subtool_click(e, "size")}
|
| 188 |
+
highlight={subtool === "size"}
|
| 189 |
+
size="medium"
|
| 190 |
+
padded={false}
|
| 191 |
+
transparent={true}
|
| 192 |
+
offset={0}
|
| 193 |
+
/>
|
| 194 |
+
{/if}
|
| 195 |
+
{:else}
|
| 196 |
+
{#if can_upload}
|
| 197 |
+
<IconButton
|
| 198 |
+
Icon={Upload}
|
| 199 |
+
label="Upload"
|
| 200 |
+
on:click={(e) => handle_subtool_click(e, "upload")}
|
| 201 |
+
highlight={subtool === "upload"}
|
| 202 |
+
size="medium"
|
| 203 |
+
padded={false}
|
| 204 |
+
transparent={true}
|
| 205 |
+
offset={0}
|
| 206 |
+
/>
|
| 207 |
+
{/if}
|
| 208 |
+
{#if can_paste}
|
| 209 |
+
<IconButton
|
| 210 |
+
Icon={ImagePaste}
|
| 211 |
+
label="Paste"
|
| 212 |
+
on:click={(e) => handle_subtool_click(e, "paste")}
|
| 213 |
+
highlight={subtool === "paste"}
|
| 214 |
+
size="large"
|
| 215 |
+
padded={false}
|
| 216 |
+
transparent={true}
|
| 217 |
+
offset={0}
|
| 218 |
+
/>
|
| 219 |
+
{/if}
|
| 220 |
+
{#if can_webcam}
|
| 221 |
+
<IconButton
|
| 222 |
+
Icon={Webcam}
|
| 223 |
+
label="Webcam"
|
| 224 |
+
on:click={(e) => handle_subtool_click(e, "webcam")}
|
| 225 |
+
highlight={subtool === "webcam"}
|
| 226 |
+
size="medium"
|
| 227 |
+
padded={false}
|
| 228 |
+
transparent={true}
|
| 229 |
+
offset={0}
|
| 230 |
+
/>
|
| 231 |
+
{/if}
|
| 232 |
+
{/if}
|
| 233 |
+
{/if}
|
| 234 |
+
|
| 235 |
+
{#if tool === "draw" && brush_options}
|
| 236 |
+
<IconButton
|
| 237 |
+
Icon={ColorPickerSolid}
|
| 238 |
+
label="Color"
|
| 239 |
+
color={selected_color}
|
| 240 |
+
on:click={(e) => handle_subtool_click(e, "color")}
|
| 241 |
+
size="medium"
|
| 242 |
+
padded={false}
|
| 243 |
+
transparent={true}
|
| 244 |
+
offset={0}
|
| 245 |
+
/>
|
| 246 |
+
<IconButton
|
| 247 |
+
Icon={Circle}
|
| 248 |
+
label="Brush Size"
|
| 249 |
+
on:click={(e) => handle_subtool_click(e, "size")}
|
| 250 |
+
highlight={subtool === "size"}
|
| 251 |
+
size="medium"
|
| 252 |
+
padded={false}
|
| 253 |
+
transparent={true}
|
| 254 |
+
offset={0}
|
| 255 |
+
/>
|
| 256 |
+
|
| 257 |
+
{#if show_brush_color || show_brush_size}
|
| 258 |
+
<BrushOptions
|
| 259 |
+
colors={brush_options.colors}
|
| 260 |
+
color_mode={brush_options.color_mode}
|
| 261 |
+
{recent_colors}
|
| 262 |
+
show_swatch={show_brush_color}
|
| 263 |
+
show_size={show_brush_size}
|
| 264 |
+
bind:selected_size
|
| 265 |
+
bind:selected_color
|
| 266 |
+
bind:selected_opacity
|
| 267 |
+
bind:preview
|
| 268 |
+
on:click_outside={(e) => {
|
| 269 |
+
e.stopPropagation();
|
| 270 |
+
preview = false;
|
| 271 |
+
show_brush_color = false;
|
| 272 |
+
show_brush_size = false;
|
| 273 |
+
handle_subtool_click(e, null);
|
| 274 |
+
}}
|
| 275 |
+
mode="brush"
|
| 276 |
+
/>
|
| 277 |
+
{/if}
|
| 278 |
+
{/if}
|
| 279 |
+
|
| 280 |
+
{#if tool === "erase" && eraser_options}
|
| 281 |
+
<IconButton
|
| 282 |
+
Icon={Circle}
|
| 283 |
+
label="Eraser Size"
|
| 284 |
+
on:click={(e) => handle_subtool_click(e, "size")}
|
| 285 |
+
highlight={subtool === "size"}
|
| 286 |
+
size="medium"
|
| 287 |
+
padded={false}
|
| 288 |
+
transparent={true}
|
| 289 |
+
offset={0}
|
| 290 |
+
/>
|
| 291 |
+
{/if}
|
| 292 |
+
{#if show_eraser_size}
|
| 293 |
+
<BrushOptions
|
| 294 |
+
colors={[]}
|
| 295 |
+
show_swatch={false}
|
| 296 |
+
show_size={true}
|
| 297 |
+
bind:selected_size={selected_eraser_size}
|
| 298 |
+
bind:selected_color
|
| 299 |
+
bind:selected_opacity
|
| 300 |
+
bind:preview
|
| 301 |
+
on:click_outside={(e) => {
|
| 302 |
+
e.stopPropagation();
|
| 303 |
+
preview = false;
|
| 304 |
+
show_eraser_size = false;
|
| 305 |
+
handle_subtool_click(e, null);
|
| 306 |
+
}}
|
| 307 |
+
mode="eraser"
|
| 308 |
+
/>
|
| 309 |
+
{/if}
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
|
| 313 |
+
<style>
|
| 314 |
+
.toolbar-wrap {
|
| 315 |
+
display: inline-flex;
|
| 316 |
+
flex-direction: column;
|
| 317 |
+
justify-content: center;
|
| 318 |
+
align-items: center;
|
| 319 |
+
position: absolute;
|
| 320 |
+
left: 15px;
|
| 321 |
+
right: 0;
|
| 322 |
+
top: 30%;
|
| 323 |
+
/* transform: translateY(-50%); */
|
| 324 |
+
width: 30px;
|
| 325 |
+
border: 1px solid var(--block-border-color);
|
| 326 |
+
border-radius: var(--radius-md);
|
| 327 |
+
|
| 328 |
+
gap: var(--spacing-sm);
|
| 329 |
+
z-index: 1000;
|
| 330 |
+
background-color: var(--block-background-fill);
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
.half-container {
|
| 334 |
+
flex: 1;
|
| 335 |
+
display: flex;
|
| 336 |
+
flex-direction: column;
|
| 337 |
+
gap: var(--spacing-sm);
|
| 338 |
+
position: relative;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
.half-container {
|
| 342 |
+
width: 100%;
|
| 343 |
+
padding: var(--spacing-sm) 0;
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
.half-container:last-child {
|
| 347 |
+
border-top: 1px solid var(--block-border-color);
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
.hide {
|
| 351 |
+
display: none;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
/* .toolbar-left,
|
| 355 |
+
.toolbar-right {
|
| 356 |
+
display: flex;
|
| 357 |
+
flex-direction: row;
|
| 358 |
+
align-items: center;
|
| 359 |
+
gap: var(--spacing-sm);
|
| 360 |
+
display: flex;
|
| 361 |
+
z-index: 1000;
|
| 362 |
+
border-radius: var(--radius-sm);
|
| 363 |
+
background-color: #fff;
|
| 364 |
+
padding: var(--spacing-sm) 0.3rem;
|
| 365 |
+
width: auto;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
.toolbar-right {
|
| 369 |
+
border-left: 0;
|
| 370 |
+
border-top-left-radius: 0;
|
| 371 |
+
border-bottom-left-radius: 0;
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
.toolbar-left {
|
| 375 |
+
border-top-right-radius: 0;
|
| 376 |
+
border-bottom-right-radius: 0;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
.toolbar-layers {
|
| 380 |
+
border-top-left-radius: 0;
|
| 381 |
+
border-bottom-left-radius: 0;
|
| 382 |
+
} */
|
| 383 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/brush/BrushOptions.svelte
ADDED
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher, tick } from "svelte";
|
| 3 |
+
import { click_outside } from "../utils/events";
|
| 4 |
+
|
| 5 |
+
import { type Brush, type ColorTuple } from "./types";
|
| 6 |
+
import ColorPicker from "./ColorPicker.svelte";
|
| 7 |
+
import ColorSwatch from "./ColorSwatch.svelte";
|
| 8 |
+
import ColorField from "./ColorField.svelte";
|
| 9 |
+
import BrushSize from "./BrushSize.svelte";
|
| 10 |
+
import type { ColorInput } from "tinycolor2";
|
| 11 |
+
|
| 12 |
+
export let colors: (ColorInput | ColorTuple)[];
|
| 13 |
+
export let selected_color: any;
|
| 14 |
+
export let color_mode: Brush["color_mode"] | undefined = undefined;
|
| 15 |
+
export let recent_colors: (ColorInput | ColorTuple)[] = [];
|
| 16 |
+
export let selected_size: number;
|
| 17 |
+
export let selected_opacity: number;
|
| 18 |
+
export let show_swatch: boolean;
|
| 19 |
+
export let show_size: boolean;
|
| 20 |
+
export let mode: "brush" | "eraser" = "brush";
|
| 21 |
+
|
| 22 |
+
let color_picker = false;
|
| 23 |
+
let current_mode: "hex" | "rgb" | "hsl" = "hex";
|
| 24 |
+
let editing_index: number | null = null;
|
| 25 |
+
|
| 26 |
+
const dispatch = createEventDispatcher<{
|
| 27 |
+
click_outside: void;
|
| 28 |
+
}>();
|
| 29 |
+
|
| 30 |
+
function handle_color_selection(
|
| 31 |
+
{
|
| 32 |
+
index,
|
| 33 |
+
color,
|
| 34 |
+
opacity
|
| 35 |
+
}: {
|
| 36 |
+
index: number | null;
|
| 37 |
+
color: string | null;
|
| 38 |
+
opacity?: number;
|
| 39 |
+
},
|
| 40 |
+
type: "core" | "user"
|
| 41 |
+
): void {
|
| 42 |
+
if (type === "user" && !color) {
|
| 43 |
+
editing_index = index;
|
| 44 |
+
color_picker = true;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
if (!color) return;
|
| 48 |
+
selected_color = color;
|
| 49 |
+
|
| 50 |
+
if (opacity !== undefined) {
|
| 51 |
+
selected_opacity = opacity;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
if (type === "core") {
|
| 55 |
+
color_picker = false;
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
function handle_color_change(color: string): void {
|
| 60 |
+
if (editing_index === null) return;
|
| 61 |
+
recent_colors[editing_index] = color;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
$: handle_color_change(selected_color);
|
| 65 |
+
let width = 0;
|
| 66 |
+
let height = 0;
|
| 67 |
+
|
| 68 |
+
function debounce(
|
| 69 |
+
func: (...args: any[]) => void,
|
| 70 |
+
delay: number
|
| 71 |
+
): (...args: any[]) => void {
|
| 72 |
+
let timeout: NodeJS.Timeout;
|
| 73 |
+
return function (...args: any[]): void {
|
| 74 |
+
clearTimeout(timeout);
|
| 75 |
+
timeout = setTimeout(() => func(...args), delay);
|
| 76 |
+
};
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
export let preview = false;
|
| 80 |
+
function handle_preview(): void {
|
| 81 |
+
if (!preview) {
|
| 82 |
+
preview = true;
|
| 83 |
+
debounced_close_preview();
|
| 84 |
+
} else {
|
| 85 |
+
debounced_close_preview();
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
function handle_close_preview(): void {
|
| 90 |
+
preview = false;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
const debounced_close_preview = debounce(handle_close_preview, 1000);
|
| 94 |
+
|
| 95 |
+
$: (selected_size, selected_color, handle_preview());
|
| 96 |
+
|
| 97 |
+
function handle_select(color: string): void {
|
| 98 |
+
selected_color = color;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
function handle_add_color(): void {
|
| 102 |
+
// limit to 5
|
| 103 |
+
if (recent_colors.length >= 5) {
|
| 104 |
+
recent_colors.pop();
|
| 105 |
+
}
|
| 106 |
+
// check if the color is already in the array
|
| 107 |
+
if (
|
| 108 |
+
recent_colors.some((color) => {
|
| 109 |
+
if (Array.isArray(color)) {
|
| 110 |
+
return color[0] === selected_color && color[1] === selected_opacity;
|
| 111 |
+
}
|
| 112 |
+
return color === selected_color;
|
| 113 |
+
})
|
| 114 |
+
) {
|
| 115 |
+
return;
|
| 116 |
+
}
|
| 117 |
+
recent_colors.push([selected_color, selected_opacity]);
|
| 118 |
+
recent_colors = recent_colors;
|
| 119 |
+
}
|
| 120 |
+
</script>
|
| 121 |
+
|
| 122 |
+
<svelte:window bind:innerHeight={height} bind:innerWidth={width} />
|
| 123 |
+
|
| 124 |
+
<div
|
| 125 |
+
class="wrap"
|
| 126 |
+
class:padded={!color_picker}
|
| 127 |
+
use:click_outside={() => dispatch("click_outside")}
|
| 128 |
+
class:color_picker
|
| 129 |
+
class:size_picker={show_size && mode === "brush"}
|
| 130 |
+
class:eraser_picker={mode === "eraser"}
|
| 131 |
+
>
|
| 132 |
+
{#if color_mode === "defaults"}
|
| 133 |
+
{#if color_picker}
|
| 134 |
+
<ColorPicker
|
| 135 |
+
bind:color={selected_color}
|
| 136 |
+
bind:opacity={selected_opacity}
|
| 137 |
+
/>
|
| 138 |
+
<ColorField
|
| 139 |
+
bind:current_mode
|
| 140 |
+
color={selected_color}
|
| 141 |
+
on:close={() => (color_picker = false)}
|
| 142 |
+
on:selected={({ detail }) => handle_select(detail)}
|
| 143 |
+
/>
|
| 144 |
+
{/if}
|
| 145 |
+
{/if}
|
| 146 |
+
{#if show_swatch}
|
| 147 |
+
<ColorSwatch
|
| 148 |
+
bind:color_picker
|
| 149 |
+
{colors}
|
| 150 |
+
on:select={({ detail }) => handle_color_selection(detail, "core")}
|
| 151 |
+
on:edit={({ detail }) => handle_color_selection(detail, "user")}
|
| 152 |
+
user_colors={color_mode === "defaults" ? recent_colors : null}
|
| 153 |
+
{selected_color}
|
| 154 |
+
{current_mode}
|
| 155 |
+
on:add_color={handle_add_color}
|
| 156 |
+
/>
|
| 157 |
+
{/if}
|
| 158 |
+
|
| 159 |
+
{#if show_size}
|
| 160 |
+
<BrushSize max={100} min={1} bind:selected_size />
|
| 161 |
+
{/if}
|
| 162 |
+
</div>
|
| 163 |
+
|
| 164 |
+
<style>
|
| 165 |
+
.wrap {
|
| 166 |
+
max-width: 230px;
|
| 167 |
+
/* width: 90%; */
|
| 168 |
+
position: absolute;
|
| 169 |
+
display: flex;
|
| 170 |
+
flex-direction: column;
|
| 171 |
+
gap: 5px;
|
| 172 |
+
background: var(--background-fill-secondary);
|
| 173 |
+
border: 1px solid var(--block-border-color);
|
| 174 |
+
border-radius: var(--radius-md);
|
| 175 |
+
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.1);
|
| 176 |
+
padding-bottom: var(--size-2);
|
| 177 |
+
pointer-events: all;
|
| 178 |
+
cursor: default;
|
| 179 |
+
z-index: var(--layer-top);
|
| 180 |
+
overflow: hidden;
|
| 181 |
+
top: 0;
|
| 182 |
+
|
| 183 |
+
transform: translate(35px, -12px);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.color_picker {
|
| 187 |
+
transform: translate(35px, -50%);
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.size_picker {
|
| 191 |
+
transform: translate(35px, 18px);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.eraser_picker {
|
| 195 |
+
transform: translate(35px, -7px);
|
| 196 |
+
}
|
| 197 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/brush/BrushSize.svelte
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { click_outside } from "../utils/events";
|
| 3 |
+
import { createEventDispatcher } from "svelte";
|
| 4 |
+
import { BrushSize } from "@gradio/icons";
|
| 5 |
+
|
| 6 |
+
export let selected_size: number;
|
| 7 |
+
export let min: number;
|
| 8 |
+
export let max: number;
|
| 9 |
+
|
| 10 |
+
const dispatch = createEventDispatcher<{
|
| 11 |
+
click_outside: void;
|
| 12 |
+
}>();
|
| 13 |
+
</script>
|
| 14 |
+
|
| 15 |
+
<div class="wrap" use:click_outside={() => dispatch("click_outside")}>
|
| 16 |
+
<span>
|
| 17 |
+
<BrushSize />
|
| 18 |
+
</span>
|
| 19 |
+
<input type="range" bind:value={selected_size} {min} {max} step={1} />
|
| 20 |
+
</div>
|
| 21 |
+
|
| 22 |
+
<style>
|
| 23 |
+
.wrap {
|
| 24 |
+
width: 230px;
|
| 25 |
+
display: flex;
|
| 26 |
+
gap: var(--size-4);
|
| 27 |
+
background: var(--background-fill-secondary);
|
| 28 |
+
padding: 0 var(--size-4);
|
| 29 |
+
cursor: default;
|
| 30 |
+
padding-top: var(--size-2-5);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
input {
|
| 34 |
+
width: 100%;
|
| 35 |
+
}
|
| 36 |
+
span {
|
| 37 |
+
width: 26px;
|
| 38 |
+
color: var(--body-text-color);
|
| 39 |
+
}
|
| 40 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/brush/ColorField.svelte
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 3 |
+
import { createEventDispatcher } from "svelte";
|
| 4 |
+
|
| 5 |
+
import tinycolor from "tinycolor2";
|
| 6 |
+
import IconButton from "../IconButton.svelte";
|
| 7 |
+
export let color: string;
|
| 8 |
+
|
| 9 |
+
export let current_mode: "hex" | "rgb" | "hsl" = "hex";
|
| 10 |
+
|
| 11 |
+
const dispatch = createEventDispatcher<{
|
| 12 |
+
selected: string;
|
| 13 |
+
close: void;
|
| 14 |
+
add_color: void;
|
| 15 |
+
}>();
|
| 16 |
+
const modes = [
|
| 17 |
+
["Hex", "hex"],
|
| 18 |
+
["RGB", "rgb"],
|
| 19 |
+
["HSL", "hsl"]
|
| 20 |
+
] as const;
|
| 21 |
+
|
| 22 |
+
function format_color(color: string, mode: "hex" | "rgb" | "hsl"): string {
|
| 23 |
+
if (mode === "hex") {
|
| 24 |
+
return tinycolor(color).toHexString();
|
| 25 |
+
} else if (mode === "rgb") {
|
| 26 |
+
return tinycolor(color).toRgbString();
|
| 27 |
+
}
|
| 28 |
+
return tinycolor(color).toHslString();
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
$: color_string = format_color(color, current_mode);
|
| 32 |
+
$: color_string && dispatch("selected", color_string);
|
| 33 |
+
|
| 34 |
+
function request_eyedropper(): void {
|
| 35 |
+
// @ts-ignore
|
| 36 |
+
const eyeDropper = new EyeDropper();
|
| 37 |
+
|
| 38 |
+
eyeDropper.open().then((result: { sRGBHex: string }) => {
|
| 39 |
+
color = result.sRGBHex;
|
| 40 |
+
});
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
//@ts-ignore
|
| 44 |
+
const eyedropper_supported = !!window.EyeDropper;
|
| 45 |
+
|
| 46 |
+
function handle_click(): void {
|
| 47 |
+
dispatch("selected", color_string);
|
| 48 |
+
dispatch("close");
|
| 49 |
+
}
|
| 50 |
+
</script>
|
| 51 |
+
|
| 52 |
+
<div class="input">
|
| 53 |
+
<button class="swatch" style:background={color} on:click={handle_click}
|
| 54 |
+
></button>
|
| 55 |
+
<div>
|
| 56 |
+
<div class="input-wrap">
|
| 57 |
+
<input
|
| 58 |
+
type="text"
|
| 59 |
+
value={color_string}
|
| 60 |
+
on:change={(e) => (color = e.currentTarget.value)}
|
| 61 |
+
/>
|
| 62 |
+
|
| 63 |
+
<button class="eyedropper" on:click={request_eyedropper}>
|
| 64 |
+
{#if eyedropper_supported}
|
| 65 |
+
<Eyedropper />
|
| 66 |
+
{/if}
|
| 67 |
+
</button>
|
| 68 |
+
</div>
|
| 69 |
+
<div class="buttons">
|
| 70 |
+
{#each modes as [label, value]}
|
| 71 |
+
<button
|
| 72 |
+
class="button"
|
| 73 |
+
class:active={current_mode === value}
|
| 74 |
+
on:click={() => (current_mode = value)}>{label}</button
|
| 75 |
+
>
|
| 76 |
+
{/each}
|
| 77 |
+
</div>
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
|
| 81 |
+
<style>
|
| 82 |
+
.input {
|
| 83 |
+
display: flex;
|
| 84 |
+
align-items: center;
|
| 85 |
+
padding: 0 10px 15px;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.input input {
|
| 89 |
+
height: 30px;
|
| 90 |
+
width: 100%;
|
| 91 |
+
flex-shrink: 1;
|
| 92 |
+
border-bottom-left-radius: 0;
|
| 93 |
+
border: 1px solid var(--block-border-color);
|
| 94 |
+
letter-spacing: -0.05rem;
|
| 95 |
+
border-left: none;
|
| 96 |
+
border-right: none;
|
| 97 |
+
font-family: var(--font-mono);
|
| 98 |
+
font-size: var(--scale-000);
|
| 99 |
+
padding-left: 15px;
|
| 100 |
+
padding-right: 0;
|
| 101 |
+
background-color: var(--background-fill-secondary);
|
| 102 |
+
color: var(--block-label-text-color);
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
.swatch {
|
| 106 |
+
width: 50px;
|
| 107 |
+
height: 50px;
|
| 108 |
+
border-top-left-radius: 15px;
|
| 109 |
+
border-bottom-left-radius: 15px;
|
| 110 |
+
flex-shrink: 0;
|
| 111 |
+
border: 1px solid var(--block-border-color);
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.buttons button {
|
| 115 |
+
display: flex;
|
| 116 |
+
align-items: center;
|
| 117 |
+
justify-content: center;
|
| 118 |
+
border: 1px solid var(--block-border-color);
|
| 119 |
+
background: var(--background-fill-secondary);
|
| 120 |
+
padding: 3px 6px;
|
| 121 |
+
font-size: var(--scale-000);
|
| 122 |
+
cursor: pointer;
|
| 123 |
+
border-right: none;
|
| 124 |
+
width: 100%;
|
| 125 |
+
border-top: none;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
.buttons button:first-child {
|
| 129 |
+
border-left: none;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.buttons button:last-child {
|
| 133 |
+
border-bottom-right-radius: 15px;
|
| 134 |
+
border-right: 1px solid var(--block-border-color);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.buttons button:hover {
|
| 138 |
+
background: var(--background-fill-secondary-hover);
|
| 139 |
+
font-weight: var(--weight-bold);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.buttons button.active {
|
| 143 |
+
background: var(--background-fill-secondary);
|
| 144 |
+
font-weight: var(--weight-bold);
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
.buttons {
|
| 148 |
+
height: 20px;
|
| 149 |
+
|
| 150 |
+
display: flex;
|
| 151 |
+
justify-content: stretch;
|
| 152 |
+
gap: 0px;
|
| 153 |
+
/* padding: 0 10px; */
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.input-wrap {
|
| 157 |
+
display: flex;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.eyedropper {
|
| 161 |
+
display: flex;
|
| 162 |
+
align-items: center;
|
| 163 |
+
justify-content: center;
|
| 164 |
+
width: 25px;
|
| 165 |
+
height: 30px;
|
| 166 |
+
border-top-right-radius: 15px;
|
| 167 |
+
border: 1px solid var(--block-border-color);
|
| 168 |
+
border-left: none;
|
| 169 |
+
background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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { onMount, tick } from "svelte";
|
| 3 |
+
import tinycolor from "tinycolor2";
|
| 4 |
+
import { clamp } from "../utils/pixi";
|
| 5 |
+
|
| 6 |
+
export let color = "rgb(255, 255, 255)";
|
| 7 |
+
|
| 8 |
+
let sl_marker_pos = [0, 0];
|
| 9 |
+
let sl_rect: DOMRect | null = null;
|
| 10 |
+
let sl_moving = false;
|
| 11 |
+
let sl = [0, 0];
|
| 12 |
+
|
| 13 |
+
let hue = 0;
|
| 14 |
+
let hue_marker_pos = 0;
|
| 15 |
+
let hue_rect: DOMRect | null = null;
|
| 16 |
+
let hue_moving = false;
|
| 17 |
+
|
| 18 |
+
let opacity_moving = false;
|
| 19 |
+
|
| 20 |
+
function handle_hue_down(
|
| 21 |
+
event: MouseEvent & { currentTarget: HTMLDivElement }
|
| 22 |
+
): void {
|
| 23 |
+
hue_rect = event.currentTarget.getBoundingClientRect();
|
| 24 |
+
|
| 25 |
+
hue_moving = true;
|
| 26 |
+
update_hue_from_mouse(event.clientX);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
function update_hue_from_mouse(x: number): void {
|
| 30 |
+
if (!hue_rect) return;
|
| 31 |
+
const _x = clamp(x - hue_rect.left, 0, hue_rect.width); // get the x-coordinate relative to the box
|
| 32 |
+
hue_marker_pos = _x;
|
| 33 |
+
const _hue = (_x / hue_rect.width) * 360; // scale the x position to a hue value (0-360)
|
| 34 |
+
|
| 35 |
+
hue = _hue;
|
| 36 |
+
|
| 37 |
+
color = hsva_to_rgba({ h: _hue, s: sl[0], v: sl[1], a: 1 });
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function hsva_to_rgba(hsva: {
|
| 41 |
+
h: number;
|
| 42 |
+
s: number;
|
| 43 |
+
v: number;
|
| 44 |
+
a: number;
|
| 45 |
+
}): string {
|
| 46 |
+
const saturation = hsva.s;
|
| 47 |
+
const value = hsva.v;
|
| 48 |
+
let chroma = saturation * value;
|
| 49 |
+
const hue_by_60 = hsva.h / 60;
|
| 50 |
+
let x = chroma * (1 - Math.abs((hue_by_60 % 2) - 1));
|
| 51 |
+
const m = value - chroma;
|
| 52 |
+
|
| 53 |
+
chroma = chroma + m;
|
| 54 |
+
x = x + m;
|
| 55 |
+
|
| 56 |
+
const index = Math.floor(hue_by_60) % 6;
|
| 57 |
+
const red = [chroma, x, m, m, x, chroma][index];
|
| 58 |
+
const green = [x, chroma, chroma, x, m, m][index];
|
| 59 |
+
const blue = [m, m, x, chroma, chroma, x][index];
|
| 60 |
+
|
| 61 |
+
return `rgba(${red * 255}, ${green * 255}, ${blue * 255}, ${hsva.a})`;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function update_color_from_mouse(x: number, y: number): void {
|
| 65 |
+
if (!sl_rect) return;
|
| 66 |
+
const _x = clamp(x - sl_rect.left, 0, sl_rect.width);
|
| 67 |
+
const _y = clamp(y - sl_rect.top, 0, sl_rect.height);
|
| 68 |
+
sl_marker_pos = [_x, _y];
|
| 69 |
+
const _hsva = {
|
| 70 |
+
h: hue * 1,
|
| 71 |
+
s: _x / sl_rect.width,
|
| 72 |
+
v: 1 - _y / sl_rect.height,
|
| 73 |
+
a: 1
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
sl = [_hsva.s, _hsva.v];
|
| 77 |
+
|
| 78 |
+
color = hsva_to_rgba(_hsva);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
function handle_sl_down(
|
| 82 |
+
event: MouseEvent & { currentTarget: HTMLDivElement }
|
| 83 |
+
): void {
|
| 84 |
+
sl_moving = true;
|
| 85 |
+
sl_rect = event.currentTarget.getBoundingClientRect();
|
| 86 |
+
update_color_from_mouse(event.clientX, event.clientY);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
function handle_move(
|
| 90 |
+
event: MouseEvent & { currentTarget: EventTarget & Window }
|
| 91 |
+
): void {
|
| 92 |
+
if (sl_moving) update_color_from_mouse(event.clientX, event.clientY);
|
| 93 |
+
if (hue_moving) update_hue_from_mouse(event.clientX);
|
| 94 |
+
if (opacity_moving) update_opacity_from_mouse(event.clientX);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
function handle_end(): void {
|
| 98 |
+
if (sl_moving) sl_moving = false;
|
| 99 |
+
if (hue_moving) hue_moving = false;
|
| 100 |
+
if (opacity_moving) opacity_moving = false;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
async function update_mouse_from_color(color: string): Promise<void> {
|
| 104 |
+
if (sl_moving || hue_moving) return;
|
| 105 |
+
await tick();
|
| 106 |
+
if (!color) return;
|
| 107 |
+
if (!sl_rect) {
|
| 108 |
+
sl_rect = sl_wrap.getBoundingClientRect();
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
if (!hue_rect) {
|
| 112 |
+
hue_rect = hue_wrap.getBoundingClientRect();
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
const hsva = tinycolor(color).toHsv();
|
| 116 |
+
const _x = hsva.s * sl_rect!.width;
|
| 117 |
+
const _y = (1 - hsva.v) * sl_rect!.height;
|
| 118 |
+
sl_marker_pos = [_x, _y];
|
| 119 |
+
sl = [hsva.s, hsva.v];
|
| 120 |
+
hue = hsva.h;
|
| 121 |
+
hue_marker_pos = (hsva.h / 360) * hue_rect!.width;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
$: update_mouse_from_color(color);
|
| 125 |
+
|
| 126 |
+
let sl_wrap: HTMLDivElement;
|
| 127 |
+
let hue_wrap: HTMLDivElement;
|
| 128 |
+
|
| 129 |
+
export let opacity = 1;
|
| 130 |
+
|
| 131 |
+
let opacity_wrap: HTMLDivElement;
|
| 132 |
+
|
| 133 |
+
function handle_opacity_down(
|
| 134 |
+
event: MouseEvent & { currentTarget: HTMLDivElement }
|
| 135 |
+
): void {
|
| 136 |
+
opacity_rect = event.currentTarget.getBoundingClientRect();
|
| 137 |
+
opacity_moving = true;
|
| 138 |
+
update_opacity_from_mouse(event.clientX);
|
| 139 |
+
}
|
| 140 |
+
let opacity_rect: DOMRect | null = null;
|
| 141 |
+
|
| 142 |
+
function update_opacity_from_mouse(x: number): void {
|
| 143 |
+
if (!opacity_rect) return;
|
| 144 |
+
const _x = clamp(x - opacity_rect.left, 0, opacity_rect.width);
|
| 145 |
+
opacity = _x / opacity_rect.width;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
onMount(() => {
|
| 149 |
+
opacity_rect = opacity_wrap.getBoundingClientRect();
|
| 150 |
+
});
|
| 151 |
+
</script>
|
| 152 |
+
|
| 153 |
+
<svelte:window on:mousemove={handle_move} on:mouseup={handle_end} />
|
| 154 |
+
|
| 155 |
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
| 156 |
+
<div
|
| 157 |
+
class="color-gradient"
|
| 158 |
+
on:mousedown={handle_sl_down}
|
| 159 |
+
style="--hue:{hue}"
|
| 160 |
+
bind:this={sl_wrap}
|
| 161 |
+
>
|
| 162 |
+
<div
|
| 163 |
+
class="marker"
|
| 164 |
+
style:transform="translate({sl_marker_pos[0]}px,{sl_marker_pos[1]}px)"
|
| 165 |
+
style:background={color}
|
| 166 |
+
/>
|
| 167 |
+
</div>
|
| 168 |
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
| 169 |
+
<div
|
| 170 |
+
class="opacity-slider"
|
| 171 |
+
on:mousedown={handle_opacity_down}
|
| 172 |
+
style="--color:{color === 'auto' ? 'transparent' : color}"
|
| 173 |
+
bind:this={opacity_wrap}
|
| 174 |
+
>
|
| 175 |
+
<div
|
| 176 |
+
class="opacity-marker"
|
| 177 |
+
style:background="white"
|
| 178 |
+
style:transform="translateX({opacity * (opacity_rect?.width ?? 1)}px)"
|
| 179 |
+
/>
|
| 180 |
+
</div>
|
| 181 |
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
| 182 |
+
<div class="hue-slider" on:mousedown={handle_hue_down} bind:this={hue_wrap}>
|
| 183 |
+
<div
|
| 184 |
+
class="marker"
|
| 185 |
+
style:background={"hsl(" + hue + ", 100%, 50%)"}
|
| 186 |
+
style:transform="translateX({hue_marker_pos}px)"
|
| 187 |
+
/>
|
| 188 |
+
</div>
|
| 189 |
+
|
| 190 |
+
<style>
|
| 191 |
+
.hue-slider {
|
| 192 |
+
position: relative;
|
| 193 |
+
width: 90%;
|
| 194 |
+
margin: 0px auto 10px auto;
|
| 195 |
+
height: 12px;
|
| 196 |
+
border-radius: 2px;
|
| 197 |
+
border: 1px solid var(--block-border-color);
|
| 198 |
+
background: linear-gradient(
|
| 199 |
+
to right,
|
| 200 |
+
hsl(0, 100%, 50%) 0%,
|
| 201 |
+
#ff0 17%,
|
| 202 |
+
lime 33%,
|
| 203 |
+
cyan 50%,
|
| 204 |
+
blue 67%,
|
| 205 |
+
magenta 83%,
|
| 206 |
+
red 100%
|
| 207 |
+
);
|
| 208 |
+
border-top: none;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.opacity-slider {
|
| 212 |
+
position: relative;
|
| 213 |
+
width: 90%;
|
| 214 |
+
margin: 10px auto 5px auto;
|
| 215 |
+
height: 12px;
|
| 216 |
+
border-radius: 2px;
|
| 217 |
+
border: 1px solid var(--block-border-color);
|
| 218 |
+
/* background: linear-gradient(45deg, #808080 25%, transparent 25%),
|
| 219 |
+
linear-gradient(-45deg, #808080 25%, transparent 25%),
|
| 220 |
+
linear-gradient(45deg, transparent 75%, #808080 75%),
|
| 221 |
+
linear-gradient(-45deg, transparent 75%, #808080 75%); */
|
| 222 |
+
background:
|
| 223 |
+
linear-gradient(to right, transparent, var(--color)),
|
| 224 |
+
repeating-conic-gradient(#808080 0% 25%, transparent 0% 50%) 0 0 / 10px
|
| 225 |
+
10px;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.opacity-marker {
|
| 229 |
+
position: absolute;
|
| 230 |
+
width: 16px;
|
| 231 |
+
height: 16px;
|
| 232 |
+
border-radius: 50%;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
.opacity-slider > .opacity-marker {
|
| 236 |
+
box-shadow: 0 0 1px #888;
|
| 237 |
+
top: -4px;
|
| 238 |
+
margin: auto;
|
| 239 |
+
height: 18px;
|
| 240 |
+
width: 18px;
|
| 241 |
+
left: -9px;
|
| 242 |
+
margin: auto;
|
| 243 |
+
border: 1px solid var(--block-border-color);
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
.color-gradient {
|
| 247 |
+
position: relative;
|
| 248 |
+
--hue: white;
|
| 249 |
+
|
| 250 |
+
background:
|
| 251 |
+
linear-gradient(rgba(0, 0, 0, 0), #000),
|
| 252 |
+
linear-gradient(90deg, #fff, hsl(var(--hue), 100%, 50%));
|
| 253 |
+
width: 100%;
|
| 254 |
+
height: 150px;
|
| 255 |
+
border-top-left-radius: 4px;
|
| 256 |
+
border-top-right-radius: 4px;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
.marker {
|
| 260 |
+
position: absolute;
|
| 261 |
+
width: 14px;
|
| 262 |
+
height: 14px;
|
| 263 |
+
border-radius: 50%;
|
| 264 |
+
border: 2px solid white;
|
| 265 |
+
|
| 266 |
+
top: -7px;
|
| 267 |
+
left: -7px;
|
| 268 |
+
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
|
| 269 |
+
pointer-events: none;
|
| 270 |
+
border: 1px solid var(--block-border-color);
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
.hue-slider > .marker {
|
| 274 |
+
box-shadow: 0 0 1px #888;
|
| 275 |
+
top: -3px;
|
| 276 |
+
margin: auto;
|
| 277 |
+
height: 18px;
|
| 278 |
+
width: 18px;
|
| 279 |
+
left: -9px;
|
| 280 |
+
|
| 281 |
+
border: 1px solid var(--block-border-color);
|
| 282 |
+
}
|
| 283 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/brush/ColorSwatch.svelte
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import tinycolor from "tinycolor2";
|
| 3 |
+
import { createEventDispatcher } from "svelte";
|
| 4 |
+
import type { ColorTuple } from "./types";
|
| 5 |
+
import type { ColorInput } from "tinycolor2";
|
| 6 |
+
import IconButton from "../IconButton.svelte";
|
| 7 |
+
import { Plus } from "@gradio/icons";
|
| 8 |
+
|
| 9 |
+
export let selected_color: string;
|
| 10 |
+
export let colors: (ColorInput | ColorTuple)[];
|
| 11 |
+
export let user_colors: (ColorInput | ColorTuple)[] | null = [];
|
| 12 |
+
|
| 13 |
+
export let show_empty = false;
|
| 14 |
+
export let current_mode: "hex" | "rgb" | "hsl" = "hex";
|
| 15 |
+
export let color_picker = false;
|
| 16 |
+
const dispatch = createEventDispatcher<{
|
| 17 |
+
select: { index: number | null; color: string | null; opacity?: number };
|
| 18 |
+
edit: { index: number; color: string | null };
|
| 19 |
+
add_color: void;
|
| 20 |
+
}>();
|
| 21 |
+
|
| 22 |
+
$: _colors = show_empty ? colors : colors.filter((c) => c);
|
| 23 |
+
|
| 24 |
+
function get_color(color_input: ColorInput | ColorTuple): string {
|
| 25 |
+
if (Array.isArray(color_input)) {
|
| 26 |
+
return color_input[0] as string;
|
| 27 |
+
}
|
| 28 |
+
return color_input as string;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function get_opacity(
|
| 32 |
+
color_input: ColorInput | ColorTuple
|
| 33 |
+
): number | undefined {
|
| 34 |
+
if (Array.isArray(color_input)) {
|
| 35 |
+
return color_input[1];
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
const color = tinycolor(color_input);
|
| 39 |
+
return color.getAlpha();
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
function get_formatted_color(
|
| 43 |
+
color: ColorInput | ColorTuple,
|
| 44 |
+
mode: "hex" | "rgb" | "hsl"
|
| 45 |
+
): string {
|
| 46 |
+
const color_value = get_color(color);
|
| 47 |
+
if (mode === "hex") {
|
| 48 |
+
return tinycolor(color_value).toHexString();
|
| 49 |
+
} else if (mode === "rgb") {
|
| 50 |
+
return tinycolor(color_value).toRgbString();
|
| 51 |
+
}
|
| 52 |
+
return tinycolor(color_value).toHslString();
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
let current_index = `select-${colors.findIndex(
|
| 56 |
+
(c) =>
|
| 57 |
+
get_formatted_color(c, current_mode) ===
|
| 58 |
+
get_formatted_color(selected_color, current_mode)
|
| 59 |
+
)}`;
|
| 60 |
+
|
| 61 |
+
$: selected_opacity = get_opacity(selected_color);
|
| 62 |
+
|
| 63 |
+
function handle_select(
|
| 64 |
+
type: "edit" | "select",
|
| 65 |
+
detail: {
|
| 66 |
+
index: number;
|
| 67 |
+
color: string | null;
|
| 68 |
+
opacity?: number;
|
| 69 |
+
}
|
| 70 |
+
): void {
|
| 71 |
+
current_index = `${type}-${detail.index}`;
|
| 72 |
+
dispatch(type, detail);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
function handle_picker_click(): void {
|
| 76 |
+
dispatch("select", { index: null, color: selected_color });
|
| 77 |
+
color_picker = !color_picker;
|
| 78 |
+
}
|
| 79 |
+
</script>
|
| 80 |
+
|
| 81 |
+
{#if !color_picker}
|
| 82 |
+
<span class:lg={user_colors}></span>
|
| 83 |
+
{/if}
|
| 84 |
+
|
| 85 |
+
<div class="swatch-wrap">
|
| 86 |
+
<div class="swatch-container">
|
| 87 |
+
{#if user_colors}
|
| 88 |
+
<div class="swatch">
|
| 89 |
+
{#if color_picker}
|
| 90 |
+
<IconButton
|
| 91 |
+
Icon={Plus}
|
| 92 |
+
on:click={() => dispatch("add_color")}
|
| 93 |
+
roundedness="very"
|
| 94 |
+
background={get_color(selected_color)}
|
| 95 |
+
color="white"
|
| 96 |
+
/>
|
| 97 |
+
{/if}
|
| 98 |
+
{#each user_colors as color, i}
|
| 99 |
+
{@const color_string = get_color(color)}
|
| 100 |
+
{@const opacity = get_opacity(color)}
|
| 101 |
+
<button
|
| 102 |
+
on:click={() =>
|
| 103 |
+
handle_select("edit", {
|
| 104 |
+
index: i,
|
| 105 |
+
color: color_string,
|
| 106 |
+
opacity: opacity
|
| 107 |
+
})}
|
| 108 |
+
class="color"
|
| 109 |
+
class:empty={color === null}
|
| 110 |
+
style="background-color: {color_string}"
|
| 111 |
+
style:opacity
|
| 112 |
+
class:selected={`${color_string}-${opacity}` ===
|
| 113 |
+
`${selected_color}-${selected_opacity}`}
|
| 114 |
+
></button>
|
| 115 |
+
{/each}
|
| 116 |
+
|
| 117 |
+
<button
|
| 118 |
+
on:click={handle_picker_click}
|
| 119 |
+
class="color colorpicker"
|
| 120 |
+
class:hidden={!color_picker}
|
| 121 |
+
></button>
|
| 122 |
+
</div>
|
| 123 |
+
{/if}
|
| 124 |
+
<menu class="swatch">
|
| 125 |
+
{#each _colors as color_item, i}
|
| 126 |
+
{@const color_string = get_color(color_item)}
|
| 127 |
+
{@const opacity = get_opacity(color_item)}
|
| 128 |
+
<button
|
| 129 |
+
on:click={() =>
|
| 130 |
+
handle_select("select", {
|
| 131 |
+
index: i,
|
| 132 |
+
color: color_string,
|
| 133 |
+
opacity: opacity
|
| 134 |
+
})}
|
| 135 |
+
class="color"
|
| 136 |
+
class:empty={color_item === null}
|
| 137 |
+
style="background-color: {color_string}"
|
| 138 |
+
style:opacity
|
| 139 |
+
class:selected={`${color_string}-${opacity}` ===
|
| 140 |
+
`${selected_color}-${selected_opacity}`}
|
| 141 |
+
></button>
|
| 142 |
+
{/each}
|
| 143 |
+
</menu>
|
| 144 |
+
</div>
|
| 145 |
+
</div>
|
| 146 |
+
|
| 147 |
+
<style>
|
| 148 |
+
.swatch-wrap {
|
| 149 |
+
display: flex;
|
| 150 |
+
justify-content: space-between;
|
| 151 |
+
align-items: flex-start;
|
| 152 |
+
/* max-width: 200px; */
|
| 153 |
+
width: 205px;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
span.lg {
|
| 157 |
+
margin-top: var(--spacing-sm);
|
| 158 |
+
}
|
| 159 |
+
.swatch-container {
|
| 160 |
+
display: flex;
|
| 161 |
+
flex-direction: column;
|
| 162 |
+
gap: 10px;
|
| 163 |
+
width: 100%;
|
| 164 |
+
}
|
| 165 |
+
.swatch {
|
| 166 |
+
display: grid;
|
| 167 |
+
grid-template-columns: repeat(auto-fill, 25px);
|
| 168 |
+
gap: 8px;
|
| 169 |
+
justify-content: flex-start;
|
| 170 |
+
|
| 171 |
+
overflow: hidden;
|
| 172 |
+
justify-content: center;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.empty {
|
| 176 |
+
border: 1px solid var(--block-border-color);
|
| 177 |
+
font-weight: var(--weight-bold);
|
| 178 |
+
display: flex;
|
| 179 |
+
justify-content: center;
|
| 180 |
+
align-items: center;
|
| 181 |
+
padding-top: 4px;
|
| 182 |
+
text-align: center;
|
| 183 |
+
font-size: var(--scale-0);
|
| 184 |
+
cursor: pointer;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.empty::after {
|
| 188 |
+
content: "+";
|
| 189 |
+
margin-bottom: var(--size-1);
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.color {
|
| 193 |
+
width: 25px;
|
| 194 |
+
height: 25px;
|
| 195 |
+
border-radius: var(--radius-md);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.empty.selected,
|
| 199 |
+
.color.selected {
|
| 200 |
+
border: 2px solid var(--color-accent);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
.colorpicker {
|
| 204 |
+
border: 1px solid var(--block-border-color);
|
| 205 |
+
|
| 206 |
+
background: conic-gradient(
|
| 207 |
+
red,
|
| 208 |
+
#ff0 60deg,
|
| 209 |
+
lime 120deg,
|
| 210 |
+
cyan 180deg,
|
| 211 |
+
blue 240deg,
|
| 212 |
+
magenta 300deg,
|
| 213 |
+
red 360deg
|
| 214 |
+
);
|
| 215 |
+
overflow: hidden;
|
| 216 |
+
position: relative;
|
| 217 |
+
color: white;
|
| 218 |
+
cursor: pointer;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
.colorpicker::before {
|
| 222 |
+
content: "";
|
| 223 |
+
position: absolute;
|
| 224 |
+
width: 100%;
|
| 225 |
+
height: 100%;
|
| 226 |
+
background: rgba(0, 0, 0, 0.6);
|
| 227 |
+
opacity: 1;
|
| 228 |
+
transition: 0.1s;
|
| 229 |
+
top: 0;
|
| 230 |
+
left: 0;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
.colorpicker::after {
|
| 234 |
+
display: flex;
|
| 235 |
+
justify-content: center;
|
| 236 |
+
align-items: center;
|
| 237 |
+
content: "⨯";
|
| 238 |
+
color: white;
|
| 239 |
+
font-size: var(--scale-5);
|
| 240 |
+
position: absolute;
|
| 241 |
+
width: 100%;
|
| 242 |
+
height: 100%;
|
| 243 |
+
transform: translateY(-7px);
|
| 244 |
+
opacity: 1;
|
| 245 |
+
transition: 0.1s;
|
| 246 |
+
top: 0;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
.colorpicker.hidden::after,
|
| 250 |
+
.colorpicker.hidden::before {
|
| 251 |
+
opacity: 0;
|
| 252 |
+
}
|
| 253 |
+
</style>
|
6.0.0-dev.5/imageeditor/shared/brush/brush-cursor.ts
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Container,
|
| 3 |
+
Graphics,
|
| 4 |
+
Sprite,
|
| 5 |
+
type FederatedPointerEvent
|
| 6 |
+
} from "pixi.js";
|
| 7 |
+
import { type ImageEditorContext } from "../core/editor";
|
| 8 |
+
import { type BrushState } from "./types";
|
| 9 |
+
import tinycolor from "tinycolor2";
|
| 10 |
+
import { clear_timeout } from "./brush-utils";
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Class to handle all cursor preview-related functionality for the brush tool.
|
| 14 |
+
*/
|
| 15 |
+
export class BrushCursor {
|
| 16 |
+
private cursor_graphics: Graphics | null = null;
|
| 17 |
+
private cursor_container: Container | null = null;
|
| 18 |
+
private brush_preview_container: Container | null = null;
|
| 19 |
+
private brush_preview_graphics: Graphics | null = null;
|
| 20 |
+
private is_preview_visible = false;
|
| 21 |
+
private is_cursor_over_image = false;
|
| 22 |
+
private cursor_position_check_timeout: number | null = null;
|
| 23 |
+
private is_brush_or_erase_active = false;
|
| 24 |
+
|
| 25 |
+
private _bound_update_cursor:
|
| 26 |
+
| ((event: FederatedPointerEvent) => void)
|
| 27 |
+
| null = null;
|
| 28 |
+
private _bound_check_cursor_over_image:
|
| 29 |
+
| ((event: FederatedPointerEvent) => void)
|
| 30 |
+
| null = null;
|
| 31 |
+
|
| 32 |
+
constructor(
|
| 33 |
+
private image_editor_context: ImageEditorContext,
|
| 34 |
+
private state: BrushState,
|
| 35 |
+
private scale: number
|
| 36 |
+
) {
|
| 37 |
+
this.initialize_cursor();
|
| 38 |
+
this.initialize_brush_preview();
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/**
|
| 42 |
+
* Returns whether the cursor is currently over the image.
|
| 43 |
+
*/
|
| 44 |
+
is_over_image(): boolean {
|
| 45 |
+
return this.is_cursor_over_image;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/**
|
| 49 |
+
* Sets up cursor-related event listeners.
|
| 50 |
+
*/
|
| 51 |
+
setup_event_listeners(): void {
|
| 52 |
+
this.cleanup_event_listeners();
|
| 53 |
+
|
| 54 |
+
this._bound_update_cursor = this.update_cursor_position.bind(this);
|
| 55 |
+
this._bound_check_cursor_over_image =
|
| 56 |
+
this.check_cursor_over_image.bind(this);
|
| 57 |
+
|
| 58 |
+
const stage = this.image_editor_context.app.stage;
|
| 59 |
+
stage.on("pointermove", this._bound_update_cursor);
|
| 60 |
+
stage.on("pointermove", this._bound_check_cursor_over_image);
|
| 61 |
+
|
| 62 |
+
this.image_editor_context.image_container.on(
|
| 63 |
+
"pointerenter",
|
| 64 |
+
this.on_image_container_pointer_enter.bind(this)
|
| 65 |
+
);
|
| 66 |
+
this.image_editor_context.image_container.on(
|
| 67 |
+
"pointerleave",
|
| 68 |
+
this.on_image_container_pointer_leave.bind(this)
|
| 69 |
+
);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/**
|
| 73 |
+
* Removes all cursor-related event listeners.
|
| 74 |
+
*/
|
| 75 |
+
cleanup_event_listeners(): void {
|
| 76 |
+
const stage = this.image_editor_context.app.stage;
|
| 77 |
+
|
| 78 |
+
if (this._bound_update_cursor) {
|
| 79 |
+
stage.off("pointermove", this._bound_update_cursor);
|
| 80 |
+
this._bound_update_cursor = null;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
if (this._bound_check_cursor_over_image) {
|
| 84 |
+
stage.off("pointermove", this._bound_check_cursor_over_image);
|
| 85 |
+
this._bound_check_cursor_over_image = null;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
this.image_editor_context.image_container.off("pointerenter");
|
| 89 |
+
this.image_editor_context.image_container.off("pointerleave");
|
| 90 |
+
|
| 91 |
+
this.cursor_position_check_timeout = clear_timeout(
|
| 92 |
+
this.cursor_position_check_timeout
|
| 93 |
+
);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
/**
|
| 97 |
+
* initializes the cursor for the brush tool.
|
| 98 |
+
*/
|
| 99 |
+
initialize_cursor(): void {
|
| 100 |
+
if (this.cursor_container) {
|
| 101 |
+
if (this.cursor_container.parent) {
|
| 102 |
+
this.cursor_container.parent.removeChild(this.cursor_container);
|
| 103 |
+
}
|
| 104 |
+
this.cursor_container.destroy({ children: true });
|
| 105 |
+
this.cursor_container = null;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
this.cursor_container = new Container();
|
| 109 |
+
this.image_editor_context.ui_container.addChild(this.cursor_container);
|
| 110 |
+
|
| 111 |
+
this.cursor_graphics = new Graphics();
|
| 112 |
+
this.cursor_container.addChild(this.cursor_graphics);
|
| 113 |
+
|
| 114 |
+
this.update_cursor_appearance();
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
/**
|
| 118 |
+
* updates the appearance of the cursor.
|
| 119 |
+
*/
|
| 120 |
+
update_cursor_appearance(): void {
|
| 121 |
+
if (!this.cursor_graphics) return;
|
| 122 |
+
|
| 123 |
+
this.cursor_graphics.clear();
|
| 124 |
+
|
| 125 |
+
const cursor_color =
|
| 126 |
+
this.state.mode === "draw"
|
| 127 |
+
? tinycolor(this.state.color).toString()
|
| 128 |
+
: 0xffffff;
|
| 129 |
+
|
| 130 |
+
this.cursor_graphics
|
| 131 |
+
.circle(0, 0, this.state.brush_size * this.scale)
|
| 132 |
+
.stroke({
|
| 133 |
+
width: 1.5,
|
| 134 |
+
color: cursor_color,
|
| 135 |
+
alpha: 0.8
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
this.cursor_graphics.circle(0, 0, 1).fill({
|
| 139 |
+
color: cursor_color,
|
| 140 |
+
alpha: 0.8
|
| 141 |
+
});
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/**
|
| 145 |
+
* updates the position of the cursor.
|
| 146 |
+
* @param {FederatedPointerEvent} event - The pointer event.
|
| 147 |
+
*/
|
| 148 |
+
update_cursor_position(event: FederatedPointerEvent): void {
|
| 149 |
+
if (!this.cursor_container) return;
|
| 150 |
+
|
| 151 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 152 |
+
event.global
|
| 153 |
+
);
|
| 154 |
+
|
| 155 |
+
const ui_pos = this.image_editor_context.ui_container.toLocal(
|
| 156 |
+
this.image_editor_context.image_container.toGlobal(local_pos)
|
| 157 |
+
);
|
| 158 |
+
|
| 159 |
+
this.cursor_container.position.set(ui_pos.x, ui_pos.y);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
/**
|
| 163 |
+
* checks if the cursor is over the image container.
|
| 164 |
+
* @param {FederatedPointerEvent} event - The pointer event.
|
| 165 |
+
*/
|
| 166 |
+
check_cursor_over_image(event: FederatedPointerEvent): void {
|
| 167 |
+
if (this.cursor_position_check_timeout !== null) {
|
| 168 |
+
window.clearTimeout(this.cursor_position_check_timeout);
|
| 169 |
+
this.cursor_position_check_timeout = null;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
const image_container = this.image_editor_context.image_container;
|
| 173 |
+
const bounds = image_container.getBounds();
|
| 174 |
+
|
| 175 |
+
const was_over_image = this.is_cursor_over_image;
|
| 176 |
+
this.is_cursor_over_image =
|
| 177 |
+
event.global.x >= bounds.x &&
|
| 178 |
+
event.global.x <= bounds.x + bounds.width &&
|
| 179 |
+
event.global.y >= bounds.y &&
|
| 180 |
+
event.global.y <= bounds.y + bounds.height;
|
| 181 |
+
|
| 182 |
+
if (was_over_image !== this.is_cursor_over_image) {
|
| 183 |
+
this.update_cursor_and_preview_visibility();
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
/**
|
| 188 |
+
* updates the size of the cursor.
|
| 189 |
+
*/
|
| 190 |
+
update_cursor_size(): void {
|
| 191 |
+
this.update_cursor_appearance();
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
/**
|
| 195 |
+
* Updates whether brush or erase tool is active
|
| 196 |
+
*/
|
| 197 |
+
set_active(is_active: boolean): void {
|
| 198 |
+
this.is_brush_or_erase_active = is_active;
|
| 199 |
+
this.update_cursor_and_preview_visibility();
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
/**
|
| 203 |
+
* updates the visibility of the cursor.
|
| 204 |
+
*/
|
| 205 |
+
update_cursor_visibility(): void {
|
| 206 |
+
if (this.cursor_container) {
|
| 207 |
+
this.cursor_container.visible =
|
| 208 |
+
this.is_cursor_over_image && this.is_brush_or_erase_active;
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
/**
|
| 213 |
+
* Shows or hides the brush preview.
|
| 214 |
+
* @param {boolean} show - Whether to show the preview.
|
| 215 |
+
*/
|
| 216 |
+
preview_brush(show: boolean): void {
|
| 217 |
+
this.is_preview_visible = show;
|
| 218 |
+
|
| 219 |
+
if (this.brush_preview_container) {
|
| 220 |
+
this.brush_preview_container.visible =
|
| 221 |
+
show && this.is_brush_or_erase_active;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
if (show) {
|
| 225 |
+
this.update_brush_preview();
|
| 226 |
+
this.update_brush_preview_position();
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
/**
|
| 231 |
+
* initializes the brush preview.
|
| 232 |
+
*/
|
| 233 |
+
initialize_brush_preview(): void {
|
| 234 |
+
if (this.brush_preview_container) {
|
| 235 |
+
if (this.brush_preview_container.parent) {
|
| 236 |
+
this.brush_preview_container.parent.removeChild(
|
| 237 |
+
this.brush_preview_container
|
| 238 |
+
);
|
| 239 |
+
}
|
| 240 |
+
this.brush_preview_container.destroy({ children: true });
|
| 241 |
+
this.brush_preview_container = null;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
this.brush_preview_container = new Container();
|
| 245 |
+
this.image_editor_context.ui_container.addChild(
|
| 246 |
+
this.brush_preview_container
|
| 247 |
+
);
|
| 248 |
+
|
| 249 |
+
this.brush_preview_graphics = new Graphics();
|
| 250 |
+
this.brush_preview_container.addChild(this.brush_preview_graphics);
|
| 251 |
+
|
| 252 |
+
this.brush_preview_container.visible = false;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
/**
|
| 256 |
+
* updates the brush preview appearance.
|
| 257 |
+
*/
|
| 258 |
+
update_brush_preview(): void {
|
| 259 |
+
if (!this.brush_preview_graphics) return;
|
| 260 |
+
|
| 261 |
+
this.brush_preview_graphics.clear();
|
| 262 |
+
|
| 263 |
+
const preview_size = this.state.brush_size * this.scale;
|
| 264 |
+
const preview_color =
|
| 265 |
+
this.state.mode === "draw"
|
| 266 |
+
? tinycolor(this.state.color).setAlpha(this.state.opacity).toString()
|
| 267 |
+
: 0xffffff;
|
| 268 |
+
|
| 269 |
+
this.brush_preview_graphics.circle(0, 0, preview_size).fill({
|
| 270 |
+
color: preview_color,
|
| 271 |
+
alpha: this.state.mode === "draw" ? this.state.opacity : 0.3
|
| 272 |
+
});
|
| 273 |
+
|
| 274 |
+
this.brush_preview_graphics.circle(0, 0, preview_size + 1).stroke({
|
| 275 |
+
width: 1,
|
| 276 |
+
color: 0x000000,
|
| 277 |
+
alpha: 0.5
|
| 278 |
+
});
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
/**
|
| 282 |
+
* updates the position of the brush preview to center of canvas.
|
| 283 |
+
*/
|
| 284 |
+
update_brush_preview_position(): void {
|
| 285 |
+
if (!this.brush_preview_container) return;
|
| 286 |
+
|
| 287 |
+
const image_container = this.image_editor_context.image_container;
|
| 288 |
+
const center_x = image_container.width / 2;
|
| 289 |
+
const center_y = image_container.height / 2;
|
| 290 |
+
|
| 291 |
+
const global_pos = image_container.toGlobal({ x: center_x, y: center_y });
|
| 292 |
+
|
| 293 |
+
const ui_pos = this.image_editor_context.ui_container.toLocal(global_pos);
|
| 294 |
+
|
| 295 |
+
this.brush_preview_container.position.set(ui_pos.x, ui_pos.y);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
/**
|
| 299 |
+
* Called when the pointer enters the image container.
|
| 300 |
+
*/
|
| 301 |
+
on_image_container_pointer_enter(): void {
|
| 302 |
+
this.is_cursor_over_image = true;
|
| 303 |
+
this.update_cursor_and_preview_visibility();
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
/**
|
| 307 |
+
* Called when the pointer leaves the image container.
|
| 308 |
+
*/
|
| 309 |
+
on_image_container_pointer_leave(): void {
|
| 310 |
+
this.is_cursor_over_image = false;
|
| 311 |
+
this.update_cursor_and_preview_visibility();
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
/**
|
| 315 |
+
* Updates both cursor and preview visibility based on cursor position.
|
| 316 |
+
*/
|
| 317 |
+
update_cursor_and_preview_visibility(): void {
|
| 318 |
+
this.update_cursor_visibility();
|
| 319 |
+
if (this.brush_preview_container) {
|
| 320 |
+
this.brush_preview_container.visible =
|
| 321 |
+
this.is_preview_visible && this.is_brush_or_erase_active;
|
| 322 |
+
}
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
/**
|
| 326 |
+
* Cleans up all cursor resources.
|
| 327 |
+
*/
|
| 328 |
+
cleanup(): void {
|
| 329 |
+
this.is_brush_or_erase_active = false;
|
| 330 |
+
|
| 331 |
+
if (this.cursor_container) {
|
| 332 |
+
if (this.cursor_container.parent) {
|
| 333 |
+
this.cursor_container.parent.removeChild(this.cursor_container);
|
| 334 |
+
}
|
| 335 |
+
this.cursor_container.destroy({ children: true });
|
| 336 |
+
this.cursor_container = null;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
if (this.brush_preview_container) {
|
| 340 |
+
if (this.brush_preview_container.parent) {
|
| 341 |
+
this.brush_preview_container.parent.removeChild(
|
| 342 |
+
this.brush_preview_container
|
| 343 |
+
);
|
| 344 |
+
}
|
| 345 |
+
this.brush_preview_container.destroy({ children: true });
|
| 346 |
+
this.brush_preview_container = null;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
if (this.cursor_position_check_timeout !== null) {
|
| 350 |
+
window.clearTimeout(this.cursor_position_check_timeout);
|
| 351 |
+
this.cursor_position_check_timeout = null;
|
| 352 |
+
}
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
/**
|
| 356 |
+
* Updates the brush state.
|
| 357 |
+
*/
|
| 358 |
+
update_state(state: BrushState, scale: number): void {
|
| 359 |
+
this.state = state;
|
| 360 |
+
this.scale = scale;
|
| 361 |
+
this.update_cursor_appearance();
|
| 362 |
+
if (this.is_preview_visible) {
|
| 363 |
+
this.update_brush_preview();
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
}
|
6.0.0-dev.5/imageeditor/shared/brush/brush-textures.ts
ADDED
|
@@ -0,0 +1,757 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Container,
|
| 3 |
+
RenderTexture,
|
| 4 |
+
Sprite,
|
| 5 |
+
Graphics,
|
| 6 |
+
Application,
|
| 7 |
+
Texture
|
| 8 |
+
} from "pixi.js";
|
| 9 |
+
import { type ImageEditorContext } from "../core/editor";
|
| 10 |
+
import { type Command } from "../core/commands";
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Represents a single drawing segment with all its parameters
|
| 14 |
+
*/
|
| 15 |
+
interface BrushSegment {
|
| 16 |
+
from_x: number;
|
| 17 |
+
from_y: number;
|
| 18 |
+
to_x: number;
|
| 19 |
+
to_y: number;
|
| 20 |
+
size: number;
|
| 21 |
+
color: string;
|
| 22 |
+
opacity: number;
|
| 23 |
+
mode: "draw" | "erase";
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/**
|
| 27 |
+
* Represents a complete brush stroke containing multiple segments
|
| 28 |
+
*/
|
| 29 |
+
interface BrushStroke {
|
| 30 |
+
segments: BrushSegment[];
|
| 31 |
+
layer_id: string;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
export class BrushCommand implements Command {
|
| 35 |
+
private stroke_data: BrushStroke;
|
| 36 |
+
private context: ImageEditorContext;
|
| 37 |
+
private original_texture: Texture | null = null;
|
| 38 |
+
|
| 39 |
+
name: string;
|
| 40 |
+
|
| 41 |
+
constructor(
|
| 42 |
+
context: ImageEditorContext,
|
| 43 |
+
stroke_data: BrushStroke,
|
| 44 |
+
original_texture?: Texture
|
| 45 |
+
) {
|
| 46 |
+
this.name = "Draw";
|
| 47 |
+
this.stroke_data = stroke_data;
|
| 48 |
+
this.context = context;
|
| 49 |
+
if (original_texture) {
|
| 50 |
+
this.original_texture = this.create_texture_from(original_texture);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Creates a new texture with the same content as the source texture
|
| 56 |
+
*/
|
| 57 |
+
private create_texture_from(source: Texture): Texture {
|
| 58 |
+
const texture = RenderTexture.create({
|
| 59 |
+
width: source.width,
|
| 60 |
+
height: source.height,
|
| 61 |
+
resolution: window.devicePixelRatio || 1
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
const sprite = new Sprite(source);
|
| 65 |
+
const container = new Container();
|
| 66 |
+
container.addChild(sprite);
|
| 67 |
+
|
| 68 |
+
this.context.app.renderer.render(container, {
|
| 69 |
+
renderTexture: texture
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
container.destroy({ children: true });
|
| 73 |
+
return texture;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/**
|
| 77 |
+
* Recreates the stroke by rendering all segments from the stored parameters
|
| 78 |
+
* Preserves the exact order of draw/erase operations
|
| 79 |
+
*/
|
| 80 |
+
private render_stroke_from_data(
|
| 81 |
+
stroke_data: BrushStroke,
|
| 82 |
+
target_texture: RenderTexture
|
| 83 |
+
): void {
|
| 84 |
+
const draw_segments = stroke_data.segments.filter((s) => s.mode === "draw");
|
| 85 |
+
const erase_segments = stroke_data.segments.filter(
|
| 86 |
+
(s) => s.mode === "erase"
|
| 87 |
+
);
|
| 88 |
+
|
| 89 |
+
if (draw_segments.length > 0) {
|
| 90 |
+
const graphics = new Graphics();
|
| 91 |
+
const container = new Container();
|
| 92 |
+
container.addChild(graphics);
|
| 93 |
+
let alpha = 1;
|
| 94 |
+
|
| 95 |
+
for (const segment of draw_segments) {
|
| 96 |
+
if (segment.opacity < alpha) {
|
| 97 |
+
alpha = segment.opacity;
|
| 98 |
+
}
|
| 99 |
+
let colorValue = 0xffffff;
|
| 100 |
+
if (segment.color.startsWith("#")) {
|
| 101 |
+
colorValue = parseInt(segment.color.replace("#", "0x"), 16);
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
graphics.setFillStyle({
|
| 105 |
+
color: colorValue,
|
| 106 |
+
alpha: 1
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
this.render_segment_to_graphics(graphics, segment);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// we need a sprite in order to set the alpha and for that we need a texture
|
| 113 |
+
// i'm not entirely sure why other approaches didn't work
|
| 114 |
+
const alpha_sprite_texture = RenderTexture.create({
|
| 115 |
+
width: target_texture.width,
|
| 116 |
+
height: target_texture.height,
|
| 117 |
+
resolution: window.devicePixelRatio || 1
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
const alpha_sprite = new Sprite(alpha_sprite_texture);
|
| 121 |
+
this.context.app.renderer.render({
|
| 122 |
+
container: container,
|
| 123 |
+
target: alpha_sprite_texture
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
alpha_sprite.alpha = alpha;
|
| 127 |
+
|
| 128 |
+
this.context.app.renderer.render({
|
| 129 |
+
container: alpha_sprite,
|
| 130 |
+
target: target_texture,
|
| 131 |
+
clear: false
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
container.destroy({ children: true });
|
| 135 |
+
alpha_sprite.destroy();
|
| 136 |
+
alpha_sprite_texture.destroy();
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
if (erase_segments.length > 0) {
|
| 140 |
+
// create a temp texture to work with
|
| 141 |
+
const temp_content_texture = RenderTexture.create({
|
| 142 |
+
width: target_texture.width,
|
| 143 |
+
height: target_texture.height,
|
| 144 |
+
resolution: window.devicePixelRatio || 1
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
const copy_sprite = new Sprite(target_texture);
|
| 148 |
+
const copy_container = new Container();
|
| 149 |
+
copy_container.addChild(copy_sprite);
|
| 150 |
+
|
| 151 |
+
this.context.app.renderer.render({
|
| 152 |
+
container: copy_container,
|
| 153 |
+
target: temp_content_texture,
|
| 154 |
+
clear: true
|
| 155 |
+
});
|
| 156 |
+
|
| 157 |
+
// create a graphics object to draw the erase segments
|
| 158 |
+
const erase_graphics = new Graphics();
|
| 159 |
+
const erase_container = new Container();
|
| 160 |
+
erase_container.addChild(erase_graphics);
|
| 161 |
+
|
| 162 |
+
erase_graphics.setFillStyle({
|
| 163 |
+
color: 0xffffff,
|
| 164 |
+
alpha: 1.0
|
| 165 |
+
});
|
| 166 |
+
|
| 167 |
+
for (const segment of erase_segments) {
|
| 168 |
+
this.render_segment_to_graphics(erase_graphics, segment);
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// create a separate texture to hold the mask
|
| 172 |
+
const mask_texture = RenderTexture.create({
|
| 173 |
+
width: target_texture.width,
|
| 174 |
+
height: target_texture.height,
|
| 175 |
+
resolution: window.devicePixelRatio || 1
|
| 176 |
+
});
|
| 177 |
+
|
| 178 |
+
this.context.app.renderer.render({
|
| 179 |
+
container: erase_container,
|
| 180 |
+
target: mask_texture,
|
| 181 |
+
clear: true
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
const content_sprite = new Sprite(temp_content_texture);
|
| 185 |
+
const mask_sprite = new Sprite(mask_texture);
|
| 186 |
+
|
| 187 |
+
// only now do we create a sprite from the original texture and add the mask
|
| 188 |
+
const masked_container = new Container();
|
| 189 |
+
masked_container.addChild(content_sprite);
|
| 190 |
+
masked_container.setMask({ mask: mask_sprite, inverse: true }); // inverse mask = erase
|
| 191 |
+
|
| 192 |
+
// now we can render the masked content back to the target texture
|
| 193 |
+
this.context.app.renderer.render({
|
| 194 |
+
container: masked_container,
|
| 195 |
+
target: target_texture,
|
| 196 |
+
clear: true
|
| 197 |
+
});
|
| 198 |
+
|
| 199 |
+
copy_container.destroy({ children: true });
|
| 200 |
+
erase_container.destroy({ children: true });
|
| 201 |
+
masked_container.destroy({ children: true });
|
| 202 |
+
temp_content_texture.destroy();
|
| 203 |
+
mask_texture.destroy();
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
/**
|
| 208 |
+
* Renders a segment to a graphics object (extracted from renderSegment)
|
| 209 |
+
*/
|
| 210 |
+
private render_segment_to_graphics(
|
| 211 |
+
graphics: Graphics,
|
| 212 |
+
segment: BrushSegment
|
| 213 |
+
): void {
|
| 214 |
+
const distance = Math.sqrt(
|
| 215 |
+
Math.pow(segment.to_x - segment.from_x, 2) +
|
| 216 |
+
Math.pow(segment.to_y - segment.from_y, 2)
|
| 217 |
+
);
|
| 218 |
+
|
| 219 |
+
if (distance < 0.1) {
|
| 220 |
+
graphics.circle(segment.from_x, segment.from_y, segment.size).fill();
|
| 221 |
+
} else {
|
| 222 |
+
const spacing = Math.max(segment.size / 3, 2);
|
| 223 |
+
const steps = Math.max(Math.ceil(distance / spacing), 2);
|
| 224 |
+
|
| 225 |
+
for (let i = 0; i < steps; i++) {
|
| 226 |
+
const t = i / (steps - 1);
|
| 227 |
+
const x = segment.from_x + (segment.to_x - segment.from_x) * t;
|
| 228 |
+
const y = segment.from_y + (segment.to_y - segment.from_y) * t;
|
| 229 |
+
|
| 230 |
+
graphics.circle(x, y, segment.size).fill();
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
async execute(context: ImageEditorContext): Promise<void> {
|
| 236 |
+
if (context) {
|
| 237 |
+
this.context = context;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
let layer_textures = this.context.layer_manager.get_layer_textures(
|
| 241 |
+
this.stroke_data.layer_id
|
| 242 |
+
);
|
| 243 |
+
|
| 244 |
+
if (!layer_textures) {
|
| 245 |
+
const all_layers = this.context.layer_manager.get_layers();
|
| 246 |
+
const top_layer = all_layers[all_layers.length - 1];
|
| 247 |
+
layer_textures = this.context.layer_manager.get_layer_textures(
|
| 248 |
+
top_layer.id
|
| 249 |
+
);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
if (!layer_textures) return;
|
| 253 |
+
|
| 254 |
+
// create a temporary texture to render the stroke
|
| 255 |
+
const temp_texture = RenderTexture.create({
|
| 256 |
+
width: layer_textures.draw.width,
|
| 257 |
+
height: layer_textures.draw.height,
|
| 258 |
+
resolution: window.devicePixelRatio || 1
|
| 259 |
+
});
|
| 260 |
+
|
| 261 |
+
// copy current layer content to temp texture
|
| 262 |
+
const current_sprite = new Sprite(layer_textures.draw);
|
| 263 |
+
const temp_container = new Container();
|
| 264 |
+
temp_container.addChild(current_sprite);
|
| 265 |
+
|
| 266 |
+
this.context.app.renderer.render({
|
| 267 |
+
container: temp_container,
|
| 268 |
+
target: temp_texture,
|
| 269 |
+
clear: true
|
| 270 |
+
});
|
| 271 |
+
|
| 272 |
+
temp_container.destroy({ children: true });
|
| 273 |
+
|
| 274 |
+
this.render_stroke_from_data(this.stroke_data, temp_texture);
|
| 275 |
+
|
| 276 |
+
// copy final result back to layer texture
|
| 277 |
+
const final_sprite = new Sprite(temp_texture);
|
| 278 |
+
const final_container = new Container();
|
| 279 |
+
final_container.addChild(final_sprite);
|
| 280 |
+
|
| 281 |
+
this.context.app.renderer.render({
|
| 282 |
+
container: final_container,
|
| 283 |
+
target: layer_textures.draw,
|
| 284 |
+
clear: true
|
| 285 |
+
});
|
| 286 |
+
|
| 287 |
+
final_container.destroy({ children: true });
|
| 288 |
+
temp_texture.destroy();
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
async undo(): Promise<void> {
|
| 292 |
+
if (!this.original_texture) return;
|
| 293 |
+
|
| 294 |
+
const layer_textures = this.context.layer_manager.get_layer_textures(
|
| 295 |
+
this.stroke_data.layer_id
|
| 296 |
+
);
|
| 297 |
+
if (!layer_textures) return;
|
| 298 |
+
|
| 299 |
+
const temp_sprite = new Sprite(this.original_texture);
|
| 300 |
+
const temp_container = new Container();
|
| 301 |
+
temp_container.addChild(temp_sprite);
|
| 302 |
+
|
| 303 |
+
this.context.app.renderer.render({
|
| 304 |
+
container: temp_container,
|
| 305 |
+
target: layer_textures.draw,
|
| 306 |
+
clear: true
|
| 307 |
+
});
|
| 308 |
+
|
| 309 |
+
temp_container.destroy({ children: true });
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
/**
|
| 314 |
+
* Class to handle texture operations for the brush tool.
|
| 315 |
+
* Simplified to work directly with layer textures.
|
| 316 |
+
*/
|
| 317 |
+
export class BrushTextures {
|
| 318 |
+
private stroke_texture: RenderTexture | null = null;
|
| 319 |
+
private erase_texture: RenderTexture | null = null;
|
| 320 |
+
private display_container: Container | null = null;
|
| 321 |
+
private stroke_container: Container | null = null;
|
| 322 |
+
private stroke_graphics: Graphics | null = null;
|
| 323 |
+
private preview_sprite: Sprite | null = null;
|
| 324 |
+
private erase_graphics: Graphics | null = null;
|
| 325 |
+
private dimensions: { width: number; height: number };
|
| 326 |
+
private image_editor_context: ImageEditorContext;
|
| 327 |
+
private app: Application;
|
| 328 |
+
private is_new_stroke = true;
|
| 329 |
+
|
| 330 |
+
private current_opacity = 1.0;
|
| 331 |
+
private original_layer_texture: Texture | null = null;
|
| 332 |
+
private active_layer_id: string | null = null;
|
| 333 |
+
private current_stroke_segments: BrushSegment[] = [];
|
| 334 |
+
|
| 335 |
+
constructor(image_editor_context: ImageEditorContext, app: Application) {
|
| 336 |
+
this.image_editor_context = image_editor_context;
|
| 337 |
+
this.app = app;
|
| 338 |
+
|
| 339 |
+
this.dimensions = {
|
| 340 |
+
width: this.image_editor_context.image_container.width,
|
| 341 |
+
height: this.image_editor_context.image_container.height
|
| 342 |
+
};
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
/**
|
| 346 |
+
* Initializes textures needed for the brush tool.
|
| 347 |
+
*/
|
| 348 |
+
initialize_textures(): void {
|
| 349 |
+
this.cleanup_textures();
|
| 350 |
+
|
| 351 |
+
const local_bounds =
|
| 352 |
+
this.image_editor_context.image_container.getLocalBounds();
|
| 353 |
+
|
| 354 |
+
this.dimensions = {
|
| 355 |
+
width: local_bounds.width,
|
| 356 |
+
height: local_bounds.height
|
| 357 |
+
};
|
| 358 |
+
|
| 359 |
+
this.stroke_texture = RenderTexture.create({
|
| 360 |
+
width: this.dimensions.width,
|
| 361 |
+
height: this.dimensions.height,
|
| 362 |
+
resolution: window.devicePixelRatio || 1
|
| 363 |
+
});
|
| 364 |
+
|
| 365 |
+
this.erase_texture = RenderTexture.create({
|
| 366 |
+
width: this.dimensions.width,
|
| 367 |
+
height: this.dimensions.height,
|
| 368 |
+
resolution: window.devicePixelRatio || 1
|
| 369 |
+
});
|
| 370 |
+
|
| 371 |
+
this.display_container = new Container();
|
| 372 |
+
|
| 373 |
+
this.image_editor_context.image_container.addChild(this.display_container);
|
| 374 |
+
|
| 375 |
+
this.stroke_container = new Container();
|
| 376 |
+
this.stroke_graphics = new Graphics();
|
| 377 |
+
this.stroke_container.addChild(this.stroke_graphics);
|
| 378 |
+
|
| 379 |
+
this.erase_graphics = new Graphics();
|
| 380 |
+
|
| 381 |
+
this.preview_sprite = new Sprite(this.stroke_texture);
|
| 382 |
+
this.preview_sprite.alpha = 0;
|
| 383 |
+
this.display_container.addChild(this.preview_sprite);
|
| 384 |
+
|
| 385 |
+
const clear_container = new Container();
|
| 386 |
+
this.app.renderer.render(clear_container, {
|
| 387 |
+
renderTexture: this.stroke_texture
|
| 388 |
+
});
|
| 389 |
+
this.app.renderer.render(clear_container, {
|
| 390 |
+
renderTexture: this.erase_texture
|
| 391 |
+
});
|
| 392 |
+
clear_container.destroy();
|
| 393 |
+
|
| 394 |
+
this.is_new_stroke = true;
|
| 395 |
+
this.current_opacity = 1.0;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
/**
|
| 399 |
+
* Reinitializes textures when needed (e.g., after resizing).
|
| 400 |
+
*/
|
| 401 |
+
reinitialize(): void {
|
| 402 |
+
if (
|
| 403 |
+
this.image_editor_context.image_container.width !==
|
| 404 |
+
this.dimensions.width ||
|
| 405 |
+
this.image_editor_context.image_container.height !==
|
| 406 |
+
this.dimensions.height
|
| 407 |
+
) {
|
| 408 |
+
this.initialize_textures();
|
| 409 |
+
}
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
/**
|
| 413 |
+
* Cleans up texture resources.
|
| 414 |
+
*/
|
| 415 |
+
cleanup_textures(): void {
|
| 416 |
+
if (this.stroke_texture) {
|
| 417 |
+
this.stroke_texture.destroy();
|
| 418 |
+
this.stroke_texture = null;
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
if (this.erase_texture) {
|
| 422 |
+
this.erase_texture.destroy();
|
| 423 |
+
this.erase_texture = null;
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
if (this.display_container) {
|
| 427 |
+
if (this.display_container.parent) {
|
| 428 |
+
this.display_container.parent.removeChild(this.display_container);
|
| 429 |
+
}
|
| 430 |
+
this.display_container.destroy({ children: true });
|
| 431 |
+
this.display_container = null;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
if (this.original_layer_texture) {
|
| 435 |
+
this.original_layer_texture.destroy();
|
| 436 |
+
this.original_layer_texture = null;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
this.stroke_container = null;
|
| 440 |
+
this.stroke_graphics = null;
|
| 441 |
+
this.preview_sprite = null;
|
| 442 |
+
this.erase_graphics = null;
|
| 443 |
+
this.active_layer_id = null;
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
/**
|
| 447 |
+
* Preserve the canvas state when starting a new stroke.
|
| 448 |
+
*/
|
| 449 |
+
preserve_canvas_state(): void {
|
| 450 |
+
const active_layer =
|
| 451 |
+
this.image_editor_context.layer_manager.get_active_layer();
|
| 452 |
+
if (!active_layer) return;
|
| 453 |
+
|
| 454 |
+
const layers = this.image_editor_context.layer_manager.get_layers();
|
| 455 |
+
const layer = layers.find((l) => l.container === active_layer);
|
| 456 |
+
if (!layer) return;
|
| 457 |
+
|
| 458 |
+
this.active_layer_id = layer.id;
|
| 459 |
+
|
| 460 |
+
const layer_textures =
|
| 461 |
+
this.image_editor_context.layer_manager.get_layer_textures(layer.id);
|
| 462 |
+
if (!layer_textures) return;
|
| 463 |
+
|
| 464 |
+
if (this.original_layer_texture) {
|
| 465 |
+
this.original_layer_texture.destroy();
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
this.original_layer_texture = RenderTexture.create({
|
| 469 |
+
width: this.dimensions.width,
|
| 470 |
+
height: this.dimensions.height,
|
| 471 |
+
resolution: window.devicePixelRatio || 1
|
| 472 |
+
});
|
| 473 |
+
|
| 474 |
+
const temp_sprite = new Sprite(layer_textures.draw);
|
| 475 |
+
const temp_container = new Container();
|
| 476 |
+
temp_container.addChild(temp_sprite);
|
| 477 |
+
|
| 478 |
+
this.app.renderer.render(temp_container, {
|
| 479 |
+
renderTexture: this.original_layer_texture
|
| 480 |
+
});
|
| 481 |
+
|
| 482 |
+
temp_container.destroy({ children: true });
|
| 483 |
+
|
| 484 |
+
this.is_new_stroke = true;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
/**
|
| 488 |
+
* Resets the eraser mask.
|
| 489 |
+
*/
|
| 490 |
+
reset_eraser_mask(): void {
|
| 491 |
+
if (!this.erase_graphics || !this.erase_texture) return;
|
| 492 |
+
|
| 493 |
+
this.erase_graphics.clear();
|
| 494 |
+
this.erase_graphics.setFillStyle({ color: 0xffffff, alpha: 1.0 });
|
| 495 |
+
this.erase_graphics
|
| 496 |
+
.rect(0, 0, this.dimensions.width, this.dimensions.height)
|
| 497 |
+
.fill();
|
| 498 |
+
this.erase_graphics.endFill();
|
| 499 |
+
|
| 500 |
+
this.app.renderer.render(this.erase_graphics, {
|
| 501 |
+
renderTexture: this.erase_texture
|
| 502 |
+
});
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
/**
|
| 506 |
+
* Commits the current stroke to the active layer and returns a command for undo/redo.
|
| 507 |
+
*/
|
| 508 |
+
commit_stroke(): void {
|
| 509 |
+
if (
|
| 510 |
+
!this.stroke_texture ||
|
| 511 |
+
!this.preview_sprite ||
|
| 512 |
+
!this.stroke_graphics ||
|
| 513 |
+
!this.original_layer_texture ||
|
| 514 |
+
!this.active_layer_id
|
| 515 |
+
)
|
| 516 |
+
return;
|
| 517 |
+
|
| 518 |
+
this.preview_sprite.visible = false;
|
| 519 |
+
|
| 520 |
+
const active_layer =
|
| 521 |
+
this.image_editor_context.layer_manager.get_active_layer();
|
| 522 |
+
|
| 523 |
+
if (!active_layer) return;
|
| 524 |
+
|
| 525 |
+
const layers = this.image_editor_context.layer_manager.get_layers();
|
| 526 |
+
const layer = layers.find((l) => l.container === active_layer);
|
| 527 |
+
if (!layer) return;
|
| 528 |
+
|
| 529 |
+
if (layer.id !== this.active_layer_id) return;
|
| 530 |
+
|
| 531 |
+
const layer_textures =
|
| 532 |
+
this.image_editor_context.layer_manager.get_layer_textures(layer.id);
|
| 533 |
+
if (!layer_textures) return;
|
| 534 |
+
|
| 535 |
+
const stroke_data: BrushStroke = {
|
| 536 |
+
segments: [...this.current_stroke_segments],
|
| 537 |
+
layer_id: this.active_layer_id
|
| 538 |
+
};
|
| 539 |
+
|
| 540 |
+
const brush_command = new BrushCommand(
|
| 541 |
+
this.image_editor_context,
|
| 542 |
+
stroke_data,
|
| 543 |
+
this.original_layer_texture
|
| 544 |
+
);
|
| 545 |
+
|
| 546 |
+
if (this.stroke_graphics) {
|
| 547 |
+
this.stroke_graphics.clear();
|
| 548 |
+
}
|
| 549 |
+
const clear_container = new Container();
|
| 550 |
+
this.app.renderer.render(clear_container, {
|
| 551 |
+
renderTexture: this.stroke_texture
|
| 552 |
+
});
|
| 553 |
+
clear_container.destroy();
|
| 554 |
+
|
| 555 |
+
this.is_new_stroke = true;
|
| 556 |
+
this.original_layer_texture = null;
|
| 557 |
+
this.active_layer_id = null;
|
| 558 |
+
this.current_stroke_segments = [];
|
| 559 |
+
|
| 560 |
+
this.image_editor_context.command_manager.execute(
|
| 561 |
+
brush_command,
|
| 562 |
+
this.image_editor_context
|
| 563 |
+
);
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
/**
|
| 567 |
+
* Calculates the distance between two points.
|
| 568 |
+
*/
|
| 569 |
+
private calculate_distance(
|
| 570 |
+
x1: number,
|
| 571 |
+
y1: number,
|
| 572 |
+
x2: number,
|
| 573 |
+
y2: number
|
| 574 |
+
): number {
|
| 575 |
+
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
/**
|
| 579 |
+
* Draws a segment for the current stroke using a simple "stamp" approach.
|
| 580 |
+
*/
|
| 581 |
+
draw_segment(
|
| 582 |
+
from_x: number,
|
| 583 |
+
from_y: number,
|
| 584 |
+
to_x: number,
|
| 585 |
+
to_y: number,
|
| 586 |
+
size: number,
|
| 587 |
+
color: string,
|
| 588 |
+
opacity: number,
|
| 589 |
+
mode: "draw" | "erase"
|
| 590 |
+
): void {
|
| 591 |
+
if (
|
| 592 |
+
!this.stroke_graphics ||
|
| 593 |
+
!this.stroke_texture ||
|
| 594 |
+
!this.stroke_container ||
|
| 595 |
+
!this.preview_sprite
|
| 596 |
+
)
|
| 597 |
+
return;
|
| 598 |
+
|
| 599 |
+
if (this.is_new_stroke && !this.original_layer_texture) {
|
| 600 |
+
this.preserve_canvas_state();
|
| 601 |
+
}
|
| 602 |
+
|
| 603 |
+
this.current_opacity =
|
| 604 |
+
mode === "draw" ? Math.min(Math.max(opacity, 0), 1) : 0.5;
|
| 605 |
+
|
| 606 |
+
const scaled_size = size;
|
| 607 |
+
|
| 608 |
+
if (this.is_new_stroke) {
|
| 609 |
+
this.stroke_graphics.clear();
|
| 610 |
+
this.current_stroke_segments = [];
|
| 611 |
+
|
| 612 |
+
const clear_container = new Container();
|
| 613 |
+
this.app.renderer.render(clear_container, {
|
| 614 |
+
renderTexture: this.stroke_texture
|
| 615 |
+
});
|
| 616 |
+
clear_container.destroy();
|
| 617 |
+
|
| 618 |
+
this.is_new_stroke = false;
|
| 619 |
+
}
|
| 620 |
+
|
| 621 |
+
this.current_stroke_segments.push({
|
| 622 |
+
from_x,
|
| 623 |
+
from_y,
|
| 624 |
+
to_x,
|
| 625 |
+
to_y,
|
| 626 |
+
size: scaled_size,
|
| 627 |
+
color,
|
| 628 |
+
opacity: this.current_opacity,
|
| 629 |
+
mode
|
| 630 |
+
});
|
| 631 |
+
|
| 632 |
+
if (mode === "draw") {
|
| 633 |
+
let colorValue = 0xffffff;
|
| 634 |
+
try {
|
| 635 |
+
if (color.startsWith("#")) {
|
| 636 |
+
colorValue = parseInt(color.replace("#", "0x"), 16);
|
| 637 |
+
}
|
| 638 |
+
} catch (e) {
|
| 639 |
+
colorValue = 0xffffff;
|
| 640 |
+
}
|
| 641 |
+
this.stroke_graphics.setFillStyle({
|
| 642 |
+
color: colorValue,
|
| 643 |
+
alpha: 1.0
|
| 644 |
+
});
|
| 645 |
+
} else {
|
| 646 |
+
this.stroke_graphics.setFillStyle({
|
| 647 |
+
color: 0xffffff,
|
| 648 |
+
alpha: 1.0
|
| 649 |
+
});
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
const distance = this.calculate_distance(from_x, from_y, to_x, to_y);
|
| 653 |
+
|
| 654 |
+
if (distance < 0.1) {
|
| 655 |
+
this.stroke_graphics.circle(from_x, from_y, scaled_size).fill();
|
| 656 |
+
} else {
|
| 657 |
+
const spacing = Math.max(scaled_size / 3, 2);
|
| 658 |
+
const steps = Math.max(Math.ceil(distance / spacing), 2);
|
| 659 |
+
|
| 660 |
+
for (let i = 0; i < steps; i++) {
|
| 661 |
+
const t = i / (steps - 1);
|
| 662 |
+
const x = from_x + (to_x - from_x) * t;
|
| 663 |
+
const y = from_y + (to_y - from_y) * t;
|
| 664 |
+
|
| 665 |
+
this.stroke_graphics.circle(x, y, scaled_size).fill();
|
| 666 |
+
}
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
this.stroke_graphics.endFill();
|
| 670 |
+
|
| 671 |
+
this.app.renderer.render(this.stroke_container, {
|
| 672 |
+
renderTexture: this.stroke_texture
|
| 673 |
+
});
|
| 674 |
+
|
| 675 |
+
if (mode === "draw") {
|
| 676 |
+
this.preview_sprite.texture = this.stroke_texture;
|
| 677 |
+
this.preview_sprite.alpha = this.current_opacity;
|
| 678 |
+
|
| 679 |
+
this.preview_sprite.tint = 0xffffff;
|
| 680 |
+
} else {
|
| 681 |
+
const active_layer =
|
| 682 |
+
this.image_editor_context.layer_manager.get_active_layer();
|
| 683 |
+
if (!active_layer) return;
|
| 684 |
+
|
| 685 |
+
const layers = this.image_editor_context.layer_manager.get_layers();
|
| 686 |
+
const layer = layers.find((l) => l.container === active_layer);
|
| 687 |
+
if (!layer) return;
|
| 688 |
+
|
| 689 |
+
const layer_textures =
|
| 690 |
+
this.image_editor_context.layer_manager.get_layer_textures(layer.id);
|
| 691 |
+
if (!layer_textures) return;
|
| 692 |
+
|
| 693 |
+
const preview_texture = RenderTexture.create({
|
| 694 |
+
width: this.dimensions.width,
|
| 695 |
+
height: this.dimensions.height,
|
| 696 |
+
resolution: window.devicePixelRatio || 1
|
| 697 |
+
});
|
| 698 |
+
|
| 699 |
+
const base_container = new Container();
|
| 700 |
+
const content_sprite = new Sprite(layer_textures.draw);
|
| 701 |
+
base_container.addChild(content_sprite);
|
| 702 |
+
this.app.renderer.render(base_container, {
|
| 703 |
+
renderTexture: preview_texture
|
| 704 |
+
});
|
| 705 |
+
base_container.destroy({ children: true });
|
| 706 |
+
|
| 707 |
+
const preview_container = new Container();
|
| 708 |
+
|
| 709 |
+
const mask_sprite = new Sprite(this.stroke_texture);
|
| 710 |
+
|
| 711 |
+
const overlay = new Graphics();
|
| 712 |
+
overlay.setFillStyle({ color: 0xffffff, alpha: 0.5 });
|
| 713 |
+
overlay.rect(0, 0, this.dimensions.width, this.dimensions.height).fill();
|
| 714 |
+
|
| 715 |
+
overlay.setMask({ mask: mask_sprite, inverse: false });
|
| 716 |
+
|
| 717 |
+
preview_container.addChild(overlay);
|
| 718 |
+
|
| 719 |
+
this.app.renderer.render(preview_container, {
|
| 720 |
+
renderTexture: preview_texture
|
| 721 |
+
});
|
| 722 |
+
|
| 723 |
+
this.preview_sprite.texture = preview_texture;
|
| 724 |
+
this.preview_sprite.alpha = 1.0;
|
| 725 |
+
|
| 726 |
+
preview_container.destroy({ children: true });
|
| 727 |
+
preview_texture.destroy();
|
| 728 |
+
}
|
| 729 |
+
|
| 730 |
+
this.preview_sprite.visible = true;
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
/**
|
| 734 |
+
* Gets current dimensions of the texture.
|
| 735 |
+
*/
|
| 736 |
+
get_dimensions(): { width: number; height: number } {
|
| 737 |
+
return this.dimensions;
|
| 738 |
+
}
|
| 739 |
+
|
| 740 |
+
/**
|
| 741 |
+
* Checks if textures are properly initialized.
|
| 742 |
+
*/
|
| 743 |
+
get textures_initialized(): boolean {
|
| 744 |
+
return !!(
|
| 745 |
+
this.stroke_texture &&
|
| 746 |
+
this.display_container &&
|
| 747 |
+
this.preview_sprite
|
| 748 |
+
);
|
| 749 |
+
}
|
| 750 |
+
|
| 751 |
+
/**
|
| 752 |
+
* Cleanup all resources.
|
| 753 |
+
*/
|
| 754 |
+
cleanup(): void {
|
| 755 |
+
this.cleanup_textures();
|
| 756 |
+
}
|
| 757 |
+
}
|
6.0.0-dev.5/imageeditor/shared/brush/brush-utils.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Container, Sprite, Graphics } from "pixi.js";
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* Sets the cursor style for a single PIXI object.
|
| 5 |
+
* @param child - The PIXI object to set the cursor for.
|
| 6 |
+
* @param cursor - The cursor style to set.
|
| 7 |
+
*/
|
| 8 |
+
export function set_cursor(
|
| 9 |
+
child: Container | Sprite | Graphics,
|
| 10 |
+
cursor: "unset" | "none"
|
| 11 |
+
): void {
|
| 12 |
+
if (child instanceof Container) {
|
| 13 |
+
child.cursor = cursor;
|
| 14 |
+
} else if ("cursor" in child) {
|
| 15 |
+
(child as any).cursor = cursor;
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
/**
|
| 20 |
+
* Recursively sets the cursor style for PIXI objects.
|
| 21 |
+
* @param children - The PIXI objects to set the cursor for.
|
| 22 |
+
* @param cursor - The cursor style to set.
|
| 23 |
+
*/
|
| 24 |
+
export function recurse_set_cursor(
|
| 25 |
+
children: (Container | Sprite | Graphics)[],
|
| 26 |
+
cursor: "unset" | "none"
|
| 27 |
+
): void {
|
| 28 |
+
for (const child of children) {
|
| 29 |
+
set_cursor(child, cursor);
|
| 30 |
+
if (child instanceof Container && child.children.length > 0) {
|
| 31 |
+
recurse_set_cursor(
|
| 32 |
+
child.children as (Container | Sprite | Graphics)[],
|
| 33 |
+
cursor
|
| 34 |
+
);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Safely removes a child from its parent.
|
| 41 |
+
* @param child - The child to remove.
|
| 42 |
+
*/
|
| 43 |
+
export function safe_remove_child(
|
| 44 |
+
child: Container | Sprite | Graphics | null
|
| 45 |
+
): void {
|
| 46 |
+
if (child && child.parent) {
|
| 47 |
+
child.parent.removeChild(child);
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/**
|
| 52 |
+
* Safely destroys a PIXI object and nullifies it.
|
| 53 |
+
* @param obj - The object to destroy.
|
| 54 |
+
* @returns null to assist with nullifying the reference.
|
| 55 |
+
*/
|
| 56 |
+
export function safe_destroy<T extends { destroy: (options?: any) => void }>(
|
| 57 |
+
obj: T | null
|
| 58 |
+
): null {
|
| 59 |
+
if (obj) {
|
| 60 |
+
safe_remove_child(obj as any);
|
| 61 |
+
obj.destroy({ children: true });
|
| 62 |
+
}
|
| 63 |
+
return null;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
/**
|
| 67 |
+
* Clears a timeout safely and nullifies the reference.
|
| 68 |
+
* @param timeout - The timeout to clear.
|
| 69 |
+
* @returns null to assist with nullifying the reference.
|
| 70 |
+
*/
|
| 71 |
+
export function clear_timeout(timeout: number | null): null {
|
| 72 |
+
if (timeout !== null) {
|
| 73 |
+
window.clearTimeout(timeout);
|
| 74 |
+
}
|
| 75 |
+
return null;
|
| 76 |
+
}
|
6.0.0-dev.5/imageeditor/shared/brush/brush.ts
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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olbarTool, type Subtool } from "../Toolbar.svelte";
|
| 4 |
+
import type { ColorInput } from "tinycolor2";
|
| 5 |
+
import tinycolor from "tinycolor2";
|
| 6 |
+
import { BrushCursor } from "./brush-cursor";
|
| 7 |
+
import { BrushTextures } from "./brush-textures";
|
| 8 |
+
import { recurse_set_cursor } from "./brush-utils";
|
| 9 |
+
import { type BrushState } from "./types";
|
| 10 |
+
|
| 11 |
+
/**
|
| 12 |
+
* BrushTool class implements the Tool interface for drawing and erasing.
|
| 13 |
+
* @class
|
| 14 |
+
* @implements {Tool}
|
| 15 |
+
*/
|
| 16 |
+
export class BrushTool implements Tool {
|
| 17 |
+
/**
|
| 18 |
+
* The name of the tool.
|
| 19 |
+
* @type {string}
|
| 20 |
+
*/
|
| 21 |
+
name = "brush" as const;
|
| 22 |
+
|
| 23 |
+
/**
|
| 24 |
+
* The context of the image editor.
|
| 25 |
+
* @private
|
| 26 |
+
*/
|
| 27 |
+
private image_editor_context!: ImageEditorContext;
|
| 28 |
+
|
| 29 |
+
/**
|
| 30 |
+
* The current tool selected in the toolbar.
|
| 31 |
+
* @private
|
| 32 |
+
*/
|
| 33 |
+
private current_tool!: ToolbarTool;
|
| 34 |
+
|
| 35 |
+
/**
|
| 36 |
+
* The current subtool selected in the toolbar.
|
| 37 |
+
* @private
|
| 38 |
+
*/
|
| 39 |
+
private current_subtool!: Subtool;
|
| 40 |
+
|
| 41 |
+
/**
|
| 42 |
+
* The state of the brush tool.
|
| 43 |
+
* @private
|
| 44 |
+
*/
|
| 45 |
+
private state: BrushState = {
|
| 46 |
+
opacity: 1,
|
| 47 |
+
brush_size: 10,
|
| 48 |
+
color: "#000000",
|
| 49 |
+
mode: "draw"
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
/**
|
| 53 |
+
* Size settings for brush and eraser
|
| 54 |
+
* @private
|
| 55 |
+
*/
|
| 56 |
+
private brush_size = 10;
|
| 57 |
+
private eraser_size = 20;
|
| 58 |
+
|
| 59 |
+
/**
|
| 60 |
+
* Drawing state
|
| 61 |
+
* @private
|
| 62 |
+
*/
|
| 63 |
+
private is_drawing = false;
|
| 64 |
+
private last_x = 0;
|
| 65 |
+
private last_y = 0;
|
| 66 |
+
private scale = 1;
|
| 67 |
+
|
| 68 |
+
/**
|
| 69 |
+
* Event listeners
|
| 70 |
+
* @private
|
| 71 |
+
*/
|
| 72 |
+
private _bound_pointer_down: ((event: FederatedPointerEvent) => void) | null =
|
| 73 |
+
null;
|
| 74 |
+
private _bound_pointer_move: ((event: FederatedPointerEvent) => void) | null =
|
| 75 |
+
null;
|
| 76 |
+
private _bound_pointer_up: (() => void) | null = null;
|
| 77 |
+
private event_callbacks: Map<string, (() => void)[]> = new Map();
|
| 78 |
+
/**
|
| 79 |
+
* Utility modules
|
| 80 |
+
* @private
|
| 81 |
+
*/
|
| 82 |
+
private brush_cursor: BrushCursor | null = null;
|
| 83 |
+
private brush_textures: BrushTextures | null = null;
|
| 84 |
+
|
| 85 |
+
/**
|
| 86 |
+
* Sets up the brush tool with the given context, tool, and subtool.
|
| 87 |
+
* @param {ImageEditorContext} context - The image editor context.
|
| 88 |
+
* @param {ToolbarTool} tool - The tool from the toolbar.
|
| 89 |
+
* @param {Subtool} subtool - The subtool from the toolbar.
|
| 90 |
+
* @returns {Promise<void>}
|
| 91 |
+
*/
|
| 92 |
+
async setup(
|
| 93 |
+
context: ImageEditorContext,
|
| 94 |
+
tool: ToolbarTool,
|
| 95 |
+
subtool: Subtool
|
| 96 |
+
): Promise<void> {
|
| 97 |
+
this.image_editor_context = context;
|
| 98 |
+
this.current_tool = tool;
|
| 99 |
+
this.current_subtool = subtool;
|
| 100 |
+
|
| 101 |
+
this.state.mode = tool === "erase" ? "erase" : "draw";
|
| 102 |
+
|
| 103 |
+
if (this.state.mode === "draw") {
|
| 104 |
+
this.state.brush_size = this.brush_size;
|
| 105 |
+
} else {
|
| 106 |
+
this.state.brush_size = this.eraser_size;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
context.scale.subscribe((scale) => {
|
| 110 |
+
this.scale = scale;
|
| 111 |
+
if (this.brush_cursor) {
|
| 112 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 113 |
+
}
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
this.brush_cursor = new BrushCursor(
|
| 117 |
+
this.image_editor_context,
|
| 118 |
+
this.state,
|
| 119 |
+
this.scale
|
| 120 |
+
);
|
| 121 |
+
|
| 122 |
+
this.brush_cursor.set_active(tool === "draw" || tool === "erase");
|
| 123 |
+
|
| 124 |
+
this.brush_textures = new BrushTextures(
|
| 125 |
+
this.image_editor_context,
|
| 126 |
+
context.app
|
| 127 |
+
);
|
| 128 |
+
this.brush_textures.initialize_textures();
|
| 129 |
+
|
| 130 |
+
this.setup_event_listeners();
|
| 131 |
+
|
| 132 |
+
this.handle_cursors(tool);
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/**
|
| 136 |
+
* Handles cursor styles for the brush tool.
|
| 137 |
+
* @param {ToolbarTool} tool - The current tool.
|
| 138 |
+
* @private
|
| 139 |
+
*/
|
| 140 |
+
private handle_cursors(tool: ToolbarTool): void {
|
| 141 |
+
recurse_set_cursor(
|
| 142 |
+
this.image_editor_context.image_container.children as Container[],
|
| 143 |
+
"none"
|
| 144 |
+
);
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
/**
|
| 148 |
+
* Sets the current tool and subtool.
|
| 149 |
+
* @param {ToolbarTool} tool - The current tool.
|
| 150 |
+
* @param {Subtool} subtool - The current subtool.
|
| 151 |
+
*/
|
| 152 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void {
|
| 153 |
+
this.current_tool = tool;
|
| 154 |
+
this.current_subtool = subtool;
|
| 155 |
+
|
| 156 |
+
if (this.current_tool !== "erase" && this.current_tool !== "draw") {
|
| 157 |
+
this.commit_pending_changes();
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
if (this.brush_cursor) {
|
| 161 |
+
const should_be_active = tool === "draw" || tool === "erase";
|
| 162 |
+
|
| 163 |
+
this.brush_cursor.set_active(should_be_active);
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
const new_mode = tool === "erase" ? "erase" : "draw";
|
| 167 |
+
const mode_changed = this.state.mode !== new_mode;
|
| 168 |
+
const needs_brush_tool = tool === "erase" || tool === "draw";
|
| 169 |
+
const textures_initialized =
|
| 170 |
+
this.brush_textures?.textures_initialized ?? false;
|
| 171 |
+
|
| 172 |
+
if (needs_brush_tool && (mode_changed || !textures_initialized)) {
|
| 173 |
+
this.brush_textures?.initialize_textures();
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
if (this.state.mode !== new_mode) {
|
| 177 |
+
this.state.mode = new_mode;
|
| 178 |
+
|
| 179 |
+
if (this.state.mode === "draw") {
|
| 180 |
+
this.state.brush_size = this.brush_size;
|
| 181 |
+
} else {
|
| 182 |
+
this.state.brush_size = this.eraser_size;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
if (this.brush_cursor) {
|
| 186 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/**
|
| 192 |
+
* Commits any pending changes to the canvas.
|
| 193 |
+
* @private
|
| 194 |
+
*/
|
| 195 |
+
private commit_pending_changes(): void {
|
| 196 |
+
if (this.is_drawing) {
|
| 197 |
+
this.on_pointer_up();
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/**
|
| 202 |
+
* Called when the user presses the mouse button.
|
| 203 |
+
* @param {FederatedPointerEvent} event - The pointer event.
|
| 204 |
+
* @private
|
| 205 |
+
*/
|
| 206 |
+
private on_pointer_down(event: FederatedPointerEvent): void {
|
| 207 |
+
const current_layer =
|
| 208 |
+
this.image_editor_context.layer_manager.get_active_layer();
|
| 209 |
+
|
| 210 |
+
if (
|
| 211 |
+
!current_layer?.visible ||
|
| 212 |
+
(this.current_tool !== "erase" && this.current_tool !== "draw")
|
| 213 |
+
) {
|
| 214 |
+
return;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
if (this.brush_cursor && !this.brush_cursor.is_over_image()) {
|
| 218 |
+
return;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
if (this.brush_textures) {
|
| 222 |
+
this.brush_textures.preserve_canvas_state();
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 226 |
+
event.global
|
| 227 |
+
);
|
| 228 |
+
|
| 229 |
+
this.is_drawing = true;
|
| 230 |
+
this.last_x = local_pos.x;
|
| 231 |
+
this.last_y = local_pos.y;
|
| 232 |
+
|
| 233 |
+
if (this.brush_textures) {
|
| 234 |
+
this.brush_textures.draw_segment(
|
| 235 |
+
local_pos.x,
|
| 236 |
+
local_pos.y,
|
| 237 |
+
local_pos.x,
|
| 238 |
+
local_pos.y,
|
| 239 |
+
this.state.brush_size,
|
| 240 |
+
this.state.color,
|
| 241 |
+
this.state.opacity,
|
| 242 |
+
this.state.mode
|
| 243 |
+
);
|
| 244 |
+
}
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
/**
|
| 248 |
+
* Called when the user moves the mouse while drawing.
|
| 249 |
+
* @param {FederatedPointerEvent} event - The pointer event.
|
| 250 |
+
* @private
|
| 251 |
+
*/
|
| 252 |
+
private on_pointer_move(event: FederatedPointerEvent): void {
|
| 253 |
+
if (this.brush_cursor) {
|
| 254 |
+
this.brush_cursor.update_cursor_position(event);
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
if (!this.is_drawing) return;
|
| 258 |
+
|
| 259 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 260 |
+
event.global
|
| 261 |
+
);
|
| 262 |
+
|
| 263 |
+
if (this.brush_textures) {
|
| 264 |
+
this.brush_textures.draw_segment(
|
| 265 |
+
this.last_x,
|
| 266 |
+
this.last_y,
|
| 267 |
+
local_pos.x,
|
| 268 |
+
local_pos.y,
|
| 269 |
+
this.state.brush_size,
|
| 270 |
+
this.state.color,
|
| 271 |
+
this.state.opacity,
|
| 272 |
+
this.state.mode
|
| 273 |
+
);
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
this.last_x = local_pos.x;
|
| 277 |
+
this.last_y = local_pos.y;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
/**
|
| 281 |
+
* Called when the user releases the mouse button.
|
| 282 |
+
* @private
|
| 283 |
+
*/
|
| 284 |
+
private on_pointer_up(): void {
|
| 285 |
+
if (!this.is_drawing) return;
|
| 286 |
+
|
| 287 |
+
this.is_drawing = false;
|
| 288 |
+
|
| 289 |
+
if (this.brush_textures) {
|
| 290 |
+
this.brush_textures.commit_stroke();
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
this.notify("change");
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
/**
|
| 297 |
+
* Sets up event listeners for drawing.
|
| 298 |
+
* @private
|
| 299 |
+
*/
|
| 300 |
+
private setup_event_listeners(): void {
|
| 301 |
+
this.cleanup_event_listeners();
|
| 302 |
+
|
| 303 |
+
this._bound_pointer_down = this.on_pointer_down.bind(this);
|
| 304 |
+
this._bound_pointer_move = this.on_pointer_move.bind(this);
|
| 305 |
+
this._bound_pointer_up = this.on_pointer_up.bind(this);
|
| 306 |
+
|
| 307 |
+
const image_container = this.image_editor_context.image_container;
|
| 308 |
+
image_container.eventMode = "static";
|
| 309 |
+
image_container.interactiveChildren = true;
|
| 310 |
+
|
| 311 |
+
const stage = this.image_editor_context.app.stage;
|
| 312 |
+
stage.eventMode = "static";
|
| 313 |
+
|
| 314 |
+
stage.on("pointerdown", this._bound_pointer_down);
|
| 315 |
+
stage.on("pointermove", this._bound_pointer_move);
|
| 316 |
+
stage.on("pointerup", this._bound_pointer_up);
|
| 317 |
+
stage.on("pointerupoutside", this._bound_pointer_up);
|
| 318 |
+
|
| 319 |
+
if (this.brush_cursor) {
|
| 320 |
+
this.brush_cursor.setup_event_listeners();
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
/**
|
| 325 |
+
* Cleans up event listeners.
|
| 326 |
+
* @private
|
| 327 |
+
*/
|
| 328 |
+
private cleanup_event_listeners(): void {
|
| 329 |
+
const stage = this.image_editor_context.app.stage;
|
| 330 |
+
|
| 331 |
+
if (this._bound_pointer_down) {
|
| 332 |
+
stage.off("pointerdown", this._bound_pointer_down);
|
| 333 |
+
this._bound_pointer_down = null;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
if (this._bound_pointer_move) {
|
| 337 |
+
stage.off("pointermove", this._bound_pointer_move);
|
| 338 |
+
this._bound_pointer_move = null;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
if (this._bound_pointer_up) {
|
| 342 |
+
stage.off("pointerup", this._bound_pointer_up);
|
| 343 |
+
stage.off("pointerupoutside", this._bound_pointer_up);
|
| 344 |
+
this._bound_pointer_up = null;
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
if (this.brush_cursor) {
|
| 348 |
+
this.brush_cursor.cleanup_event_listeners();
|
| 349 |
+
}
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
/**
|
| 353 |
+
* Sets the brush size.
|
| 354 |
+
* @param {number} size - The new brush size.
|
| 355 |
+
*/
|
| 356 |
+
set_brush_size(size: number): void {
|
| 357 |
+
this.brush_size = size;
|
| 358 |
+
|
| 359 |
+
if (this.state.mode === "draw") {
|
| 360 |
+
this.state.brush_size = size;
|
| 361 |
+
|
| 362 |
+
if (this.brush_cursor) {
|
| 363 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
/**
|
| 369 |
+
* Sets the brush color.
|
| 370 |
+
* @param {string|ColorInput} color - The new brush color.
|
| 371 |
+
*/
|
| 372 |
+
set_brush_color(color: string | ColorInput): void {
|
| 373 |
+
const color_string = tinycolor(color).toHexString();
|
| 374 |
+
|
| 375 |
+
this.state.color = color_string;
|
| 376 |
+
|
| 377 |
+
if (this.brush_cursor) {
|
| 378 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 379 |
+
}
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
/**
|
| 383 |
+
* Sets the brush opacity.
|
| 384 |
+
* @param {number} opacity - The new brush opacity.
|
| 385 |
+
*/
|
| 386 |
+
set_brush_opacity(opacity: number): void {
|
| 387 |
+
const clamped_opacity = Math.max(0, Math.min(1, opacity));
|
| 388 |
+
|
| 389 |
+
this.state.opacity = clamped_opacity;
|
| 390 |
+
|
| 391 |
+
if (this.brush_cursor) {
|
| 392 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
/**
|
| 397 |
+
* Sets the eraser size.
|
| 398 |
+
* @param {number} size - The new eraser size.
|
| 399 |
+
*/
|
| 400 |
+
set_eraser_size(size: number): void {
|
| 401 |
+
this.eraser_size = size;
|
| 402 |
+
|
| 403 |
+
if (this.state.mode === "erase") {
|
| 404 |
+
this.state.brush_size = size;
|
| 405 |
+
|
| 406 |
+
if (this.brush_cursor) {
|
| 407 |
+
this.brush_cursor.update_state(this.state, this.scale);
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
/**
|
| 413 |
+
* Gets the current size based on mode (brush or eraser).
|
| 414 |
+
* @returns {number} The current size.
|
| 415 |
+
*/
|
| 416 |
+
get_current_size(): number {
|
| 417 |
+
return this.state.mode === "draw" ? this.brush_size : this.eraser_size;
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
/**
|
| 421 |
+
* Shows or hides the brush preview.
|
| 422 |
+
* @param {boolean} show - Whether to show the preview.
|
| 423 |
+
*/
|
| 424 |
+
preview_brush(show: boolean): void {
|
| 425 |
+
if (this.brush_cursor) {
|
| 426 |
+
this.brush_cursor.preview_brush(show);
|
| 427 |
+
}
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
/**
|
| 431 |
+
* Cleans up resources used by the brush tool.
|
| 432 |
+
*/
|
| 433 |
+
cleanup(): void {
|
| 434 |
+
this.commit_pending_changes();
|
| 435 |
+
|
| 436 |
+
this.cleanup_event_listeners();
|
| 437 |
+
|
| 438 |
+
if (this.brush_textures) {
|
| 439 |
+
this.brush_textures.cleanup();
|
| 440 |
+
this.brush_textures = null;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
if (this.brush_cursor) {
|
| 444 |
+
this.brush_cursor.cleanup();
|
| 445 |
+
this.brush_cursor = null;
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
on<T extends string>(event: T, callback: () => void): void {
|
| 450 |
+
this.event_callbacks.set(event, [
|
| 451 |
+
...(this.event_callbacks.get(event) || []),
|
| 452 |
+
callback
|
| 453 |
+
]);
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
off<T extends string>(event: T, callback: () => void): void {
|
| 457 |
+
this.event_callbacks.set(
|
| 458 |
+
event,
|
| 459 |
+
this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || []
|
| 460 |
+
);
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
private notify<T extends string>(event: T): void {
|
| 464 |
+
for (const callback of this.event_callbacks.get(event) || []) {
|
| 465 |
+
callback();
|
| 466 |
+
}
|
| 467 |
+
}
|
| 468 |
+
}
|
6.0.0-dev.5/imageeditor/shared/brush/types.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type ColorInput } from "tinycolor2";
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* Represents the state of the brush tool.
|
| 5 |
+
* @interface BrushState
|
| 6 |
+
* @property {number} opacity - The opacity of the brush.
|
| 7 |
+
* @property {number} brush_size - The size of the brush.
|
| 8 |
+
* @property {string} color - The color of the brush in hex format.
|
| 9 |
+
* @property {"draw"|"erase"} mode - The mode of the brush, either draw or erase.
|
| 10 |
+
*/
|
| 11 |
+
export interface BrushState {
|
| 12 |
+
opacity: number;
|
| 13 |
+
brush_size: number;
|
| 14 |
+
color: string;
|
| 15 |
+
mode: "draw" | "erase";
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export interface Eraser {
|
| 19 |
+
/**
|
| 20 |
+
* The default size of the eraser.
|
| 21 |
+
*/
|
| 22 |
+
default_size: number | "auto";
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
export type ColorTuple = [ColorInput, number]; // [color, opacity]
|
| 26 |
+
|
| 27 |
+
export interface Brush extends Eraser {
|
| 28 |
+
/**
|
| 29 |
+
* The default color of the brush.
|
| 30 |
+
*/
|
| 31 |
+
default_color: ColorInput;
|
| 32 |
+
/**
|
| 33 |
+
* The colors to show in the color swatch.
|
| 34 |
+
* Can be either simple color strings or [color, opacity] tuples.
|
| 35 |
+
*/
|
| 36 |
+
colors: (ColorInput | ColorTuple)[];
|
| 37 |
+
/**
|
| 38 |
+
* Whether to show _only_ the color swatches specified in `colors`, or to show the color swatches specified in `colors` along with the colorpicker.
|
| 39 |
+
*/
|
| 40 |
+
color_mode: "fixed" | "defaults";
|
| 41 |
+
}
|
6.0.0-dev.5/imageeditor/shared/core/commands.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { writable } from "svelte/store";
|
| 2 |
+
import type { ImageEditorContext } from "./editor";
|
| 3 |
+
|
| 4 |
+
/**
|
| 5 |
+
* Base command interface that is added to the command_managers history
|
| 6 |
+
*/
|
| 7 |
+
export interface Command {
|
| 8 |
+
/**
|
| 9 |
+
* The name of the command
|
| 10 |
+
*/
|
| 11 |
+
name: string;
|
| 12 |
+
/**
|
| 13 |
+
* Optionally called when the command is first executed for multi-step commands
|
| 14 |
+
* @param args arguments to pass to the command
|
| 15 |
+
*/
|
| 16 |
+
start?: (...args: any) => any | Promise<any>;
|
| 17 |
+
/**
|
| 18 |
+
* Optionally called when the command is continued for multi-step commands
|
| 19 |
+
* @param args arguments to pass to the command
|
| 20 |
+
*/
|
| 21 |
+
continue?: (...args: any) => any | Promise<any>;
|
| 22 |
+
/**
|
| 23 |
+
* Optionally called when the command is stopped for multi-step commands
|
| 24 |
+
* @param args arguments to pass to the command
|
| 25 |
+
*/
|
| 26 |
+
stop?: (...args: any) => any | Promise<any>;
|
| 27 |
+
/**
|
| 28 |
+
* Called by the command manager to execute the command, can act as a no-op if the work has already been done
|
| 29 |
+
* This function must be able to recreate the command if the command is undone and redone (`stop`/`start`/`continue` will not be called again)
|
| 30 |
+
*/
|
| 31 |
+
execute(context?: ImageEditorContext): any | Promise<any>;
|
| 32 |
+
/**
|
| 33 |
+
* Called by the command manager to undo the command
|
| 34 |
+
* This function must be able to undo the work done by the execute function
|
| 35 |
+
*/
|
| 36 |
+
undo(): any | Promise<any>;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Creates a command node
|
| 41 |
+
* @param command command to add to the node
|
| 42 |
+
* @returns a command node
|
| 43 |
+
*/
|
| 44 |
+
export class CommandNode {
|
| 45 |
+
command: Command | null;
|
| 46 |
+
next: CommandNode | null;
|
| 47 |
+
previous: CommandNode | null;
|
| 48 |
+
|
| 49 |
+
constructor(command?: Command) {
|
| 50 |
+
this.command = command || null;
|
| 51 |
+
this.next = null;
|
| 52 |
+
this.previous = null;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
push(command: Command): void {
|
| 56 |
+
this.next = null;
|
| 57 |
+
|
| 58 |
+
const node = new CommandNode(command);
|
| 59 |
+
node.previous = this;
|
| 60 |
+
this.next = node;
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/**
|
| 65 |
+
* Creates a command manager
|
| 66 |
+
* @returns a command manager
|
| 67 |
+
*/
|
| 68 |
+
export class CommandManager {
|
| 69 |
+
history: CommandNode = new CommandNode();
|
| 70 |
+
current_history = writable(this.history);
|
| 71 |
+
|
| 72 |
+
undo(): void {
|
| 73 |
+
if (this.history.previous) {
|
| 74 |
+
this.history.command?.undo();
|
| 75 |
+
this.history = this.history.previous;
|
| 76 |
+
|
| 77 |
+
this.current_history.update(() => this.history);
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
redo(context?: ImageEditorContext): void {
|
| 81 |
+
if (this.history.next) {
|
| 82 |
+
this.history = this.history.next;
|
| 83 |
+
this.history.command?.execute(context);
|
| 84 |
+
this.current_history.update(() => this.history);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
async execute(command: Command, context: ImageEditorContext): Promise<void> {
|
| 89 |
+
await command.execute(context);
|
| 90 |
+
this.history.push(command);
|
| 91 |
+
this.history = this.history.next!;
|
| 92 |
+
|
| 93 |
+
this.current_history.update(() => this.history);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
async wait_for_next_frame(): Promise<void> {
|
| 97 |
+
return new Promise((resolve) => {
|
| 98 |
+
requestAnimationFrame(() => {
|
| 99 |
+
resolve();
|
| 100 |
+
});
|
| 101 |
+
});
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
async replay(
|
| 105 |
+
full_history: CommandNode,
|
| 106 |
+
context: ImageEditorContext
|
| 107 |
+
): Promise<void> {
|
| 108 |
+
while (full_history.previous) {
|
| 109 |
+
full_history = full_history.previous;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
while (full_history.next) {
|
| 113 |
+
await full_history.next.command!.execute(context);
|
| 114 |
+
full_history = full_history.next;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
this.history = full_history;
|
| 118 |
+
this.current_history.update(() => this.history);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
contains(command_name: string): boolean {
|
| 122 |
+
let current: CommandNode | null = this.history;
|
| 123 |
+
while (current) {
|
| 124 |
+
if (current.command?.name === command_name) return true;
|
| 125 |
+
current = current.next;
|
| 126 |
+
}
|
| 127 |
+
return false;
|
| 128 |
+
}
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Application, Container, Graphics, Sprite, Assets } from "pixi.js";
|
| 2 |
+
|
| 3 |
+
import { DropShadowFilter as BlurFilter } from "pixi-filters/drop-shadow";
|
| 4 |
+
|
| 5 |
+
import { ImageTool } from "../image/image";
|
| 6 |
+
|
| 7 |
+
import { ZoomTool } from "../zoom/zoom";
|
| 8 |
+
import type { Subtool, Tool as ToolbarTool } from "../Toolbar.svelte";
|
| 9 |
+
import type { Readable, Writable } from "svelte/store";
|
| 10 |
+
import { spring, type Spring } from "svelte/motion";
|
| 11 |
+
import { writable } from "svelte/store";
|
| 12 |
+
import { type ImageBlobs, type LayerOptions } from "../types";
|
| 13 |
+
import type { BrushTool } from "../brush/brush";
|
| 14 |
+
import type { CropTool } from "../crop/crop";
|
| 15 |
+
import { CommandManager, type Command } from "./commands";
|
| 16 |
+
import {
|
| 17 |
+
LayerManager,
|
| 18 |
+
AddLayerCommand,
|
| 19 |
+
RemoveLayerCommand,
|
| 20 |
+
ReorderLayerCommand
|
| 21 |
+
} from "./layers";
|
| 22 |
+
|
| 23 |
+
export interface Tool {
|
| 24 |
+
name: string;
|
| 25 |
+
setup(
|
| 26 |
+
context: ImageEditorContext,
|
| 27 |
+
tool: ToolbarTool,
|
| 28 |
+
subtool: Subtool
|
| 29 |
+
): Promise<void>;
|
| 30 |
+
cleanup(): void;
|
| 31 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void;
|
| 32 |
+
on?: (event: string, callback: () => void) => void;
|
| 33 |
+
off?: (event: string, callback: () => void) => void;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
const core_tools = ["image", "zoom"] as const;
|
| 37 |
+
|
| 38 |
+
interface ImageEditorOptions {
|
| 39 |
+
target_element: HTMLElement;
|
| 40 |
+
width: number;
|
| 41 |
+
height: number;
|
| 42 |
+
tools: ((typeof core_tools)[number] | Tool)[];
|
| 43 |
+
fixed_canvas?: boolean;
|
| 44 |
+
dark?: boolean;
|
| 45 |
+
border_region?: number;
|
| 46 |
+
layer_options?: LayerOptions;
|
| 47 |
+
pad_bottom?: number;
|
| 48 |
+
theme_mode?: "dark" | "light";
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
const core_tool_map = {
|
| 52 |
+
image: () => new ImageTool(),
|
| 53 |
+
zoom: () => new ZoomTool()
|
| 54 |
+
} as const;
|
| 55 |
+
|
| 56 |
+
export interface ImageEditorContext {
|
| 57 |
+
app: Application;
|
| 58 |
+
ui_container: Container;
|
| 59 |
+
image_container: Container;
|
| 60 |
+
background_image?: Sprite;
|
| 61 |
+
command_manager: CommandManager;
|
| 62 |
+
layer_manager: LayerManager;
|
| 63 |
+
dimensions: Readable<{ width: number; height: number }>;
|
| 64 |
+
scale: Readable<number>;
|
| 65 |
+
position: Readable<{ x: number; y: number }>;
|
| 66 |
+
set_image_properties: (properties: {
|
| 67 |
+
width?: number;
|
| 68 |
+
height?: number;
|
| 69 |
+
scale?: number;
|
| 70 |
+
position?: { x: number; y: number };
|
| 71 |
+
animate?: boolean;
|
| 72 |
+
}) => Promise<void>;
|
| 73 |
+
execute_command: (command: Command) => Promise<void> | void;
|
| 74 |
+
resize_canvas: (width: number, height: number) => void;
|
| 75 |
+
reset: () => void;
|
| 76 |
+
set_background_image: (image: Sprite) => void;
|
| 77 |
+
pad_bottom: number;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const spring_config = {
|
| 81 |
+
stiffness: 0.45,
|
| 82 |
+
damping: 0.8
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
interface EditorStatePublic {
|
| 86 |
+
position: { x: number; y: number };
|
| 87 |
+
scale: number;
|
| 88 |
+
subscribe: (callback: (value: any) => void) => () => void;
|
| 89 |
+
current_tool: ToolbarTool;
|
| 90 |
+
current_subtool: Subtool;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
class EditorState {
|
| 94 |
+
state: EditorStatePublic;
|
| 95 |
+
private scale: number;
|
| 96 |
+
private position: { x: number; y: number };
|
| 97 |
+
private subscribers: Set<(value: any) => void>;
|
| 98 |
+
constructor(editor: ImageEditor) {
|
| 99 |
+
if (!(editor instanceof ImageEditor)) {
|
| 100 |
+
throw new Error("EditorState must be created by ImageEditor");
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
this.scale = 1.0;
|
| 104 |
+
this.position = { x: 0, y: 0 };
|
| 105 |
+
this.subscribers = new Set();
|
| 106 |
+
// @ts-ignore
|
| 107 |
+
this.state = Object.freeze({
|
| 108 |
+
get position() {
|
| 109 |
+
return { ...this.position };
|
| 110 |
+
},
|
| 111 |
+
get scale() {
|
| 112 |
+
return this.scale;
|
| 113 |
+
},
|
| 114 |
+
get current_tool() {
|
| 115 |
+
return this.current_tool;
|
| 116 |
+
},
|
| 117 |
+
get current_subtool() {
|
| 118 |
+
return this.current_subtool;
|
| 119 |
+
},
|
| 120 |
+
subscribe: this.subscribe.bind(this)
|
| 121 |
+
});
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
_set_position(x: number, y: number): void {
|
| 125 |
+
const oldPosition = { ...this.position };
|
| 126 |
+
this.position = { x, y };
|
| 127 |
+
this._notify_subscribers("position", oldPosition, this.position);
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
_set_scale(scale: number): void {
|
| 131 |
+
const oldScale = this.scale;
|
| 132 |
+
this.scale = scale;
|
| 133 |
+
this._notify_subscribers("scale", oldScale, scale);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
_notify_subscribers(property: string, oldValue: any, newValue: any): void {
|
| 137 |
+
this.subscribers.forEach((callback) => {
|
| 138 |
+
callback({
|
| 139 |
+
property,
|
| 140 |
+
oldValue,
|
| 141 |
+
newValue,
|
| 142 |
+
timestamp: Date.now()
|
| 143 |
+
});
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
subscribe(callback: (value: any) => void): () => void {
|
| 148 |
+
this.subscribers.add(callback);
|
| 149 |
+
return () => this.subscribers.delete(callback);
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
export class ImageEditor {
|
| 154 |
+
public ready: Promise<void>;
|
| 155 |
+
public background_image_present = writable(false);
|
| 156 |
+
public min_zoom = writable(true);
|
| 157 |
+
|
| 158 |
+
private app!: Application;
|
| 159 |
+
private ui_container!: Container;
|
| 160 |
+
private image_container!: Container;
|
| 161 |
+
command_manager: CommandManager;
|
| 162 |
+
|
| 163 |
+
private layer_manager!: LayerManager;
|
| 164 |
+
private tools: Map<string, Tool> = new Map<string, Tool>();
|
| 165 |
+
private current_tool!: ToolbarTool;
|
| 166 |
+
private current_subtool!: Subtool;
|
| 167 |
+
private target_element: HTMLElement;
|
| 168 |
+
private width: number;
|
| 169 |
+
private height: number;
|
| 170 |
+
private original_width: number;
|
| 171 |
+
private original_height: number;
|
| 172 |
+
dimensions: Spring<{ width: number; height: number }>;
|
| 173 |
+
scale: Spring<number>;
|
| 174 |
+
private position: Spring<{ x: number; y: number }>;
|
| 175 |
+
private state: EditorState;
|
| 176 |
+
private scale_value = 1;
|
| 177 |
+
private position_value: { x: number; y: number } = { x: 0, y: 0 };
|
| 178 |
+
private dimensions_value: { width: number; height: number } = {
|
| 179 |
+
width: 0,
|
| 180 |
+
height: 0
|
| 181 |
+
};
|
| 182 |
+
layers: Writable<{
|
| 183 |
+
active_layer: string;
|
| 184 |
+
layers: {
|
| 185 |
+
name: string;
|
| 186 |
+
id: string;
|
| 187 |
+
user_created: boolean;
|
| 188 |
+
visible: boolean;
|
| 189 |
+
}[];
|
| 190 |
+
}> = writable({
|
| 191 |
+
active_layer: "",
|
| 192 |
+
layers: []
|
| 193 |
+
});
|
| 194 |
+
private outline_container!: Container;
|
| 195 |
+
private outline_graphics!: Graphics;
|
| 196 |
+
private background_image?: Sprite;
|
| 197 |
+
private ready_resolve!: (value: void | PromiseLike<void>) => void;
|
| 198 |
+
private event_callbacks: Map<string, (() => void)[]> = new Map();
|
| 199 |
+
private fixed_canvas: boolean;
|
| 200 |
+
private dark: boolean;
|
| 201 |
+
private border_region: number;
|
| 202 |
+
private layer_options: LayerOptions;
|
| 203 |
+
private overlay_container!: Container;
|
| 204 |
+
private overlay_graphics!: Graphics;
|
| 205 |
+
private pad_bottom: number;
|
| 206 |
+
private theme_mode: "dark" | "light";
|
| 207 |
+
constructor(options: ImageEditorOptions) {
|
| 208 |
+
this.pad_bottom = options.pad_bottom || 0;
|
| 209 |
+
this.dark = options.dark || false;
|
| 210 |
+
this.theme_mode = options.theme_mode || "dark";
|
| 211 |
+
this.target_element = options.target_element;
|
| 212 |
+
this.width = options.width;
|
| 213 |
+
this.height = options.height;
|
| 214 |
+
this.original_width = options.width;
|
| 215 |
+
this.original_height = options.height;
|
| 216 |
+
this.command_manager = new CommandManager();
|
| 217 |
+
|
| 218 |
+
this.ready = new Promise((resolve) => {
|
| 219 |
+
this.ready_resolve = resolve;
|
| 220 |
+
});
|
| 221 |
+
this.fixed_canvas = options.fixed_canvas || false;
|
| 222 |
+
this.tools = new Map<string, Tool>(
|
| 223 |
+
options.tools.map((tool) => {
|
| 224 |
+
if (typeof tool === "string") {
|
| 225 |
+
return [tool, core_tool_map[tool]()];
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
return [tool.name, tool];
|
| 229 |
+
})
|
| 230 |
+
);
|
| 231 |
+
|
| 232 |
+
for (const tool of this.tools.values()) {
|
| 233 |
+
if (tool?.on) {
|
| 234 |
+
tool.on("change", () => {
|
| 235 |
+
this.notify("change");
|
| 236 |
+
});
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
this.dimensions = spring(
|
| 241 |
+
{ width: this.width, height: this.height },
|
| 242 |
+
spring_config
|
| 243 |
+
);
|
| 244 |
+
this.scale = spring(1, spring_config);
|
| 245 |
+
this.position = spring({ x: 0, y: 0 }, spring_config);
|
| 246 |
+
this.state = new EditorState(this);
|
| 247 |
+
this.border_region = options.border_region || 0;
|
| 248 |
+
this.layer_options = options.layer_options || {
|
| 249 |
+
allow_additional_layers: true,
|
| 250 |
+
layers: ["Layer 1"],
|
| 251 |
+
disabled: false
|
| 252 |
+
};
|
| 253 |
+
this.scale.subscribe((scale) => {
|
| 254 |
+
this.state._set_scale(scale);
|
| 255 |
+
});
|
| 256 |
+
this.position.subscribe((position) => {
|
| 257 |
+
this.state._set_position(position.x, position.y);
|
| 258 |
+
});
|
| 259 |
+
this.init();
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
get context(): ImageEditorContext {
|
| 263 |
+
const editor = this;
|
| 264 |
+
return {
|
| 265 |
+
app: this.app,
|
| 266 |
+
ui_container: this.ui_container,
|
| 267 |
+
image_container: this.image_container,
|
| 268 |
+
get background_image() {
|
| 269 |
+
return editor.background_image;
|
| 270 |
+
},
|
| 271 |
+
pad_bottom: this.pad_bottom,
|
| 272 |
+
command_manager: this.command_manager,
|
| 273 |
+
layer_manager: this.layer_manager,
|
| 274 |
+
dimensions: { subscribe: this.dimensions.subscribe },
|
| 275 |
+
scale: { subscribe: this.scale.subscribe },
|
| 276 |
+
position: { subscribe: this.position.subscribe },
|
| 277 |
+
set_image_properties: this.set_image_properties.bind(this),
|
| 278 |
+
execute_command: this.execute_command.bind(this),
|
| 279 |
+
resize_canvas: this.resize_canvas.bind(this),
|
| 280 |
+
reset: this.reset.bind(this),
|
| 281 |
+
set_background_image: this.set_background_image.bind(this)
|
| 282 |
+
};
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
async init(): Promise<void> {
|
| 286 |
+
const container_box = this.target_element.getBoundingClientRect();
|
| 287 |
+
new ResizeObserver((entries) => {
|
| 288 |
+
entries.forEach((entry) => {
|
| 289 |
+
this.resize_canvas(
|
| 290 |
+
entry.contentBoxSize[0].inlineSize,
|
| 291 |
+
entry.contentBoxSize[0].blockSize
|
| 292 |
+
);
|
| 293 |
+
});
|
| 294 |
+
}).observe(this.target_element);
|
| 295 |
+
|
| 296 |
+
this.app = new Application();
|
| 297 |
+
|
| 298 |
+
if (!this.dark) {
|
| 299 |
+
(globalThis as any).__PIXI_APP__ = this.app;
|
| 300 |
+
}
|
| 301 |
+
await this.app.init({
|
| 302 |
+
width: container_box.width,
|
| 303 |
+
height: container_box.height,
|
| 304 |
+
backgroundAlpha: this.dark ? 0 : 1,
|
| 305 |
+
backgroundColor: this.theme_mode === "dark" ? "#27272a" : "#ffffff",
|
| 306 |
+
resolution: window.devicePixelRatio,
|
| 307 |
+
autoDensity: true,
|
| 308 |
+
antialias: true,
|
| 309 |
+
powerPreference: "high-performance"
|
| 310 |
+
});
|
| 311 |
+
|
| 312 |
+
const canvas = this.app.canvas as HTMLCanvasElement;
|
| 313 |
+
canvas.style.background = "transparent";
|
| 314 |
+
|
| 315 |
+
this.setup_containers();
|
| 316 |
+
|
| 317 |
+
this.layer_manager.create_background_layer(this.width, this.height);
|
| 318 |
+
|
| 319 |
+
this.layer_manager.init_layers(this.width, this.height);
|
| 320 |
+
|
| 321 |
+
for (const tool of this.tools.values()) {
|
| 322 |
+
await tool.setup(this.context, this.current_tool, this.current_subtool);
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
const zoom = this.tools.get("zoom") as ZoomTool;
|
| 326 |
+
if (zoom) {
|
| 327 |
+
zoom.min_zoom.subscribe((is_min_zoom) => {
|
| 328 |
+
this.min_zoom.set(is_min_zoom);
|
| 329 |
+
});
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
this.target_element.appendChild(canvas);
|
| 333 |
+
|
| 334 |
+
this.target_element.style.background = "transparent";
|
| 335 |
+
|
| 336 |
+
this.dimensions.subscribe((dimensions) => {
|
| 337 |
+
this.dimensions_value = dimensions;
|
| 338 |
+
this.image_container.width = dimensions.width;
|
| 339 |
+
this.image_container.height = dimensions.height;
|
| 340 |
+
});
|
| 341 |
+
|
| 342 |
+
this.scale.subscribe((scale) => {
|
| 343 |
+
this.scale_value = scale;
|
| 344 |
+
});
|
| 345 |
+
|
| 346 |
+
this.position.subscribe((position) => {
|
| 347 |
+
this.position_value = position;
|
| 348 |
+
});
|
| 349 |
+
|
| 350 |
+
this.app.ticker.add(() => {
|
| 351 |
+
this.image_container.scale.set(this.scale_value);
|
| 352 |
+
this.image_container.position.set(
|
| 353 |
+
this.position_value.x,
|
| 354 |
+
this.position_value.y
|
| 355 |
+
);
|
| 356 |
+
|
| 357 |
+
const effective_width = this.dimensions_value.width * this.scale_value;
|
| 358 |
+
const effective_height = this.dimensions_value.height * this.scale_value;
|
| 359 |
+
|
| 360 |
+
const local_x = Math.round(
|
| 361 |
+
this.image_container.position.x - this.outline_container.position.x
|
| 362 |
+
);
|
| 363 |
+
const local_y = Math.round(
|
| 364 |
+
this.image_container.position.y - this.outline_container.position.y
|
| 365 |
+
);
|
| 366 |
+
|
| 367 |
+
this.overlay_container.position.set(
|
| 368 |
+
this.outline_container.position.x,
|
| 369 |
+
this.outline_container.position.y
|
| 370 |
+
);
|
| 371 |
+
|
| 372 |
+
this.outline_graphics.clear();
|
| 373 |
+
this.outline_graphics
|
| 374 |
+
.rect(local_x, local_y, effective_width, effective_height)
|
| 375 |
+
.fill({
|
| 376 |
+
color: this.dark ? 0x333333 : 0xffffff,
|
| 377 |
+
alpha: 1
|
| 378 |
+
});
|
| 379 |
+
|
| 380 |
+
if (this.border_region > 0) {
|
| 381 |
+
const scaled_border = this.border_region * this.scale_value;
|
| 382 |
+
const border_x = local_x + scaled_border - 1;
|
| 383 |
+
const border_y = local_y + scaled_border - 1;
|
| 384 |
+
const border_width = effective_width - scaled_border * 2 + 1;
|
| 385 |
+
const border_height = effective_height - scaled_border * 2 + 1;
|
| 386 |
+
|
| 387 |
+
this.overlay_graphics.clear();
|
| 388 |
+
|
| 389 |
+
this.overlay_graphics
|
| 390 |
+
.rect(border_x, border_y, border_width, border_height)
|
| 391 |
+
.stroke({
|
| 392 |
+
color: 0x999999,
|
| 393 |
+
width: 1,
|
| 394 |
+
alpha: 0,
|
| 395 |
+
pixelLine: true
|
| 396 |
+
});
|
| 397 |
+
|
| 398 |
+
const dashLength = 5;
|
| 399 |
+
const gapLength = 5;
|
| 400 |
+
const totalLength = dashLength + gapLength;
|
| 401 |
+
const lineColor = 0x999999;
|
| 402 |
+
|
| 403 |
+
for (let x = border_x; x < border_x + border_width; x += totalLength) {
|
| 404 |
+
this.overlay_graphics
|
| 405 |
+
.rect(
|
| 406 |
+
x,
|
| 407 |
+
border_y,
|
| 408 |
+
Math.min(dashLength, border_x + border_width - x),
|
| 409 |
+
1
|
| 410 |
+
)
|
| 411 |
+
.fill({ color: lineColor, alpha: 0.7 });
|
| 412 |
+
|
| 413 |
+
this.overlay_graphics
|
| 414 |
+
.rect(
|
| 415 |
+
x,
|
| 416 |
+
border_y + border_height,
|
| 417 |
+
Math.min(dashLength, border_x + border_width - x),
|
| 418 |
+
1
|
| 419 |
+
)
|
| 420 |
+
.fill({ color: lineColor, alpha: 0.7 });
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
for (let y = border_y; y < border_y + border_height; y += totalLength) {
|
| 424 |
+
this.overlay_graphics
|
| 425 |
+
.rect(
|
| 426 |
+
border_x,
|
| 427 |
+
y,
|
| 428 |
+
1,
|
| 429 |
+
Math.min(dashLength, border_y + border_height - y)
|
| 430 |
+
)
|
| 431 |
+
.fill({ color: lineColor, alpha: 0.7 });
|
| 432 |
+
|
| 433 |
+
this.overlay_graphics
|
| 434 |
+
.rect(
|
| 435 |
+
border_x + border_width,
|
| 436 |
+
y,
|
| 437 |
+
1,
|
| 438 |
+
Math.min(dashLength, border_y + border_height - y)
|
| 439 |
+
)
|
| 440 |
+
.fill({ color: lineColor, alpha: 0.7 });
|
| 441 |
+
}
|
| 442 |
+
} else {
|
| 443 |
+
this.overlay_graphics.clear();
|
| 444 |
+
}
|
| 445 |
+
});
|
| 446 |
+
|
| 447 |
+
this.ready_resolve();
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
private setup_containers(): void {
|
| 451 |
+
this.image_container = new Container({
|
| 452 |
+
eventMode: "static",
|
| 453 |
+
sortableChildren: true
|
| 454 |
+
});
|
| 455 |
+
|
| 456 |
+
this.image_container.width = this.width;
|
| 457 |
+
this.image_container.height = this.height;
|
| 458 |
+
|
| 459 |
+
this.app.stage.sortableChildren = true;
|
| 460 |
+
this.app.stage.alpha = 1;
|
| 461 |
+
this.app.stage.addChild(this.image_container);
|
| 462 |
+
|
| 463 |
+
this.image_container.scale.set(1);
|
| 464 |
+
|
| 465 |
+
this.ui_container = new Container({
|
| 466 |
+
eventMode: "static"
|
| 467 |
+
});
|
| 468 |
+
this.app.stage.addChild(this.ui_container);
|
| 469 |
+
this.ui_container.width = this.width;
|
| 470 |
+
this.ui_container.height = this.height;
|
| 471 |
+
|
| 472 |
+
this.outline_container = new Container();
|
| 473 |
+
this.outline_container.zIndex = -10;
|
| 474 |
+
|
| 475 |
+
this.outline_graphics = new Graphics();
|
| 476 |
+
|
| 477 |
+
this.outline_graphics.rect(0, 0, this.width, this.height).fill({
|
| 478 |
+
color: this.dark ? 0x333333 : 0xffffff,
|
| 479 |
+
alpha: 0
|
| 480 |
+
});
|
| 481 |
+
|
| 482 |
+
if (!this.dark) {
|
| 483 |
+
const blurFilter = new BlurFilter({
|
| 484 |
+
alpha: 0.1,
|
| 485 |
+
blur: 2,
|
| 486 |
+
color: 0x000000,
|
| 487 |
+
offset: { x: 0, y: 0 },
|
| 488 |
+
quality: 4,
|
| 489 |
+
shadowOnly: false
|
| 490 |
+
});
|
| 491 |
+
|
| 492 |
+
this.outline_graphics.filters = [blurFilter];
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
this.outline_container.addChild(this.outline_graphics);
|
| 496 |
+
this.app.stage.addChild(this.outline_container);
|
| 497 |
+
|
| 498 |
+
this.overlay_container = new Container();
|
| 499 |
+
this.app.stage.addChild(this.overlay_container);
|
| 500 |
+
this.overlay_container.width = this.width;
|
| 501 |
+
this.overlay_container.height = this.height;
|
| 502 |
+
|
| 503 |
+
this.overlay_container.zIndex = 999;
|
| 504 |
+
this.overlay_container.eventMode = "static";
|
| 505 |
+
this.overlay_container.interactiveChildren = true;
|
| 506 |
+
this.overlay_graphics = new Graphics();
|
| 507 |
+
this.overlay_container.addChild(this.overlay_graphics);
|
| 508 |
+
|
| 509 |
+
this.overlay_graphics.rect(0, 0, this.width, this.height).fill({
|
| 510 |
+
color: 0x000000,
|
| 511 |
+
alpha: 0
|
| 512 |
+
});
|
| 513 |
+
|
| 514 |
+
const app_center_x = this.app.screen.width / 2;
|
| 515 |
+
const app_center_y = this.app.screen.height / 2;
|
| 516 |
+
|
| 517 |
+
this.image_container.position.set(app_center_x, app_center_y);
|
| 518 |
+
this.outline_container.position.set(app_center_x, app_center_y);
|
| 519 |
+
|
| 520 |
+
this.layer_manager = new LayerManager(
|
| 521 |
+
this.image_container,
|
| 522 |
+
this.app,
|
| 523 |
+
this.fixed_canvas,
|
| 524 |
+
this.dark,
|
| 525 |
+
this.border_region,
|
| 526 |
+
this.layer_options
|
| 527 |
+
);
|
| 528 |
+
this.layers = this.layer_manager.layer_store;
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
resize_canvas(width: number, height: number): void {
|
| 532 |
+
if (this.app.renderer) {
|
| 533 |
+
this.app.renderer.resize(width, height);
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
const app_center_x = width / 2;
|
| 537 |
+
const app_center_y = height / 2;
|
| 538 |
+
|
| 539 |
+
if (this.image_container) {
|
| 540 |
+
this.image_container.position.set(app_center_x, app_center_y);
|
| 541 |
+
}
|
| 542 |
+
if (this.outline_container) {
|
| 543 |
+
this.outline_container.position.set(app_center_x, app_center_y);
|
| 544 |
+
}
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
reset(): void {
|
| 548 |
+
const zoom = this.tools.get("zoom");
|
| 549 |
+
|
| 550 |
+
if (zoom) {
|
| 551 |
+
zoom.cleanup();
|
| 552 |
+
zoom.setup(this.context, this.current_tool, this.current_subtool);
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
const brush = this.tools.get("brush") as BrushTool;
|
| 556 |
+
|
| 557 |
+
if (brush) {
|
| 558 |
+
this.set_tool("draw");
|
| 559 |
+
}
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
async set_image_properties(properties: {
|
| 563 |
+
width?: number;
|
| 564 |
+
height?: number;
|
| 565 |
+
scale?: number;
|
| 566 |
+
position?: { x: number; y: number };
|
| 567 |
+
animate?: boolean;
|
| 568 |
+
}): Promise<void> {
|
| 569 |
+
let hard =
|
| 570 |
+
typeof properties.animate !== "undefined" ? !properties.animate : true;
|
| 571 |
+
if (properties.position) {
|
| 572 |
+
const pos = this.position.set(properties.position, { hard });
|
| 573 |
+
if (hard) {
|
| 574 |
+
await pos;
|
| 575 |
+
}
|
| 576 |
+
}
|
| 577 |
+
if (properties.scale) {
|
| 578 |
+
const scale = this.scale.set(properties.scale, { hard });
|
| 579 |
+
if (hard) {
|
| 580 |
+
await scale;
|
| 581 |
+
}
|
| 582 |
+
}
|
| 583 |
+
if (properties.width && properties.height) {
|
| 584 |
+
this.width = properties.width;
|
| 585 |
+
this.height = properties.height;
|
| 586 |
+
const dimensions = this.dimensions.set(
|
| 587 |
+
{ width: properties.width, height: properties.height },
|
| 588 |
+
{ hard }
|
| 589 |
+
);
|
| 590 |
+
if (hard) {
|
| 591 |
+
await dimensions;
|
| 592 |
+
}
|
| 593 |
+
}
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
async execute_command(command: Command): Promise<void> {
|
| 597 |
+
await this.command_manager.execute(command, this.context);
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
undo(): void {
|
| 601 |
+
this.command_manager.undo();
|
| 602 |
+
this.notify("change");
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
redo(): void {
|
| 606 |
+
this.command_manager.redo(this.context);
|
| 607 |
+
this.notify("change");
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
async add_image({
|
| 611 |
+
image,
|
| 612 |
+
resize = true
|
| 613 |
+
}: {
|
| 614 |
+
image: Blob | File;
|
| 615 |
+
resize?: boolean;
|
| 616 |
+
}): Promise<void> {
|
| 617 |
+
const image_tool = this.tools.get("image") as ImageTool;
|
| 618 |
+
await image_tool.add_image({
|
| 619 |
+
image,
|
| 620 |
+
fixed_canvas: this.fixed_canvas,
|
| 621 |
+
border_region: this.border_region
|
| 622 |
+
});
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
/**
|
| 626 |
+
* Adds an image from a URL as the background layer
|
| 627 |
+
* @param url The URL of the image to add
|
| 628 |
+
*/
|
| 629 |
+
async add_image_from_url(url: string): Promise<void> {
|
| 630 |
+
const image_tool = this.tools.get("image") as ImageTool;
|
| 631 |
+
const texture = await Assets.load(url);
|
| 632 |
+
await image_tool.add_image({
|
| 633 |
+
image: texture,
|
| 634 |
+
fixed_canvas: this.fixed_canvas,
|
| 635 |
+
border_region: this.border_region
|
| 636 |
+
});
|
| 637 |
+
|
| 638 |
+
const resize_tool = this.tools.get("resize") as any;
|
| 639 |
+
if (resize_tool && typeof resize_tool.set_border_region === "function") {
|
| 640 |
+
resize_tool.set_border_region(this.border_region);
|
| 641 |
+
}
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
set_tool(tool: ToolbarTool): void {
|
| 645 |
+
this.current_tool = tool;
|
| 646 |
+
|
| 647 |
+
for (const tool of this.tools.values()) {
|
| 648 |
+
tool.set_tool(this.current_tool, this.current_subtool);
|
| 649 |
+
}
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
set_subtool(subtool: Subtool): void {
|
| 653 |
+
this.current_subtool = subtool;
|
| 654 |
+
|
| 655 |
+
for (const tool of this.tools.values()) {
|
| 656 |
+
tool.set_tool(this.current_tool, this.current_subtool);
|
| 657 |
+
}
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
set_background_image(image: Sprite): void {
|
| 661 |
+
this.background_image = image;
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
async reset_canvas(): Promise<void> {
|
| 665 |
+
this.width = this.original_width;
|
| 666 |
+
this.height = this.original_height;
|
| 667 |
+
|
| 668 |
+
this.layer_manager.reset_layers(this.width, this.height);
|
| 669 |
+
this.background_image = undefined;
|
| 670 |
+
this.background_image_present.set(false);
|
| 671 |
+
this.command_manager.reset();
|
| 672 |
+
|
| 673 |
+
await this.set_image_properties({
|
| 674 |
+
width: this.width,
|
| 675 |
+
height: this.height,
|
| 676 |
+
scale: 1,
|
| 677 |
+
position: { x: 0, y: 0 },
|
| 678 |
+
animate: false
|
| 679 |
+
});
|
| 680 |
+
|
| 681 |
+
this.layer_manager.create_background_layer(this.width, this.height);
|
| 682 |
+
|
| 683 |
+
for (const tool of this.tools.values()) {
|
| 684 |
+
tool.cleanup();
|
| 685 |
+
tool.setup(this.context, this.current_tool, this.current_subtool);
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
const zoom_tool = this.tools.get("zoom") as ZoomTool;
|
| 689 |
+
if (zoom_tool) {
|
| 690 |
+
zoom_tool.min_zoom.subscribe((is_min_zoom) => {
|
| 691 |
+
this.min_zoom.set(is_min_zoom);
|
| 692 |
+
});
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
this.notify("change");
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
add_layer(): void {
|
| 699 |
+
const add_layer_command = new AddLayerCommand(this.context, {
|
| 700 |
+
width: this.width,
|
| 701 |
+
height: this.height,
|
| 702 |
+
user_created: true,
|
| 703 |
+
make_active: true
|
| 704 |
+
});
|
| 705 |
+
|
| 706 |
+
this.execute_command(add_layer_command);
|
| 707 |
+
this.notify("change");
|
| 708 |
+
}
|
| 709 |
+
|
| 710 |
+
/**
|
| 711 |
+
* Adds a new layer with an image loaded from a URL
|
| 712 |
+
* @param layer_urls The URLs of the images to load
|
| 713 |
+
* @returns A Promise that resolves when all layers are added
|
| 714 |
+
*/
|
| 715 |
+
async add_layers_from_url(layer_urls: string[] | undefined): Promise<void> {
|
| 716 |
+
this.command_manager.reset();
|
| 717 |
+
const _layers = this.layer_manager.get_layers();
|
| 718 |
+
_layers.forEach((l) => this.layer_manager.delete_layer(l.id));
|
| 719 |
+
|
| 720 |
+
if (layer_urls === undefined || layer_urls.length === 0) {
|
| 721 |
+
this.layer_manager.create_layer({
|
| 722 |
+
width: this.width,
|
| 723 |
+
height: this.height,
|
| 724 |
+
layer_name: undefined,
|
| 725 |
+
user_created: false
|
| 726 |
+
});
|
| 727 |
+
return;
|
| 728 |
+
}
|
| 729 |
+
|
| 730 |
+
const created_layer_ids: string[] = [];
|
| 731 |
+
for await (const url of layer_urls) {
|
| 732 |
+
const layer_id = await this.layer_manager.add_layer_from_url(url);
|
| 733 |
+
if (layer_id) {
|
| 734 |
+
created_layer_ids.push(layer_id);
|
| 735 |
+
}
|
| 736 |
+
}
|
| 737 |
+
|
| 738 |
+
if (created_layer_ids.length > 0) {
|
| 739 |
+
this.layer_manager.set_active_layer(created_layer_ids[0]);
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
const resize_tool = this.tools.get("resize") as any;
|
| 743 |
+
if (resize_tool && typeof resize_tool.set_border_region === "function") {
|
| 744 |
+
resize_tool.set_border_region(this.border_region);
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
this.notify("change");
|
| 748 |
+
this.notify("input");
|
| 749 |
+
}
|
| 750 |
+
|
| 751 |
+
set_layer(id: string): void {
|
| 752 |
+
this.layer_manager.set_active_layer(id);
|
| 753 |
+
this.notify("change");
|
| 754 |
+
}
|
| 755 |
+
|
| 756 |
+
move_layer(id: string, direction: "up" | "down"): void {
|
| 757 |
+
const reorder_layer_command = new ReorderLayerCommand(
|
| 758 |
+
this.context,
|
| 759 |
+
id,
|
| 760 |
+
direction
|
| 761 |
+
);
|
| 762 |
+
this.execute_command(reorder_layer_command);
|
| 763 |
+
this.notify("change");
|
| 764 |
+
}
|
| 765 |
+
|
| 766 |
+
delete_layer(id: string): void {
|
| 767 |
+
const remove_layer_command = new RemoveLayerCommand(this.context, id);
|
| 768 |
+
this.execute_command(remove_layer_command);
|
| 769 |
+
this.notify("change");
|
| 770 |
+
}
|
| 771 |
+
|
| 772 |
+
modify_canvas_size(
|
| 773 |
+
width: number,
|
| 774 |
+
height: number,
|
| 775 |
+
anchor:
|
| 776 |
+
| "top-left"
|
| 777 |
+
| "top"
|
| 778 |
+
| "top-right"
|
| 779 |
+
| "left"
|
| 780 |
+
| "center"
|
| 781 |
+
| "right"
|
| 782 |
+
| "bottom-left"
|
| 783 |
+
| "bottom"
|
| 784 |
+
| "bottom-right",
|
| 785 |
+
scale: boolean
|
| 786 |
+
): void {
|
| 787 |
+
const oldWidth = this.width;
|
| 788 |
+
const oldHeight = this.height;
|
| 789 |
+
|
| 790 |
+
this.layer_manager.resize_all_layers(
|
| 791 |
+
width,
|
| 792 |
+
height,
|
| 793 |
+
scale,
|
| 794 |
+
anchor,
|
| 795 |
+
oldWidth,
|
| 796 |
+
oldHeight
|
| 797 |
+
);
|
| 798 |
+
|
| 799 |
+
this.width = width;
|
| 800 |
+
this.height = height;
|
| 801 |
+
|
| 802 |
+
this.set_image_properties({
|
| 803 |
+
width,
|
| 804 |
+
height,
|
| 805 |
+
scale: 1,
|
| 806 |
+
position: { x: 0, y: 0 },
|
| 807 |
+
animate: false
|
| 808 |
+
});
|
| 809 |
+
this.notify("change");
|
| 810 |
+
}
|
| 811 |
+
|
| 812 |
+
async get_blobs(): Promise<ImageBlobs> {
|
| 813 |
+
const blobs = await this.layer_manager.get_blobs(this.width, this.height);
|
| 814 |
+
return blobs;
|
| 815 |
+
}
|
| 816 |
+
|
| 817 |
+
on(event: "change" | "input", callback: () => void): void {
|
| 818 |
+
this.event_callbacks.set(event, [
|
| 819 |
+
...(this.event_callbacks.get(event) || []),
|
| 820 |
+
callback
|
| 821 |
+
]);
|
| 822 |
+
}
|
| 823 |
+
|
| 824 |
+
off(event: "change", callback: () => void): void {
|
| 825 |
+
this.event_callbacks.set(
|
| 826 |
+
event,
|
| 827 |
+
this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || []
|
| 828 |
+
);
|
| 829 |
+
}
|
| 830 |
+
|
| 831 |
+
private notify(event: "change" | "input"): void {
|
| 832 |
+
for (const callback of this.event_callbacks.get(event) || []) {
|
| 833 |
+
callback();
|
| 834 |
+
}
|
| 835 |
+
}
|
| 836 |
+
|
| 837 |
+
destroy(): void {
|
| 838 |
+
if (!this.app) return;
|
| 839 |
+
|
| 840 |
+
this.app?.destroy();
|
| 841 |
+
}
|
| 842 |
+
|
| 843 |
+
resize(width: number, height: number): void {
|
| 844 |
+
this.set_image_properties({
|
| 845 |
+
width,
|
| 846 |
+
height
|
| 847 |
+
});
|
| 848 |
+
|
| 849 |
+
this.reset();
|
| 850 |
+
}
|
| 851 |
+
|
| 852 |
+
async get_crop_bounds(): Promise<{
|
| 853 |
+
image: Blob | null;
|
| 854 |
+
crop_dimensions: { width: number; height: number };
|
| 855 |
+
image_dimensions: { width: number; height: number };
|
| 856 |
+
x: number;
|
| 857 |
+
y: number;
|
| 858 |
+
}> {
|
| 859 |
+
const crop_tool = this.tools.get("crop") as CropTool;
|
| 860 |
+
const crop_bounds = crop_tool.get_crop_bounds();
|
| 861 |
+
const image = await crop_tool.get_image();
|
| 862 |
+
|
| 863 |
+
return {
|
| 864 |
+
image,
|
| 865 |
+
...crop_bounds
|
| 866 |
+
};
|
| 867 |
+
}
|
| 868 |
+
|
| 869 |
+
get background_image_sprite(): Sprite | undefined {
|
| 870 |
+
return this.background_image;
|
| 871 |
+
}
|
| 872 |
+
|
| 873 |
+
set_layer_options(layer_options: LayerOptions): void {
|
| 874 |
+
this.layer_options = layer_options;
|
| 875 |
+
this.layer_manager.set_layer_options(
|
| 876 |
+
layer_options,
|
| 877 |
+
this.width,
|
| 878 |
+
this.height
|
| 879 |
+
);
|
| 880 |
+
}
|
| 881 |
+
|
| 882 |
+
toggle_layer_visibility(id: string): void {
|
| 883 |
+
this.layer_manager.toggle_layer_visibility(id);
|
| 884 |
+
}
|
| 885 |
+
}
|
6.0.0-dev.5/imageeditor/shared/core/layers.ts
ADDED
|
@@ -0,0 +1,1092 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Application,
|
| 3 |
+
Container,
|
| 4 |
+
Graphics,
|
| 5 |
+
Sprite,
|
| 6 |
+
RenderTexture,
|
| 7 |
+
SCALE_MODES,
|
| 8 |
+
Texture,
|
| 9 |
+
Assets
|
| 10 |
+
} from "pixi.js";
|
| 11 |
+
|
| 12 |
+
import { get_canvas_blob } from "../utils/pixi";
|
| 13 |
+
import type { Writable } from "svelte/store";
|
| 14 |
+
import { writable, get } from "svelte/store";
|
| 15 |
+
import { type ImageBlobs, type LayerOptions } from "../types";
|
| 16 |
+
import { type Command } from "./commands";
|
| 17 |
+
import { type ImageEditorContext } from "./editor";
|
| 18 |
+
|
| 19 |
+
export class LayerManager {
|
| 20 |
+
private layers: {
|
| 21 |
+
name: string;
|
| 22 |
+
id: string;
|
| 23 |
+
container: Container;
|
| 24 |
+
user_created: boolean;
|
| 25 |
+
visible: boolean;
|
| 26 |
+
}[] = [];
|
| 27 |
+
private active_layer: Container | null = null;
|
| 28 |
+
private active_layer_id: string | null = null;
|
| 29 |
+
private draw_textures: Map<Container, RenderTexture> = new Map();
|
| 30 |
+
layer_store: Writable<{
|
| 31 |
+
active_layer: string;
|
| 32 |
+
layers: {
|
| 33 |
+
name: string;
|
| 34 |
+
id: string;
|
| 35 |
+
user_created: boolean;
|
| 36 |
+
visible: boolean;
|
| 37 |
+
}[];
|
| 38 |
+
}> = writable({
|
| 39 |
+
active_layer: "",
|
| 40 |
+
|
| 41 |
+
layers: []
|
| 42 |
+
});
|
| 43 |
+
private background_layer: Container | null = null;
|
| 44 |
+
private image_container: Container;
|
| 45 |
+
private app: Application;
|
| 46 |
+
private fixed_canvas: boolean;
|
| 47 |
+
private dark: boolean;
|
| 48 |
+
private border_region: number;
|
| 49 |
+
private layer_options: LayerOptions;
|
| 50 |
+
constructor(
|
| 51 |
+
image_container: Container,
|
| 52 |
+
app: Application,
|
| 53 |
+
fixed_canvas: boolean,
|
| 54 |
+
dark: boolean,
|
| 55 |
+
border_region: number,
|
| 56 |
+
layer_options: LayerOptions
|
| 57 |
+
) {
|
| 58 |
+
this.image_container = image_container;
|
| 59 |
+
this.app = app;
|
| 60 |
+
this.fixed_canvas = fixed_canvas;
|
| 61 |
+
this.dark = dark;
|
| 62 |
+
this.border_region = border_region;
|
| 63 |
+
this.layer_options = layer_options;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
toggle_layer_visibility(id: string): void {
|
| 67 |
+
const layer = this.layers.find((l) => l.id === id);
|
| 68 |
+
if (layer) {
|
| 69 |
+
layer.container.visible = !layer.container.visible;
|
| 70 |
+
layer.visible = layer.container.visible;
|
| 71 |
+
this.layer_store.update((state) => ({
|
| 72 |
+
active_layer: state.active_layer,
|
| 73 |
+
layers: this.layers
|
| 74 |
+
}));
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
create_background_layer(width: number, height: number): Container {
|
| 79 |
+
if (this.background_layer) {
|
| 80 |
+
this.background_layer.destroy();
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
const layer = new Container();
|
| 84 |
+
|
| 85 |
+
this.background_layer = layer;
|
| 86 |
+
|
| 87 |
+
const bg_texture = RenderTexture.create({
|
| 88 |
+
width,
|
| 89 |
+
height,
|
| 90 |
+
resolution: window.devicePixelRatio,
|
| 91 |
+
antialias: true,
|
| 92 |
+
scaleMode: SCALE_MODES.NEAREST
|
| 93 |
+
});
|
| 94 |
+
|
| 95 |
+
const bg_sprite = new Sprite(bg_texture);
|
| 96 |
+
layer.addChild(bg_sprite);
|
| 97 |
+
|
| 98 |
+
const clear_graphics = new Graphics();
|
| 99 |
+
clear_graphics.clear();
|
| 100 |
+
|
| 101 |
+
clear_graphics
|
| 102 |
+
.rect(0, 0, width, height)
|
| 103 |
+
.fill({ color: this.dark ? 0x333333 : 0xffffff, alpha: 1 });
|
| 104 |
+
|
| 105 |
+
this.app.renderer.render({
|
| 106 |
+
container: clear_graphics,
|
| 107 |
+
target: bg_texture,
|
| 108 |
+
clear: true
|
| 109 |
+
});
|
| 110 |
+
|
| 111 |
+
this.image_container.addChild(layer);
|
| 112 |
+
|
| 113 |
+
layer.zIndex = -1;
|
| 114 |
+
|
| 115 |
+
this.update_layer_order();
|
| 116 |
+
return layer;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
set_layer_options(
|
| 120 |
+
layer_options: LayerOptions,
|
| 121 |
+
width: number,
|
| 122 |
+
height: number
|
| 123 |
+
): void {
|
| 124 |
+
this.layer_options = layer_options;
|
| 125 |
+
this.reset_layers(width, height);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
/**
|
| 129 |
+
* Creates a background layer from an image URL
|
| 130 |
+
* @param url The URL of the image to use as background
|
| 131 |
+
* @param width The width of the layer (if fixed_canvas is true)
|
| 132 |
+
* @param height The height of the layer (if fixed_canvas is true)
|
| 133 |
+
* @param borderRegion Minimum border region to add around the image for outpainting (in pixels)
|
| 134 |
+
* @returns A Promise that resolves to the background Container when the image is loaded
|
| 135 |
+
*/
|
| 136 |
+
async create_background_layer_from_url(
|
| 137 |
+
url: string,
|
| 138 |
+
width?: number,
|
| 139 |
+
height?: number
|
| 140 |
+
): Promise<Container> {
|
| 141 |
+
const layer = this.create_background_layer(
|
| 142 |
+
width || this.image_container.width,
|
| 143 |
+
height || this.image_container.height
|
| 144 |
+
);
|
| 145 |
+
|
| 146 |
+
try {
|
| 147 |
+
const texture = await Texture.from(url);
|
| 148 |
+
const sprite = new Sprite(texture);
|
| 149 |
+
|
| 150 |
+
const imageWidth = sprite.texture.width;
|
| 151 |
+
const imageHeight = sprite.texture.height;
|
| 152 |
+
|
| 153 |
+
const containerWidth = width || this.image_container.width;
|
| 154 |
+
const containerHeight = height || this.image_container.height;
|
| 155 |
+
|
| 156 |
+
if (this.fixed_canvas) {
|
| 157 |
+
const effectiveContainerWidth = Math.max(
|
| 158 |
+
containerWidth - this.border_region * 2,
|
| 159 |
+
10
|
| 160 |
+
);
|
| 161 |
+
const effectiveContainerHeight = Math.max(
|
| 162 |
+
containerHeight - this.border_region * 2,
|
| 163 |
+
10
|
| 164 |
+
);
|
| 165 |
+
|
| 166 |
+
const imageAspectRatio = imageWidth / imageHeight;
|
| 167 |
+
const containerAspectRatio =
|
| 168 |
+
effectiveContainerWidth / effectiveContainerHeight;
|
| 169 |
+
|
| 170 |
+
let finalWidth, finalHeight;
|
| 171 |
+
let posX = this.border_region,
|
| 172 |
+
posY = this.border_region;
|
| 173 |
+
|
| 174 |
+
if (
|
| 175 |
+
imageWidth <= effectiveContainerWidth &&
|
| 176 |
+
imageHeight <= effectiveContainerHeight
|
| 177 |
+
) {
|
| 178 |
+
finalWidth = imageWidth;
|
| 179 |
+
finalHeight = imageHeight;
|
| 180 |
+
} else {
|
| 181 |
+
if (imageAspectRatio > containerAspectRatio) {
|
| 182 |
+
finalWidth = effectiveContainerWidth;
|
| 183 |
+
finalHeight = effectiveContainerWidth / imageAspectRatio;
|
| 184 |
+
} else {
|
| 185 |
+
finalHeight = effectiveContainerHeight;
|
| 186 |
+
finalWidth = effectiveContainerHeight * imageAspectRatio;
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
posX += Math.round((effectiveContainerWidth - finalWidth) / 2);
|
| 191 |
+
posY += Math.round((effectiveContainerHeight - finalHeight) / 2);
|
| 192 |
+
sprite.width = finalWidth;
|
| 193 |
+
sprite.height = finalHeight;
|
| 194 |
+
sprite.position.set(posX, posY);
|
| 195 |
+
} else {
|
| 196 |
+
sprite.position.set(this.border_region, this.border_region);
|
| 197 |
+
|
| 198 |
+
if (this.background_layer) {
|
| 199 |
+
this.background_layer.destroy();
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
const totalWidth = imageWidth + this.border_region * 2;
|
| 203 |
+
const totalHeight = imageHeight + this.border_region * 2;
|
| 204 |
+
const newLayer = this.create_background_layer(totalWidth, totalHeight);
|
| 205 |
+
|
| 206 |
+
sprite.width = imageWidth;
|
| 207 |
+
sprite.height = imageHeight;
|
| 208 |
+
newLayer.addChild(sprite);
|
| 209 |
+
|
| 210 |
+
return newLayer;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
layer.addChild(sprite);
|
| 214 |
+
|
| 215 |
+
return layer;
|
| 216 |
+
} catch (error) {
|
| 217 |
+
console.error("Error loading image from URL:", error);
|
| 218 |
+
return layer;
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
create_layer({
|
| 223 |
+
width,
|
| 224 |
+
height,
|
| 225 |
+
layer_name,
|
| 226 |
+
user_created,
|
| 227 |
+
layer_id = undefined,
|
| 228 |
+
make_active = false
|
| 229 |
+
}: {
|
| 230 |
+
width: number;
|
| 231 |
+
height: number;
|
| 232 |
+
layer_name?: string;
|
| 233 |
+
user_created: boolean;
|
| 234 |
+
layer_id?: string;
|
| 235 |
+
make_active?: boolean;
|
| 236 |
+
}): Container {
|
| 237 |
+
const layer = new Container();
|
| 238 |
+
const _layer_id = layer_id || Math.random().toString(36).substring(2, 15);
|
| 239 |
+
const _layer_name = layer_name || `Layer ${this.layers.length + 1}`;
|
| 240 |
+
|
| 241 |
+
this.layers.push({
|
| 242 |
+
name: _layer_name,
|
| 243 |
+
id: _layer_id,
|
| 244 |
+
container: layer,
|
| 245 |
+
user_created,
|
| 246 |
+
visible: true
|
| 247 |
+
});
|
| 248 |
+
|
| 249 |
+
this.image_container.addChild(layer);
|
| 250 |
+
|
| 251 |
+
const draw_texture = RenderTexture.create({
|
| 252 |
+
width,
|
| 253 |
+
height,
|
| 254 |
+
resolution: window.devicePixelRatio,
|
| 255 |
+
antialias: true,
|
| 256 |
+
scaleMode: SCALE_MODES.NEAREST
|
| 257 |
+
});
|
| 258 |
+
|
| 259 |
+
const canvas_sprite = new Sprite(draw_texture);
|
| 260 |
+
layer.addChild(canvas_sprite);
|
| 261 |
+
|
| 262 |
+
const clear_graphics = new Graphics();
|
| 263 |
+
clear_graphics.clear();
|
| 264 |
+
clear_graphics.beginFill(0, 0);
|
| 265 |
+
clear_graphics.drawRect(0, 0, width, height);
|
| 266 |
+
clear_graphics.endFill();
|
| 267 |
+
|
| 268 |
+
this.app.renderer.render({
|
| 269 |
+
container: clear_graphics,
|
| 270 |
+
target: draw_texture,
|
| 271 |
+
clear: true
|
| 272 |
+
});
|
| 273 |
+
|
| 274 |
+
this.draw_textures.set(layer, draw_texture);
|
| 275 |
+
|
| 276 |
+
this.update_layer_order();
|
| 277 |
+
if (make_active) {
|
| 278 |
+
this.set_active_layer(_layer_id);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
this.layer_store.set({
|
| 282 |
+
active_layer: this.active_layer_id || "",
|
| 283 |
+
layers: this.layers
|
| 284 |
+
});
|
| 285 |
+
return layer;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
/**
|
| 289 |
+
* Creates a new layer with an image loaded from a URL
|
| 290 |
+
* @param url The URL of the image to load
|
| 291 |
+
* @param borderRegion Minimum border region to add around the image for outpainting (in pixels)
|
| 292 |
+
* @returns A Promise that resolves to the layer ID
|
| 293 |
+
*/
|
| 294 |
+
async add_layer_from_url(url: string): Promise<string> {
|
| 295 |
+
const { width, height } = this.image_container.getLocalBounds();
|
| 296 |
+
const layer = this.create_layer({
|
| 297 |
+
width,
|
| 298 |
+
height,
|
| 299 |
+
layer_name: "Layer 1",
|
| 300 |
+
user_created: true
|
| 301 |
+
});
|
| 302 |
+
|
| 303 |
+
const layerIndex = this.layers.findIndex((l) => l.container === layer);
|
| 304 |
+
if (layerIndex === -1) {
|
| 305 |
+
console.error("Could not find newly created layer");
|
| 306 |
+
return "";
|
| 307 |
+
}
|
| 308 |
+
const layerId = this.layers[layerIndex].id;
|
| 309 |
+
|
| 310 |
+
try {
|
| 311 |
+
const texture = await Assets.load(url);
|
| 312 |
+
|
| 313 |
+
const drawTexture = this.draw_textures.get(layer);
|
| 314 |
+
if (!drawTexture) {
|
| 315 |
+
console.error("No draw texture found for layer");
|
| 316 |
+
return layerId;
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
const sprite = new Sprite(texture);
|
| 320 |
+
const imageWidth = sprite.width;
|
| 321 |
+
const imageHeight = sprite.height;
|
| 322 |
+
|
| 323 |
+
let posX = this.border_region;
|
| 324 |
+
let posY = this.border_region;
|
| 325 |
+
|
| 326 |
+
const effectiveWidth = this.fixed_canvas
|
| 327 |
+
? width - this.border_region * 2
|
| 328 |
+
: width;
|
| 329 |
+
const effectiveHeight = this.fixed_canvas
|
| 330 |
+
? height - this.border_region * 2
|
| 331 |
+
: height;
|
| 332 |
+
|
| 333 |
+
if (imageWidth < effectiveWidth || imageHeight < effectiveHeight) {
|
| 334 |
+
posX = Math.floor((effectiveWidth - imageWidth) / 2);
|
| 335 |
+
posY = Math.floor((effectiveHeight - imageHeight) / 2);
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
sprite.position.set(posX, posY);
|
| 339 |
+
|
| 340 |
+
if (imageWidth > effectiveWidth || imageHeight > effectiveHeight) {
|
| 341 |
+
const imageAspectRatio = imageWidth / imageHeight;
|
| 342 |
+
const areaAspectRatio = effectiveWidth / effectiveHeight;
|
| 343 |
+
|
| 344 |
+
let finalWidth, finalHeight;
|
| 345 |
+
|
| 346 |
+
if (imageAspectRatio > areaAspectRatio) {
|
| 347 |
+
finalWidth = effectiveWidth;
|
| 348 |
+
finalHeight = effectiveWidth / imageAspectRatio;
|
| 349 |
+
} else {
|
| 350 |
+
finalHeight = effectiveHeight;
|
| 351 |
+
finalWidth = effectiveHeight * imageAspectRatio;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
sprite.width = finalWidth;
|
| 355 |
+
sprite.height = finalHeight;
|
| 356 |
+
|
| 357 |
+
posX =
|
| 358 |
+
this.border_region + Math.floor((effectiveWidth - finalWidth) / 2);
|
| 359 |
+
posY =
|
| 360 |
+
this.border_region + Math.floor((effectiveHeight - finalHeight) / 2);
|
| 361 |
+
sprite.position.set(posX, posY);
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
this.app.renderer.render(sprite, { renderTexture: drawTexture });
|
| 365 |
+
|
| 366 |
+
this.set_active_layer(layerId);
|
| 367 |
+
|
| 368 |
+
return layerId;
|
| 369 |
+
} catch (error) {
|
| 370 |
+
console.error("Error loading image from URL:", error);
|
| 371 |
+
return layerId;
|
| 372 |
+
}
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
get_active_layer(): typeof this.active_layer {
|
| 376 |
+
return this.active_layer;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
set_active_layer(id: string): void {
|
| 380 |
+
if (this.layers.some((l) => l.id === id)) {
|
| 381 |
+
this.active_layer =
|
| 382 |
+
this.layers.find((l) => l.id === id)?.container ||
|
| 383 |
+
this.layers[0]?.container ||
|
| 384 |
+
null;
|
| 385 |
+
this.active_layer_id = id;
|
| 386 |
+
this.layer_store.set({
|
| 387 |
+
active_layer: id,
|
| 388 |
+
layers: this.layers
|
| 389 |
+
});
|
| 390 |
+
}
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
get_layers(): typeof this.layers {
|
| 394 |
+
return this.layers;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
get_layer_textures(id: string): { draw: RenderTexture } | null {
|
| 398 |
+
const layer = this.layers.find((l) => l.id === id);
|
| 399 |
+
if (layer) {
|
| 400 |
+
const draw = this.draw_textures.get(layer.container);
|
| 401 |
+
if (draw) {
|
| 402 |
+
return { draw };
|
| 403 |
+
}
|
| 404 |
+
}
|
| 405 |
+
return null;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
delete_layer(id: string): void {
|
| 409 |
+
const index = this.layers.findIndex((l) => l.id === id);
|
| 410 |
+
if (index > -1) {
|
| 411 |
+
const draw_texture = this.draw_textures.get(this.layers[index].container);
|
| 412 |
+
if (draw_texture) {
|
| 413 |
+
draw_texture.destroy();
|
| 414 |
+
this.draw_textures.delete(this.layers[index].container);
|
| 415 |
+
}
|
| 416 |
+
this.layers[index].container.destroy();
|
| 417 |
+
if (this.active_layer === this.layers[index].container) {
|
| 418 |
+
const new_active_layer = this.layers[Math.max(0, index - 1)] || null;
|
| 419 |
+
this.active_layer = new_active_layer?.container || null;
|
| 420 |
+
this.active_layer_id = new_active_layer?.id || null;
|
| 421 |
+
}
|
| 422 |
+
this.layers = this.layers.filter((l) => l.id !== id);
|
| 423 |
+
|
| 424 |
+
this.layer_store.update((_layers) => ({
|
| 425 |
+
active_layer:
|
| 426 |
+
_layers.active_layer === id
|
| 427 |
+
? this.layers[this.layers.length - 1]?.id
|
| 428 |
+
: _layers.active_layer,
|
| 429 |
+
layers: this.layers
|
| 430 |
+
}));
|
| 431 |
+
|
| 432 |
+
this.update_layer_order();
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
private update_layer_order(): void {
|
| 437 |
+
if (this.background_layer) {
|
| 438 |
+
this.background_layer.zIndex = -1;
|
| 439 |
+
}
|
| 440 |
+
this.layers.forEach((layer, index) => {
|
| 441 |
+
layer.container.zIndex = index;
|
| 442 |
+
});
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
move_layer(id: string, direction: "up" | "down"): void {
|
| 446 |
+
const index = this.layers.findIndex((l) => l.id === id);
|
| 447 |
+
if (index > -1) {
|
| 448 |
+
const new_index = direction === "up" ? index - 1 : index + 1;
|
| 449 |
+
|
| 450 |
+
this.layers = this.layers.map((l, i) => {
|
| 451 |
+
if (i === index) {
|
| 452 |
+
return this.layers[new_index];
|
| 453 |
+
}
|
| 454 |
+
if (i === new_index) {
|
| 455 |
+
return this.layers[index];
|
| 456 |
+
}
|
| 457 |
+
return l;
|
| 458 |
+
});
|
| 459 |
+
this.update_layer_order();
|
| 460 |
+
this.layer_store.update((_layers) => ({
|
| 461 |
+
active_layer: id,
|
| 462 |
+
layers: this.layers
|
| 463 |
+
}));
|
| 464 |
+
}
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
/**
|
| 468 |
+
* Resizes all layers to a new width and height
|
| 469 |
+
* @param newWidth The new width of the layers
|
| 470 |
+
* @param newHeight The new height of the layers
|
| 471 |
+
* @param scale If true, scales the layer content to fit the new dimensions. If false, keeps the content size unchanged.
|
| 472 |
+
* @param anchor The anchor point to position the content relative to the new size
|
| 473 |
+
*/
|
| 474 |
+
resize_all_layers(
|
| 475 |
+
newWidth: number,
|
| 476 |
+
newHeight: number,
|
| 477 |
+
scale: boolean,
|
| 478 |
+
anchor:
|
| 479 |
+
| "top-left"
|
| 480 |
+
| "top"
|
| 481 |
+
| "top-right"
|
| 482 |
+
| "left"
|
| 483 |
+
| "center"
|
| 484 |
+
| "right"
|
| 485 |
+
| "bottom-left"
|
| 486 |
+
| "bottom"
|
| 487 |
+
| "bottom-right",
|
| 488 |
+
oldCanvasWidth: number,
|
| 489 |
+
oldCanvasHeight: number
|
| 490 |
+
): void {
|
| 491 |
+
const oldLayersById = new Map(this.layers.map((l) => [l.id, l]));
|
| 492 |
+
const oldBackgroundLayer = this.background_layer;
|
| 493 |
+
|
| 494 |
+
const calculateOffset = (): { offsetX: number; offsetY: number } => {
|
| 495 |
+
let offsetX = 0;
|
| 496 |
+
let offsetY = 0;
|
| 497 |
+
const deltaWidth = newWidth - oldCanvasWidth;
|
| 498 |
+
const deltaHeight = newHeight - oldCanvasHeight;
|
| 499 |
+
|
| 500 |
+
if (anchor.includes("left")) {
|
| 501 |
+
offsetX = 0;
|
| 502 |
+
} else if (anchor.includes("right")) {
|
| 503 |
+
offsetX = deltaWidth;
|
| 504 |
+
} else {
|
| 505 |
+
offsetX = Math.floor(deltaWidth / 2);
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
if (anchor.includes("top")) {
|
| 509 |
+
offsetY = 0;
|
| 510 |
+
} else if (anchor.includes("bottom")) {
|
| 511 |
+
offsetY = deltaHeight;
|
| 512 |
+
} else {
|
| 513 |
+
offsetY = Math.floor(deltaHeight / 2);
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
return { offsetX, offsetY };
|
| 517 |
+
};
|
| 518 |
+
|
| 519 |
+
this.background_layer = this._resize_background_layer(
|
| 520 |
+
oldBackgroundLayer,
|
| 521 |
+
newWidth,
|
| 522 |
+
newHeight,
|
| 523 |
+
scale,
|
| 524 |
+
calculateOffset
|
| 525 |
+
);
|
| 526 |
+
|
| 527 |
+
const processedLayers: {
|
| 528 |
+
name: string;
|
| 529 |
+
id: string;
|
| 530 |
+
container: Container;
|
| 531 |
+
user_created: boolean;
|
| 532 |
+
}[] = [];
|
| 533 |
+
|
| 534 |
+
const oldLayerData = this.layers.map((oldLayer) => ({
|
| 535 |
+
id: oldLayer.id,
|
| 536 |
+
name: oldLayer.name,
|
| 537 |
+
user_created: oldLayer.user_created,
|
| 538 |
+
texture: this.draw_textures.get(oldLayer.container),
|
| 539 |
+
container: oldLayer.container
|
| 540 |
+
}));
|
| 541 |
+
|
| 542 |
+
this.layers = [];
|
| 543 |
+
this.draw_textures.clear();
|
| 544 |
+
for (const oldData of oldLayerData) {
|
| 545 |
+
const newLayer = this._resize_single_layer(
|
| 546 |
+
oldData,
|
| 547 |
+
newWidth,
|
| 548 |
+
newHeight,
|
| 549 |
+
scale,
|
| 550 |
+
calculateOffset
|
| 551 |
+
);
|
| 552 |
+
if (newLayer) {
|
| 553 |
+
processedLayers.push(newLayer);
|
| 554 |
+
}
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
const currentActiveId = get(this.layer_store).active_layer;
|
| 558 |
+
const activeLayerExists = processedLayers.some(
|
| 559 |
+
(l) => l.id === currentActiveId
|
| 560 |
+
);
|
| 561 |
+
|
| 562 |
+
if (!activeLayerExists && processedLayers.length > 0) {
|
| 563 |
+
this.set_active_layer(processedLayers[0].id);
|
| 564 |
+
} else if (processedLayers.length === 0) {
|
| 565 |
+
this.layer_store.update((s) => ({ ...s, active_layer: "" }));
|
| 566 |
+
} else {
|
| 567 |
+
this.layer_store.update((s) => ({ ...s }));
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
this.update_layer_order();
|
| 571 |
+
|
| 572 |
+
setTimeout(() => {
|
| 573 |
+
Assets.cache.reset();
|
| 574 |
+
this.app.renderer.textureGC.run();
|
| 575 |
+
}, 100);
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
/**
|
| 579 |
+
* Helper method to resize the background layer.
|
| 580 |
+
*/
|
| 581 |
+
private _resize_background_layer(
|
| 582 |
+
oldBackgroundLayer: Container | null,
|
| 583 |
+
newWidth: number,
|
| 584 |
+
newHeight: number,
|
| 585 |
+
scale: boolean,
|
| 586 |
+
calculateOffset: () => { offsetX: number; offsetY: number }
|
| 587 |
+
): Container | null {
|
| 588 |
+
if (!oldBackgroundLayer) {
|
| 589 |
+
return this.create_background_layer(newWidth, newHeight);
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
let backgroundImage: Sprite | null = oldBackgroundLayer.children.find(
|
| 593 |
+
(child) =>
|
| 594 |
+
child instanceof Sprite &&
|
| 595 |
+
child.texture !== (oldBackgroundLayer.children[0] as Sprite)?.texture
|
| 596 |
+
) as Sprite | null;
|
| 597 |
+
|
| 598 |
+
const newBackgroundLayer = this.create_background_layer(
|
| 599 |
+
newWidth,
|
| 600 |
+
newHeight
|
| 601 |
+
);
|
| 602 |
+
|
| 603 |
+
if (backgroundImage) {
|
| 604 |
+
const newBgImage = new Sprite(backgroundImage.texture);
|
| 605 |
+
newBgImage.width = backgroundImage.width;
|
| 606 |
+
newBgImage.height = backgroundImage.height;
|
| 607 |
+
|
| 608 |
+
if (scale) {
|
| 609 |
+
newBgImage.width = newWidth;
|
| 610 |
+
newBgImage.height = newHeight;
|
| 611 |
+
newBgImage.position.set(0, 0);
|
| 612 |
+
} else {
|
| 613 |
+
const { offsetX, offsetY } = calculateOffset();
|
| 614 |
+
newBgImage.position.set(
|
| 615 |
+
backgroundImage.x + offsetX,
|
| 616 |
+
backgroundImage.y + offsetY
|
| 617 |
+
);
|
| 618 |
+
}
|
| 619 |
+
newBackgroundLayer.addChild(newBgImage);
|
| 620 |
+
}
|
| 621 |
+
|
| 622 |
+
return newBackgroundLayer;
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
/**
|
| 626 |
+
* Helper method to resize a single regular layer.
|
| 627 |
+
*/
|
| 628 |
+
private _resize_single_layer(
|
| 629 |
+
oldData: {
|
| 630 |
+
id: string;
|
| 631 |
+
name: string;
|
| 632 |
+
user_created: boolean;
|
| 633 |
+
texture: RenderTexture | undefined;
|
| 634 |
+
container: Container;
|
| 635 |
+
},
|
| 636 |
+
newWidth: number,
|
| 637 |
+
newHeight: number,
|
| 638 |
+
scale: boolean,
|
| 639 |
+
calculateOffset: () => { offsetX: number; offsetY: number }
|
| 640 |
+
): {
|
| 641 |
+
name: string;
|
| 642 |
+
id: string;
|
| 643 |
+
container: Container;
|
| 644 |
+
user_created: boolean;
|
| 645 |
+
} | null {
|
| 646 |
+
if (!oldData.texture) {
|
| 647 |
+
console.warn(
|
| 648 |
+
`No texture found for layer ${oldData.id}, skipping cleanup.`
|
| 649 |
+
);
|
| 650 |
+
if (oldData.container && !oldData.container.destroyed) {
|
| 651 |
+
if (this.image_container.children.includes(oldData.container)) {
|
| 652 |
+
this.image_container.removeChild(oldData.container);
|
| 653 |
+
}
|
| 654 |
+
oldData.container.destroy({ children: true });
|
| 655 |
+
}
|
| 656 |
+
return null;
|
| 657 |
+
}
|
| 658 |
+
|
| 659 |
+
const newContainer = this.create_layer({
|
| 660 |
+
width: newWidth,
|
| 661 |
+
height: newHeight,
|
| 662 |
+
layer_name: oldData.name,
|
| 663 |
+
user_created: oldData.user_created
|
| 664 |
+
});
|
| 665 |
+
|
| 666 |
+
const newLayer = this.layers[this.layers.length - 1];
|
| 667 |
+
newLayer.id = oldData.id;
|
| 668 |
+
const newTexture = this.draw_textures.get(newContainer);
|
| 669 |
+
|
| 670 |
+
if (!newTexture) {
|
| 671 |
+
console.error(
|
| 672 |
+
`Failed to get texture for newly created layer ${newLayer.id}. Cleaning up.`
|
| 673 |
+
);
|
| 674 |
+
if (newContainer && !newContainer.destroyed) {
|
| 675 |
+
if (this.image_container.children.includes(newContainer)) {
|
| 676 |
+
this.image_container.removeChild(newContainer);
|
| 677 |
+
}
|
| 678 |
+
newContainer.destroy({ children: true });
|
| 679 |
+
const idx = this.layers.findIndex((l) => l.container === newContainer);
|
| 680 |
+
if (idx > -1) this.layers.splice(idx, 1);
|
| 681 |
+
this.draw_textures.delete(newContainer);
|
| 682 |
+
}
|
| 683 |
+
if (oldData.texture && !oldData.texture.destroyed)
|
| 684 |
+
oldData.texture.destroy(true);
|
| 685 |
+
if (oldData.container && !oldData.container.destroyed)
|
| 686 |
+
oldData.container.destroy({ children: true });
|
| 687 |
+
return null;
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
this.app.renderer.clear({ target: newTexture, clearColor: [0, 0, 0, 0] });
|
| 691 |
+
|
| 692 |
+
const sprite = new Sprite(oldData.texture);
|
| 693 |
+
|
| 694 |
+
if (scale) {
|
| 695 |
+
sprite.width = newWidth;
|
| 696 |
+
sprite.height = newHeight;
|
| 697 |
+
sprite.position.set(0, 0);
|
| 698 |
+
} else {
|
| 699 |
+
const { offsetX, offsetY } = calculateOffset();
|
| 700 |
+
sprite.position.set(offsetX, offsetY);
|
| 701 |
+
}
|
| 702 |
+
|
| 703 |
+
this.app.renderer.render(sprite, { renderTexture: newTexture });
|
| 704 |
+
|
| 705 |
+
sprite.destroy();
|
| 706 |
+
if (oldData.texture && !oldData.texture.destroyed) {
|
| 707 |
+
oldData.texture.destroy(true);
|
| 708 |
+
}
|
| 709 |
+
if (oldData.container && !oldData.container.destroyed) {
|
| 710 |
+
if (this.image_container.children.includes(oldData.container)) {
|
| 711 |
+
this.image_container.removeChild(oldData.container);
|
| 712 |
+
}
|
| 713 |
+
oldData.container.destroy({ children: true });
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
return newLayer;
|
| 717 |
+
}
|
| 718 |
+
|
| 719 |
+
async get_blobs(width: number, height: number): Promise<ImageBlobs> {
|
| 720 |
+
const blobs = {
|
| 721 |
+
background: await get_canvas_blob(
|
| 722 |
+
this.app.renderer,
|
| 723 |
+
this.background_layer,
|
| 724 |
+
{
|
| 725 |
+
width,
|
| 726 |
+
height,
|
| 727 |
+
x: 0,
|
| 728 |
+
y: 0
|
| 729 |
+
}
|
| 730 |
+
),
|
| 731 |
+
layers: await Promise.all(
|
| 732 |
+
this.layers.map(async (layer) => {
|
| 733 |
+
const blob = await get_canvas_blob(
|
| 734 |
+
this.app.renderer,
|
| 735 |
+
layer.container,
|
| 736 |
+
{
|
| 737 |
+
width,
|
| 738 |
+
height,
|
| 739 |
+
x: 0,
|
| 740 |
+
y: 0
|
| 741 |
+
}
|
| 742 |
+
);
|
| 743 |
+
if (blob) {
|
| 744 |
+
return blob;
|
| 745 |
+
}
|
| 746 |
+
return null;
|
| 747 |
+
})
|
| 748 |
+
),
|
| 749 |
+
composite: await get_canvas_blob(
|
| 750 |
+
this.app.renderer,
|
| 751 |
+
this.image_container,
|
| 752 |
+
{
|
| 753 |
+
width,
|
| 754 |
+
height,
|
| 755 |
+
x: 0,
|
| 756 |
+
y: 0
|
| 757 |
+
}
|
| 758 |
+
)
|
| 759 |
+
};
|
| 760 |
+
|
| 761 |
+
return blobs;
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
reset_layers(width: number, height: number, persist = false): void {
|
| 765 |
+
const _layers_to_recreate = persist
|
| 766 |
+
? this.layers.map((layer) => [layer.name, layer.id])
|
| 767 |
+
: this.layer_options.layers.map((layer) => [layer, undefined]);
|
| 768 |
+
|
| 769 |
+
this.layers.forEach((layer) => {
|
| 770 |
+
this.delete_layer(layer.id);
|
| 771 |
+
});
|
| 772 |
+
|
| 773 |
+
for (const [layer_name, layer_id] of _layers_to_recreate) {
|
| 774 |
+
this.create_layer({
|
| 775 |
+
width,
|
| 776 |
+
height,
|
| 777 |
+
layer_name: layer_name,
|
| 778 |
+
user_created: this.layer_options.layers.find((l) => l === layer_name)
|
| 779 |
+
? false
|
| 780 |
+
: true,
|
| 781 |
+
layer_id: layer_id
|
| 782 |
+
});
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
if (!persist) {
|
| 786 |
+
this.active_layer = this.layers[0].container;
|
| 787 |
+
this.active_layer_id = this.layers[0].id;
|
| 788 |
+
} else {
|
| 789 |
+
this.active_layer =
|
| 790 |
+
this.layers.find((l) => l.id === this.active_layer_id)?.container ||
|
| 791 |
+
this.layers[0]?.container;
|
| 792 |
+
|
| 793 |
+
if (!this.active_layer) return;
|
| 794 |
+
}
|
| 795 |
+
|
| 796 |
+
this.layer_store.update((state) => ({
|
| 797 |
+
active_layer: this.active_layer_id || this.layers[0].id,
|
| 798 |
+
layers: this.layers
|
| 799 |
+
}));
|
| 800 |
+
}
|
| 801 |
+
|
| 802 |
+
init_layers(width: number, height: number): void {
|
| 803 |
+
for (const layer of this.layers) {
|
| 804 |
+
this.delete_layer(layer.id);
|
| 805 |
+
}
|
| 806 |
+
let i = 0;
|
| 807 |
+
for (const layer of this.layer_options.layers) {
|
| 808 |
+
this.create_layer({
|
| 809 |
+
width,
|
| 810 |
+
height,
|
| 811 |
+
layer_name: layer,
|
| 812 |
+
user_created: false,
|
| 813 |
+
layer_id: `layer-${i}`
|
| 814 |
+
});
|
| 815 |
+
i++;
|
| 816 |
+
}
|
| 817 |
+
|
| 818 |
+
this.active_layer = this.layers[0].container;
|
| 819 |
+
this.active_layer_id = this.layers[0].id;
|
| 820 |
+
this.layer_store.update((_layers) => ({
|
| 821 |
+
active_layer: this.layers[0].id,
|
| 822 |
+
layers: this.layers
|
| 823 |
+
}));
|
| 824 |
+
}
|
| 825 |
+
}
|
| 826 |
+
|
| 827 |
+
/**
|
| 828 |
+
* Command to add a new layer
|
| 829 |
+
*/
|
| 830 |
+
export class AddLayerCommand implements Command {
|
| 831 |
+
private layer_id: string;
|
| 832 |
+
private layer_name: string;
|
| 833 |
+
private width: number;
|
| 834 |
+
private height: number;
|
| 835 |
+
private user_created: boolean;
|
| 836 |
+
private make_active: boolean;
|
| 837 |
+
private previous_active_layer: string | null = null;
|
| 838 |
+
name: string;
|
| 839 |
+
constructor(
|
| 840 |
+
private context: ImageEditorContext,
|
| 841 |
+
options: {
|
| 842 |
+
width: number;
|
| 843 |
+
height: number;
|
| 844 |
+
layer_name?: string;
|
| 845 |
+
user_created: boolean;
|
| 846 |
+
layer_id?: string;
|
| 847 |
+
make_active?: boolean;
|
| 848 |
+
}
|
| 849 |
+
) {
|
| 850 |
+
this.width = options.width;
|
| 851 |
+
this.height = options.height;
|
| 852 |
+
this.layer_name =
|
| 853 |
+
options.layer_name ||
|
| 854 |
+
`Layer ${this.context.layer_manager.get_layers().length + 1}`;
|
| 855 |
+
this.user_created = options.user_created;
|
| 856 |
+
this.layer_id =
|
| 857 |
+
options.layer_id || Math.random().toString(36).substring(2, 15);
|
| 858 |
+
this.make_active = options.make_active || false;
|
| 859 |
+
this.name = "AddLayer";
|
| 860 |
+
const current_layers = this.context.layer_manager.get_layers();
|
| 861 |
+
const current_active = current_layers.find(
|
| 862 |
+
(l) => l.container === this.context.layer_manager.get_active_layer()
|
| 863 |
+
);
|
| 864 |
+
this.previous_active_layer = current_active?.id || null;
|
| 865 |
+
}
|
| 866 |
+
|
| 867 |
+
async execute(context?: ImageEditorContext): Promise<void> {
|
| 868 |
+
if (context) {
|
| 869 |
+
this.context = context;
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
this.context.layer_manager.create_layer({
|
| 873 |
+
width: this.width,
|
| 874 |
+
height: this.height,
|
| 875 |
+
layer_name: this.layer_name,
|
| 876 |
+
user_created: this.user_created,
|
| 877 |
+
layer_id: this.layer_id,
|
| 878 |
+
make_active: this.make_active
|
| 879 |
+
});
|
| 880 |
+
}
|
| 881 |
+
|
| 882 |
+
async undo(): Promise<void> {
|
| 883 |
+
this.context.layer_manager.delete_layer(this.layer_id);
|
| 884 |
+
|
| 885 |
+
if (this.previous_active_layer) {
|
| 886 |
+
this.context.layer_manager.set_active_layer(this.previous_active_layer);
|
| 887 |
+
}
|
| 888 |
+
}
|
| 889 |
+
}
|
| 890 |
+
|
| 891 |
+
/**
|
| 892 |
+
* Command to remove a layer
|
| 893 |
+
*/
|
| 894 |
+
export class RemoveLayerCommand implements Command {
|
| 895 |
+
private layer_data: {
|
| 896 |
+
id: string;
|
| 897 |
+
name: string;
|
| 898 |
+
user_created: boolean;
|
| 899 |
+
visible: boolean;
|
| 900 |
+
was_active: boolean;
|
| 901 |
+
};
|
| 902 |
+
private previous_active_layer: string | null = null;
|
| 903 |
+
private texture_copy: RenderTexture | null = null;
|
| 904 |
+
name: string;
|
| 905 |
+
|
| 906 |
+
constructor(
|
| 907 |
+
private context: ImageEditorContext,
|
| 908 |
+
layer_id: string
|
| 909 |
+
) {
|
| 910 |
+
this.name = "RemoveLayer";
|
| 911 |
+
const layers = this.context.layer_manager.get_layers();
|
| 912 |
+
const layer_to_remove = layers.find((l) => l.id === layer_id);
|
| 913 |
+
|
| 914 |
+
if (!layer_to_remove) {
|
| 915 |
+
throw new Error(`Layer with ID ${layer_id} not found`);
|
| 916 |
+
}
|
| 917 |
+
|
| 918 |
+
const active_layer = this.context.layer_manager.get_active_layer();
|
| 919 |
+
const was_active = layer_to_remove.container === active_layer;
|
| 920 |
+
|
| 921 |
+
if (was_active) {
|
| 922 |
+
const layer_index = layers.findIndex((l) => l.id === layer_id);
|
| 923 |
+
const next_active = layers[Math.max(0, layer_index - 1)];
|
| 924 |
+
this.previous_active_layer = next_active?.id || null;
|
| 925 |
+
}
|
| 926 |
+
|
| 927 |
+
this.layer_data = {
|
| 928 |
+
id: layer_to_remove.id,
|
| 929 |
+
name: layer_to_remove.name,
|
| 930 |
+
user_created: layer_to_remove.user_created,
|
| 931 |
+
visible: layer_to_remove.visible,
|
| 932 |
+
was_active
|
| 933 |
+
};
|
| 934 |
+
|
| 935 |
+
this.captureTextureData(layer_id);
|
| 936 |
+
}
|
| 937 |
+
|
| 938 |
+
/**
|
| 939 |
+
* Create a direct copy of the layer's texture
|
| 940 |
+
*/
|
| 941 |
+
private captureTextureData(layer_id: string): void {
|
| 942 |
+
const layer_textures =
|
| 943 |
+
this.context.layer_manager.get_layer_textures(layer_id);
|
| 944 |
+
if (!layer_textures) return;
|
| 945 |
+
|
| 946 |
+
try {
|
| 947 |
+
const original_texture = layer_textures.draw;
|
| 948 |
+
|
| 949 |
+
const texture_copy = RenderTexture.create({
|
| 950 |
+
width: original_texture.width,
|
| 951 |
+
height: original_texture.height,
|
| 952 |
+
resolution: window.devicePixelRatio || 1
|
| 953 |
+
});
|
| 954 |
+
|
| 955 |
+
const sprite = new Sprite(original_texture);
|
| 956 |
+
|
| 957 |
+
this.context.app.renderer.render(sprite, { renderTexture: texture_copy });
|
| 958 |
+
|
| 959 |
+
this.texture_copy = texture_copy;
|
| 960 |
+
|
| 961 |
+
sprite.destroy();
|
| 962 |
+
} catch (e) {
|
| 963 |
+
console.error("Failed to copy layer texture:", e);
|
| 964 |
+
this.texture_copy = null;
|
| 965 |
+
}
|
| 966 |
+
}
|
| 967 |
+
|
| 968 |
+
async execute(context?: ImageEditorContext): Promise<void> {
|
| 969 |
+
if (context) {
|
| 970 |
+
this.context = context;
|
| 971 |
+
}
|
| 972 |
+
|
| 973 |
+
this.context.layer_manager.delete_layer(this.layer_data.id);
|
| 974 |
+
}
|
| 975 |
+
|
| 976 |
+
async undo(): Promise<void> {
|
| 977 |
+
const dimensions = get(this.context.dimensions);
|
| 978 |
+
|
| 979 |
+
const container = this.context.layer_manager.create_layer({
|
| 980 |
+
width: dimensions.width,
|
| 981 |
+
height: dimensions.height,
|
| 982 |
+
layer_name: this.layer_data.name,
|
| 983 |
+
user_created: this.layer_data.user_created,
|
| 984 |
+
layer_id: this.layer_data.id,
|
| 985 |
+
make_active: this.layer_data.was_active
|
| 986 |
+
});
|
| 987 |
+
|
| 988 |
+
if (this.texture_copy) {
|
| 989 |
+
try {
|
| 990 |
+
const layer_textures = this.context.layer_manager.get_layer_textures(
|
| 991 |
+
this.layer_data.id
|
| 992 |
+
);
|
| 993 |
+
if (layer_textures) {
|
| 994 |
+
const sprite = new Sprite(this.texture_copy);
|
| 995 |
+
|
| 996 |
+
this.context.app.renderer.render(sprite, {
|
| 997 |
+
renderTexture: layer_textures.draw
|
| 998 |
+
});
|
| 999 |
+
|
| 1000 |
+
sprite.destroy();
|
| 1001 |
+
}
|
| 1002 |
+
} catch (e) {
|
| 1003 |
+
console.error("Failed to restore layer content:", e);
|
| 1004 |
+
}
|
| 1005 |
+
}
|
| 1006 |
+
|
| 1007 |
+
if (!this.layer_data.visible) {
|
| 1008 |
+
this.context.layer_manager.toggle_layer_visibility(this.layer_data.id);
|
| 1009 |
+
}
|
| 1010 |
+
|
| 1011 |
+
if (!this.layer_data.was_active && this.previous_active_layer) {
|
| 1012 |
+
this.context.layer_manager.set_active_layer(this.previous_active_layer);
|
| 1013 |
+
}
|
| 1014 |
+
}
|
| 1015 |
+
|
| 1016 |
+
/**
|
| 1017 |
+
* Clean up resources when the command is no longer needed
|
| 1018 |
+
* This would need to be called by a garbage collection mechanism
|
| 1019 |
+
*/
|
| 1020 |
+
destroy(): void {
|
| 1021 |
+
if (this.texture_copy) {
|
| 1022 |
+
this.texture_copy.destroy();
|
| 1023 |
+
this.texture_copy = null;
|
| 1024 |
+
}
|
| 1025 |
+
}
|
| 1026 |
+
}
|
| 1027 |
+
|
| 1028 |
+
/**
|
| 1029 |
+
* Command to reorder layers
|
| 1030 |
+
*/
|
| 1031 |
+
export class ReorderLayerCommand implements Command {
|
| 1032 |
+
private original_order: string[];
|
| 1033 |
+
private new_order: string[];
|
| 1034 |
+
private layer_id: string;
|
| 1035 |
+
private direction: "up" | "down";
|
| 1036 |
+
name: string;
|
| 1037 |
+
|
| 1038 |
+
constructor(
|
| 1039 |
+
private context: ImageEditorContext,
|
| 1040 |
+
layer_id: string,
|
| 1041 |
+
direction: "up" | "down"
|
| 1042 |
+
) {
|
| 1043 |
+
this.layer_id = layer_id;
|
| 1044 |
+
this.direction = direction;
|
| 1045 |
+
this.name = "ReorderLayer";
|
| 1046 |
+
const layers = this.context.layer_manager.get_layers();
|
| 1047 |
+
this.original_order = layers.map((l) => l.id);
|
| 1048 |
+
|
| 1049 |
+
const index = layers.findIndex((l) => l.id === layer_id);
|
| 1050 |
+
if (index === -1) {
|
| 1051 |
+
throw new Error(`Layer with ID ${layer_id} not found`);
|
| 1052 |
+
}
|
| 1053 |
+
|
| 1054 |
+
const new_index = direction === "up" ? index - 1 : index + 1;
|
| 1055 |
+
if (new_index < 0 || new_index >= layers.length) {
|
| 1056 |
+
this.new_order = [...this.original_order];
|
| 1057 |
+
} else {
|
| 1058 |
+
this.new_order = [...this.original_order];
|
| 1059 |
+
[this.new_order[index], this.new_order[new_index]] = [
|
| 1060 |
+
this.new_order[new_index],
|
| 1061 |
+
this.new_order[index]
|
| 1062 |
+
];
|
| 1063 |
+
}
|
| 1064 |
+
}
|
| 1065 |
+
|
| 1066 |
+
async execute(context?: ImageEditorContext): Promise<void> {
|
| 1067 |
+
if (context) {
|
| 1068 |
+
this.context = context;
|
| 1069 |
+
}
|
| 1070 |
+
|
| 1071 |
+
this.context.layer_manager.move_layer(this.layer_id, this.direction);
|
| 1072 |
+
}
|
| 1073 |
+
|
| 1074 |
+
async undo(): Promise<void> {
|
| 1075 |
+
const current_layers = this.context.layer_manager.get_layers();
|
| 1076 |
+
|
| 1077 |
+
if (
|
| 1078 |
+
this.original_order.join(",") ===
|
| 1079 |
+
current_layers.map((l) => l.id).join(",")
|
| 1080 |
+
) {
|
| 1081 |
+
return;
|
| 1082 |
+
}
|
| 1083 |
+
|
| 1084 |
+
const layer_index = current_layers.findIndex((l) => l.id === this.layer_id);
|
| 1085 |
+
if (layer_index === -1) return;
|
| 1086 |
+
|
| 1087 |
+
const original_index = this.original_order.indexOf(this.layer_id);
|
| 1088 |
+
const move_direction = layer_index > original_index ? "up" : "down";
|
| 1089 |
+
|
| 1090 |
+
this.context.layer_manager.move_layer(this.layer_id, move_direction);
|
| 1091 |
+
}
|
| 1092 |
+
}
|
6.0.0-dev.5/imageeditor/shared/crop/crop.ts
ADDED
|
@@ -0,0 +1,1027 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Container,
|
| 3 |
+
Graphics,
|
| 4 |
+
Point,
|
| 5 |
+
Rectangle,
|
| 6 |
+
FederatedPointerEvent,
|
| 7 |
+
Sprite
|
| 8 |
+
} from "pixi.js";
|
| 9 |
+
import { type ImageEditorContext, type Tool } from "../core/editor";
|
| 10 |
+
|
| 11 |
+
import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte";
|
| 12 |
+
import { get_canvas_blob } from "../utils/pixi";
|
| 13 |
+
|
| 14 |
+
/**
|
| 15 |
+
* @interface CropBounds
|
| 16 |
+
* @description Defines the bounds of a crop area
|
| 17 |
+
* @property {number} x - The x-coordinate of the crop area
|
| 18 |
+
* @property {number} y - The y-coordinate of the crop area
|
| 19 |
+
* @property {number} width - The width of the crop area
|
| 20 |
+
* @property {number} height - The height of the crop area
|
| 21 |
+
*/
|
| 22 |
+
interface CropBounds {
|
| 23 |
+
x: number;
|
| 24 |
+
y: number;
|
| 25 |
+
width: number;
|
| 26 |
+
height: number;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/**
|
| 30 |
+
* @interface EditorState
|
| 31 |
+
* @description Defines the state of the editor
|
| 32 |
+
* @property {ToolbarTool} current_tool - The currently selected tool
|
| 33 |
+
* @property {Subtool} current_subtool - The currently selected subtool
|
| 34 |
+
*/
|
| 35 |
+
interface EditorState {
|
| 36 |
+
current_tool: ToolbarTool;
|
| 37 |
+
current_subtool: Subtool;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/**
|
| 41 |
+
* @class CropTool
|
| 42 |
+
* @implements {Tool}
|
| 43 |
+
* @description Implements the crop tool functionality for the image editor
|
| 44 |
+
*/
|
| 45 |
+
export class CropTool implements Tool {
|
| 46 |
+
name = "crop" as const;
|
| 47 |
+
private image_editor_context!: ImageEditorContext;
|
| 48 |
+
current_tool: ToolbarTool = "image";
|
| 49 |
+
current_subtool: Subtool = "crop";
|
| 50 |
+
|
| 51 |
+
// Constants
|
| 52 |
+
private readonly CORNER_SIZE = 25;
|
| 53 |
+
private readonly LINE_THICKNESS = 5;
|
| 54 |
+
private readonly HANDLE_COLOR = 0x000000;
|
| 55 |
+
private readonly HIT_AREA_SIZE = 40;
|
| 56 |
+
|
| 57 |
+
// State
|
| 58 |
+
private is_dragging = false;
|
| 59 |
+
private selected_handle: Container | null = null;
|
| 60 |
+
private active_corner_index = -1;
|
| 61 |
+
private active_edge_index = -1;
|
| 62 |
+
private last_pointer_position: Point | null = null;
|
| 63 |
+
crop_bounds: CropBounds = { x: 0, y: 0, width: 0, height: 0 };
|
| 64 |
+
private crop_ui_container: Container | null = null;
|
| 65 |
+
private crop_mask: Graphics | null = null;
|
| 66 |
+
private dimensions: { width: number; height: number } = {
|
| 67 |
+
width: 0,
|
| 68 |
+
height: 0
|
| 69 |
+
};
|
| 70 |
+
private position: { x: number; y: number } = { x: 0, y: 0 };
|
| 71 |
+
private scale = 1;
|
| 72 |
+
private is_dragging_window = false;
|
| 73 |
+
private drag_start_position: Point | null = null;
|
| 74 |
+
private drag_start_bounds: CropBounds | null = null;
|
| 75 |
+
private background_image_watcher: (() => void) | null = null;
|
| 76 |
+
private has_been_manually_changed = false;
|
| 77 |
+
private event_callbacks: Map<string, (() => void)[]> = new Map();
|
| 78 |
+
/**
|
| 79 |
+
* @method setup
|
| 80 |
+
* @async
|
| 81 |
+
* @description Sets up the crop tool with the given context and state
|
| 82 |
+
* @param {ImageEditorContext} context - The image editor context
|
| 83 |
+
* @param {ToolbarTool} tool - The current tool
|
| 84 |
+
* @param {Subtool} subtool - The current subtool
|
| 85 |
+
* @returns {Promise<void>}
|
| 86 |
+
*/
|
| 87 |
+
async setup(
|
| 88 |
+
context: ImageEditorContext,
|
| 89 |
+
tool: ToolbarTool,
|
| 90 |
+
subtool: Subtool
|
| 91 |
+
): Promise<void> {
|
| 92 |
+
this.image_editor_context = context;
|
| 93 |
+
this.current_tool = tool;
|
| 94 |
+
this.current_subtool = subtool;
|
| 95 |
+
|
| 96 |
+
context.dimensions.subscribe((dimensions) => {
|
| 97 |
+
this.dimensions = dimensions;
|
| 98 |
+
|
| 99 |
+
// Update crop bounds to match new dimensions
|
| 100 |
+
this.crop_bounds = {
|
| 101 |
+
x: 0,
|
| 102 |
+
y: 0,
|
| 103 |
+
width: dimensions.width,
|
| 104 |
+
height: dimensions.height
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
// Force UI update if it exists
|
| 108 |
+
if (this.crop_ui_container) {
|
| 109 |
+
this.update_crop_ui();
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// Always update the mask when dimensions change
|
| 113 |
+
this.set_crop_mask();
|
| 114 |
+
this.update_crop_mask();
|
| 115 |
+
});
|
| 116 |
+
|
| 117 |
+
context.position.subscribe((position) => {
|
| 118 |
+
this.position = position;
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
context.scale.subscribe((scale) => {
|
| 122 |
+
this.scale = scale;
|
| 123 |
+
});
|
| 124 |
+
|
| 125 |
+
// Setup a background image watcher to ensure we always apply the mask to the current background image
|
| 126 |
+
this.background_image_watcher = () => {
|
| 127 |
+
// Only apply when crop tool is active and we have a mask
|
| 128 |
+
if (
|
| 129 |
+
this.crop_mask &&
|
| 130 |
+
this.current_tool === "image" &&
|
| 131 |
+
this.current_subtool === "crop"
|
| 132 |
+
) {
|
| 133 |
+
// Get a fresh reference to the background image and apply the mask
|
| 134 |
+
if (this.image_editor_context.background_image) {
|
| 135 |
+
this.image_editor_context.background_image.setMask(this.crop_mask);
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
// Add the watcher to the ticker to regularly check for background image changes
|
| 141 |
+
this.image_editor_context.app.ticker.add(this.background_image_watcher);
|
| 142 |
+
|
| 143 |
+
// Initialize crop UI
|
| 144 |
+
await this.init_crop_ui();
|
| 145 |
+
|
| 146 |
+
// Setup event listeners
|
| 147 |
+
this.setup_event_listeners();
|
| 148 |
+
|
| 149 |
+
// Ensure the crop UI visibility matches the current tool state
|
| 150 |
+
const isCropActive = tool === "image" && subtool === "crop";
|
| 151 |
+
if (this.crop_ui_container) {
|
| 152 |
+
this.crop_ui_container.visible = isCropActive;
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
/**
|
| 157 |
+
* @method cleanup
|
| 158 |
+
* @description Cleans up the crop tool resources and removes event listeners
|
| 159 |
+
* @returns {void}
|
| 160 |
+
*/
|
| 161 |
+
cleanup(): void {
|
| 162 |
+
// Remove the crop UI container
|
| 163 |
+
if (this.crop_ui_container) {
|
| 164 |
+
this.image_editor_context.app.stage.removeChild(this.crop_ui_container);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
// Remove the mask and reset the image container's mask property
|
| 168 |
+
if (this.crop_mask) {
|
| 169 |
+
if (this.crop_mask.parent) {
|
| 170 |
+
this.crop_mask.parent.removeChild(this.crop_mask);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Clear mask from background_image if applied
|
| 174 |
+
if (
|
| 175 |
+
this.image_editor_context.background_image &&
|
| 176 |
+
this.image_editor_context.background_image.mask === this.crop_mask
|
| 177 |
+
) {
|
| 178 |
+
this.image_editor_context.background_image.mask = null;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
// Clear mask from image_container if applied
|
| 182 |
+
if (this.image_editor_context.image_container.mask === this.crop_mask) {
|
| 183 |
+
this.image_editor_context.image_container.mask = null;
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
// Ensure all references to the crop mask are cleared
|
| 188 |
+
this.crop_mask = null;
|
| 189 |
+
this.crop_ui_container = null;
|
| 190 |
+
|
| 191 |
+
// Remove the background image watcher from the ticker
|
| 192 |
+
if (this.background_image_watcher) {
|
| 193 |
+
this.image_editor_context.app.ticker.remove(
|
| 194 |
+
this.background_image_watcher
|
| 195 |
+
);
|
| 196 |
+
this.background_image_watcher = null;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// Remove event listeners
|
| 200 |
+
this.cleanup_event_listeners();
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
/**
|
| 204 |
+
* @method set_tool
|
| 205 |
+
* @description Sets the current tool and subtool, updating UI visibility
|
| 206 |
+
* @param {ToolbarTool} tool - The tool to set
|
| 207 |
+
* @param {Subtool} subtool - The subtool to set
|
| 208 |
+
* @returns {void}
|
| 209 |
+
*/
|
| 210 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void {
|
| 211 |
+
this.current_tool = tool;
|
| 212 |
+
this.current_subtool = subtool;
|
| 213 |
+
|
| 214 |
+
// Show/hide crop UI based on tool and subtool
|
| 215 |
+
const isCropActive = tool === "image" && subtool === "crop";
|
| 216 |
+
|
| 217 |
+
// Only update the UI visibility, not the mask
|
| 218 |
+
if (this.crop_ui_container) {
|
| 219 |
+
this.crop_ui_container.visible = isCropActive;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
// If crop tool is active, ensure the mask is up to date
|
| 223 |
+
if (isCropActive && this.crop_mask) {
|
| 224 |
+
this.update_crop_mask();
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
/**
|
| 229 |
+
* @method set_crop_mask
|
| 230 |
+
* @private
|
| 231 |
+
* @description Sets up the crop mask for the image
|
| 232 |
+
* @returns {void}
|
| 233 |
+
*/
|
| 234 |
+
private set_crop_mask(): void {
|
| 235 |
+
// Remove any existing mask to avoid duplicates
|
| 236 |
+
if (this.crop_mask) {
|
| 237 |
+
if (this.crop_mask.parent) {
|
| 238 |
+
this.crop_mask.parent.removeChild(this.crop_mask);
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
// Clear mask from image_container if it was applied there
|
| 242 |
+
if (this.image_editor_context.image_container.mask === this.crop_mask) {
|
| 243 |
+
this.image_editor_context.image_container.mask = null;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
// Clear mask from background_image if it was applied there
|
| 247 |
+
if (
|
| 248 |
+
this.image_editor_context.background_image &&
|
| 249 |
+
this.image_editor_context.background_image.mask === this.crop_mask
|
| 250 |
+
) {
|
| 251 |
+
this.image_editor_context.background_image.mask = null;
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Create a new mask that will actually be used as an overlay
|
| 256 |
+
this.crop_mask = new Graphics();
|
| 257 |
+
|
| 258 |
+
// Add the mask/overlay to the display list
|
| 259 |
+
// This time it should be visible since we want to see the semi-transparent overlay
|
| 260 |
+
this.image_editor_context.image_container.addChild(this.crop_mask);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
/**
|
| 264 |
+
* @method init_crop_ui
|
| 265 |
+
* @private
|
| 266 |
+
* @async
|
| 267 |
+
* @description Initializes the crop UI elements and sets up the update loop
|
| 268 |
+
* @returns {Promise<void>}
|
| 269 |
+
*/
|
| 270 |
+
private async init_crop_ui(): Promise<void> {
|
| 271 |
+
// Create crop UI with scaled dimensions
|
| 272 |
+
const { width, height } =
|
| 273 |
+
this.image_editor_context.background_image?.getLocalBounds() || {
|
| 274 |
+
width: false,
|
| 275 |
+
height: false
|
| 276 |
+
};
|
| 277 |
+
|
| 278 |
+
this.crop_ui_container = this.make_crop_ui(
|
| 279 |
+
this.dimensions.width * this.scale,
|
| 280 |
+
this.dimensions.height * this.scale
|
| 281 |
+
);
|
| 282 |
+
|
| 283 |
+
// Position the crop UI container at the image position
|
| 284 |
+
this.crop_ui_container.position.set(this.position.x, this.position.y);
|
| 285 |
+
|
| 286 |
+
// Initially hide the crop UI until explicitly enabled by set_tool
|
| 287 |
+
this.crop_ui_container.visible = false;
|
| 288 |
+
|
| 289 |
+
this.image_editor_context.app.stage.addChild(this.crop_ui_container);
|
| 290 |
+
|
| 291 |
+
// Create and apply mask to the image container
|
| 292 |
+
this.set_crop_mask();
|
| 293 |
+
|
| 294 |
+
// Start the update loop for the UI
|
| 295 |
+
this.image_editor_context.app.ticker.add(this.update_crop_ui.bind(this));
|
| 296 |
+
|
| 297 |
+
// Initialize the mask with the current crop bounds
|
| 298 |
+
this.update_crop_mask();
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
/**
|
| 302 |
+
* @method make_crop_ui
|
| 303 |
+
* @private
|
| 304 |
+
* @description Creates the crop UI with the specified dimensions
|
| 305 |
+
* @param {number} width - Width of the crop area
|
| 306 |
+
* @param {number} height - Height of the crop area
|
| 307 |
+
* @returns {Container} The created crop UI container
|
| 308 |
+
*/
|
| 309 |
+
private make_crop_ui(width: number, height: number): Container {
|
| 310 |
+
const crop_container = new Container();
|
| 311 |
+
crop_container.eventMode = "static";
|
| 312 |
+
crop_container.interactiveChildren = true;
|
| 313 |
+
|
| 314 |
+
// Create crop outline with hit area for dragging
|
| 315 |
+
const crop_outline = new Graphics()
|
| 316 |
+
.rect(0, 0, width, height)
|
| 317 |
+
.stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.5 });
|
| 318 |
+
|
| 319 |
+
// Make the outline interactive
|
| 320 |
+
crop_outline.eventMode = "static";
|
| 321 |
+
crop_outline.cursor = "move";
|
| 322 |
+
|
| 323 |
+
// Set hit area to match the outline's dimensions
|
| 324 |
+
crop_outline.hitArea = new Rectangle(0, 0, width, height);
|
| 325 |
+
|
| 326 |
+
// Add drag handlers to the outline
|
| 327 |
+
crop_outline.on("pointerdown", this.handle_window_drag_start.bind(this));
|
| 328 |
+
|
| 329 |
+
crop_container.addChild(crop_outline);
|
| 330 |
+
|
| 331 |
+
// Create corner handles
|
| 332 |
+
this.create_corner_handles(crop_container, width, height);
|
| 333 |
+
|
| 334 |
+
// Create edge handles
|
| 335 |
+
this.create_edge_handles(crop_container, width, height);
|
| 336 |
+
|
| 337 |
+
return crop_container;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
/**
|
| 341 |
+
* @method create_handle
|
| 342 |
+
* @private
|
| 343 |
+
* @description Creates a handle for the crop UI
|
| 344 |
+
* @param {boolean} [is_edge=false] - Whether the handle is an edge handle
|
| 345 |
+
* @returns {Container} The created handle container
|
| 346 |
+
*/
|
| 347 |
+
private create_handle(is_edge = false): Container {
|
| 348 |
+
const handle = new Container();
|
| 349 |
+
handle.eventMode = "static";
|
| 350 |
+
|
| 351 |
+
const handle_graphics = new Graphics();
|
| 352 |
+
|
| 353 |
+
if (is_edge) {
|
| 354 |
+
// Create a shorter bar for edge handles (2x instead of 3x)
|
| 355 |
+
handle_graphics
|
| 356 |
+
.rect(0, 0, this.CORNER_SIZE * 1.5, this.LINE_THICKNESS)
|
| 357 |
+
.fill(this.HANDLE_COLOR);
|
| 358 |
+
} else {
|
| 359 |
+
// Create L-shaped corner handles
|
| 360 |
+
handle_graphics
|
| 361 |
+
.rect(0, 0, this.CORNER_SIZE, this.LINE_THICKNESS)
|
| 362 |
+
.rect(0, 0, this.LINE_THICKNESS, this.CORNER_SIZE)
|
| 363 |
+
.fill(this.HANDLE_COLOR);
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
handle.addChild(handle_graphics);
|
| 367 |
+
|
| 368 |
+
// Adjust hit area based on handle type
|
| 369 |
+
const hit_size = is_edge ? this.HIT_AREA_SIZE * 1.5 : this.HIT_AREA_SIZE;
|
| 370 |
+
handle.hitArea = new Rectangle(
|
| 371 |
+
-hit_size / 2 + this.LINE_THICKNESS,
|
| 372 |
+
-hit_size / 2 + this.LINE_THICKNESS,
|
| 373 |
+
hit_size,
|
| 374 |
+
hit_size
|
| 375 |
+
);
|
| 376 |
+
|
| 377 |
+
return handle;
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
/**
|
| 381 |
+
* @method create_edge_handles
|
| 382 |
+
* @private
|
| 383 |
+
* @description Creates edge handles for the crop UI
|
| 384 |
+
* @param {Container} container - The container to add handles to
|
| 385 |
+
* @param {number} width - Width of the crop area
|
| 386 |
+
* @param {number} height - Height of the crop area
|
| 387 |
+
* @returns {void}
|
| 388 |
+
*/
|
| 389 |
+
private create_edge_handles(
|
| 390 |
+
container: Container,
|
| 391 |
+
width: number,
|
| 392 |
+
height: number
|
| 393 |
+
): void {
|
| 394 |
+
// Horizontal handles at top and bottom
|
| 395 |
+
[-1, 1].forEach((i, index) => {
|
| 396 |
+
const handle = this.create_handle(true); // true for edge handle
|
| 397 |
+
handle.rotation = 0;
|
| 398 |
+
|
| 399 |
+
handle.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 400 |
+
this.handle_pointer_down(event, handle, -1, index); // 0 for top, 1 for bottom
|
| 401 |
+
});
|
| 402 |
+
|
| 403 |
+
// Center the handle
|
| 404 |
+
handle.x = width / 2 - (this.CORNER_SIZE * 3) / 2;
|
| 405 |
+
handle.y = i < 0 ? -this.LINE_THICKNESS : height;
|
| 406 |
+
handle.cursor = "ns-resize";
|
| 407 |
+
|
| 408 |
+
container.addChild(handle);
|
| 409 |
+
});
|
| 410 |
+
|
| 411 |
+
// Vertical handles at left and right
|
| 412 |
+
[-1, 1].forEach((i, index) => {
|
| 413 |
+
const handle = this.create_handle(true); // true for edge handle
|
| 414 |
+
handle.rotation = Math.PI / 2; // Rotate 90 degrees
|
| 415 |
+
|
| 416 |
+
handle.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 417 |
+
this.handle_pointer_down(event, handle, -1, index + 2); // 2 for left, 3 for right
|
| 418 |
+
});
|
| 419 |
+
|
| 420 |
+
// Center the handle
|
| 421 |
+
handle.x = i < 0 ? -this.LINE_THICKNESS : width;
|
| 422 |
+
handle.y = height / 2 - (this.CORNER_SIZE * 3) / 2;
|
| 423 |
+
handle.cursor = "ew-resize";
|
| 424 |
+
|
| 425 |
+
container.addChild(handle);
|
| 426 |
+
});
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
/**
|
| 430 |
+
* @method create_corner_handles
|
| 431 |
+
* @private
|
| 432 |
+
* @description Creates corner handles for the crop UI
|
| 433 |
+
* @param {Container} container - The container to add handles to
|
| 434 |
+
* @param {number} width - Width of the crop area
|
| 435 |
+
* @param {number} height - Height of the crop area
|
| 436 |
+
* @returns {void}
|
| 437 |
+
*/
|
| 438 |
+
private create_corner_handles(
|
| 439 |
+
container: Container,
|
| 440 |
+
width: number,
|
| 441 |
+
height: number
|
| 442 |
+
): void {
|
| 443 |
+
const corners = [
|
| 444 |
+
{ x: 0, y: 0, xScale: 1, yScale: 1, cursor: "nwse-resize" }, // Top-left
|
| 445 |
+
{ x: width, y: 0, xScale: -1, yScale: 1, cursor: "nesw-resize" }, // Top-right
|
| 446 |
+
{ x: 0, y: height, xScale: 1, yScale: -1, cursor: "nesw-resize" }, // Bottom-left
|
| 447 |
+
{ x: width, y: height, xScale: -1, yScale: -1, cursor: "nwse-resize" } // Bottom-right
|
| 448 |
+
];
|
| 449 |
+
|
| 450 |
+
corners.forEach(({ x, y, xScale, yScale, cursor }, i) => {
|
| 451 |
+
const corner = this.create_handle(false); // false for corner handle
|
| 452 |
+
corner.x = x - (xScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS);
|
| 453 |
+
corner.y = y - (yScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS);
|
| 454 |
+
corner.scale.set(xScale, yScale);
|
| 455 |
+
|
| 456 |
+
corner.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 457 |
+
this.handle_pointer_down(event, corner, i, -1);
|
| 458 |
+
});
|
| 459 |
+
|
| 460 |
+
corner.cursor = cursor;
|
| 461 |
+
|
| 462 |
+
container.addChild(corner);
|
| 463 |
+
});
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
/**
|
| 467 |
+
* @method handle_pointer_down
|
| 468 |
+
* @private
|
| 469 |
+
* @description Handles pointer down events on handles
|
| 470 |
+
* @param {FederatedPointerEvent} event - The pointer event
|
| 471 |
+
* @param {Container} handle - The handle that was clicked
|
| 472 |
+
* @param {number} corner_index - Index of the corner (-1 if not a corner)
|
| 473 |
+
* @param {number} edge_index - Index of the edge (-1 if not an edge)
|
| 474 |
+
* @returns {void}
|
| 475 |
+
*/
|
| 476 |
+
private handle_pointer_down(
|
| 477 |
+
event: FederatedPointerEvent,
|
| 478 |
+
handle: Container,
|
| 479 |
+
corner_index: number,
|
| 480 |
+
edge_index: number
|
| 481 |
+
): void {
|
| 482 |
+
// Only handle crop events if crop tool is active
|
| 483 |
+
if (this.current_subtool !== "crop") return;
|
| 484 |
+
|
| 485 |
+
event.stopPropagation();
|
| 486 |
+
|
| 487 |
+
this.is_dragging = true;
|
| 488 |
+
this.selected_handle = handle;
|
| 489 |
+
this.active_corner_index = corner_index;
|
| 490 |
+
this.active_edge_index = edge_index;
|
| 491 |
+
|
| 492 |
+
// Convert global position to the container's local coordinates.
|
| 493 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 494 |
+
event.global
|
| 495 |
+
);
|
| 496 |
+
|
| 497 |
+
this.last_pointer_position = new Point(local_pos.x, local_pos.y);
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
/**
|
| 501 |
+
* updates the crop bounds based on pointer movement
|
| 502 |
+
* @param delta - the change in position
|
| 503 |
+
*/
|
| 504 |
+
private update_crop_bounds(delta: Point): void {
|
| 505 |
+
// Set flag indicating the crop has been manually changed
|
| 506 |
+
this.has_been_manually_changed = true;
|
| 507 |
+
|
| 508 |
+
// Scale the delta to match the image coordinates
|
| 509 |
+
const scaled_delta = new Point(delta.x, delta.y);
|
| 510 |
+
|
| 511 |
+
// Store original values to calculate actual changes
|
| 512 |
+
const original = {
|
| 513 |
+
x: this.crop_bounds.x,
|
| 514 |
+
y: this.crop_bounds.y,
|
| 515 |
+
width: this.crop_bounds.width,
|
| 516 |
+
height: this.crop_bounds.height
|
| 517 |
+
};
|
| 518 |
+
|
| 519 |
+
if (this.active_corner_index !== -1) {
|
| 520 |
+
// Handle corner dragging
|
| 521 |
+
switch (this.active_corner_index) {
|
| 522 |
+
case 0: // Top-left
|
| 523 |
+
// Update width and x position
|
| 524 |
+
const newWidth0 = Math.max(20, original.width - scaled_delta.x);
|
| 525 |
+
const widthDiff0 = original.width - newWidth0;
|
| 526 |
+
this.crop_bounds.width = newWidth0;
|
| 527 |
+
this.crop_bounds.x = original.x + widthDiff0;
|
| 528 |
+
|
| 529 |
+
// Update height and y position
|
| 530 |
+
const newHeight0 = Math.max(20, original.height - scaled_delta.y);
|
| 531 |
+
const heightDiff0 = original.height - newHeight0;
|
| 532 |
+
this.crop_bounds.height = newHeight0;
|
| 533 |
+
this.crop_bounds.y = original.y + heightDiff0;
|
| 534 |
+
break;
|
| 535 |
+
|
| 536 |
+
case 1: // Top-right
|
| 537 |
+
// Update width (right side)
|
| 538 |
+
this.crop_bounds.width = Math.max(
|
| 539 |
+
20,
|
| 540 |
+
original.width + scaled_delta.x
|
| 541 |
+
);
|
| 542 |
+
|
| 543 |
+
// Update height and y position (top side)
|
| 544 |
+
const newHeight1 = Math.max(20, original.height - scaled_delta.y);
|
| 545 |
+
const heightDiff1 = original.height - newHeight1;
|
| 546 |
+
this.crop_bounds.height = newHeight1;
|
| 547 |
+
this.crop_bounds.y = original.y + heightDiff1;
|
| 548 |
+
break;
|
| 549 |
+
|
| 550 |
+
case 2: // Bottom-left
|
| 551 |
+
// Update width and x position (left side)
|
| 552 |
+
const newWidth2 = Math.max(20, original.width - scaled_delta.x);
|
| 553 |
+
const widthDiff2 = original.width - newWidth2;
|
| 554 |
+
this.crop_bounds.width = newWidth2;
|
| 555 |
+
this.crop_bounds.x = original.x + widthDiff2;
|
| 556 |
+
|
| 557 |
+
// Update height (bottom side)
|
| 558 |
+
this.crop_bounds.height = Math.max(
|
| 559 |
+
20,
|
| 560 |
+
original.height + scaled_delta.y
|
| 561 |
+
);
|
| 562 |
+
break;
|
| 563 |
+
|
| 564 |
+
case 3: // Bottom-right
|
| 565 |
+
// Update width (right side)
|
| 566 |
+
this.crop_bounds.width = Math.max(
|
| 567 |
+
20,
|
| 568 |
+
original.width + scaled_delta.x
|
| 569 |
+
);
|
| 570 |
+
|
| 571 |
+
// Update height (bottom side)
|
| 572 |
+
this.crop_bounds.height = Math.max(
|
| 573 |
+
20,
|
| 574 |
+
original.height + scaled_delta.y
|
| 575 |
+
);
|
| 576 |
+
break;
|
| 577 |
+
}
|
| 578 |
+
} else if (this.active_edge_index !== -1) {
|
| 579 |
+
// Handle edge dragging
|
| 580 |
+
switch (this.active_edge_index) {
|
| 581 |
+
case 0: // Top
|
| 582 |
+
// Update height and y position
|
| 583 |
+
const newHeight = Math.max(20, original.height - scaled_delta.y);
|
| 584 |
+
const heightDiff = original.height - newHeight;
|
| 585 |
+
this.crop_bounds.height = newHeight;
|
| 586 |
+
this.crop_bounds.y = original.y + heightDiff;
|
| 587 |
+
break;
|
| 588 |
+
|
| 589 |
+
case 1: // Bottom
|
| 590 |
+
// Update height only
|
| 591 |
+
this.crop_bounds.height = Math.max(
|
| 592 |
+
20,
|
| 593 |
+
original.height + scaled_delta.y
|
| 594 |
+
);
|
| 595 |
+
break;
|
| 596 |
+
|
| 597 |
+
case 2: // Left
|
| 598 |
+
// Update width and x position
|
| 599 |
+
const newWidth = Math.max(20, original.width - scaled_delta.x);
|
| 600 |
+
const widthDiff = original.width - newWidth;
|
| 601 |
+
this.crop_bounds.width = newWidth;
|
| 602 |
+
this.crop_bounds.x = original.x + widthDiff;
|
| 603 |
+
break;
|
| 604 |
+
|
| 605 |
+
case 3: // Right
|
| 606 |
+
// Update width only
|
| 607 |
+
this.crop_bounds.width = Math.max(
|
| 608 |
+
20,
|
| 609 |
+
original.width + scaled_delta.x
|
| 610 |
+
);
|
| 611 |
+
break;
|
| 612 |
+
}
|
| 613 |
+
}
|
| 614 |
+
|
| 615 |
+
// Apply constraints
|
| 616 |
+
this.constrain_crop_bounds();
|
| 617 |
+
|
| 618 |
+
// Update the UI and mask
|
| 619 |
+
this.update_crop_ui();
|
| 620 |
+
this.update_crop_mask();
|
| 621 |
+
|
| 622 |
+
// Make sure to apply mask to the current background image
|
| 623 |
+
if (this.crop_mask && this.image_editor_context.background_image) {
|
| 624 |
+
this.image_editor_context.background_image.setMask(this.crop_mask);
|
| 625 |
+
}
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
/**
|
| 629 |
+
* constrains the crop bounds to the image dimensions
|
| 630 |
+
*/
|
| 631 |
+
private constrain_crop_bounds(): void {
|
| 632 |
+
// Store original values
|
| 633 |
+
const original = {
|
| 634 |
+
x: this.crop_bounds.x,
|
| 635 |
+
y: this.crop_bounds.y,
|
| 636 |
+
width: this.crop_bounds.width,
|
| 637 |
+
height: this.crop_bounds.height
|
| 638 |
+
};
|
| 639 |
+
|
| 640 |
+
// First, constrain width and height to prevent exceeding image dimensions
|
| 641 |
+
// and ensure minimum size
|
| 642 |
+
this.crop_bounds.width = Math.max(
|
| 643 |
+
20,
|
| 644 |
+
Math.min(this.crop_bounds.width, this.dimensions.width)
|
| 645 |
+
);
|
| 646 |
+
|
| 647 |
+
this.crop_bounds.height = Math.max(
|
| 648 |
+
20,
|
| 649 |
+
Math.min(this.crop_bounds.height, this.dimensions.height)
|
| 650 |
+
);
|
| 651 |
+
|
| 652 |
+
// Then constrain x and y to keep the crop window within the image bounds
|
| 653 |
+
const oldX = this.crop_bounds.x;
|
| 654 |
+
this.crop_bounds.x = Math.max(
|
| 655 |
+
0,
|
| 656 |
+
Math.min(
|
| 657 |
+
this.crop_bounds.x,
|
| 658 |
+
this.dimensions.width - this.crop_bounds.width
|
| 659 |
+
)
|
| 660 |
+
);
|
| 661 |
+
|
| 662 |
+
// If x position was constrained, adjust width to prevent opposite handle drift
|
| 663 |
+
if (
|
| 664 |
+
(this.crop_bounds.x !== oldX && this.active_corner_index === 0) ||
|
| 665 |
+
this.active_edge_index === 2
|
| 666 |
+
) {
|
| 667 |
+
// Left side was constrained, adjust width to compensate
|
| 668 |
+
const xDiff = this.crop_bounds.x - oldX;
|
| 669 |
+
this.crop_bounds.width -= xDiff;
|
| 670 |
+
}
|
| 671 |
+
|
| 672 |
+
const oldY = this.crop_bounds.y;
|
| 673 |
+
this.crop_bounds.y = Math.max(
|
| 674 |
+
0,
|
| 675 |
+
Math.min(
|
| 676 |
+
this.crop_bounds.y,
|
| 677 |
+
this.dimensions.height - this.crop_bounds.height
|
| 678 |
+
)
|
| 679 |
+
);
|
| 680 |
+
|
| 681 |
+
// If y position was constrained, adjust height to prevent opposite handle drift
|
| 682 |
+
if (
|
| 683 |
+
this.crop_bounds.y !== oldY &&
|
| 684 |
+
(this.active_corner_index === 0 ||
|
| 685 |
+
this.active_corner_index === 1 ||
|
| 686 |
+
this.active_edge_index === 0)
|
| 687 |
+
) {
|
| 688 |
+
// Top side was constrained, adjust height to compensate
|
| 689 |
+
const yDiff = this.crop_bounds.y - oldY;
|
| 690 |
+
this.crop_bounds.height -= yDiff;
|
| 691 |
+
}
|
| 692 |
+
|
| 693 |
+
// Final check to ensure width and height don't exceed image dimensions from current position
|
| 694 |
+
this.crop_bounds.width = Math.max(
|
| 695 |
+
20,
|
| 696 |
+
Math.min(
|
| 697 |
+
this.crop_bounds.width,
|
| 698 |
+
this.dimensions.width - this.crop_bounds.x
|
| 699 |
+
)
|
| 700 |
+
);
|
| 701 |
+
|
| 702 |
+
this.crop_bounds.height = Math.max(
|
| 703 |
+
20,
|
| 704 |
+
Math.min(
|
| 705 |
+
this.crop_bounds.height,
|
| 706 |
+
this.dimensions.height - this.crop_bounds.y
|
| 707 |
+
)
|
| 708 |
+
);
|
| 709 |
+
}
|
| 710 |
+
|
| 711 |
+
/**
|
| 712 |
+
* updates the crop mask graphics
|
| 713 |
+
*/
|
| 714 |
+
private update_crop_mask(): void {
|
| 715 |
+
if (!this.crop_mask) return;
|
| 716 |
+
|
| 717 |
+
// Clear the previous mask shape
|
| 718 |
+
this.crop_mask.clear();
|
| 719 |
+
|
| 720 |
+
const { width, height } =
|
| 721 |
+
this.image_editor_context.background_image?.getLocalBounds() ?? {
|
| 722 |
+
width: 0,
|
| 723 |
+
height: 0
|
| 724 |
+
};
|
| 725 |
+
this.crop_mask
|
| 726 |
+
.rect(0, 0, width, height)
|
| 727 |
+
.fill({ color: 0x000000, alpha: 0.4 })
|
| 728 |
+
.rect(
|
| 729 |
+
this.crop_bounds.x,
|
| 730 |
+
this.crop_bounds.y,
|
| 731 |
+
this.crop_bounds.width,
|
| 732 |
+
this.crop_bounds.height
|
| 733 |
+
)
|
| 734 |
+
.cut();
|
| 735 |
+
|
| 736 |
+
// Reset blend mode
|
| 737 |
+
// this.crop_mask.blendMode = "normal";
|
| 738 |
+
|
| 739 |
+
// Make the mask visible (previously we had alpha=0)
|
| 740 |
+
// this.crop_mask.alpha = 1;
|
| 741 |
+
|
| 742 |
+
// Since we're not using this as a mask anymore, but as an overlay,
|
| 743 |
+
// we should remove any existing mask assignment
|
| 744 |
+
if (this.image_editor_context.background_image) {
|
| 745 |
+
this.image_editor_context.background_image.mask = null;
|
| 746 |
+
}
|
| 747 |
+
|
| 748 |
+
// Make sure the mask is not applied to the image container
|
| 749 |
+
if (this.image_editor_context.image_container.mask === this.crop_mask) {
|
| 750 |
+
this.image_editor_context.image_container.mask = null;
|
| 751 |
+
}
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
/**
|
| 755 |
+
* updates the crop ui position and dimensions
|
| 756 |
+
*/
|
| 757 |
+
private update_crop_ui(): void {
|
| 758 |
+
if (!this.crop_mask || !this.crop_ui_container) return;
|
| 759 |
+
|
| 760 |
+
// Move crop_ui_container so that (0,0) of its local space aligns with the crop_bounds on the stage
|
| 761 |
+
this.crop_ui_container.position.set(
|
| 762 |
+
this.position.x + this.crop_bounds.x * this.scale,
|
| 763 |
+
this.position.y + this.crop_bounds.y * this.scale
|
| 764 |
+
);
|
| 765 |
+
|
| 766 |
+
// Get the scaled dimensions
|
| 767 |
+
const scaled_width = this.crop_bounds.width * this.scale;
|
| 768 |
+
const scaled_height = this.crop_bounds.height * this.scale;
|
| 769 |
+
|
| 770 |
+
// Redraw the outline so it matches the crop_bounds size
|
| 771 |
+
const outline = this.crop_ui_container.getChildAt(0) as Graphics;
|
| 772 |
+
outline
|
| 773 |
+
.clear()
|
| 774 |
+
.rect(0, 0, scaled_width, scaled_height)
|
| 775 |
+
.stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.5 });
|
| 776 |
+
|
| 777 |
+
// Update the hit area to match the new dimensions
|
| 778 |
+
outline.hitArea = new Rectangle(0, 0, scaled_width, scaled_height);
|
| 779 |
+
|
| 780 |
+
// Update handle positions to reflect the cropped area, not the full image
|
| 781 |
+
this.update_handle_positions(scaled_width, scaled_height);
|
| 782 |
+
}
|
| 783 |
+
|
| 784 |
+
/**
|
| 785 |
+
* updates the positions of all handles
|
| 786 |
+
* @param width - width of the crop area
|
| 787 |
+
* @param height - height of the crop area
|
| 788 |
+
*/
|
| 789 |
+
private update_handle_positions(width: number, height: number): void {
|
| 790 |
+
if (!this.crop_ui_container) return;
|
| 791 |
+
|
| 792 |
+
// Skip the outline (index 0), get corner/edge handles
|
| 793 |
+
const handles = this.crop_ui_container.children.slice(1);
|
| 794 |
+
|
| 795 |
+
// First 4 are corners
|
| 796 |
+
const corners = handles.slice(0, 4);
|
| 797 |
+
const cornerPositions = [
|
| 798 |
+
{ x: 0, y: 0 }, // Top-left
|
| 799 |
+
{ x: width, y: 0 }, // Top-right
|
| 800 |
+
{ x: 0, y: height }, // Bottom-left
|
| 801 |
+
{ x: width, y: height } // Bottom-right
|
| 802 |
+
];
|
| 803 |
+
|
| 804 |
+
corners.forEach((handle, i) => {
|
| 805 |
+
const xScale = i % 2 === 0 ? 1 : -1;
|
| 806 |
+
const yScale = i < 2 ? 1 : -1;
|
| 807 |
+
handle.position.set(
|
| 808 |
+
cornerPositions[i].x -
|
| 809 |
+
(xScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS),
|
| 810 |
+
cornerPositions[i].y -
|
| 811 |
+
(yScale < 0 ? -this.LINE_THICKNESS : this.LINE_THICKNESS)
|
| 812 |
+
);
|
| 813 |
+
});
|
| 814 |
+
|
| 815 |
+
// Remaining 4 are edges
|
| 816 |
+
const edges = handles.slice(4);
|
| 817 |
+
edges.forEach((handle, i) => {
|
| 818 |
+
if (i < 2) {
|
| 819 |
+
// Top/Bottom edges
|
| 820 |
+
handle.position.set(
|
| 821 |
+
width / 2 - (this.CORNER_SIZE * 1.5) / 2, // Changed from 3 to 2
|
| 822 |
+
i === 0 ? -this.LINE_THICKNESS : height
|
| 823 |
+
);
|
| 824 |
+
} else {
|
| 825 |
+
// Left/Right edges
|
| 826 |
+
handle.position.set(
|
| 827 |
+
i === 2 ? 0 : width + this.LINE_THICKNESS,
|
| 828 |
+
height / 2 - (this.CORNER_SIZE * 1.5) / 2 // Changed from 3 to 2
|
| 829 |
+
);
|
| 830 |
+
}
|
| 831 |
+
});
|
| 832 |
+
}
|
| 833 |
+
|
| 834 |
+
/**
|
| 835 |
+
* sets up event listeners for the crop tool
|
| 836 |
+
*/
|
| 837 |
+
private setup_event_listeners(): void {
|
| 838 |
+
const stage = this.image_editor_context.app.stage;
|
| 839 |
+
stage.eventMode = "static";
|
| 840 |
+
stage.on("pointermove", this.handle_pointer_move.bind(this));
|
| 841 |
+
stage.on("pointerup", this.handle_pointer_up.bind(this));
|
| 842 |
+
stage.on("pointerupoutside", this.handle_pointer_up.bind(this));
|
| 843 |
+
}
|
| 844 |
+
|
| 845 |
+
/**
|
| 846 |
+
* removes event listeners for the crop tool
|
| 847 |
+
*/
|
| 848 |
+
private cleanup_event_listeners(): void {
|
| 849 |
+
const stage = this.image_editor_context.app.stage;
|
| 850 |
+
|
| 851 |
+
stage.off("pointermove", this.handle_pointer_move.bind(this));
|
| 852 |
+
stage.off("pointerup", this.handle_pointer_up.bind(this));
|
| 853 |
+
stage.off("pointerupoutside", this.handle_pointer_up.bind(this));
|
| 854 |
+
}
|
| 855 |
+
|
| 856 |
+
/**
|
| 857 |
+
* handles pointer move events
|
| 858 |
+
* @param event - the pointer event
|
| 859 |
+
*/
|
| 860 |
+
private handle_pointer_move(event: FederatedPointerEvent): void {
|
| 861 |
+
if (this.current_subtool !== "crop") return;
|
| 862 |
+
|
| 863 |
+
if (
|
| 864 |
+
this.is_dragging_window &&
|
| 865 |
+
this.drag_start_position &&
|
| 866 |
+
this.drag_start_bounds
|
| 867 |
+
) {
|
| 868 |
+
// Handle window dragging
|
| 869 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 870 |
+
event.global
|
| 871 |
+
);
|
| 872 |
+
const delta = new Point(
|
| 873 |
+
local_pos.x - this.drag_start_position.x,
|
| 874 |
+
local_pos.y - this.drag_start_position.y
|
| 875 |
+
);
|
| 876 |
+
|
| 877 |
+
// Update crop bounds based on drag
|
| 878 |
+
this.crop_bounds = {
|
| 879 |
+
x: this.drag_start_bounds.x + delta.x,
|
| 880 |
+
y: this.drag_start_bounds.y + delta.y,
|
| 881 |
+
width: this.drag_start_bounds.width,
|
| 882 |
+
height: this.drag_start_bounds.height
|
| 883 |
+
};
|
| 884 |
+
|
| 885 |
+
// Constrain the bounds
|
| 886 |
+
this.constrain_crop_bounds();
|
| 887 |
+
|
| 888 |
+
// Update the mask
|
| 889 |
+
this.update_crop_mask();
|
| 890 |
+
|
| 891 |
+
// Make sure to apply mask to the current background image
|
| 892 |
+
if (this.crop_mask && this.image_editor_context.background_image) {
|
| 893 |
+
this.image_editor_context.background_image.setMask(this.crop_mask);
|
| 894 |
+
}
|
| 895 |
+
|
| 896 |
+
return;
|
| 897 |
+
}
|
| 898 |
+
|
| 899 |
+
// Handle existing resize logic
|
| 900 |
+
if (
|
| 901 |
+
this.is_dragging &&
|
| 902 |
+
this.selected_handle &&
|
| 903 |
+
this.last_pointer_position
|
| 904 |
+
) {
|
| 905 |
+
// Convert from global to local, no extra divide
|
| 906 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 907 |
+
event.global
|
| 908 |
+
);
|
| 909 |
+
const current_position = new Point(local_pos.x, local_pos.y);
|
| 910 |
+
|
| 911 |
+
const delta = new Point(
|
| 912 |
+
current_position.x - this.last_pointer_position.x,
|
| 913 |
+
current_position.y - this.last_pointer_position.y
|
| 914 |
+
);
|
| 915 |
+
|
| 916 |
+
this.update_crop_bounds(delta);
|
| 917 |
+
this.last_pointer_position = current_position;
|
| 918 |
+
this.update_crop_mask();
|
| 919 |
+
}
|
| 920 |
+
}
|
| 921 |
+
|
| 922 |
+
/**
|
| 923 |
+
* handles pointer up events
|
| 924 |
+
*/
|
| 925 |
+
private handle_pointer_up(): void {
|
| 926 |
+
if (this.current_subtool !== "crop") return;
|
| 927 |
+
|
| 928 |
+
this.is_dragging = false;
|
| 929 |
+
this.is_dragging_window = false;
|
| 930 |
+
this.selected_handle = null;
|
| 931 |
+
this.last_pointer_position = null;
|
| 932 |
+
this.drag_start_position = null;
|
| 933 |
+
this.drag_start_bounds = null;
|
| 934 |
+
this.active_corner_index = -1;
|
| 935 |
+
this.active_edge_index = -1;
|
| 936 |
+
|
| 937 |
+
this.notify("change");
|
| 938 |
+
}
|
| 939 |
+
|
| 940 |
+
/**
|
| 941 |
+
* handles the start of window dragging
|
| 942 |
+
* @param event - the pointer event
|
| 943 |
+
*/
|
| 944 |
+
private handle_window_drag_start(event: FederatedPointerEvent): void {
|
| 945 |
+
if (this.current_subtool !== "crop") return;
|
| 946 |
+
event.stopPropagation();
|
| 947 |
+
|
| 948 |
+
this.is_dragging_window = true;
|
| 949 |
+
// Set flag indicating the crop has been manually changed
|
| 950 |
+
this.has_been_manually_changed = true;
|
| 951 |
+
|
| 952 |
+
// Store the initial position and bounds
|
| 953 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 954 |
+
event.global
|
| 955 |
+
);
|
| 956 |
+
this.drag_start_position = new Point(local_pos.x, local_pos.y);
|
| 957 |
+
this.drag_start_bounds = { ...this.crop_bounds };
|
| 958 |
+
}
|
| 959 |
+
|
| 960 |
+
/**
|
| 961 |
+
* @returns whether the crop has been manually changed by the user
|
| 962 |
+
*/
|
| 963 |
+
public get crop_manually_changed(): boolean {
|
| 964 |
+
return this.has_been_manually_changed;
|
| 965 |
+
}
|
| 966 |
+
|
| 967 |
+
on<T extends string>(event: T, callback: () => void): void {
|
| 968 |
+
this.event_callbacks.set(event, [
|
| 969 |
+
...(this.event_callbacks.get(event) || []),
|
| 970 |
+
callback
|
| 971 |
+
]);
|
| 972 |
+
}
|
| 973 |
+
|
| 974 |
+
off<T extends string>(event: T, callback: () => void): void {
|
| 975 |
+
this.event_callbacks.set(
|
| 976 |
+
event,
|
| 977 |
+
this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || []
|
| 978 |
+
);
|
| 979 |
+
}
|
| 980 |
+
|
| 981 |
+
private notify<T extends string>(event: T): void {
|
| 982 |
+
for (const callback of this.event_callbacks.get(event) || []) {
|
| 983 |
+
callback();
|
| 984 |
+
}
|
| 985 |
+
}
|
| 986 |
+
|
| 987 |
+
public get_crop_bounds(): {
|
| 988 |
+
x: number;
|
| 989 |
+
y: number;
|
| 990 |
+
crop_dimensions: { width: number; height: number };
|
| 991 |
+
image_dimensions: { width: number; height: number };
|
| 992 |
+
} {
|
| 993 |
+
const { width, height } =
|
| 994 |
+
this.image_editor_context.background_image?.getLocalBounds() ?? {
|
| 995 |
+
width: 0,
|
| 996 |
+
height: 0
|
| 997 |
+
};
|
| 998 |
+
return {
|
| 999 |
+
x: this.crop_bounds.x,
|
| 1000 |
+
y: this.crop_bounds.y,
|
| 1001 |
+
crop_dimensions: {
|
| 1002 |
+
width: this.crop_bounds.width,
|
| 1003 |
+
height: this.crop_bounds.height
|
| 1004 |
+
},
|
| 1005 |
+
image_dimensions: {
|
| 1006 |
+
width,
|
| 1007 |
+
height
|
| 1008 |
+
}
|
| 1009 |
+
};
|
| 1010 |
+
}
|
| 1011 |
+
|
| 1012 |
+
public get_image(): Promise<Blob | null> {
|
| 1013 |
+
if (!this.image_editor_context.background_image)
|
| 1014 |
+
return Promise.resolve(null);
|
| 1015 |
+
const container = new Container();
|
| 1016 |
+
const sprite = new Sprite(
|
| 1017 |
+
this.image_editor_context.background_image.texture
|
| 1018 |
+
);
|
| 1019 |
+
container.addChild(sprite);
|
| 1020 |
+
|
| 1021 |
+
return get_canvas_blob(
|
| 1022 |
+
this.image_editor_context.app.renderer,
|
| 1023 |
+
container,
|
| 1024 |
+
this.crop_bounds
|
| 1025 |
+
);
|
| 1026 |
+
}
|
| 1027 |
+
}
|
6.0.0-dev.5/imageeditor/shared/image/image.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Container,
|
| 3 |
+
Graphics,
|
| 4 |
+
Color,
|
| 5 |
+
type ColorSource,
|
| 6 |
+
RenderTexture,
|
| 7 |
+
Sprite,
|
| 8 |
+
type Renderer,
|
| 9 |
+
Texture
|
| 10 |
+
} from "pixi.js";
|
| 11 |
+
import { type ImageEditorContext, type Tool } from "../core/editor";
|
| 12 |
+
import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte";
|
| 13 |
+
import { type Command } from "../core/commands";
|
| 14 |
+
import { make_graphics } from "../utils/pixi";
|
| 15 |
+
import { get } from "svelte/store";
|
| 16 |
+
|
| 17 |
+
/**
|
| 18 |
+
* Handles adding images to the editor
|
| 19 |
+
* Only a single image can be added at a time
|
| 20 |
+
* Adding an image removes the current image is one exists
|
| 21 |
+
*/
|
| 22 |
+
export class ImageTool implements Tool {
|
| 23 |
+
name = "image";
|
| 24 |
+
context!: ImageEditorContext;
|
| 25 |
+
current_tool!: ToolbarTool;
|
| 26 |
+
current_subtool!: Subtool;
|
| 27 |
+
async setup(
|
| 28 |
+
context: ImageEditorContext,
|
| 29 |
+
tool: ToolbarTool,
|
| 30 |
+
subtool: Subtool
|
| 31 |
+
): Promise<void> {
|
| 32 |
+
this.context = context;
|
| 33 |
+
this.current_tool = tool;
|
| 34 |
+
this.current_subtool = subtool;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
cleanup(): void {}
|
| 38 |
+
|
| 39 |
+
async add_image({
|
| 40 |
+
image,
|
| 41 |
+
fixed_canvas,
|
| 42 |
+
border_region = 0
|
| 43 |
+
}: {
|
| 44 |
+
image: Blob | File | Texture;
|
| 45 |
+
fixed_canvas: boolean;
|
| 46 |
+
border_region?: number;
|
| 47 |
+
}): Promise<void> {
|
| 48 |
+
const image_command = new AddImageCommand(
|
| 49 |
+
this.context,
|
| 50 |
+
image,
|
| 51 |
+
fixed_canvas,
|
| 52 |
+
border_region
|
| 53 |
+
);
|
| 54 |
+
|
| 55 |
+
await this.context.execute_command(image_command);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void {
|
| 59 |
+
this.current_tool = tool;
|
| 60 |
+
this.current_subtool = subtool;
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
interface BgImageCommand extends Command {
|
| 65 |
+
/**
|
| 66 |
+
* Initial setup for the bg command
|
| 67 |
+
* @returns
|
| 68 |
+
*/
|
| 69 |
+
start: () => Promise<void>;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
export class AddImageCommand implements BgImageCommand {
|
| 73 |
+
sprite: Sprite | null = null;
|
| 74 |
+
fixed_canvas: boolean;
|
| 75 |
+
context: ImageEditorContext;
|
| 76 |
+
background: Blob | File | Texture;
|
| 77 |
+
current_canvas_size: { width: number; height: number };
|
| 78 |
+
computed_dimensions: { width: number; height: number };
|
| 79 |
+
current_scale: number;
|
| 80 |
+
current_position: { x: number; y: number };
|
| 81 |
+
border_region: number;
|
| 82 |
+
|
| 83 |
+
scaled_width!: number;
|
| 84 |
+
scaled_height!: number;
|
| 85 |
+
|
| 86 |
+
private previous_image: {
|
| 87 |
+
texture: Texture;
|
| 88 |
+
width: number;
|
| 89 |
+
height: number;
|
| 90 |
+
x: number;
|
| 91 |
+
y: number;
|
| 92 |
+
border_region: number;
|
| 93 |
+
} | null = null;
|
| 94 |
+
name: string;
|
| 95 |
+
|
| 96 |
+
constructor(
|
| 97 |
+
context: ImageEditorContext,
|
| 98 |
+
background: Blob | File | Texture,
|
| 99 |
+
fixed_canvas: boolean,
|
| 100 |
+
border_region = 0
|
| 101 |
+
) {
|
| 102 |
+
this.name = "AddImage";
|
| 103 |
+
this.context = context;
|
| 104 |
+
this.background = background;
|
| 105 |
+
this.fixed_canvas = fixed_canvas;
|
| 106 |
+
this.border_region = border_region;
|
| 107 |
+
this.current_canvas_size = get(this.context.dimensions);
|
| 108 |
+
this.current_scale = get(this.context.scale);
|
| 109 |
+
this.current_position = get(this.context.position);
|
| 110 |
+
|
| 111 |
+
this.computed_dimensions = { width: 0, height: 0 };
|
| 112 |
+
|
| 113 |
+
if (
|
| 114 |
+
this.context.background_image &&
|
| 115 |
+
this.context.background_image.texture
|
| 116 |
+
) {
|
| 117 |
+
const bg = this.context.background_image;
|
| 118 |
+
const stored_border_region = (bg as any).borderRegion || 0;
|
| 119 |
+
|
| 120 |
+
this.previous_image = {
|
| 121 |
+
texture: this.clone_texture(bg.texture),
|
| 122 |
+
width: bg.width,
|
| 123 |
+
height: bg.height,
|
| 124 |
+
x: bg.position.x,
|
| 125 |
+
y: bg.position.y,
|
| 126 |
+
border_region: stored_border_region
|
| 127 |
+
};
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/**
|
| 132 |
+
* Creates a copy of a texture to avoid reference issues
|
| 133 |
+
*/
|
| 134 |
+
private clone_texture(texture: Texture): Texture {
|
| 135 |
+
const render_texture = RenderTexture.create({
|
| 136 |
+
width: texture.width,
|
| 137 |
+
height: texture.height,
|
| 138 |
+
resolution: window.devicePixelRatio || 1
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
const sprite = new Sprite(texture);
|
| 142 |
+
const container = new Container();
|
| 143 |
+
container.addChild(sprite);
|
| 144 |
+
|
| 145 |
+
this.context.app.renderer.render(container, {
|
| 146 |
+
renderTexture: render_texture
|
| 147 |
+
});
|
| 148 |
+
|
| 149 |
+
container.destroy({ children: true });
|
| 150 |
+
return render_texture;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
async start(): Promise<void> {
|
| 154 |
+
let image_texture: Texture;
|
| 155 |
+
if (this.background instanceof Texture) {
|
| 156 |
+
image_texture = this.background;
|
| 157 |
+
} else {
|
| 158 |
+
const img = await createImageBitmap(this.background);
|
| 159 |
+
image_texture = Texture.from(img);
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
this.sprite = new Sprite(image_texture);
|
| 163 |
+
|
| 164 |
+
const [width, height] = this.handle_image();
|
| 165 |
+
this.computed_dimensions = { width, height };
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
private handle_image(): [number, number] {
|
| 169 |
+
if (this.sprite === null) {
|
| 170 |
+
return [0, 0];
|
| 171 |
+
}
|
| 172 |
+
if (this.fixed_canvas) {
|
| 173 |
+
const effectiveCanvasWidth = Math.max(
|
| 174 |
+
this.current_canvas_size.width - this.border_region * 2,
|
| 175 |
+
10
|
| 176 |
+
);
|
| 177 |
+
const effectiveCanvasHeight = Math.max(
|
| 178 |
+
this.current_canvas_size.height - this.border_region * 2,
|
| 179 |
+
10
|
| 180 |
+
);
|
| 181 |
+
|
| 182 |
+
const { width, height, x, y } = fit_image_to_canvas(
|
| 183 |
+
this.sprite.width,
|
| 184 |
+
this.sprite.height,
|
| 185 |
+
effectiveCanvasWidth,
|
| 186 |
+
effectiveCanvasHeight
|
| 187 |
+
);
|
| 188 |
+
|
| 189 |
+
this.sprite.width = width;
|
| 190 |
+
this.sprite.height = height;
|
| 191 |
+
|
| 192 |
+
this.sprite.x = x + this.border_region;
|
| 193 |
+
this.sprite.y = y + this.border_region;
|
| 194 |
+
} else {
|
| 195 |
+
const width = this.sprite.width;
|
| 196 |
+
const height = this.sprite.height;
|
| 197 |
+
|
| 198 |
+
this.sprite.x = this.border_region;
|
| 199 |
+
this.sprite.y = this.border_region;
|
| 200 |
+
|
| 201 |
+
return [width + this.border_region * 2, height + this.border_region * 2];
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
return [this.current_canvas_size.width, this.current_canvas_size.height];
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
async execute(context?: ImageEditorContext): Promise<void> {
|
| 208 |
+
if (context) {
|
| 209 |
+
this.context = context;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
await this.start();
|
| 213 |
+
|
| 214 |
+
if (this.sprite === null) {
|
| 215 |
+
return;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
const { width, height } = this.computed_dimensions;
|
| 219 |
+
|
| 220 |
+
await this.context.set_image_properties({
|
| 221 |
+
scale: 1,
|
| 222 |
+
position: {
|
| 223 |
+
x: this.context.app.screen.width / 2,
|
| 224 |
+
y: this.context.app.screen.height / 2
|
| 225 |
+
},
|
| 226 |
+
width: this.fixed_canvas ? this.current_canvas_size.width : width,
|
| 227 |
+
height: this.fixed_canvas ? this.current_canvas_size.height : height
|
| 228 |
+
});
|
| 229 |
+
|
| 230 |
+
const background_layer = this.context.layer_manager.create_background_layer(
|
| 231 |
+
this.fixed_canvas ? this.current_canvas_size.width : width,
|
| 232 |
+
this.fixed_canvas ? this.current_canvas_size.height : height
|
| 233 |
+
);
|
| 234 |
+
this.sprite.zIndex = 0;
|
| 235 |
+
background_layer.addChild(this.sprite);
|
| 236 |
+
|
| 237 |
+
this.context.layer_manager.reset_layers(
|
| 238 |
+
this.fixed_canvas ? this.current_canvas_size.width : width,
|
| 239 |
+
this.fixed_canvas ? this.current_canvas_size.height : height,
|
| 240 |
+
true
|
| 241 |
+
);
|
| 242 |
+
|
| 243 |
+
if (this.border_region > 0) {
|
| 244 |
+
(this.sprite as any).borderRegion = this.border_region;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
this.context.set_background_image(this.sprite);
|
| 248 |
+
|
| 249 |
+
this.context.reset();
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
async undo(): Promise<void> {
|
| 253 |
+
if (this.sprite) {
|
| 254 |
+
this.sprite.destroy();
|
| 255 |
+
|
| 256 |
+
if (this.previous_image) {
|
| 257 |
+
const previous_sprite = new Sprite(this.previous_image.texture);
|
| 258 |
+
previous_sprite.width = this.previous_image.width;
|
| 259 |
+
previous_sprite.height = this.previous_image.height;
|
| 260 |
+
previous_sprite.position.set(
|
| 261 |
+
this.previous_image.x,
|
| 262 |
+
this.previous_image.y
|
| 263 |
+
);
|
| 264 |
+
|
| 265 |
+
if (this.previous_image.border_region > 0) {
|
| 266 |
+
(previous_sprite as any).borderRegion =
|
| 267 |
+
this.previous_image.border_region;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
await this.context.set_image_properties({
|
| 271 |
+
scale: 1,
|
| 272 |
+
position: {
|
| 273 |
+
x: this.context.app.screen.width / 2,
|
| 274 |
+
y: this.context.app.screen.height / 2
|
| 275 |
+
},
|
| 276 |
+
width: this.previous_image.width,
|
| 277 |
+
height: this.previous_image.height
|
| 278 |
+
});
|
| 279 |
+
|
| 280 |
+
const background_layer =
|
| 281 |
+
this.context.layer_manager.create_background_layer(
|
| 282 |
+
this.previous_image.width,
|
| 283 |
+
this.previous_image.height
|
| 284 |
+
);
|
| 285 |
+
|
| 286 |
+
previous_sprite.zIndex = 0;
|
| 287 |
+
background_layer.addChild(previous_sprite);
|
| 288 |
+
|
| 289 |
+
this.context.layer_manager.reset_layers(
|
| 290 |
+
this.previous_image.width,
|
| 291 |
+
this.previous_image.height,
|
| 292 |
+
true
|
| 293 |
+
);
|
| 294 |
+
|
| 295 |
+
this.context.set_background_image(previous_sprite);
|
| 296 |
+
} else {
|
| 297 |
+
await this.context.set_image_properties({
|
| 298 |
+
scale: 1,
|
| 299 |
+
position: {
|
| 300 |
+
x: this.context.app.screen.width / 2,
|
| 301 |
+
y: this.context.app.screen.height / 2
|
| 302 |
+
},
|
| 303 |
+
width: this.current_canvas_size.width,
|
| 304 |
+
height: this.current_canvas_size.height
|
| 305 |
+
});
|
| 306 |
+
|
| 307 |
+
this.context.layer_manager.create_background_layer(
|
| 308 |
+
this.current_canvas_size.width,
|
| 309 |
+
this.current_canvas_size.height
|
| 310 |
+
);
|
| 311 |
+
|
| 312 |
+
this.context.layer_manager.reset_layers(
|
| 313 |
+
this.current_canvas_size.width,
|
| 314 |
+
this.current_canvas_size.height
|
| 315 |
+
);
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
this.context.reset();
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/**
|
| 324 |
+
* Command that sets a background
|
| 325 |
+
*/
|
| 326 |
+
interface BgColorCommand extends Command {
|
| 327 |
+
/**
|
| 328 |
+
* Initial setup for the bg command
|
| 329 |
+
* @returns
|
| 330 |
+
*/
|
| 331 |
+
start: () => [number, number];
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
/**
|
| 335 |
+
* Adds a background color to the canvas.
|
| 336 |
+
* @param container The container to add the image to.
|
| 337 |
+
* @param renderer The renderer to use for the image.
|
| 338 |
+
* @param color The background color to add.
|
| 339 |
+
* @param width The width of the background.
|
| 340 |
+
* @param height The height of the background.
|
| 341 |
+
* @param resize The function to resize the canvas.
|
| 342 |
+
* @returns A command that can be used to undo the action.
|
| 343 |
+
*/
|
| 344 |
+
export function add_bg_color(
|
| 345 |
+
container: Container,
|
| 346 |
+
renderer: Renderer,
|
| 347 |
+
color: ColorSource,
|
| 348 |
+
width: number,
|
| 349 |
+
height: number,
|
| 350 |
+
resize: (width: number, height: number) => void
|
| 351 |
+
): BgColorCommand {
|
| 352 |
+
let sprite: Sprite;
|
| 353 |
+
return {
|
| 354 |
+
name: "AddBgColor",
|
| 355 |
+
start() {
|
| 356 |
+
container.removeChildren();
|
| 357 |
+
const graphics = make_graphics(1);
|
| 358 |
+
const texture = RenderTexture.create({
|
| 359 |
+
width,
|
| 360 |
+
height
|
| 361 |
+
});
|
| 362 |
+
|
| 363 |
+
renderer.render(graphics, { renderTexture: texture });
|
| 364 |
+
sprite = new Sprite(texture);
|
| 365 |
+
return [sprite.width, sprite.height];
|
| 366 |
+
},
|
| 367 |
+
async execute() {
|
| 368 |
+
resize(sprite.width, sprite.height);
|
| 369 |
+
sprite.zIndex = 1;
|
| 370 |
+
container.addChild(sprite);
|
| 371 |
+
},
|
| 372 |
+
undo() {
|
| 373 |
+
container.removeChildren();
|
| 374 |
+
}
|
| 375 |
+
};
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
/**
|
| 379 |
+
* Calculates new dimensions and position for an image to fit within a canvas while maintaining aspect ratio
|
| 380 |
+
* @param image_width Original width of the image
|
| 381 |
+
* @param image_height Original height of the image
|
| 382 |
+
* @param canvas_width Width of the canvas
|
| 383 |
+
* @param canvas_height Height of the canvas
|
| 384 |
+
* @returns Object containing new dimensions and position
|
| 385 |
+
*/
|
| 386 |
+
export function fit_image_to_canvas(
|
| 387 |
+
image_width: number,
|
| 388 |
+
image_height: number,
|
| 389 |
+
canvas_width: number,
|
| 390 |
+
canvas_height: number
|
| 391 |
+
): {
|
| 392 |
+
width: number;
|
| 393 |
+
height: number;
|
| 394 |
+
x: number;
|
| 395 |
+
y: number;
|
| 396 |
+
} {
|
| 397 |
+
const image_aspect_ratio = image_width / image_height;
|
| 398 |
+
const canvas_aspect_ratio = canvas_width / canvas_height;
|
| 399 |
+
|
| 400 |
+
let new_width: number;
|
| 401 |
+
let new_height: number;
|
| 402 |
+
|
| 403 |
+
if (image_aspect_ratio > canvas_aspect_ratio) {
|
| 404 |
+
new_width = canvas_width;
|
| 405 |
+
new_height = canvas_width / image_aspect_ratio;
|
| 406 |
+
} else {
|
| 407 |
+
new_height = canvas_height;
|
| 408 |
+
new_width = canvas_height * image_aspect_ratio;
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
const x = Math.round((canvas_width - new_width) / 2);
|
| 412 |
+
const y = Math.round((canvas_height - new_height) / 2);
|
| 413 |
+
|
| 414 |
+
return {
|
| 415 |
+
width: Math.round(new_width),
|
| 416 |
+
height: Math.round(new_height),
|
| 417 |
+
x,
|
| 418 |
+
y
|
| 419 |
+
};
|
| 420 |
+
}
|
6.0.0-dev.5/imageeditor/shared/resize/resize.ts
ADDED
|
@@ -0,0 +1,1759 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Container,
|
| 3 |
+
Graphics,
|
| 4 |
+
Point,
|
| 5 |
+
Rectangle,
|
| 6 |
+
FederatedPointerEvent,
|
| 7 |
+
type FederatedOptions
|
| 8 |
+
} from "pixi.js";
|
| 9 |
+
import { type ImageEditorContext, type Tool } from "../core/editor";
|
| 10 |
+
import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte";
|
| 11 |
+
import { type Command } from "../core/commands";
|
| 12 |
+
|
| 13 |
+
/**
|
| 14 |
+
* Command that handles resizing the image and supports undo/redo
|
| 15 |
+
*/
|
| 16 |
+
export class ResizeCommand implements Command {
|
| 17 |
+
private original_width: number;
|
| 18 |
+
private original_height: number;
|
| 19 |
+
private original_x: number;
|
| 20 |
+
private original_y: number;
|
| 21 |
+
|
| 22 |
+
private new_width: number;
|
| 23 |
+
private new_height: number;
|
| 24 |
+
private new_x: number;
|
| 25 |
+
private new_y: number;
|
| 26 |
+
private reset_ui: () => void;
|
| 27 |
+
name: string;
|
| 28 |
+
|
| 29 |
+
constructor(
|
| 30 |
+
private context: ImageEditorContext,
|
| 31 |
+
original_state: { width: number; height: number; x: number; y: number },
|
| 32 |
+
new_state: { width: number; height: number; x: number; y: number },
|
| 33 |
+
reset_ui: () => void
|
| 34 |
+
) {
|
| 35 |
+
this.name = "Resize";
|
| 36 |
+
this.original_width = original_state.width;
|
| 37 |
+
this.original_height = original_state.height;
|
| 38 |
+
this.original_x = original_state.x;
|
| 39 |
+
this.original_y = original_state.y;
|
| 40 |
+
|
| 41 |
+
this.new_width = new_state.width;
|
| 42 |
+
this.new_height = new_state.height;
|
| 43 |
+
this.new_x = new_state.x;
|
| 44 |
+
this.new_y = new_state.y;
|
| 45 |
+
this.reset_ui = reset_ui;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
async execute(context?: ImageEditorContext): Promise<void> {
|
| 49 |
+
if (context) {
|
| 50 |
+
this.context = context;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
if (!this.context.background_image) return;
|
| 54 |
+
|
| 55 |
+
this.context.background_image.width = this.new_width;
|
| 56 |
+
this.context.background_image.height = this.new_height;
|
| 57 |
+
this.context.background_image.position.set(this.new_x, this.new_y);
|
| 58 |
+
|
| 59 |
+
this.reset_ui();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
async undo(): Promise<void> {
|
| 63 |
+
if (!this.context.background_image) return;
|
| 64 |
+
|
| 65 |
+
this.context.background_image.width = this.original_width;
|
| 66 |
+
this.context.background_image.height = this.original_height;
|
| 67 |
+
this.context.background_image.position.set(
|
| 68 |
+
this.original_x,
|
| 69 |
+
this.original_y
|
| 70 |
+
);
|
| 71 |
+
|
| 72 |
+
this.reset_ui();
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/**
|
| 77 |
+
* @class ResizeTool
|
| 78 |
+
* @implements {Tool}
|
| 79 |
+
* @description Implements the resize tool functionality for the image editor
|
| 80 |
+
*/
|
| 81 |
+
export class ResizeTool implements Tool {
|
| 82 |
+
name = "resize" as const;
|
| 83 |
+
private image_editor_context!: ImageEditorContext;
|
| 84 |
+
current_tool: ToolbarTool = "image";
|
| 85 |
+
current_subtool: Subtool = "size";
|
| 86 |
+
private current_cursor: FederatedOptions["cursor"] = "unset";
|
| 87 |
+
private readonly CORNER_SIZE = 25;
|
| 88 |
+
private readonly LINE_THICKNESS = 5;
|
| 89 |
+
private readonly HANDLE_COLOR = 0x000000;
|
| 90 |
+
private readonly HIT_AREA_SIZE = 40;
|
| 91 |
+
private borderRegion = 0;
|
| 92 |
+
|
| 93 |
+
private is_dragging = false;
|
| 94 |
+
private selected_handle: Container | null = null;
|
| 95 |
+
private active_corner_index = -1;
|
| 96 |
+
private active_edge_index = -1;
|
| 97 |
+
private last_pointer_position: Point | null = null;
|
| 98 |
+
private resize_ui_container: Container | null = null;
|
| 99 |
+
private dimensions: { width: number; height: number } = {
|
| 100 |
+
width: 0,
|
| 101 |
+
height: 0
|
| 102 |
+
};
|
| 103 |
+
private position: { x: number; y: number } = { x: 0, y: 0 };
|
| 104 |
+
private scale = 1;
|
| 105 |
+
private is_moving = false;
|
| 106 |
+
private dom_mousedown_handler: ((e: MouseEvent) => void) | null = null;
|
| 107 |
+
private dom_mousemove_handler: ((e: MouseEvent) => void) | null = null;
|
| 108 |
+
private dom_mouseup_handler: ((e: MouseEvent) => void) | null = null;
|
| 109 |
+
private event_callbacks: Map<string, (() => void)[]> = new Map();
|
| 110 |
+
private last_scale = 1;
|
| 111 |
+
|
| 112 |
+
private original_state: {
|
| 113 |
+
width: number;
|
| 114 |
+
height: number;
|
| 115 |
+
x: number;
|
| 116 |
+
y: number;
|
| 117 |
+
} | null = null;
|
| 118 |
+
|
| 119 |
+
/**
|
| 120 |
+
* @method setup
|
| 121 |
+
* @async
|
| 122 |
+
* @description Sets up the resize tool with the given context and tool/subtool
|
| 123 |
+
* @param {ImageEditorContext} context - The image editor context
|
| 124 |
+
* @param {ToolbarTool} tool - The current tool
|
| 125 |
+
* @param {Subtool} subtool - The current subtool
|
| 126 |
+
* @returns {Promise<void>}
|
| 127 |
+
*/
|
| 128 |
+
async setup(
|
| 129 |
+
context: ImageEditorContext,
|
| 130 |
+
tool: ToolbarTool,
|
| 131 |
+
subtool: Subtool
|
| 132 |
+
): Promise<void> {
|
| 133 |
+
this.image_editor_context = context;
|
| 134 |
+
this.current_tool = tool;
|
| 135 |
+
this.current_subtool = subtool;
|
| 136 |
+
|
| 137 |
+
if (context.background_image) {
|
| 138 |
+
if ((context.background_image as any).borderRegion !== undefined) {
|
| 139 |
+
this.borderRegion = (context.background_image as any).borderRegion;
|
| 140 |
+
} else {
|
| 141 |
+
const bg = context.background_image;
|
| 142 |
+
const container = context.image_container;
|
| 143 |
+
|
| 144 |
+
const isHorizontallyCentered =
|
| 145 |
+
Math.abs(bg.position.x - (container.width - bg.width) / 2) < 2;
|
| 146 |
+
const isVerticallyCentered =
|
| 147 |
+
Math.abs(bg.position.y - (container.height - bg.height) / 2) < 2;
|
| 148 |
+
|
| 149 |
+
if (!isHorizontallyCentered || !isVerticallyCentered) {
|
| 150 |
+
this.borderRegion = Math.max(bg.position.x, bg.position.y);
|
| 151 |
+
|
| 152 |
+
(context.background_image as any).borderRegion = this.borderRegion;
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
context.dimensions.subscribe((dimensions) => {
|
| 158 |
+
this.dimensions = dimensions;
|
| 159 |
+
if (this.resize_ui_container) {
|
| 160 |
+
this.update_resize_ui();
|
| 161 |
+
}
|
| 162 |
+
});
|
| 163 |
+
|
| 164 |
+
context.position.subscribe((position) => {
|
| 165 |
+
this.position = position;
|
| 166 |
+
if (
|
| 167 |
+
this.resize_ui_container &&
|
| 168 |
+
this.current_tool === "image" &&
|
| 169 |
+
this.current_subtool === "size"
|
| 170 |
+
) {
|
| 171 |
+
this.update_resize_ui();
|
| 172 |
+
}
|
| 173 |
+
});
|
| 174 |
+
|
| 175 |
+
context.scale.subscribe((scale) => {
|
| 176 |
+
this.scale = scale;
|
| 177 |
+
if (
|
| 178 |
+
this.resize_ui_container &&
|
| 179 |
+
this.current_tool === "image" &&
|
| 180 |
+
this.current_subtool === "size"
|
| 181 |
+
) {
|
| 182 |
+
this.update_resize_ui();
|
| 183 |
+
}
|
| 184 |
+
});
|
| 185 |
+
|
| 186 |
+
await this.init_resize_ui();
|
| 187 |
+
this.setup_event_listeners();
|
| 188 |
+
|
| 189 |
+
if (
|
| 190 |
+
this.current_subtool === "size" &&
|
| 191 |
+
this.image_editor_context.background_image
|
| 192 |
+
) {
|
| 193 |
+
const bg = this.image_editor_context.background_image;
|
| 194 |
+
const container = this.image_editor_context.image_container;
|
| 195 |
+
|
| 196 |
+
const bg_width = bg.width;
|
| 197 |
+
const bg_height = bg.height;
|
| 198 |
+
const container_bounds = container.getLocalBounds();
|
| 199 |
+
const container_width = container_bounds.width;
|
| 200 |
+
const container_height = container_bounds.height;
|
| 201 |
+
|
| 202 |
+
const min_x = -(bg_width - container_width);
|
| 203 |
+
const min_y = -(bg_height - container_height);
|
| 204 |
+
const max_x = 0;
|
| 205 |
+
const max_y = 0;
|
| 206 |
+
|
| 207 |
+
const x_out_of_bounds =
|
| 208 |
+
bg_width > container_width &&
|
| 209 |
+
(bg.position.x < min_x || bg.position.x > max_x);
|
| 210 |
+
const y_out_of_bounds =
|
| 211 |
+
bg_height > container_height &&
|
| 212 |
+
(bg.position.y < min_y || bg.position.y > max_y);
|
| 213 |
+
|
| 214 |
+
if (x_out_of_bounds || y_out_of_bounds) {
|
| 215 |
+
this.apply_boundary_constraints();
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
/**
|
| 221 |
+
* @method cleanup
|
| 222 |
+
* @description Cleans up the resize tool resources and removes event listeners
|
| 223 |
+
* @returns {void}
|
| 224 |
+
*/
|
| 225 |
+
cleanup(): void {
|
| 226 |
+
if (this.resize_ui_container) {
|
| 227 |
+
this.image_editor_context.app.stage.removeChild(this.resize_ui_container);
|
| 228 |
+
}
|
| 229 |
+
this.cleanup_event_listeners();
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
/**
|
| 233 |
+
* @method set_tool
|
| 234 |
+
* @description Sets the current tool and subtool, updating UI visibility
|
| 235 |
+
* @param {ToolbarTool} tool - The tool to set
|
| 236 |
+
* @param {Subtool} subtool - The subtool to set
|
| 237 |
+
* @returns {void}
|
| 238 |
+
*/
|
| 239 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void {
|
| 240 |
+
this.current_tool = tool;
|
| 241 |
+
this.current_subtool = subtool;
|
| 242 |
+
|
| 243 |
+
if (tool === "image" && subtool === "size") {
|
| 244 |
+
if (this.image_editor_context.background_image) {
|
| 245 |
+
const storedBorderRegion = (
|
| 246 |
+
this.image_editor_context.background_image as any
|
| 247 |
+
).borderRegion;
|
| 248 |
+
if (typeof storedBorderRegion === "number" && storedBorderRegion > 0) {
|
| 249 |
+
this.borderRegion = storedBorderRegion;
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
this.show_resize_ui();
|
| 254 |
+
|
| 255 |
+
if (this.image_editor_context.background_image) {
|
| 256 |
+
const bg = this.image_editor_context.background_image;
|
| 257 |
+
const container = this.image_editor_context.image_container;
|
| 258 |
+
|
| 259 |
+
const bg_width = bg.width;
|
| 260 |
+
const bg_height = bg.height;
|
| 261 |
+
const container_bounds = container.getLocalBounds();
|
| 262 |
+
const container_width = container_bounds.width;
|
| 263 |
+
const container_height = container_bounds.height;
|
| 264 |
+
|
| 265 |
+
const effectiveWidth = container_width - this.borderRegion * 2;
|
| 266 |
+
const effectiveHeight = container_height - this.borderRegion * 2;
|
| 267 |
+
|
| 268 |
+
const min_x =
|
| 269 |
+
bg_width > effectiveWidth
|
| 270 |
+
? -(bg_width - effectiveWidth) + this.borderRegion
|
| 271 |
+
: this.borderRegion;
|
| 272 |
+
const max_x =
|
| 273 |
+
bg_width > effectiveWidth
|
| 274 |
+
? this.borderRegion
|
| 275 |
+
: container_width - bg_width - this.borderRegion;
|
| 276 |
+
|
| 277 |
+
const min_y =
|
| 278 |
+
bg_height > effectiveHeight
|
| 279 |
+
? -(bg_height - effectiveHeight) + this.borderRegion
|
| 280 |
+
: this.borderRegion;
|
| 281 |
+
const max_y =
|
| 282 |
+
bg_height > effectiveHeight
|
| 283 |
+
? this.borderRegion
|
| 284 |
+
: container_height - bg_height - this.borderRegion;
|
| 285 |
+
|
| 286 |
+
const x_out_of_bounds = bg.position.x < min_x || bg.position.x > max_x;
|
| 287 |
+
const y_out_of_bounds = bg.position.y < min_y || bg.position.y > max_y;
|
| 288 |
+
|
| 289 |
+
if (x_out_of_bounds || y_out_of_bounds) {
|
| 290 |
+
this.apply_boundary_constraints();
|
| 291 |
+
}
|
| 292 |
+
}
|
| 293 |
+
} else {
|
| 294 |
+
this.hide_resize_ui();
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
/**
|
| 299 |
+
* @method init_resize_ui
|
| 300 |
+
* @private
|
| 301 |
+
* @async
|
| 302 |
+
* @description Initializes the resize UI elements and sets up the update loop
|
| 303 |
+
* @returns {Promise<void>}
|
| 304 |
+
*/
|
| 305 |
+
private async init_resize_ui(): Promise<void> {
|
| 306 |
+
this.resize_ui_container = this.make_resize_ui(
|
| 307 |
+
this.dimensions.width * this.scale,
|
| 308 |
+
this.dimensions.height * this.scale
|
| 309 |
+
);
|
| 310 |
+
|
| 311 |
+
this.resize_ui_container.position.set(this.position.x, this.position.y);
|
| 312 |
+
|
| 313 |
+
this.resize_ui_container.visible = false;
|
| 314 |
+
|
| 315 |
+
this.image_editor_context.app.stage.addChild(this.resize_ui_container);
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
/**
|
| 319 |
+
* @method make_resize_ui
|
| 320 |
+
* @private
|
| 321 |
+
* @description Creates the resize UI with the specified dimensions
|
| 322 |
+
* @param {number} width - Width of the resize area
|
| 323 |
+
* @param {number} height - Height of the resize area
|
| 324 |
+
* @returns {Container} The created resize UI container
|
| 325 |
+
*/
|
| 326 |
+
private make_resize_ui(width: number, height: number): Container {
|
| 327 |
+
const resize_container = new Container();
|
| 328 |
+
resize_container.eventMode = "static";
|
| 329 |
+
resize_container.interactiveChildren = true;
|
| 330 |
+
|
| 331 |
+
const outline = new Graphics().rect(0, 0, width, height).stroke({
|
| 332 |
+
width: 1,
|
| 333 |
+
color: 0x000000,
|
| 334 |
+
alignment: 0,
|
| 335 |
+
alpha: 0.3
|
| 336 |
+
});
|
| 337 |
+
|
| 338 |
+
const dotted_outline = new Graphics();
|
| 339 |
+
|
| 340 |
+
dotted_outline.rect(0, 0, width, height).stroke({
|
| 341 |
+
width: 1,
|
| 342 |
+
color: 0x000000,
|
| 343 |
+
alpha: 0.7,
|
| 344 |
+
pixelLine: true
|
| 345 |
+
});
|
| 346 |
+
|
| 347 |
+
const dash_length = 5;
|
| 348 |
+
const gap_length = 5;
|
| 349 |
+
const total_length = dash_length + gap_length;
|
| 350 |
+
|
| 351 |
+
for (let x = 0; x < width; x += total_length * 2) {
|
| 352 |
+
dotted_outline
|
| 353 |
+
.rect(x, 0, Math.min(dash_length, width - x), 1)
|
| 354 |
+
.fill(0x000000);
|
| 355 |
+
|
| 356 |
+
dotted_outline
|
| 357 |
+
.rect(x, height - 1, Math.min(dash_length, width - x), 1)
|
| 358 |
+
.fill(0x000000);
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
for (let y = 0; y < height; y += total_length * 2) {
|
| 362 |
+
dotted_outline
|
| 363 |
+
.rect(0, y, 1, Math.min(dash_length, height - y))
|
| 364 |
+
.fill(0x000000);
|
| 365 |
+
|
| 366 |
+
dotted_outline
|
| 367 |
+
.rect(width - 1, y, 1, Math.min(dash_length, height - y))
|
| 368 |
+
.fill(0x000000);
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
resize_container.addChild(outline);
|
| 372 |
+
resize_container.addChild(dotted_outline);
|
| 373 |
+
|
| 374 |
+
const move_area = new Graphics()
|
| 375 |
+
.rect(0, 0, width, height)
|
| 376 |
+
.fill(0xffffff, 0);
|
| 377 |
+
move_area.eventMode = "static";
|
| 378 |
+
move_area.cursor = "move";
|
| 379 |
+
resize_container.addChild(move_area);
|
| 380 |
+
|
| 381 |
+
this.create_corner_handles(resize_container, width, height);
|
| 382 |
+
this.create_edge_handles(resize_container, width, height);
|
| 383 |
+
|
| 384 |
+
return resize_container;
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
/**
|
| 388 |
+
* @method create_handle
|
| 389 |
+
* @private
|
| 390 |
+
* @description Creates a handle for the resize UI
|
| 391 |
+
* @param {boolean} [is_edge=false] - Whether the handle is an edge handle
|
| 392 |
+
* @returns {Container} The created handle container
|
| 393 |
+
*/
|
| 394 |
+
private create_handle(is_edge = false): Container {
|
| 395 |
+
const handle = new Container();
|
| 396 |
+
handle.eventMode = "static";
|
| 397 |
+
handle.cursor = "pointer";
|
| 398 |
+
|
| 399 |
+
const handle_graphics = new Graphics();
|
| 400 |
+
const handle_size = is_edge ? 8 : 10;
|
| 401 |
+
handle_graphics
|
| 402 |
+
.rect(-handle_size / 2, -handle_size / 2, handle_size, handle_size)
|
| 403 |
+
.fill(0xffffff)
|
| 404 |
+
.stroke({ width: 1, color: this.HANDLE_COLOR });
|
| 405 |
+
handle.addChild(handle_graphics);
|
| 406 |
+
|
| 407 |
+
const hit_size = is_edge ? this.HIT_AREA_SIZE * 1.5 : this.HIT_AREA_SIZE;
|
| 408 |
+
handle.hitArea = new Rectangle(
|
| 409 |
+
-hit_size / 2,
|
| 410 |
+
-hit_size / 2,
|
| 411 |
+
hit_size,
|
| 412 |
+
hit_size
|
| 413 |
+
);
|
| 414 |
+
|
| 415 |
+
return handle;
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
/**
|
| 419 |
+
* @method create_edge_handles
|
| 420 |
+
* @private
|
| 421 |
+
* @description Creates edge handles for the resize UI
|
| 422 |
+
* @param {Container} container - The container to add handles to
|
| 423 |
+
* @param {number} width - Width of the resize area
|
| 424 |
+
* @param {number} height - Height of the resize area
|
| 425 |
+
* @returns {void}
|
| 426 |
+
*/
|
| 427 |
+
private create_edge_handles(
|
| 428 |
+
container: Container,
|
| 429 |
+
width: number,
|
| 430 |
+
height: number
|
| 431 |
+
): void {
|
| 432 |
+
[-1, 1].forEach((i, index) => {
|
| 433 |
+
const handle = this.create_handle(true);
|
| 434 |
+
handle.rotation = 0;
|
| 435 |
+
|
| 436 |
+
handle.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 437 |
+
this.handle_pointer_down(event, handle, -1, index);
|
| 438 |
+
});
|
| 439 |
+
|
| 440 |
+
handle.x = width / 2;
|
| 441 |
+
handle.y = i < 0 ? 0 : height;
|
| 442 |
+
handle.cursor = "ns-resize";
|
| 443 |
+
|
| 444 |
+
container.addChild(handle);
|
| 445 |
+
});
|
| 446 |
+
|
| 447 |
+
[-1, 1].forEach((i, index) => {
|
| 448 |
+
const handle = this.create_handle(true);
|
| 449 |
+
handle.rotation = 0;
|
| 450 |
+
handle.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 451 |
+
this.handle_pointer_down(event, handle, -1, index + 2);
|
| 452 |
+
});
|
| 453 |
+
|
| 454 |
+
handle.x = i < 0 ? 0 : width;
|
| 455 |
+
handle.y = height / 2;
|
| 456 |
+
handle.cursor = "ew-resize";
|
| 457 |
+
container.addChild(handle);
|
| 458 |
+
});
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
/**
|
| 462 |
+
* @method create_corner_handles
|
| 463 |
+
* @private
|
| 464 |
+
* @description Creates corner handles for the resize UI
|
| 465 |
+
* @param {Container} container - The container to add handles to
|
| 466 |
+
* @param {number} width - Width of the resize area
|
| 467 |
+
* @param {number} height - Height of the resize area
|
| 468 |
+
* @returns {void}
|
| 469 |
+
*/
|
| 470 |
+
private create_corner_handles(
|
| 471 |
+
container: Container,
|
| 472 |
+
width: number,
|
| 473 |
+
height: number
|
| 474 |
+
): void {
|
| 475 |
+
const corners = [
|
| 476 |
+
{ x: 0, y: 0, cursor: "nwse-resize" },
|
| 477 |
+
{ x: width, y: 0, cursor: "nesw-resize" },
|
| 478 |
+
{ x: 0, y: height, cursor: "nesw-resize" },
|
| 479 |
+
{ x: width, y: height, cursor: "nwse-resize" }
|
| 480 |
+
];
|
| 481 |
+
|
| 482 |
+
corners.forEach(({ x, y, cursor }, i) => {
|
| 483 |
+
const corner = this.create_handle(false);
|
| 484 |
+
corner.x = x;
|
| 485 |
+
corner.y = y;
|
| 486 |
+
|
| 487 |
+
corner.on("pointerdown", (event: FederatedPointerEvent) => {
|
| 488 |
+
this.handle_pointer_down(event, corner, i, -1);
|
| 489 |
+
});
|
| 490 |
+
|
| 491 |
+
corner.cursor = cursor;
|
| 492 |
+
|
| 493 |
+
container.addChild(corner);
|
| 494 |
+
});
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
/**
|
| 498 |
+
* @method handle_pointer_down
|
| 499 |
+
* @private
|
| 500 |
+
* @description Handles pointer down events on handles
|
| 501 |
+
* @param {FederatedPointerEvent} event - The pointer event
|
| 502 |
+
* @param {Container} handle - The handle that was clicked
|
| 503 |
+
* @param {number} corner_index - Index of the corner (-1 if not a corner)
|
| 504 |
+
* @param {number} edge_index - Index of the edge (-1 if not an edge)
|
| 505 |
+
* @returns {void}
|
| 506 |
+
*/
|
| 507 |
+
private handle_pointer_down(
|
| 508 |
+
event: FederatedPointerEvent,
|
| 509 |
+
handle: Container,
|
| 510 |
+
corner_index: number,
|
| 511 |
+
edge_index: number
|
| 512 |
+
): void {
|
| 513 |
+
if (this.current_subtool !== "size") return;
|
| 514 |
+
|
| 515 |
+
event.stopPropagation();
|
| 516 |
+
|
| 517 |
+
this.is_dragging = true;
|
| 518 |
+
this.selected_handle = handle;
|
| 519 |
+
this.active_corner_index = corner_index;
|
| 520 |
+
this.active_edge_index = edge_index;
|
| 521 |
+
|
| 522 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 523 |
+
event.global
|
| 524 |
+
);
|
| 525 |
+
this.last_pointer_position = new Point(local_pos.x, local_pos.y);
|
| 526 |
+
|
| 527 |
+
if (this.image_editor_context.background_image) {
|
| 528 |
+
const bg = this.image_editor_context.background_image;
|
| 529 |
+
this.original_state = {
|
| 530 |
+
width: bg.width,
|
| 531 |
+
height: bg.height,
|
| 532 |
+
x: bg.position.x,
|
| 533 |
+
y: bg.position.y
|
| 534 |
+
};
|
| 535 |
+
}
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
/**
|
| 539 |
+
* Maintains aspect ratio during resizing
|
| 540 |
+
* @param new_width - The new width
|
| 541 |
+
* @param new_height - The new height
|
| 542 |
+
* @param original_aspect_ratio - The original aspect ratio
|
| 543 |
+
* @param delta - The delta to apply
|
| 544 |
+
* @returns The adjusted dimensions
|
| 545 |
+
*/
|
| 546 |
+
private maintain_aspect_ratio(
|
| 547 |
+
new_width: number,
|
| 548 |
+
new_height: number,
|
| 549 |
+
original_aspect_ratio: number,
|
| 550 |
+
delta: Point
|
| 551 |
+
): { width: number; height: number } {
|
| 552 |
+
const abs_delta_x = Math.abs(delta.x);
|
| 553 |
+
const abs_delta_y = Math.abs(delta.y);
|
| 554 |
+
|
| 555 |
+
if (abs_delta_x > abs_delta_y * 1.2) {
|
| 556 |
+
new_height = new_width / original_aspect_ratio;
|
| 557 |
+
} else if (abs_delta_y > abs_delta_x * 1.2) {
|
| 558 |
+
new_width = new_height * original_aspect_ratio;
|
| 559 |
+
} else {
|
| 560 |
+
if (new_width / original_aspect_ratio > new_height) {
|
| 561 |
+
new_height = new_width / original_aspect_ratio;
|
| 562 |
+
} else {
|
| 563 |
+
new_width = new_height * original_aspect_ratio;
|
| 564 |
+
}
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
return { width: new_width, height: new_height };
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
/**
|
| 571 |
+
* Limits dimensions based on constraints
|
| 572 |
+
* @param width - The width to limit
|
| 573 |
+
* @param height - The height to limit
|
| 574 |
+
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
|
| 575 |
+
* @returns The limited dimensions
|
| 576 |
+
*/
|
| 577 |
+
private limit_dimensions(
|
| 578 |
+
width: number,
|
| 579 |
+
height: number,
|
| 580 |
+
maintain_aspect_ratio: boolean
|
| 581 |
+
): { width: number; height: number } {
|
| 582 |
+
const max_width = this.dimensions.width;
|
| 583 |
+
const max_height = this.dimensions.height;
|
| 584 |
+
|
| 585 |
+
let new_width = width;
|
| 586 |
+
let new_height = height;
|
| 587 |
+
|
| 588 |
+
if (new_width > max_width) {
|
| 589 |
+
if (maintain_aspect_ratio && this.active_corner_index !== -1) {
|
| 590 |
+
new_height = (max_width / new_width) * new_height;
|
| 591 |
+
}
|
| 592 |
+
new_width = max_width;
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
if (new_height > max_height) {
|
| 596 |
+
if (maintain_aspect_ratio && this.active_corner_index !== -1) {
|
| 597 |
+
new_width = (max_height / new_height) * new_width;
|
| 598 |
+
}
|
| 599 |
+
new_height = max_height;
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
new_width = Math.max(20, new_width);
|
| 603 |
+
new_height = Math.max(20, new_height);
|
| 604 |
+
|
| 605 |
+
return { width: new_width, height: new_height };
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
/**
|
| 609 |
+
* Calculates position deltas based on dimension changes
|
| 610 |
+
* @param old_width - The original width
|
| 611 |
+
* @param old_height - The original height
|
| 612 |
+
* @param new_width - The new width
|
| 613 |
+
* @param new_height - The new height
|
| 614 |
+
* @returns Object containing x and y position deltas
|
| 615 |
+
*/
|
| 616 |
+
private calculate_position_deltas(
|
| 617 |
+
old_width: number,
|
| 618 |
+
old_height: number,
|
| 619 |
+
new_width: number,
|
| 620 |
+
new_height: number
|
| 621 |
+
): { x: number; y: number } {
|
| 622 |
+
let delta_x = 0;
|
| 623 |
+
let delta_y = 0;
|
| 624 |
+
|
| 625 |
+
if (this.active_corner_index === 0 || this.active_edge_index === 2) {
|
| 626 |
+
delta_x = old_width - new_width;
|
| 627 |
+
} else if (this.active_corner_index === 2) {
|
| 628 |
+
delta_x = old_width - new_width;
|
| 629 |
+
delta_y = 0;
|
| 630 |
+
}
|
| 631 |
+
|
| 632 |
+
if (
|
| 633 |
+
this.active_corner_index === 0 ||
|
| 634 |
+
this.active_corner_index === 1 ||
|
| 635 |
+
this.active_edge_index === 0
|
| 636 |
+
) {
|
| 637 |
+
delta_y = old_height - new_height;
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
return { x: delta_x, y: delta_y };
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
/**
|
| 644 |
+
* Applies boundary constraints to keep the image within the container
|
| 645 |
+
* @private
|
| 646 |
+
*/
|
| 647 |
+
private apply_boundary_constraints(): void {
|
| 648 |
+
if (!this.image_editor_context.background_image) return;
|
| 649 |
+
|
| 650 |
+
const bg = this.image_editor_context.background_image;
|
| 651 |
+
const image_container = this.image_editor_context.image_container;
|
| 652 |
+
|
| 653 |
+
let new_x = bg.position.x;
|
| 654 |
+
let new_y = bg.position.y;
|
| 655 |
+
|
| 656 |
+
const container_width = image_container.width;
|
| 657 |
+
const container_height = image_container.height;
|
| 658 |
+
const bg_width = bg.width;
|
| 659 |
+
const bg_height = bg.height;
|
| 660 |
+
|
| 661 |
+
const effectiveWidth = container_width - this.borderRegion * 2;
|
| 662 |
+
const effectiveHeight = container_height - this.borderRegion * 2;
|
| 663 |
+
|
| 664 |
+
if (bg_width > effectiveWidth) {
|
| 665 |
+
const min_x = -(bg_width - effectiveWidth) + this.borderRegion;
|
| 666 |
+
const max_x = this.borderRegion;
|
| 667 |
+
new_x = Math.max(min_x, Math.min(max_x, new_x));
|
| 668 |
+
} else {
|
| 669 |
+
const min_x = this.borderRegion;
|
| 670 |
+
const max_x = container_width - bg_width - this.borderRegion;
|
| 671 |
+
if (new_x < min_x || new_x > max_x) {
|
| 672 |
+
new_x = Math.max(min_x, Math.min(max_x, new_x));
|
| 673 |
+
}
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
if (bg_height > effectiveHeight) {
|
| 677 |
+
const min_y = -(bg_height - effectiveHeight) + this.borderRegion;
|
| 678 |
+
const max_y = this.borderRegion;
|
| 679 |
+
new_y = Math.max(min_y, Math.min(max_y, new_y));
|
| 680 |
+
} else {
|
| 681 |
+
const min_y = this.borderRegion;
|
| 682 |
+
const max_y = container_height - bg_height - this.borderRegion;
|
| 683 |
+
if (new_y < min_y || new_y > max_y) {
|
| 684 |
+
new_y = Math.max(min_y, Math.min(max_y, new_y));
|
| 685 |
+
}
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
bg.position.set(new_x, new_y);
|
| 689 |
+
|
| 690 |
+
if (this.resize_ui_container) {
|
| 691 |
+
const total_x = image_container.position.x + new_x * this.scale;
|
| 692 |
+
const total_y = image_container.position.y + new_y * this.scale;
|
| 693 |
+
this.resize_ui_container.position.set(total_x, total_y);
|
| 694 |
+
}
|
| 695 |
+
}
|
| 696 |
+
|
| 697 |
+
/**
|
| 698 |
+
* Updates the resize bounds based on the delta
|
| 699 |
+
* @param delta - The delta to apply
|
| 700 |
+
* @param maintain_aspect_ratio - Whether to maintain the aspect ratio
|
| 701 |
+
*/
|
| 702 |
+
private update_resize_bounds(
|
| 703 |
+
delta: Point,
|
| 704 |
+
maintain_aspect_ratio = false
|
| 705 |
+
): void {
|
| 706 |
+
const background_image = this.image_editor_context.background_image;
|
| 707 |
+
|
| 708 |
+
if (!background_image) return;
|
| 709 |
+
|
| 710 |
+
const original_width = background_image.width;
|
| 711 |
+
const original_height = background_image.height;
|
| 712 |
+
const original_x = background_image.position.x;
|
| 713 |
+
const original_y = background_image.position.y;
|
| 714 |
+
const original_aspect_ratio = original_width / original_height;
|
| 715 |
+
|
| 716 |
+
let current_position: Point;
|
| 717 |
+
|
| 718 |
+
if (this.active_corner_index !== -1) {
|
| 719 |
+
switch (this.active_corner_index) {
|
| 720 |
+
case 0:
|
| 721 |
+
current_position = new Point(
|
| 722 |
+
original_x - delta.x,
|
| 723 |
+
original_y - delta.y
|
| 724 |
+
);
|
| 725 |
+
break;
|
| 726 |
+
case 1:
|
| 727 |
+
current_position = new Point(
|
| 728 |
+
original_x + original_width + delta.x,
|
| 729 |
+
original_y - delta.y
|
| 730 |
+
);
|
| 731 |
+
break;
|
| 732 |
+
case 2:
|
| 733 |
+
current_position = new Point(
|
| 734 |
+
original_x - delta.x,
|
| 735 |
+
original_y + original_height + delta.y
|
| 736 |
+
);
|
| 737 |
+
break;
|
| 738 |
+
case 3:
|
| 739 |
+
current_position = new Point(
|
| 740 |
+
original_x + original_width + delta.x,
|
| 741 |
+
original_y + original_height + delta.y
|
| 742 |
+
);
|
| 743 |
+
break;
|
| 744 |
+
default:
|
| 745 |
+
return;
|
| 746 |
+
}
|
| 747 |
+
|
| 748 |
+
const dimensions = this.handle_direct_corner_resize(
|
| 749 |
+
current_position,
|
| 750 |
+
original_width,
|
| 751 |
+
original_height,
|
| 752 |
+
original_x,
|
| 753 |
+
original_y,
|
| 754 |
+
original_aspect_ratio,
|
| 755 |
+
maintain_aspect_ratio
|
| 756 |
+
);
|
| 757 |
+
|
| 758 |
+
background_image.width = dimensions.width;
|
| 759 |
+
background_image.height = dimensions.height;
|
| 760 |
+
background_image.position.set(dimensions.x, dimensions.y);
|
| 761 |
+
} else if (this.active_edge_index !== -1) {
|
| 762 |
+
switch (this.active_edge_index) {
|
| 763 |
+
case 0:
|
| 764 |
+
current_position = new Point(
|
| 765 |
+
original_x + original_width / 2,
|
| 766 |
+
original_y - delta.y
|
| 767 |
+
);
|
| 768 |
+
break;
|
| 769 |
+
case 1:
|
| 770 |
+
current_position = new Point(
|
| 771 |
+
original_x + original_width / 2,
|
| 772 |
+
original_y + original_height + delta.y
|
| 773 |
+
);
|
| 774 |
+
break;
|
| 775 |
+
case 2:
|
| 776 |
+
current_position = new Point(
|
| 777 |
+
original_x - delta.x,
|
| 778 |
+
original_y + original_height / 2
|
| 779 |
+
);
|
| 780 |
+
break;
|
| 781 |
+
case 3:
|
| 782 |
+
current_position = new Point(
|
| 783 |
+
original_x + original_width + delta.x,
|
| 784 |
+
original_y + original_height / 2
|
| 785 |
+
);
|
| 786 |
+
break;
|
| 787 |
+
default:
|
| 788 |
+
return;
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
const dimensions = this.handle_direct_edge_resize(
|
| 792 |
+
current_position,
|
| 793 |
+
original_width,
|
| 794 |
+
original_height,
|
| 795 |
+
original_x,
|
| 796 |
+
original_y
|
| 797 |
+
);
|
| 798 |
+
|
| 799 |
+
background_image.width = dimensions.width;
|
| 800 |
+
background_image.height = dimensions.height;
|
| 801 |
+
background_image.position.set(dimensions.x, dimensions.y);
|
| 802 |
+
}
|
| 803 |
+
|
| 804 |
+
this.update_resize_ui();
|
| 805 |
+
}
|
| 806 |
+
|
| 807 |
+
/**
|
| 808 |
+
* updates the resize ui position and dimensions
|
| 809 |
+
*/
|
| 810 |
+
private update_resize_ui(): void {
|
| 811 |
+
if (
|
| 812 |
+
!this.resize_ui_container ||
|
| 813 |
+
!this.image_editor_context.background_image ||
|
| 814 |
+
!this.image_editor_context.background_image.position
|
| 815 |
+
)
|
| 816 |
+
return;
|
| 817 |
+
|
| 818 |
+
const background_image = this.image_editor_context.background_image;
|
| 819 |
+
const image_container = this.image_editor_context.image_container;
|
| 820 |
+
|
| 821 |
+
const container_global_pos = image_container.getGlobalPosition();
|
| 822 |
+
|
| 823 |
+
const bg_pos_x = background_image.position.x * this.scale;
|
| 824 |
+
const bg_pos_y = background_image.position.y * this.scale;
|
| 825 |
+
|
| 826 |
+
this.resize_ui_container.position.set(
|
| 827 |
+
container_global_pos.x + bg_pos_x,
|
| 828 |
+
container_global_pos.y + bg_pos_y
|
| 829 |
+
);
|
| 830 |
+
|
| 831 |
+
const scaled_width = background_image.width * this.scale;
|
| 832 |
+
const scaled_height = background_image.height * this.scale;
|
| 833 |
+
|
| 834 |
+
const outline = this.resize_ui_container.getChildAt(0) as Graphics;
|
| 835 |
+
outline
|
| 836 |
+
.clear()
|
| 837 |
+
.rect(0, 0, scaled_width, scaled_height)
|
| 838 |
+
.stroke({ width: 1, color: 0x000000, alignment: 0, alpha: 0.3 });
|
| 839 |
+
|
| 840 |
+
const dotted_outline = this.resize_ui_container.getChildAt(1) as Graphics;
|
| 841 |
+
dotted_outline.clear();
|
| 842 |
+
|
| 843 |
+
dotted_outline.rect(0, 0, scaled_width, scaled_height).stroke({
|
| 844 |
+
width: 1,
|
| 845 |
+
color: 0x000000,
|
| 846 |
+
alpha: 0.7,
|
| 847 |
+
pixelLine: true
|
| 848 |
+
});
|
| 849 |
+
|
| 850 |
+
if (
|
| 851 |
+
!this.is_dragging &&
|
| 852 |
+
!this.is_moving &&
|
| 853 |
+
Math.abs(this.scale - this.last_scale) < 0.001
|
| 854 |
+
) {
|
| 855 |
+
const dash_length = 5;
|
| 856 |
+
const gap_length = 5;
|
| 857 |
+
const total_length = dash_length + gap_length;
|
| 858 |
+
|
| 859 |
+
for (let x = 0; x < scaled_width; x += total_length * 2) {
|
| 860 |
+
dotted_outline
|
| 861 |
+
.rect(x, 0, Math.min(dash_length, scaled_width - x), 1)
|
| 862 |
+
.fill(0x000000);
|
| 863 |
+
|
| 864 |
+
dotted_outline
|
| 865 |
+
.rect(
|
| 866 |
+
x,
|
| 867 |
+
scaled_height - 1,
|
| 868 |
+
Math.min(dash_length, scaled_width - x),
|
| 869 |
+
1
|
| 870 |
+
)
|
| 871 |
+
.fill(0x000000);
|
| 872 |
+
}
|
| 873 |
+
|
| 874 |
+
for (let y = 0; y < scaled_height; y += total_length * 2) {
|
| 875 |
+
dotted_outline
|
| 876 |
+
.rect(0, y, 1, Math.min(dash_length, scaled_height - y))
|
| 877 |
+
.fill(0x000000);
|
| 878 |
+
|
| 879 |
+
dotted_outline
|
| 880 |
+
.rect(
|
| 881 |
+
scaled_width - 1,
|
| 882 |
+
y,
|
| 883 |
+
1,
|
| 884 |
+
Math.min(dash_length, scaled_height - y)
|
| 885 |
+
)
|
| 886 |
+
.fill(0x000000);
|
| 887 |
+
}
|
| 888 |
+
}
|
| 889 |
+
|
| 890 |
+
this.last_scale = this.scale;
|
| 891 |
+
|
| 892 |
+
const move_area = this.resize_ui_container.getChildAt(2) as Graphics;
|
| 893 |
+
move_area.clear().rect(0, 0, scaled_width, scaled_height).fill(0xffffff, 0);
|
| 894 |
+
|
| 895 |
+
this.update_handle_positions(scaled_width, scaled_height);
|
| 896 |
+
}
|
| 897 |
+
|
| 898 |
+
/**
|
| 899 |
+
* updates the positions of all handles
|
| 900 |
+
* @param width - width of the resize area
|
| 901 |
+
* @param height - height of the resize area
|
| 902 |
+
*/
|
| 903 |
+
private update_handle_positions(width: number, height: number): void {
|
| 904 |
+
if (!this.resize_ui_container) return;
|
| 905 |
+
|
| 906 |
+
const handles = this.resize_ui_container.children.slice(3);
|
| 907 |
+
|
| 908 |
+
const move_area = this.resize_ui_container.getChildAt(2) as Graphics;
|
| 909 |
+
move_area.clear().rect(0, 0, width, height).fill(0xffffff, 0);
|
| 910 |
+
|
| 911 |
+
const corners = handles.slice(0, 4);
|
| 912 |
+
const cornerPositions = [
|
| 913 |
+
{ x: 0, y: 0 },
|
| 914 |
+
{ x: width, y: 0 },
|
| 915 |
+
{ x: 0, y: height },
|
| 916 |
+
{ x: width, y: height }
|
| 917 |
+
];
|
| 918 |
+
|
| 919 |
+
corners.forEach((handle, i) => {
|
| 920 |
+
handle.position.set(cornerPositions[i].x, cornerPositions[i].y);
|
| 921 |
+
});
|
| 922 |
+
|
| 923 |
+
const edges = handles.slice(4);
|
| 924 |
+
edges.forEach((handle, i) => {
|
| 925 |
+
if (i < 2) {
|
| 926 |
+
handle.position.set(width / 2, i === 0 ? 0 : height);
|
| 927 |
+
} else {
|
| 928 |
+
handle.position.set(i === 2 ? 0 : width, height / 2);
|
| 929 |
+
}
|
| 930 |
+
});
|
| 931 |
+
}
|
| 932 |
+
|
| 933 |
+
/**
|
| 934 |
+
* sets up event listeners for the resize tool
|
| 935 |
+
*/
|
| 936 |
+
private setup_event_listeners(): void {
|
| 937 |
+
const stage = this.image_editor_context.app.stage;
|
| 938 |
+
stage.eventMode = "static";
|
| 939 |
+
stage.on("pointermove", this.handle_pointer_move.bind(this));
|
| 940 |
+
stage.on("pointerup", this.handle_pointer_up.bind(this));
|
| 941 |
+
stage.on("pointerupoutside", this.handle_pointer_up.bind(this));
|
| 942 |
+
|
| 943 |
+
this.setup_dom_event_listeners();
|
| 944 |
+
}
|
| 945 |
+
|
| 946 |
+
/**
|
| 947 |
+
* Sets up direct DOM event listeners for dragging the background image
|
| 948 |
+
*/
|
| 949 |
+
private setup_dom_event_listeners(): void {
|
| 950 |
+
this.cleanup_dom_event_listeners();
|
| 951 |
+
|
| 952 |
+
const canvas = this.image_editor_context.app.canvas;
|
| 953 |
+
|
| 954 |
+
this.dom_mousedown_handler = (e: MouseEvent) => {
|
| 955 |
+
if (this.current_subtool !== "size") return;
|
| 956 |
+
|
| 957 |
+
if (this.is_dragging || this.selected_handle) return;
|
| 958 |
+
|
| 959 |
+
if (!this.image_editor_context.background_image) return;
|
| 960 |
+
|
| 961 |
+
const bg = this.image_editor_context.background_image;
|
| 962 |
+
|
| 963 |
+
const container = this.image_editor_context.image_container;
|
| 964 |
+
const containerBounds = container.getBounds();
|
| 965 |
+
|
| 966 |
+
const rect = canvas.getBoundingClientRect();
|
| 967 |
+
const x = e.clientX - rect.left;
|
| 968 |
+
const y = e.clientY - rect.top;
|
| 969 |
+
|
| 970 |
+
const localX = (x - containerBounds.x) / this.scale;
|
| 971 |
+
const localY = (y - containerBounds.y) / this.scale;
|
| 972 |
+
|
| 973 |
+
if (
|
| 974 |
+
localX >= bg.position.x &&
|
| 975 |
+
localX <= bg.position.x + bg.width &&
|
| 976 |
+
localY >= bg.position.y &&
|
| 977 |
+
localY <= bg.position.y + bg.height
|
| 978 |
+
) {
|
| 979 |
+
this.is_moving = true;
|
| 980 |
+
|
| 981 |
+
this.last_pointer_position = new Point(localX, localY);
|
| 982 |
+
|
| 983 |
+
this.original_state = {
|
| 984 |
+
width: bg.width,
|
| 985 |
+
height: bg.height,
|
| 986 |
+
x: bg.position.x,
|
| 987 |
+
y: bg.position.y
|
| 988 |
+
};
|
| 989 |
+
|
| 990 |
+
e.preventDefault();
|
| 991 |
+
e.stopPropagation();
|
| 992 |
+
}
|
| 993 |
+
};
|
| 994 |
+
|
| 995 |
+
this.dom_mousemove_handler = (e: MouseEvent) => {
|
| 996 |
+
if (this.current_subtool !== "size" || !this.is_moving) return;
|
| 997 |
+
|
| 998 |
+
if (
|
| 999 |
+
!this.last_pointer_position ||
|
| 1000 |
+
!this.image_editor_context.background_image
|
| 1001 |
+
)
|
| 1002 |
+
return;
|
| 1003 |
+
|
| 1004 |
+
const container = this.image_editor_context.image_container;
|
| 1005 |
+
const containerBounds = container.getBounds();
|
| 1006 |
+
|
| 1007 |
+
const rect = canvas.getBoundingClientRect();
|
| 1008 |
+
const x = e.clientX - rect.left;
|
| 1009 |
+
const y = e.clientY - rect.top;
|
| 1010 |
+
|
| 1011 |
+
const localX = (x - containerBounds.x) / this.scale;
|
| 1012 |
+
const localY = (y - containerBounds.y) / this.scale;
|
| 1013 |
+
|
| 1014 |
+
const current_position = new Point(localX, localY);
|
| 1015 |
+
|
| 1016 |
+
this.handle_image_dragging(current_position);
|
| 1017 |
+
|
| 1018 |
+
this.last_pointer_position = current_position;
|
| 1019 |
+
|
| 1020 |
+
e.preventDefault();
|
| 1021 |
+
e.stopPropagation();
|
| 1022 |
+
};
|
| 1023 |
+
|
| 1024 |
+
this.dom_mouseup_handler = (e: MouseEvent) => {
|
| 1025 |
+
if (this.current_subtool !== "size") return;
|
| 1026 |
+
|
| 1027 |
+
if (this.original_state && this.image_editor_context.background_image) {
|
| 1028 |
+
const bg = this.image_editor_context.background_image;
|
| 1029 |
+
const new_state = {
|
| 1030 |
+
width: bg.width,
|
| 1031 |
+
height: bg.height,
|
| 1032 |
+
x: bg.position.x,
|
| 1033 |
+
y: bg.position.y
|
| 1034 |
+
};
|
| 1035 |
+
|
| 1036 |
+
if (
|
| 1037 |
+
this.original_state.width !== new_state.width ||
|
| 1038 |
+
this.original_state.height !== new_state.height ||
|
| 1039 |
+
this.original_state.x !== new_state.x ||
|
| 1040 |
+
this.original_state.y !== new_state.y
|
| 1041 |
+
) {
|
| 1042 |
+
const resize_command = new ResizeCommand(
|
| 1043 |
+
this.image_editor_context,
|
| 1044 |
+
this.original_state,
|
| 1045 |
+
new_state,
|
| 1046 |
+
this.update_resize_ui.bind(this)
|
| 1047 |
+
);
|
| 1048 |
+
|
| 1049 |
+
this.image_editor_context.execute_command(resize_command);
|
| 1050 |
+
}
|
| 1051 |
+
|
| 1052 |
+
this.original_state = null;
|
| 1053 |
+
}
|
| 1054 |
+
|
| 1055 |
+
this.is_moving = false;
|
| 1056 |
+
this.last_pointer_position = null;
|
| 1057 |
+
|
| 1058 |
+
this.update_resize_ui();
|
| 1059 |
+
|
| 1060 |
+
e.preventDefault();
|
| 1061 |
+
e.stopPropagation();
|
| 1062 |
+
};
|
| 1063 |
+
|
| 1064 |
+
canvas.addEventListener("mousedown", this.dom_mousedown_handler);
|
| 1065 |
+
window.addEventListener("mousemove", this.dom_mousemove_handler);
|
| 1066 |
+
window.addEventListener("mouseup", this.dom_mouseup_handler);
|
| 1067 |
+
}
|
| 1068 |
+
|
| 1069 |
+
/**
|
| 1070 |
+
* Cleans up DOM event listeners
|
| 1071 |
+
*/
|
| 1072 |
+
private cleanup_dom_event_listeners(): void {
|
| 1073 |
+
const canvas = this.image_editor_context.app.canvas;
|
| 1074 |
+
|
| 1075 |
+
if (this.dom_mousedown_handler) {
|
| 1076 |
+
canvas.removeEventListener("mousedown", this.dom_mousedown_handler);
|
| 1077 |
+
this.dom_mousedown_handler = null;
|
| 1078 |
+
}
|
| 1079 |
+
|
| 1080 |
+
if (this.dom_mousemove_handler) {
|
| 1081 |
+
window.removeEventListener("mousemove", this.dom_mousemove_handler);
|
| 1082 |
+
this.dom_mousemove_handler = null;
|
| 1083 |
+
}
|
| 1084 |
+
|
| 1085 |
+
if (this.dom_mouseup_handler) {
|
| 1086 |
+
window.removeEventListener("mouseup", this.dom_mouseup_handler);
|
| 1087 |
+
this.dom_mouseup_handler = null;
|
| 1088 |
+
}
|
| 1089 |
+
}
|
| 1090 |
+
|
| 1091 |
+
/**
|
| 1092 |
+
* removes event listeners for the resize tool
|
| 1093 |
+
*/
|
| 1094 |
+
private cleanup_event_listeners(): void {
|
| 1095 |
+
const stage = this.image_editor_context.app.stage;
|
| 1096 |
+
|
| 1097 |
+
stage.off("pointermove", this.handle_pointer_move.bind(this));
|
| 1098 |
+
stage.off("pointerup", this.handle_pointer_up.bind(this));
|
| 1099 |
+
stage.off("pointerupoutside", this.handle_pointer_up.bind(this));
|
| 1100 |
+
|
| 1101 |
+
this.cleanup_dom_event_listeners();
|
| 1102 |
+
}
|
| 1103 |
+
|
| 1104 |
+
/**
|
| 1105 |
+
* Handles pointer move events
|
| 1106 |
+
*/
|
| 1107 |
+
private handle_pointer_move(event: FederatedPointerEvent): void {
|
| 1108 |
+
if (this.current_subtool !== "size") return;
|
| 1109 |
+
|
| 1110 |
+
const local_pos = this.image_editor_context.image_container.toLocal(
|
| 1111 |
+
event.global
|
| 1112 |
+
);
|
| 1113 |
+
const current_position = new Point(local_pos.x, local_pos.y);
|
| 1114 |
+
|
| 1115 |
+
if (this.is_moving && this.last_pointer_position) {
|
| 1116 |
+
this.handle_image_dragging(current_position);
|
| 1117 |
+
|
| 1118 |
+
this.last_pointer_position = current_position;
|
| 1119 |
+
} else if (this.is_dragging && this.selected_handle) {
|
| 1120 |
+
if (!this.image_editor_context.background_image) return;
|
| 1121 |
+
|
| 1122 |
+
const bg = this.image_editor_context.background_image;
|
| 1123 |
+
const maintain_aspect_ratio =
|
| 1124 |
+
this.active_corner_index !== -1 && !event.shiftKey;
|
| 1125 |
+
|
| 1126 |
+
const original_width = bg.width;
|
| 1127 |
+
const original_height = bg.height;
|
| 1128 |
+
const original_x = bg.position.x;
|
| 1129 |
+
const original_y = bg.position.y;
|
| 1130 |
+
const original_aspect_ratio = original_width / original_height;
|
| 1131 |
+
|
| 1132 |
+
let dimensions: { width: number; height: number; x: number; y: number };
|
| 1133 |
+
|
| 1134 |
+
if (this.active_corner_index !== -1) {
|
| 1135 |
+
dimensions = this.handle_direct_corner_resize(
|
| 1136 |
+
current_position,
|
| 1137 |
+
original_width,
|
| 1138 |
+
original_height,
|
| 1139 |
+
original_x,
|
| 1140 |
+
original_y,
|
| 1141 |
+
original_aspect_ratio,
|
| 1142 |
+
maintain_aspect_ratio
|
| 1143 |
+
);
|
| 1144 |
+
} else if (this.active_edge_index !== -1) {
|
| 1145 |
+
dimensions = this.handle_direct_edge_resize(
|
| 1146 |
+
current_position,
|
| 1147 |
+
original_width,
|
| 1148 |
+
original_height,
|
| 1149 |
+
original_x,
|
| 1150 |
+
original_y
|
| 1151 |
+
);
|
| 1152 |
+
} else {
|
| 1153 |
+
return;
|
| 1154 |
+
}
|
| 1155 |
+
|
| 1156 |
+
dimensions = this.limit_dimensions_to_container(
|
| 1157 |
+
dimensions,
|
| 1158 |
+
original_x,
|
| 1159 |
+
original_y,
|
| 1160 |
+
original_width,
|
| 1161 |
+
original_height,
|
| 1162 |
+
maintain_aspect_ratio
|
| 1163 |
+
);
|
| 1164 |
+
|
| 1165 |
+
bg.width = dimensions.width;
|
| 1166 |
+
bg.height = dimensions.height;
|
| 1167 |
+
bg.position.set(dimensions.x, dimensions.y);
|
| 1168 |
+
|
| 1169 |
+
this.update_resize_ui();
|
| 1170 |
+
|
| 1171 |
+
this.last_pointer_position = current_position;
|
| 1172 |
+
}
|
| 1173 |
+
}
|
| 1174 |
+
/**
|
| 1175 |
+
* handles pointer up events
|
| 1176 |
+
*/
|
| 1177 |
+
private handle_pointer_up(): void {
|
| 1178 |
+
if (this.current_subtool !== "size") return;
|
| 1179 |
+
|
| 1180 |
+
this.is_dragging = false;
|
| 1181 |
+
this.is_moving = false;
|
| 1182 |
+
this.selected_handle = null;
|
| 1183 |
+
this.last_pointer_position = null;
|
| 1184 |
+
this.active_corner_index = -1;
|
| 1185 |
+
this.active_edge_index = -1;
|
| 1186 |
+
|
| 1187 |
+
this.update_resize_ui();
|
| 1188 |
+
this.notify("change");
|
| 1189 |
+
}
|
| 1190 |
+
|
| 1191 |
+
/**
|
| 1192 |
+
* Handles dragging of the background image
|
| 1193 |
+
* @param current_position - The current pointer position
|
| 1194 |
+
* @private
|
| 1195 |
+
*/
|
| 1196 |
+
private handle_image_dragging(current_position: Point): void {
|
| 1197 |
+
if (
|
| 1198 |
+
!this.last_pointer_position ||
|
| 1199 |
+
!this.image_editor_context.background_image
|
| 1200 |
+
) {
|
| 1201 |
+
return;
|
| 1202 |
+
}
|
| 1203 |
+
|
| 1204 |
+
const bg = this.image_editor_context.background_image;
|
| 1205 |
+
const image_container =
|
| 1206 |
+
this.image_editor_context.image_container.getLocalBounds();
|
| 1207 |
+
|
| 1208 |
+
const dx = current_position.x - this.last_pointer_position.x;
|
| 1209 |
+
const dy = current_position.y - this.last_pointer_position.y;
|
| 1210 |
+
|
| 1211 |
+
let new_x = bg.position.x + dx;
|
| 1212 |
+
let new_y = bg.position.y + dy;
|
| 1213 |
+
|
| 1214 |
+
const container_width = image_container.width;
|
| 1215 |
+
const container_height = image_container.height;
|
| 1216 |
+
const bg_width = bg.width;
|
| 1217 |
+
const bg_height = bg.height;
|
| 1218 |
+
|
| 1219 |
+
const effectiveWidth = container_width - this.borderRegion * 2;
|
| 1220 |
+
const effectiveHeight = container_height - this.borderRegion * 2;
|
| 1221 |
+
|
| 1222 |
+
if (bg_width > effectiveWidth) {
|
| 1223 |
+
const min_x = -(bg_width - effectiveWidth) + this.borderRegion;
|
| 1224 |
+
const max_x = this.borderRegion;
|
| 1225 |
+
new_x = Math.max(min_x, Math.min(max_x, new_x));
|
| 1226 |
+
} else {
|
| 1227 |
+
const min_x = this.borderRegion;
|
| 1228 |
+
const max_x = container_width - bg_width - this.borderRegion;
|
| 1229 |
+
new_x = Math.max(min_x, Math.min(max_x, new_x));
|
| 1230 |
+
}
|
| 1231 |
+
|
| 1232 |
+
if (bg_height > effectiveHeight) {
|
| 1233 |
+
const min_y = -(bg_height - effectiveHeight) + this.borderRegion;
|
| 1234 |
+
const max_y = this.borderRegion;
|
| 1235 |
+
new_y = Math.max(min_y, Math.min(max_y, new_y));
|
| 1236 |
+
} else {
|
| 1237 |
+
const min_y = this.borderRegion;
|
| 1238 |
+
const max_y = container_height - bg_height - this.borderRegion;
|
| 1239 |
+
new_y = Math.max(min_y, Math.min(max_y, new_y));
|
| 1240 |
+
}
|
| 1241 |
+
|
| 1242 |
+
bg.position.set(new_x, new_y);
|
| 1243 |
+
|
| 1244 |
+
this.update_resize_ui();
|
| 1245 |
+
}
|
| 1246 |
+
|
| 1247 |
+
/**
|
| 1248 |
+
* Handles corner resizing
|
| 1249 |
+
* @param current_position - The current mouse position
|
| 1250 |
+
* @param original_width - The original width
|
| 1251 |
+
* @param original_height - The original height
|
| 1252 |
+
* @param original_x - The original x position
|
| 1253 |
+
* @param original_y - The original y position
|
| 1254 |
+
* @param original_aspect_ratio - The original aspect ratio
|
| 1255 |
+
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
|
| 1256 |
+
* @returns The new dimensions and position
|
| 1257 |
+
* @private
|
| 1258 |
+
*/
|
| 1259 |
+
private handle_direct_corner_resize(
|
| 1260 |
+
current_position: Point,
|
| 1261 |
+
original_width: number,
|
| 1262 |
+
original_height: number,
|
| 1263 |
+
original_x: number,
|
| 1264 |
+
original_y: number,
|
| 1265 |
+
original_aspect_ratio: number,
|
| 1266 |
+
maintain_aspect_ratio: boolean
|
| 1267 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1268 |
+
let raw_width = 0;
|
| 1269 |
+
let raw_height = 0;
|
| 1270 |
+
let new_x = original_x;
|
| 1271 |
+
let new_y = original_y;
|
| 1272 |
+
|
| 1273 |
+
switch (this.active_corner_index) {
|
| 1274 |
+
case 0:
|
| 1275 |
+
raw_width = original_x + original_width - current_position.x;
|
| 1276 |
+
raw_height = original_y + original_height - current_position.y;
|
| 1277 |
+
break;
|
| 1278 |
+
case 1:
|
| 1279 |
+
raw_width = current_position.x - original_x;
|
| 1280 |
+
raw_height = original_y + original_height - current_position.y;
|
| 1281 |
+
break;
|
| 1282 |
+
case 2:
|
| 1283 |
+
raw_width = original_x + original_width - current_position.x;
|
| 1284 |
+
raw_height = current_position.y - original_y;
|
| 1285 |
+
break;
|
| 1286 |
+
case 3:
|
| 1287 |
+
raw_width = current_position.x - original_x;
|
| 1288 |
+
raw_height = current_position.y - original_y;
|
| 1289 |
+
break;
|
| 1290 |
+
}
|
| 1291 |
+
|
| 1292 |
+
raw_width = Math.max(20, raw_width);
|
| 1293 |
+
raw_height = Math.max(20, raw_height);
|
| 1294 |
+
|
| 1295 |
+
let new_width = raw_width;
|
| 1296 |
+
let new_height = raw_height;
|
| 1297 |
+
|
| 1298 |
+
if (maintain_aspect_ratio) {
|
| 1299 |
+
const diagonal_vector = {
|
| 1300 |
+
x: current_position.x - (original_x + original_width / 2),
|
| 1301 |
+
y: current_position.y - (original_y + original_height / 2)
|
| 1302 |
+
};
|
| 1303 |
+
|
| 1304 |
+
const angle = Math.abs(Math.atan2(diagonal_vector.y, diagonal_vector.x));
|
| 1305 |
+
|
| 1306 |
+
if (angle < Math.PI / 4 || angle > (3 * Math.PI) / 4) {
|
| 1307 |
+
new_height = new_width / original_aspect_ratio;
|
| 1308 |
+
} else {
|
| 1309 |
+
new_width = new_height * original_aspect_ratio;
|
| 1310 |
+
}
|
| 1311 |
+
}
|
| 1312 |
+
|
| 1313 |
+
switch (this.active_corner_index) {
|
| 1314 |
+
case 0:
|
| 1315 |
+
new_x = original_x + original_width - new_width;
|
| 1316 |
+
new_y = original_y + original_height - new_height;
|
| 1317 |
+
break;
|
| 1318 |
+
case 1:
|
| 1319 |
+
new_y = original_y + original_height - new_height;
|
| 1320 |
+
break;
|
| 1321 |
+
case 2:
|
| 1322 |
+
new_x = original_x + original_width - new_width;
|
| 1323 |
+
break;
|
| 1324 |
+
case 3:
|
| 1325 |
+
break;
|
| 1326 |
+
}
|
| 1327 |
+
|
| 1328 |
+
return { width: new_width, height: new_height, x: new_x, y: new_y };
|
| 1329 |
+
}
|
| 1330 |
+
|
| 1331 |
+
/**
|
| 1332 |
+
* Handles edge resizing
|
| 1333 |
+
* @param current_position - The current mouse position
|
| 1334 |
+
* @param original_width - The original width
|
| 1335 |
+
* @param original_height - The original height
|
| 1336 |
+
* @param original_x - The original x position
|
| 1337 |
+
* @param original_y - The original y position
|
| 1338 |
+
* @returns The new dimensions and position
|
| 1339 |
+
* @private
|
| 1340 |
+
*/
|
| 1341 |
+
private handle_direct_edge_resize(
|
| 1342 |
+
current_position: Point,
|
| 1343 |
+
original_width: number,
|
| 1344 |
+
original_height: number,
|
| 1345 |
+
original_x: number,
|
| 1346 |
+
original_y: number
|
| 1347 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1348 |
+
let new_width = original_width;
|
| 1349 |
+
let new_height = original_height;
|
| 1350 |
+
let new_x = original_x;
|
| 1351 |
+
let new_y = original_y;
|
| 1352 |
+
|
| 1353 |
+
switch (this.active_edge_index) {
|
| 1354 |
+
case 0:
|
| 1355 |
+
new_height = Math.max(
|
| 1356 |
+
20,
|
| 1357 |
+
original_y + original_height - current_position.y
|
| 1358 |
+
);
|
| 1359 |
+
new_y = current_position.y;
|
| 1360 |
+
break;
|
| 1361 |
+
case 1:
|
| 1362 |
+
new_height = Math.max(20, current_position.y - original_y);
|
| 1363 |
+
break;
|
| 1364 |
+
case 2:
|
| 1365 |
+
new_width = Math.max(
|
| 1366 |
+
20,
|
| 1367 |
+
original_x + original_width - current_position.x
|
| 1368 |
+
);
|
| 1369 |
+
new_x = current_position.x;
|
| 1370 |
+
break;
|
| 1371 |
+
case 3:
|
| 1372 |
+
new_width = Math.max(20, current_position.x - original_x);
|
| 1373 |
+
break;
|
| 1374 |
+
}
|
| 1375 |
+
|
| 1376 |
+
return { width: new_width, height: new_height, x: new_x, y: new_y };
|
| 1377 |
+
}
|
| 1378 |
+
|
| 1379 |
+
/**
|
| 1380 |
+
* Limits dimensions to container size
|
| 1381 |
+
* @param dimensions - The dimensions to limit
|
| 1382 |
+
* @param original_x - The original x position
|
| 1383 |
+
* @param original_y - The original y position
|
| 1384 |
+
* @param original_width - The original width
|
| 1385 |
+
* @param original_height - The original height
|
| 1386 |
+
* @param maintain_aspect_ratio - Whether to maintain aspect ratio
|
| 1387 |
+
* @returns The limited dimensions and position
|
| 1388 |
+
* @private
|
| 1389 |
+
*/
|
| 1390 |
+
private limit_dimensions_to_container(
|
| 1391 |
+
dimensions: { width: number; height: number; x: number; y: number },
|
| 1392 |
+
original_x: number,
|
| 1393 |
+
original_y: number,
|
| 1394 |
+
original_width: number,
|
| 1395 |
+
original_height: number,
|
| 1396 |
+
maintain_aspect_ratio: boolean
|
| 1397 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1398 |
+
let {
|
| 1399 |
+
width: new_width,
|
| 1400 |
+
height: new_height,
|
| 1401 |
+
x: new_x,
|
| 1402 |
+
y: new_y
|
| 1403 |
+
} = dimensions;
|
| 1404 |
+
|
| 1405 |
+
const effective_container_width =
|
| 1406 |
+
this.dimensions.width - this.borderRegion * 2;
|
| 1407 |
+
const effective_container_height =
|
| 1408 |
+
this.dimensions.height - this.borderRegion * 2;
|
| 1409 |
+
|
| 1410 |
+
const pre_limit_width = new_width;
|
| 1411 |
+
const pre_limit_height = new_height;
|
| 1412 |
+
|
| 1413 |
+
const size_limited = this.apply_size_limits(
|
| 1414 |
+
new_width,
|
| 1415 |
+
new_height,
|
| 1416 |
+
effective_container_width,
|
| 1417 |
+
effective_container_height,
|
| 1418 |
+
maintain_aspect_ratio,
|
| 1419 |
+
pre_limit_width / pre_limit_height
|
| 1420 |
+
);
|
| 1421 |
+
|
| 1422 |
+
new_width = size_limited.width;
|
| 1423 |
+
new_height = size_limited.height;
|
| 1424 |
+
|
| 1425 |
+
const position_adjusted = this.adjust_position_for_resizing(
|
| 1426 |
+
new_width,
|
| 1427 |
+
new_height,
|
| 1428 |
+
original_x,
|
| 1429 |
+
original_y,
|
| 1430 |
+
original_width,
|
| 1431 |
+
original_height
|
| 1432 |
+
);
|
| 1433 |
+
|
| 1434 |
+
new_x = position_adjusted.x;
|
| 1435 |
+
new_y = position_adjusted.y;
|
| 1436 |
+
|
| 1437 |
+
const border_constrained = this.apply_border_constraints(
|
| 1438 |
+
new_width,
|
| 1439 |
+
new_height,
|
| 1440 |
+
new_x,
|
| 1441 |
+
new_y,
|
| 1442 |
+
maintain_aspect_ratio,
|
| 1443 |
+
pre_limit_width / pre_limit_height
|
| 1444 |
+
);
|
| 1445 |
+
|
| 1446 |
+
return border_constrained;
|
| 1447 |
+
}
|
| 1448 |
+
|
| 1449 |
+
/**
|
| 1450 |
+
* Apply size limits to ensure dimensions don't exceed container
|
| 1451 |
+
* @private
|
| 1452 |
+
*/
|
| 1453 |
+
private apply_size_limits(
|
| 1454 |
+
width: number,
|
| 1455 |
+
height: number,
|
| 1456 |
+
max_width: number,
|
| 1457 |
+
max_height: number,
|
| 1458 |
+
maintain_aspect_ratio: boolean,
|
| 1459 |
+
aspect_ratio: number
|
| 1460 |
+
): { width: number; height: number } {
|
| 1461 |
+
let new_width = width;
|
| 1462 |
+
let new_height = height;
|
| 1463 |
+
|
| 1464 |
+
const needs_width_limit = new_width > max_width;
|
| 1465 |
+
const needs_height_limit = new_height > max_height;
|
| 1466 |
+
|
| 1467 |
+
if (needs_width_limit && needs_height_limit) {
|
| 1468 |
+
const width_scale = max_width / new_width;
|
| 1469 |
+
const height_scale = max_height / new_height;
|
| 1470 |
+
|
| 1471 |
+
if (width_scale < height_scale) {
|
| 1472 |
+
new_width = max_width;
|
| 1473 |
+
if (maintain_aspect_ratio) {
|
| 1474 |
+
new_height = new_width / aspect_ratio;
|
| 1475 |
+
}
|
| 1476 |
+
} else {
|
| 1477 |
+
new_height = max_height;
|
| 1478 |
+
if (maintain_aspect_ratio) {
|
| 1479 |
+
new_width = new_height * aspect_ratio;
|
| 1480 |
+
}
|
| 1481 |
+
}
|
| 1482 |
+
} else if (needs_width_limit) {
|
| 1483 |
+
new_width = max_width;
|
| 1484 |
+
if (maintain_aspect_ratio) {
|
| 1485 |
+
new_height = new_width / aspect_ratio;
|
| 1486 |
+
}
|
| 1487 |
+
} else if (needs_height_limit) {
|
| 1488 |
+
new_height = max_height;
|
| 1489 |
+
if (maintain_aspect_ratio) {
|
| 1490 |
+
new_width = new_height * aspect_ratio;
|
| 1491 |
+
}
|
| 1492 |
+
}
|
| 1493 |
+
|
| 1494 |
+
return { width: new_width, height: new_height };
|
| 1495 |
+
}
|
| 1496 |
+
|
| 1497 |
+
/**
|
| 1498 |
+
* Adjust position based on which handles are being dragged
|
| 1499 |
+
* @private
|
| 1500 |
+
*/
|
| 1501 |
+
private adjust_position_for_resizing(
|
| 1502 |
+
new_width: number,
|
| 1503 |
+
new_height: number,
|
| 1504 |
+
original_x: number,
|
| 1505 |
+
original_y: number,
|
| 1506 |
+
original_width: number,
|
| 1507 |
+
original_height: number
|
| 1508 |
+
): { x: number; y: number } {
|
| 1509 |
+
let new_x = original_x;
|
| 1510 |
+
let new_y = original_y;
|
| 1511 |
+
|
| 1512 |
+
switch (this.active_corner_index) {
|
| 1513 |
+
case 0:
|
| 1514 |
+
new_x = original_x + original_width - new_width;
|
| 1515 |
+
new_y = original_y + original_height - new_height;
|
| 1516 |
+
break;
|
| 1517 |
+
case 1:
|
| 1518 |
+
new_y = original_y + original_height - new_height;
|
| 1519 |
+
break;
|
| 1520 |
+
case 2:
|
| 1521 |
+
new_x = original_x + original_width - new_width;
|
| 1522 |
+
break;
|
| 1523 |
+
case 3:
|
| 1524 |
+
break;
|
| 1525 |
+
}
|
| 1526 |
+
|
| 1527 |
+
if (this.active_edge_index !== -1) {
|
| 1528 |
+
switch (this.active_edge_index) {
|
| 1529 |
+
case 0:
|
| 1530 |
+
new_y = original_y + original_height - new_height;
|
| 1531 |
+
break;
|
| 1532 |
+
case 2:
|
| 1533 |
+
new_x = original_x + original_width - new_width;
|
| 1534 |
+
break;
|
| 1535 |
+
}
|
| 1536 |
+
}
|
| 1537 |
+
|
| 1538 |
+
return { x: new_x, y: new_y };
|
| 1539 |
+
}
|
| 1540 |
+
|
| 1541 |
+
/**
|
| 1542 |
+
* Apply border region constraints to prevent image from intruding on border
|
| 1543 |
+
* @private
|
| 1544 |
+
*/
|
| 1545 |
+
private apply_border_constraints(
|
| 1546 |
+
width: number,
|
| 1547 |
+
height: number,
|
| 1548 |
+
x: number,
|
| 1549 |
+
y: number,
|
| 1550 |
+
maintain_aspect_ratio: boolean,
|
| 1551 |
+
aspect_ratio: number
|
| 1552 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1553 |
+
const top_left_constrained = this.apply_top_left_constraints(
|
| 1554 |
+
width,
|
| 1555 |
+
height,
|
| 1556 |
+
x,
|
| 1557 |
+
y,
|
| 1558 |
+
maintain_aspect_ratio,
|
| 1559 |
+
aspect_ratio
|
| 1560 |
+
);
|
| 1561 |
+
|
| 1562 |
+
const all_constrained = this.apply_bottom_right_constraints(
|
| 1563 |
+
top_left_constrained.width,
|
| 1564 |
+
top_left_constrained.height,
|
| 1565 |
+
top_left_constrained.x,
|
| 1566 |
+
top_left_constrained.y,
|
| 1567 |
+
maintain_aspect_ratio,
|
| 1568 |
+
aspect_ratio
|
| 1569 |
+
);
|
| 1570 |
+
|
| 1571 |
+
all_constrained.width = Math.max(20, all_constrained.width);
|
| 1572 |
+
all_constrained.height = Math.max(20, all_constrained.height);
|
| 1573 |
+
|
| 1574 |
+
return all_constrained;
|
| 1575 |
+
}
|
| 1576 |
+
|
| 1577 |
+
/**
|
| 1578 |
+
* Apply top and left border constraints
|
| 1579 |
+
* @private
|
| 1580 |
+
*/
|
| 1581 |
+
private apply_top_left_constraints(
|
| 1582 |
+
width: number,
|
| 1583 |
+
height: number,
|
| 1584 |
+
x: number,
|
| 1585 |
+
y: number,
|
| 1586 |
+
maintain_aspect_ratio: boolean,
|
| 1587 |
+
aspect_ratio: number
|
| 1588 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1589 |
+
let new_width = width;
|
| 1590 |
+
let new_height = height;
|
| 1591 |
+
let new_x = x;
|
| 1592 |
+
let new_y = y;
|
| 1593 |
+
|
| 1594 |
+
if (new_x < this.borderRegion) {
|
| 1595 |
+
if (
|
| 1596 |
+
this.active_corner_index === 0 ||
|
| 1597 |
+
this.active_corner_index === 2 ||
|
| 1598 |
+
this.active_edge_index === 2
|
| 1599 |
+
) {
|
| 1600 |
+
new_width -= this.borderRegion - new_x;
|
| 1601 |
+
if (maintain_aspect_ratio) {
|
| 1602 |
+
new_height = new_width / aspect_ratio;
|
| 1603 |
+
}
|
| 1604 |
+
}
|
| 1605 |
+
new_x = this.borderRegion;
|
| 1606 |
+
}
|
| 1607 |
+
|
| 1608 |
+
if (new_y < this.borderRegion) {
|
| 1609 |
+
if (
|
| 1610 |
+
this.active_corner_index === 0 ||
|
| 1611 |
+
this.active_corner_index === 1 ||
|
| 1612 |
+
this.active_edge_index === 0
|
| 1613 |
+
) {
|
| 1614 |
+
new_height -= this.borderRegion - new_y;
|
| 1615 |
+
if (maintain_aspect_ratio) {
|
| 1616 |
+
new_width = new_height * aspect_ratio;
|
| 1617 |
+
}
|
| 1618 |
+
}
|
| 1619 |
+
new_y = this.borderRegion;
|
| 1620 |
+
}
|
| 1621 |
+
|
| 1622 |
+
return { width: new_width, height: new_height, x: new_x, y: new_y };
|
| 1623 |
+
}
|
| 1624 |
+
|
| 1625 |
+
/**
|
| 1626 |
+
* Apply bottom and right border constraints
|
| 1627 |
+
* @private
|
| 1628 |
+
*/
|
| 1629 |
+
private apply_bottom_right_constraints(
|
| 1630 |
+
width: number,
|
| 1631 |
+
height: number,
|
| 1632 |
+
x: number,
|
| 1633 |
+
y: number,
|
| 1634 |
+
maintain_aspect_ratio: boolean,
|
| 1635 |
+
aspect_ratio: number
|
| 1636 |
+
): { width: number; height: number; x: number; y: number } {
|
| 1637 |
+
let new_width = width;
|
| 1638 |
+
let new_height = height;
|
| 1639 |
+
let new_x = x;
|
| 1640 |
+
let new_y = y;
|
| 1641 |
+
|
| 1642 |
+
if (new_x + new_width > this.dimensions.width - this.borderRegion) {
|
| 1643 |
+
if (
|
| 1644 |
+
this.active_corner_index === 1 ||
|
| 1645 |
+
this.active_corner_index === 3 ||
|
| 1646 |
+
this.active_edge_index === 3
|
| 1647 |
+
) {
|
| 1648 |
+
new_width = this.dimensions.width - this.borderRegion - new_x;
|
| 1649 |
+
if (maintain_aspect_ratio) {
|
| 1650 |
+
new_height = new_width / aspect_ratio;
|
| 1651 |
+
}
|
| 1652 |
+
} else {
|
| 1653 |
+
const right_edge = new_x + new_width;
|
| 1654 |
+
const excess = right_edge - (this.dimensions.width - this.borderRegion);
|
| 1655 |
+
new_x = Math.max(this.borderRegion, new_x - excess);
|
| 1656 |
+
}
|
| 1657 |
+
}
|
| 1658 |
+
|
| 1659 |
+
if (new_y + new_height > this.dimensions.height - this.borderRegion) {
|
| 1660 |
+
if (
|
| 1661 |
+
this.active_corner_index === 2 ||
|
| 1662 |
+
this.active_corner_index === 3 ||
|
| 1663 |
+
this.active_edge_index === 1
|
| 1664 |
+
) {
|
| 1665 |
+
new_height = this.dimensions.height - this.borderRegion - new_y;
|
| 1666 |
+
if (maintain_aspect_ratio) {
|
| 1667 |
+
new_width = new_height * aspect_ratio;
|
| 1668 |
+
}
|
| 1669 |
+
} else {
|
| 1670 |
+
const bottom_edge = new_y + new_height;
|
| 1671 |
+
const excess =
|
| 1672 |
+
bottom_edge - (this.dimensions.height - this.borderRegion);
|
| 1673 |
+
new_y = Math.max(this.borderRegion, new_y - excess);
|
| 1674 |
+
}
|
| 1675 |
+
}
|
| 1676 |
+
|
| 1677 |
+
return { width: new_width, height: new_height, x: new_x, y: new_y };
|
| 1678 |
+
}
|
| 1679 |
+
|
| 1680 |
+
/**
|
| 1681 |
+
* Shows the resize UI
|
| 1682 |
+
* @private
|
| 1683 |
+
*/
|
| 1684 |
+
private show_resize_ui(): void {
|
| 1685 |
+
if (this.resize_ui_container) {
|
| 1686 |
+
this.resize_ui_container.visible = true;
|
| 1687 |
+
this.update_resize_ui();
|
| 1688 |
+
}
|
| 1689 |
+
|
| 1690 |
+
if (this.image_editor_context.background_image) {
|
| 1691 |
+
const bg = this.image_editor_context.background_image;
|
| 1692 |
+
this.current_cursor = bg.cursor;
|
| 1693 |
+
bg.cursor = "move";
|
| 1694 |
+
}
|
| 1695 |
+
|
| 1696 |
+
this.setup_dom_event_listeners();
|
| 1697 |
+
}
|
| 1698 |
+
|
| 1699 |
+
/**
|
| 1700 |
+
* Hides the resize UI
|
| 1701 |
+
* @private
|
| 1702 |
+
*/
|
| 1703 |
+
private hide_resize_ui(): void {
|
| 1704 |
+
if (this.resize_ui_container) {
|
| 1705 |
+
this.resize_ui_container.visible = false;
|
| 1706 |
+
}
|
| 1707 |
+
|
| 1708 |
+
if (this.image_editor_context.background_image) {
|
| 1709 |
+
const bg = this.image_editor_context.background_image;
|
| 1710 |
+
bg.cursor = this.current_cursor;
|
| 1711 |
+
}
|
| 1712 |
+
|
| 1713 |
+
this.cleanup_dom_event_listeners();
|
| 1714 |
+
}
|
| 1715 |
+
|
| 1716 |
+
on<T extends string>(event: T, callback: () => void): void {
|
| 1717 |
+
this.event_callbacks.set(event, [
|
| 1718 |
+
...(this.event_callbacks.get(event) || []),
|
| 1719 |
+
callback
|
| 1720 |
+
]);
|
| 1721 |
+
}
|
| 1722 |
+
|
| 1723 |
+
off<T extends string>(event: T, callback: () => void): void {
|
| 1724 |
+
this.event_callbacks.set(
|
| 1725 |
+
event,
|
| 1726 |
+
this.event_callbacks.get(event)?.filter((cb) => cb !== callback) || []
|
| 1727 |
+
);
|
| 1728 |
+
}
|
| 1729 |
+
|
| 1730 |
+
private notify<T extends string>(event: T): void {
|
| 1731 |
+
for (const callback of this.event_callbacks.get(event) || []) {
|
| 1732 |
+
callback();
|
| 1733 |
+
}
|
| 1734 |
+
}
|
| 1735 |
+
|
| 1736 |
+
/**
|
| 1737 |
+
* Sets the border region explicitly
|
| 1738 |
+
* @param borderRegion The border region size in pixels
|
| 1739 |
+
*/
|
| 1740 |
+
public set_border_region(borderRegion: number): void {
|
| 1741 |
+
this.borderRegion = borderRegion;
|
| 1742 |
+
|
| 1743 |
+
if (this.image_editor_context?.background_image) {
|
| 1744 |
+
(this.image_editor_context.background_image as any).borderRegion =
|
| 1745 |
+
borderRegion;
|
| 1746 |
+
}
|
| 1747 |
+
|
| 1748 |
+
if (this.resize_ui_container) {
|
| 1749 |
+
this.update_resize_ui();
|
| 1750 |
+
}
|
| 1751 |
+
|
| 1752 |
+
if (
|
| 1753 |
+
this.current_subtool === "size" &&
|
| 1754 |
+
this.image_editor_context?.background_image
|
| 1755 |
+
) {
|
| 1756 |
+
this.apply_boundary_constraints();
|
| 1757 |
+
}
|
| 1758 |
+
}
|
| 1759 |
+
}
|
6.0.0-dev.5/imageeditor/shared/types.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export interface ImageBlobs {
|
| 2 |
+
background: Blob | null;
|
| 3 |
+
layers: (Blob | null)[];
|
| 4 |
+
composite: Blob | null;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
export interface LayerOptions {
|
| 8 |
+
allow_additional_layers: boolean;
|
| 9 |
+
layers: string[];
|
| 10 |
+
disabled: boolean;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export type Source = "upload" | "webcam" | "clipboard";
|
| 14 |
+
export type Transform = "crop" | "resize";
|
| 15 |
+
|
| 16 |
+
export interface WebcamOptions {
|
| 17 |
+
mirror: boolean;
|
| 18 |
+
constraints: Record<string, any>;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
export interface WatermarkOptions {
|
| 22 |
+
watermark: string | Blob | null;
|
| 23 |
+
position:
|
| 24 |
+
| [number, number]
|
| 25 |
+
| "top-left"
|
| 26 |
+
| "top-right"
|
| 27 |
+
| "bottom-left"
|
| 28 |
+
| "bottom-right";
|
| 29 |
+
}
|
6.0.0-dev.5/imageeditor/shared/utils/events.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { tick } from "svelte";
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* Svelte action to handle clicks outside of a DOM node
|
| 5 |
+
* @param node DOM node to check the click is outside of
|
| 6 |
+
* @param callback callback function to call if click is outside
|
| 7 |
+
* @returns svelte action return object with destroy method to remove event listener
|
| 8 |
+
*/
|
| 9 |
+
export function click_outside(
|
| 10 |
+
node: Node,
|
| 11 |
+
callback: (arg: MouseEvent) => void
|
| 12 |
+
): any {
|
| 13 |
+
const handle_click = (event: MouseEvent): void => {
|
| 14 |
+
if (
|
| 15 |
+
node &&
|
| 16 |
+
!node.contains(event.target as Node) &&
|
| 17 |
+
!event.defaultPrevented
|
| 18 |
+
) {
|
| 19 |
+
callback(event);
|
| 20 |
+
}
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
document.addEventListener("mousedown", handle_click, true);
|
| 24 |
+
|
| 25 |
+
return {
|
| 26 |
+
destroy() {
|
| 27 |
+
document.removeEventListener("mousedown", handle_click, true);
|
| 28 |
+
}
|
| 29 |
+
};
|
| 30 |
+
}
|
6.0.0-dev.5/imageeditor/shared/utils/parse_placeholder.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const RE_HEADING = /^(#\s*)(.+)$/m;
|
| 2 |
+
|
| 3 |
+
export function inject(text: string): [string | false, string | false] {
|
| 4 |
+
const trimmed_text = text.trim();
|
| 5 |
+
|
| 6 |
+
const heading_match = trimmed_text.match(RE_HEADING);
|
| 7 |
+
if (!heading_match) {
|
| 8 |
+
return [false, trimmed_text || false];
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const [full_match, , heading_content] = heading_match;
|
| 12 |
+
const _heading = heading_content.trim();
|
| 13 |
+
|
| 14 |
+
if (trimmed_text === full_match) {
|
| 15 |
+
return [_heading, false];
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const heading_end_index =
|
| 19 |
+
heading_match.index !== undefined
|
| 20 |
+
? heading_match.index + full_match.length
|
| 21 |
+
: 0;
|
| 22 |
+
const remaining_text = trimmed_text.substring(heading_end_index).trim();
|
| 23 |
+
|
| 24 |
+
const _paragraph = remaining_text || false;
|
| 25 |
+
|
| 26 |
+
return [_heading, _paragraph];
|
| 27 |
+
}
|
6.0.0-dev.5/imageeditor/shared/utils/pixi.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Graphics, Rectangle, type Renderer, type Container } from "pixi.js";
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* Creates a pixi graphics object.
|
| 5 |
+
* @param z_index the z index of the graphics object
|
| 6 |
+
* @returns a graphics object
|
| 7 |
+
*/
|
| 8 |
+
export function make_graphics(z_index: number): Graphics {
|
| 9 |
+
const graphics = new Graphics();
|
| 10 |
+
graphics.eventMode = "none";
|
| 11 |
+
graphics.zIndex = z_index;
|
| 12 |
+
|
| 13 |
+
return graphics;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
/**
|
| 17 |
+
* Clamps a number between a min and max value.
|
| 18 |
+
* @param n The number to clamp.
|
| 19 |
+
* @param min The minimum value.
|
| 20 |
+
* @param max The maximum value.
|
| 21 |
+
* @returns The clamped number.
|
| 22 |
+
*/
|
| 23 |
+
export function clamp(n: number, min: number, max: number): number {
|
| 24 |
+
return n < min ? min : n > max ? max : n;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
/**
|
| 28 |
+
* Generates a blob from a pixi object.s
|
| 29 |
+
* @param renderer The pixi renderer.
|
| 30 |
+
* @param obj The pixi object to generate a blob from.
|
| 31 |
+
* @param bounds The bounds of the canvas that we wish to extract
|
| 32 |
+
* @param width The full width of the canvas
|
| 33 |
+
* @param height The full height of the canvas
|
| 34 |
+
* @returns A promise with the blob.
|
| 35 |
+
*/
|
| 36 |
+
export function get_canvas_blob(
|
| 37 |
+
renderer: Renderer,
|
| 38 |
+
obj: Container | null,
|
| 39 |
+
bounds?: { x: number; y: number; width: number; height: number }
|
| 40 |
+
): Promise<Blob | null> {
|
| 41 |
+
return new Promise((resolve) => {
|
| 42 |
+
if (!obj) {
|
| 43 |
+
resolve(null);
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const image_bounds = obj.getLocalBounds();
|
| 48 |
+
const frame = bounds
|
| 49 |
+
? new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height)
|
| 50 |
+
: new Rectangle(0, 0, image_bounds.width, image_bounds.height);
|
| 51 |
+
|
| 52 |
+
const src_canvas = renderer.extract.canvas({
|
| 53 |
+
target: obj,
|
| 54 |
+
resolution: 1,
|
| 55 |
+
frame
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
src_canvas.toBlob?.((blob) => {
|
| 59 |
+
if (!blob) {
|
| 60 |
+
resolve(null);
|
| 61 |
+
}
|
| 62 |
+
resolve(blob);
|
| 63 |
+
});
|
| 64 |
+
});
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
export interface ImageBlobs {
|
| 68 |
+
background: Blob | null;
|
| 69 |
+
layers: (Blob | null)[];
|
| 70 |
+
composite: Blob | null;
|
| 71 |
+
}
|
6.0.0-dev.5/imageeditor/shared/zoom/zoom.ts
ADDED
|
@@ -0,0 +1,675 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
Point,
|
| 3 |
+
type FederatedWheelEvent,
|
| 4 |
+
type FederatedPointerEvent
|
| 5 |
+
} from "pixi.js";
|
| 6 |
+
import { type ImageEditorContext, type Tool } from "../core/editor";
|
| 7 |
+
import { type Tool as ToolbarTool, type Subtool } from "../Toolbar.svelte";
|
| 8 |
+
import { get, writable } from "svelte/store";
|
| 9 |
+
|
| 10 |
+
/**
|
| 11 |
+
* ZoomTool class for handling zoom and pan functionality in the image editor
|
| 12 |
+
* Implements the Tool interface
|
| 13 |
+
*/
|
| 14 |
+
export class ZoomTool implements Tool {
|
| 15 |
+
name = "zoom";
|
| 16 |
+
min_zoom = writable(true);
|
| 17 |
+
|
| 18 |
+
private image_editor_context!: ImageEditorContext;
|
| 19 |
+
|
| 20 |
+
private readonly max_zoom = 10;
|
| 21 |
+
private readonly border_padding = 30;
|
| 22 |
+
|
| 23 |
+
private pad_bottom = 0;
|
| 24 |
+
private is_pinching = false;
|
| 25 |
+
private is_dragging = false;
|
| 26 |
+
private is_pointer_dragging = false;
|
| 27 |
+
private last_touch_position: Point | null = null;
|
| 28 |
+
private last_pinch_distance = 0;
|
| 29 |
+
private drag_start = new Point();
|
| 30 |
+
private current_tool!: ToolbarTool;
|
| 31 |
+
private current_subtool!: Subtool;
|
| 32 |
+
private local_scale = 1;
|
| 33 |
+
private local_dimensions: { width: number; height: number } = {
|
| 34 |
+
width: 0,
|
| 35 |
+
height: 0
|
| 36 |
+
};
|
| 37 |
+
private local_position: { x: number; y: number } = { x: 0, y: 0 };
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Prevents default behavior for events
|
| 41 |
+
* @param {FederatedWheelEvent | FederatedPointerEvent} event - The event to prevent default behavior for
|
| 42 |
+
*/
|
| 43 |
+
prevent_default(
|
| 44 |
+
event: FederatedWheelEvent | FederatedPointerEvent | WheelEvent
|
| 45 |
+
): void {
|
| 46 |
+
event.preventDefault();
|
| 47 |
+
event.stopPropagation();
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/**
|
| 51 |
+
* Sets the current tool and subtool
|
| 52 |
+
* @param {ToolbarTool} tool - The tool to set
|
| 53 |
+
* @param {Subtool} subtool - The subtool to set
|
| 54 |
+
*/
|
| 55 |
+
set_tool(tool: ToolbarTool, subtool: Subtool): void {
|
| 56 |
+
this.current_tool = tool;
|
| 57 |
+
this.current_subtool = subtool;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
/**
|
| 61 |
+
* Sets the zoom level to a specific percentage or fits the image to the screen
|
| 62 |
+
* @param {number | 'fit'} zoom_level - The zoom level to set (0-1) or 'fit' to use min_zoom
|
| 63 |
+
* @returns {void}
|
| 64 |
+
*/
|
| 65 |
+
set_zoom(zoom_level: number | "fit"): void {
|
| 66 |
+
const fit_zoom = this.calculate_min_zoom(
|
| 67 |
+
this.local_dimensions.width,
|
| 68 |
+
this.local_dimensions.height
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
let target_zoom: number;
|
| 72 |
+
const is_fit_zoom = zoom_level === "fit";
|
| 73 |
+
if (is_fit_zoom) {
|
| 74 |
+
target_zoom = fit_zoom;
|
| 75 |
+
} else {
|
| 76 |
+
target_zoom = Math.max(0, Math.min(this.max_zoom, zoom_level));
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
const canvas = this.image_editor_context.app.screen;
|
| 80 |
+
const canvas_width = canvas.width;
|
| 81 |
+
const canvas_height = canvas.height;
|
| 82 |
+
|
| 83 |
+
let center_point: { x: number; y: number };
|
| 84 |
+
|
| 85 |
+
center_point = {
|
| 86 |
+
x: canvas_width / 2,
|
| 87 |
+
y: canvas_height / 2
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
this.zoom_to_point(target_zoom, center_point, true, is_fit_zoom);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
/**
|
| 94 |
+
* Sets up the zoom tool with the given context and state
|
| 95 |
+
* @param {ImageEditorContext} context - The image editor context
|
| 96 |
+
* @param {ToolbarTool} tool - The current tool
|
| 97 |
+
* @param {Subtool} subtool - The current subtool
|
| 98 |
+
* @returns {Promise<void>}
|
| 99 |
+
*/
|
| 100 |
+
async setup(
|
| 101 |
+
context: ImageEditorContext,
|
| 102 |
+
tool: ToolbarTool,
|
| 103 |
+
subtool: Subtool
|
| 104 |
+
): Promise<void> {
|
| 105 |
+
this.image_editor_context = context;
|
| 106 |
+
this.current_tool = tool;
|
| 107 |
+
this.current_subtool = subtool;
|
| 108 |
+
this.pad_bottom = context.pad_bottom;
|
| 109 |
+
const { width, height } = await this.get_container_dimensions();
|
| 110 |
+
const fit_zoom = this.calculate_min_zoom(width, height);
|
| 111 |
+
const min_zoom = Math.min(fit_zoom, 1);
|
| 112 |
+
|
| 113 |
+
this.local_scale = min_zoom;
|
| 114 |
+
|
| 115 |
+
const canvas = this.image_editor_context.app.screen;
|
| 116 |
+
|
| 117 |
+
const center_x = canvas.width / 2;
|
| 118 |
+
const center_y = canvas.height / 2;
|
| 119 |
+
|
| 120 |
+
const scaled_width = width * min_zoom;
|
| 121 |
+
const scaled_height = height * min_zoom;
|
| 122 |
+
const initial_x = center_x - scaled_width / 2;
|
| 123 |
+
const initial_y = center_y - scaled_height / 2;
|
| 124 |
+
|
| 125 |
+
this.local_position = { x: initial_x, y: initial_y };
|
| 126 |
+
|
| 127 |
+
this.setup_event_listeners();
|
| 128 |
+
|
| 129 |
+
await this.image_editor_context.set_image_properties({
|
| 130 |
+
scale: min_zoom,
|
| 131 |
+
position: { x: initial_x, y: initial_y }
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
this.image_editor_context.dimensions.subscribe((dimensions) => {
|
| 135 |
+
this.local_dimensions = dimensions;
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
this.image_editor_context.scale.subscribe((scale) => {
|
| 139 |
+
this.local_scale = scale;
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
this.image_editor_context.position.subscribe((position) => {
|
| 143 |
+
this.local_position = position;
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
/**
|
| 148 |
+
* Sets up event listeners for zoom and pan functionality
|
| 149 |
+
* @private
|
| 150 |
+
*/
|
| 151 |
+
private setup_event_listeners(): void {
|
| 152 |
+
const stage = this.image_editor_context.app.stage;
|
| 153 |
+
const canvas = this.image_editor_context.app.canvas;
|
| 154 |
+
|
| 155 |
+
canvas.addEventListener("wheel", this.prevent_default, { passive: false });
|
| 156 |
+
|
| 157 |
+
stage.eventMode = "static";
|
| 158 |
+
stage.hitArea = this.image_editor_context.app.screen;
|
| 159 |
+
|
| 160 |
+
const wheel_listener = this.handle_wheel.bind(this);
|
| 161 |
+
stage.addEventListener("wheel", wheel_listener, { passive: false });
|
| 162 |
+
|
| 163 |
+
if ("ontouchstart" in window) {
|
| 164 |
+
stage.addEventListener(
|
| 165 |
+
"touchstart",
|
| 166 |
+
this.handle_touch_start.bind(this) as EventListener
|
| 167 |
+
);
|
| 168 |
+
stage.addEventListener(
|
| 169 |
+
"touchmove",
|
| 170 |
+
this.handle_touch_move.bind(this) as EventListener
|
| 171 |
+
);
|
| 172 |
+
stage.addEventListener(
|
| 173 |
+
"touchend",
|
| 174 |
+
this.handle_touch_end.bind(this) as EventListener
|
| 175 |
+
);
|
| 176 |
+
} else {
|
| 177 |
+
stage.addEventListener(
|
| 178 |
+
"pointerdown",
|
| 179 |
+
this.handle_pointer_down.bind(this)
|
| 180 |
+
);
|
| 181 |
+
stage.addEventListener(
|
| 182 |
+
"pointermove",
|
| 183 |
+
this.handle_pointer_move.bind(this)
|
| 184 |
+
);
|
| 185 |
+
stage.addEventListener("pointerup", this.handle_pointer_up.bind(this));
|
| 186 |
+
stage.addEventListener(
|
| 187 |
+
"pointerupoutside",
|
| 188 |
+
this.handle_pointer_up.bind(this)
|
| 189 |
+
);
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
/**
|
| 194 |
+
* Handles wheel events for zooming
|
| 195 |
+
* @param {FederatedWheelEvent} event - The wheel event
|
| 196 |
+
* @private
|
| 197 |
+
*/
|
| 198 |
+
private handle_wheel(event: FederatedWheelEvent): void {
|
| 199 |
+
const is_trackpad = event.deltaMode === 0 && Math.abs(event.deltaY) < 50;
|
| 200 |
+
const scroll_speed = is_trackpad ? 30 : 10;
|
| 201 |
+
|
| 202 |
+
if (event.altKey || event.metaKey) {
|
| 203 |
+
const local_point = this.image_editor_context.app.stage.toLocal(
|
| 204 |
+
event.global
|
| 205 |
+
);
|
| 206 |
+
const zoom_delta = -event.deltaY * (is_trackpad ? 0.001 : 0.0005);
|
| 207 |
+
const new_scale = this.local_scale * (1 + zoom_delta);
|
| 208 |
+
this.zoom_to_point(new_scale, local_point, true);
|
| 209 |
+
} else {
|
| 210 |
+
const delta_x = event.deltaX;
|
| 211 |
+
const delta_y = event.deltaY;
|
| 212 |
+
const raw_position = {
|
| 213 |
+
x: this.local_position.x - (delta_x / 100) * scroll_speed,
|
| 214 |
+
y: this.local_position.y - (delta_y / 100) * scroll_speed
|
| 215 |
+
};
|
| 216 |
+
|
| 217 |
+
const canvas = this.image_editor_context.app.screen;
|
| 218 |
+
const scaled_width = this.local_dimensions.width * this.local_scale;
|
| 219 |
+
const scaled_height = this.local_dimensions.height * this.local_scale;
|
| 220 |
+
const available_width = canvas.width - this.border_padding * 2;
|
| 221 |
+
const available_height =
|
| 222 |
+
canvas.height - this.border_padding * 2 - this.pad_bottom;
|
| 223 |
+
|
| 224 |
+
let final_position = { ...raw_position };
|
| 225 |
+
|
| 226 |
+
if (scaled_width <= available_width) {
|
| 227 |
+
final_position.x = (canvas.width - scaled_width) / 2;
|
| 228 |
+
} else {
|
| 229 |
+
const max_offset = canvas.width / 2;
|
| 230 |
+
const left_bound = max_offset;
|
| 231 |
+
const right_bound = canvas.width - max_offset - scaled_width;
|
| 232 |
+
final_position.x = Math.min(
|
| 233 |
+
Math.max(raw_position.x, right_bound),
|
| 234 |
+
left_bound
|
| 235 |
+
);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
if (scaled_height <= available_height) {
|
| 239 |
+
final_position.y =
|
| 240 |
+
(canvas.height - this.pad_bottom - scaled_height) / 2;
|
| 241 |
+
} else {
|
| 242 |
+
const max_offset = (canvas.height - this.pad_bottom) / 2;
|
| 243 |
+
const top_bound = max_offset;
|
| 244 |
+
const bottom_bound =
|
| 245 |
+
canvas.height - this.pad_bottom - max_offset - scaled_height;
|
| 246 |
+
final_position.y = Math.min(
|
| 247 |
+
Math.max(raw_position.y, bottom_bound),
|
| 248 |
+
top_bound
|
| 249 |
+
);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
this.image_editor_context.set_image_properties({
|
| 253 |
+
scale: this.local_scale,
|
| 254 |
+
position: final_position
|
| 255 |
+
});
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
/**
|
| 260 |
+
* Cleans up resources and event listeners
|
| 261 |
+
*/
|
| 262 |
+
cleanup(): void {
|
| 263 |
+
const stage = this?.image_editor_context?.app?.stage;
|
| 264 |
+
if (!stage) return;
|
| 265 |
+
stage.removeEventListener("wheel", this.handle_wheel.bind(this));
|
| 266 |
+
|
| 267 |
+
if ("ontouchstart" in window) {
|
| 268 |
+
stage.removeEventListener(
|
| 269 |
+
"touchstart",
|
| 270 |
+
this.handle_touch_start.bind(this) as EventListener
|
| 271 |
+
);
|
| 272 |
+
stage.removeEventListener(
|
| 273 |
+
"touchmove",
|
| 274 |
+
this.handle_touch_move.bind(this) as EventListener
|
| 275 |
+
);
|
| 276 |
+
stage.removeEventListener(
|
| 277 |
+
"touchend",
|
| 278 |
+
this.handle_touch_end.bind(this) as EventListener
|
| 279 |
+
);
|
| 280 |
+
} else {
|
| 281 |
+
stage.removeEventListener(
|
| 282 |
+
"pointerdown",
|
| 283 |
+
this.handle_pointer_down.bind(this)
|
| 284 |
+
);
|
| 285 |
+
stage.removeEventListener(
|
| 286 |
+
"pointermove",
|
| 287 |
+
this.handle_pointer_move.bind(this)
|
| 288 |
+
);
|
| 289 |
+
stage.removeEventListener("pointerup", this.handle_pointer_up.bind(this));
|
| 290 |
+
stage.removeEventListener(
|
| 291 |
+
"pointerupoutside",
|
| 292 |
+
this.handle_pointer_up.bind(this)
|
| 293 |
+
);
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
/**
|
| 298 |
+
* Gets the dimensions of the container
|
| 299 |
+
* @returns {Promise<{width: number, height: number}>} The container dimensions
|
| 300 |
+
* @private
|
| 301 |
+
*/
|
| 302 |
+
private async get_container_dimensions(): Promise<{
|
| 303 |
+
width: number;
|
| 304 |
+
height: number;
|
| 305 |
+
}> {
|
| 306 |
+
const bounds = this.image_editor_context.image_container.getLocalBounds();
|
| 307 |
+
return {
|
| 308 |
+
width: bounds.width,
|
| 309 |
+
height: bounds.height
|
| 310 |
+
};
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
/**
|
| 314 |
+
* Calculates the minimum zoom level to fit the image in the viewport
|
| 315 |
+
* @param {number} container_width - The width of the container
|
| 316 |
+
* @param {number} container_height - The height of the container
|
| 317 |
+
* @returns {number} The minimum zoom level
|
| 318 |
+
* @private
|
| 319 |
+
*/
|
| 320 |
+
private calculate_min_zoom(
|
| 321 |
+
container_width: number,
|
| 322 |
+
container_height: number
|
| 323 |
+
): number {
|
| 324 |
+
const canvas = this.image_editor_context.app.screen;
|
| 325 |
+
const viewport_width = canvas.width;
|
| 326 |
+
const viewport_height = canvas.height;
|
| 327 |
+
|
| 328 |
+
if (
|
| 329 |
+
!container_width ||
|
| 330 |
+
!viewport_width ||
|
| 331 |
+
!container_height ||
|
| 332 |
+
!viewport_height
|
| 333 |
+
) {
|
| 334 |
+
return 1;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
const available_width = viewport_width - this.border_padding * 2;
|
| 338 |
+
const available_height =
|
| 339 |
+
viewport_height - this.border_padding * 2 - this.pad_bottom;
|
| 340 |
+
|
| 341 |
+
const width_ratio = available_width / container_width;
|
| 342 |
+
const height_ratio = available_height / container_height;
|
| 343 |
+
|
| 344 |
+
return Math.min(width_ratio, height_ratio);
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
/**
|
| 348 |
+
* Handles touch start events
|
| 349 |
+
* @param {TouchEvent} event - The touch event
|
| 350 |
+
* @private
|
| 351 |
+
*/
|
| 352 |
+
private handle_touch_start(event: FederatedPointerEvent): void {
|
| 353 |
+
event.preventDefault();
|
| 354 |
+
|
| 355 |
+
const touchEvent = event as any;
|
| 356 |
+
|
| 357 |
+
if (touchEvent.touches && touchEvent.touches.length === 2) {
|
| 358 |
+
this.is_pinching = true;
|
| 359 |
+
this.last_pinch_distance = Math.hypot(
|
| 360 |
+
touchEvent.touches[0].pageX - touchEvent.touches[1].pageX,
|
| 361 |
+
touchEvent.touches[0].pageY - touchEvent.touches[1].pageY
|
| 362 |
+
);
|
| 363 |
+
} else if (touchEvent.touches && touchEvent.touches.length === 1) {
|
| 364 |
+
this.is_dragging = true;
|
| 365 |
+
const rect = this.image_editor_context.app.view.getBoundingClientRect();
|
| 366 |
+
this.last_touch_position = new Point(
|
| 367 |
+
touchEvent.touches[0].pageX - rect.left,
|
| 368 |
+
touchEvent.touches[0].pageY - rect.top
|
| 369 |
+
);
|
| 370 |
+
}
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
/**
|
| 374 |
+
* Handles touch move events
|
| 375 |
+
* @param {TouchEvent} event - The touch event
|
| 376 |
+
* @private
|
| 377 |
+
*/
|
| 378 |
+
private handle_touch_move(event: FederatedPointerEvent): void {
|
| 379 |
+
event.preventDefault();
|
| 380 |
+
|
| 381 |
+
const touchEvent = event as any;
|
| 382 |
+
|
| 383 |
+
if (
|
| 384 |
+
this.is_pinching &&
|
| 385 |
+
touchEvent.touches &&
|
| 386 |
+
touchEvent.touches.length === 2
|
| 387 |
+
) {
|
| 388 |
+
const rect = this.image_editor_context.app.view.getBoundingClientRect();
|
| 389 |
+
const current_distance = Math.hypot(
|
| 390 |
+
touchEvent.touches[0].pageX - touchEvent.touches[1].pageX,
|
| 391 |
+
touchEvent.touches[0].pageY - touchEvent.touches[1].pageY
|
| 392 |
+
);
|
| 393 |
+
|
| 394 |
+
const pinch_center = {
|
| 395 |
+
x:
|
| 396 |
+
(touchEvent.touches[0].pageX + touchEvent.touches[1].pageX) / 2 -
|
| 397 |
+
rect.left,
|
| 398 |
+
y:
|
| 399 |
+
(touchEvent.touches[0].pageY + touchEvent.touches[1].pageY) / 2 -
|
| 400 |
+
rect.top
|
| 401 |
+
};
|
| 402 |
+
|
| 403 |
+
const scale_val = current_distance / this.last_pinch_distance;
|
| 404 |
+
this.last_pinch_distance = current_distance;
|
| 405 |
+
|
| 406 |
+
this.zoom_to_point(this.local_scale * scale_val, pinch_center);
|
| 407 |
+
} else if (
|
| 408 |
+
this.is_dragging &&
|
| 409 |
+
touchEvent.touches &&
|
| 410 |
+
touchEvent.touches.length === 1 &&
|
| 411 |
+
this.last_touch_position
|
| 412 |
+
) {
|
| 413 |
+
const rect = this.image_editor_context.app.view.getBoundingClientRect();
|
| 414 |
+
const current_position = new Point(
|
| 415 |
+
touchEvent.touches[0].pageX - rect.left,
|
| 416 |
+
touchEvent.touches[0].pageY - rect.top
|
| 417 |
+
);
|
| 418 |
+
|
| 419 |
+
const dx = current_position.x - this.last_touch_position.x;
|
| 420 |
+
const dy = current_position.y - this.last_touch_position.y;
|
| 421 |
+
|
| 422 |
+
this.image_editor_context.set_image_properties({
|
| 423 |
+
position: {
|
| 424 |
+
x: this.local_position.x + dx,
|
| 425 |
+
y: this.local_position.y + dy
|
| 426 |
+
}
|
| 427 |
+
});
|
| 428 |
+
|
| 429 |
+
this.last_touch_position = current_position;
|
| 430 |
+
}
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
/**
|
| 434 |
+
* Handles touch end events
|
| 435 |
+
* @param {TouchEvent} event - The touch event
|
| 436 |
+
* @private
|
| 437 |
+
*/
|
| 438 |
+
private handle_touch_end(event: FederatedPointerEvent): void {
|
| 439 |
+
event.preventDefault();
|
| 440 |
+
|
| 441 |
+
const touchEvent = event as any;
|
| 442 |
+
|
| 443 |
+
if (touchEvent.touches && touchEvent.touches.length < 2) {
|
| 444 |
+
this.is_pinching = false;
|
| 445 |
+
if (touchEvent.touches && touchEvent.touches.length === 1) {
|
| 446 |
+
const rect = this.image_editor_context.app.view.getBoundingClientRect();
|
| 447 |
+
this.last_touch_position = new Point(
|
| 448 |
+
touchEvent.touches[0].pageX - rect.left,
|
| 449 |
+
touchEvent.touches[0].pageY - rect.top
|
| 450 |
+
);
|
| 451 |
+
this.is_dragging = true;
|
| 452 |
+
}
|
| 453 |
+
}
|
| 454 |
+
if (touchEvent.touches && touchEvent.touches.length === 0) {
|
| 455 |
+
this.is_dragging = false;
|
| 456 |
+
this.last_touch_position = null;
|
| 457 |
+
this.last_pinch_distance = 0;
|
| 458 |
+
}
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
/**
|
| 462 |
+
* Gets the bounded position to keep the image within the viewport
|
| 463 |
+
* @param {Object} position - The position to bound
|
| 464 |
+
* @param {number} position.x - The x coordinate
|
| 465 |
+
* @param {number} position.y - The y coordinate
|
| 466 |
+
* @returns {Object} The bounded position
|
| 467 |
+
* @private
|
| 468 |
+
*/
|
| 469 |
+
private get_bounded_position(position: { x: number; y: number }): {
|
| 470 |
+
x: number;
|
| 471 |
+
y: number;
|
| 472 |
+
} {
|
| 473 |
+
const canvas = this.image_editor_context.app.screen;
|
| 474 |
+
const scaled_width = this.local_dimensions.width * this.local_scale;
|
| 475 |
+
const scaled_height = this.local_dimensions.height * this.local_scale;
|
| 476 |
+
|
| 477 |
+
const center_position = {
|
| 478 |
+
x: (canvas.width - scaled_width) / 2,
|
| 479 |
+
y: (canvas.height - scaled_height - this.pad_bottom) / 2
|
| 480 |
+
};
|
| 481 |
+
|
| 482 |
+
if (scaled_width <= canvas.width && scaled_height <= canvas.height) {
|
| 483 |
+
return center_position;
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
let x = position.x;
|
| 487 |
+
let y = position.y;
|
| 488 |
+
|
| 489 |
+
if (scaled_width <= canvas.width) {
|
| 490 |
+
x = center_position.x;
|
| 491 |
+
} else {
|
| 492 |
+
const min_x = canvas.width - scaled_width;
|
| 493 |
+
const max_x = 0;
|
| 494 |
+
x = Math.max(min_x, Math.min(max_x, position.x));
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
if (scaled_height <= canvas.height - this.pad_bottom) {
|
| 498 |
+
y = center_position.y;
|
| 499 |
+
} else {
|
| 500 |
+
const min_y = canvas.height - scaled_height - this.pad_bottom;
|
| 501 |
+
const max_y = 0;
|
| 502 |
+
y = Math.max(min_y, Math.min(max_y, position.y));
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
return { x, y };
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
/**
|
| 509 |
+
* Zooms to a specific point with a new zoom level
|
| 510 |
+
* @param {number} new_zoom - The new zoom level
|
| 511 |
+
* @param {Object} point - The point to zoom to
|
| 512 |
+
* @param {number} point.x - The x coordinate of the point
|
| 513 |
+
* @param {number} point.y - The y coordinate of the point
|
| 514 |
+
* @param {boolean} hard - Whether to apply a hard zoom (no animation)
|
| 515 |
+
* @param {boolean} is_fit_zoom - Whether this is a fit zoom operation
|
| 516 |
+
* @private
|
| 517 |
+
*/
|
| 518 |
+
private zoom_to_point(
|
| 519 |
+
new_zoom: number,
|
| 520 |
+
point: { x: number; y: number },
|
| 521 |
+
hard?: boolean,
|
| 522 |
+
is_fit_zoom?: boolean
|
| 523 |
+
): void {
|
| 524 |
+
const container = this.image_editor_context.image_container;
|
| 525 |
+
const current_world_pos = container.getGlobalPosition();
|
| 526 |
+
const current_scale = container.scale.x;
|
| 527 |
+
|
| 528 |
+
const cursor_image_pixel = {
|
| 529 |
+
x: (point.x - current_world_pos.x) / current_scale,
|
| 530 |
+
y: (point.y - current_world_pos.y) / current_scale
|
| 531 |
+
};
|
| 532 |
+
|
| 533 |
+
const fit_zoom = this.calculate_min_zoom(
|
| 534 |
+
this.local_dimensions.width,
|
| 535 |
+
this.local_dimensions.height
|
| 536 |
+
);
|
| 537 |
+
const min_zoom = Math.min(fit_zoom, 1);
|
| 538 |
+
new_zoom = Math.min(Math.max(new_zoom, min_zoom), this.max_zoom);
|
| 539 |
+
|
| 540 |
+
const new_container_world_pos = {
|
| 541 |
+
x: point.x - cursor_image_pixel.x * new_zoom,
|
| 542 |
+
y: point.y - cursor_image_pixel.y * new_zoom
|
| 543 |
+
};
|
| 544 |
+
|
| 545 |
+
if (new_zoom === min_zoom || is_fit_zoom) {
|
| 546 |
+
const canvas_width = this.image_editor_context.app.screen.width;
|
| 547 |
+
const canvas_height = this.image_editor_context.app.screen.height;
|
| 548 |
+
const new_scaled_width = this.local_dimensions.width * new_zoom;
|
| 549 |
+
const new_scaled_height = this.local_dimensions.height * new_zoom;
|
| 550 |
+
|
| 551 |
+
new_container_world_pos.x = (canvas_width - new_scaled_width) / 2;
|
| 552 |
+
new_container_world_pos.y =
|
| 553 |
+
(canvas_height - this.pad_bottom - new_scaled_height) / 2;
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
this.image_editor_context.set_image_properties({
|
| 557 |
+
scale: new_zoom,
|
| 558 |
+
position: new_container_world_pos,
|
| 559 |
+
animate: typeof hard === "boolean" ? !hard : new_zoom === min_zoom
|
| 560 |
+
});
|
| 561 |
+
this.min_zoom.set(new_zoom === min_zoom);
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
/**
|
| 565 |
+
* Handles pointer down events
|
| 566 |
+
* @param {FederatedPointerEvent} event - The pointer event
|
| 567 |
+
* @private
|
| 568 |
+
*/
|
| 569 |
+
private handle_pointer_down(event: FederatedPointerEvent): void {
|
| 570 |
+
if (event.button === 0 && this.current_tool === "pan") {
|
| 571 |
+
this.is_pointer_dragging = true;
|
| 572 |
+
this.drag_start.copyFrom(event.global);
|
| 573 |
+
this.drag_start.x -= this.local_position.x;
|
| 574 |
+
this.drag_start.y -= this.local_position.y;
|
| 575 |
+
}
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
/**
|
| 579 |
+
* Handles pointer move events
|
| 580 |
+
* @param {FederatedPointerEvent} event - The pointer event
|
| 581 |
+
* @private
|
| 582 |
+
*/
|
| 583 |
+
private handle_pointer_move(event: FederatedPointerEvent): void {
|
| 584 |
+
if (this.is_pointer_dragging && this.current_tool === "pan") {
|
| 585 |
+
const raw_position = {
|
| 586 |
+
x: event.global.x - this.drag_start.x,
|
| 587 |
+
y: event.global.y - this.drag_start.y
|
| 588 |
+
};
|
| 589 |
+
|
| 590 |
+
const canvas = this.image_editor_context.app.screen;
|
| 591 |
+
const scaled_width = this.local_dimensions.width * this.local_scale;
|
| 592 |
+
const scaled_height = this.local_dimensions.height * this.local_scale;
|
| 593 |
+
const available_width = canvas.width - this.border_padding * 2;
|
| 594 |
+
const available_height =
|
| 595 |
+
canvas.height - this.border_padding * 2 - this.pad_bottom;
|
| 596 |
+
|
| 597 |
+
let final_position = { ...raw_position };
|
| 598 |
+
|
| 599 |
+
if (scaled_width <= available_width) {
|
| 600 |
+
final_position.x = (canvas.width - scaled_width) / 2;
|
| 601 |
+
} else {
|
| 602 |
+
final_position.x = raw_position.x;
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
if (scaled_height <= available_height) {
|
| 606 |
+
final_position.y =
|
| 607 |
+
(canvas.height - this.pad_bottom - scaled_height) / 2;
|
| 608 |
+
} else {
|
| 609 |
+
final_position.y = raw_position.y;
|
| 610 |
+
}
|
| 611 |
+
|
| 612 |
+
this.image_editor_context.set_image_properties({
|
| 613 |
+
scale: this.local_scale,
|
| 614 |
+
position: final_position
|
| 615 |
+
});
|
| 616 |
+
}
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
/**
|
| 620 |
+
* Handles pointer up events
|
| 621 |
+
* @param {FederatedPointerEvent} event - The pointer event
|
| 622 |
+
* @private
|
| 623 |
+
*/
|
| 624 |
+
private handle_pointer_up(event: FederatedPointerEvent): void {
|
| 625 |
+
if (this.is_pointer_dragging && this.current_tool === "pan") {
|
| 626 |
+
this.is_pointer_dragging = false;
|
| 627 |
+
|
| 628 |
+
const raw_position = {
|
| 629 |
+
x: event.global.x - this.drag_start.x,
|
| 630 |
+
y: event.global.y - this.drag_start.y
|
| 631 |
+
};
|
| 632 |
+
|
| 633 |
+
const canvas = this.image_editor_context.app.screen;
|
| 634 |
+
const scaled_width = this.local_dimensions.width * this.local_scale;
|
| 635 |
+
const scaled_height = this.local_dimensions.height * this.local_scale;
|
| 636 |
+
const available_width = canvas.width - this.border_padding * 2;
|
| 637 |
+
const available_height =
|
| 638 |
+
canvas.height - this.border_padding * 2 - this.pad_bottom;
|
| 639 |
+
|
| 640 |
+
let final_position = { ...raw_position };
|
| 641 |
+
|
| 642 |
+
if (scaled_width <= available_width) {
|
| 643 |
+
final_position.x = (canvas.width - scaled_width) / 2;
|
| 644 |
+
} else {
|
| 645 |
+
const max_offset = canvas.width / 2;
|
| 646 |
+
const left_bound = max_offset;
|
| 647 |
+
const right_bound = canvas.width - max_offset - scaled_width;
|
| 648 |
+
final_position.x = Math.min(
|
| 649 |
+
Math.max(raw_position.x, right_bound),
|
| 650 |
+
left_bound
|
| 651 |
+
);
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
if (scaled_height <= available_height) {
|
| 655 |
+
final_position.y =
|
| 656 |
+
(canvas.height - this.pad_bottom - scaled_height) / 2;
|
| 657 |
+
} else {
|
| 658 |
+
const max_offset = (canvas.height - this.pad_bottom) / 2;
|
| 659 |
+
const top_bound = max_offset;
|
| 660 |
+
const bottom_bound =
|
| 661 |
+
canvas.height - this.pad_bottom - max_offset - scaled_height;
|
| 662 |
+
final_position.y = Math.min(
|
| 663 |
+
Math.max(raw_position.y, bottom_bound),
|
| 664 |
+
top_bound
|
| 665 |
+
);
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
this.image_editor_context.set_image_properties({
|
| 669 |
+
scale: this.local_scale,
|
| 670 |
+
position: final_position,
|
| 671 |
+
animate: true
|
| 672 |
+
});
|
| 673 |
+
}
|
| 674 |
+
}
|
| 675 |
+
}
|
6.0.0-dev.5/imageeditor/types.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { SelectData, ShareData } from "@gradio/utils";
|
| 2 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
| 3 |
+
import type { CommandNode } from "./shared/core/commands";
|
| 4 |
+
import type {
|
| 5 |
+
LayerOptions,
|
| 6 |
+
Source,
|
| 7 |
+
Transform,
|
| 8 |
+
WebcamOptions
|
| 9 |
+
} from "./shared/types";
|
| 10 |
+
import type { Brush, Eraser } from "./shared/brush/types";
|
| 11 |
+
import type { EditorData } from "./InteractiveImageEditor.svelte";
|
| 12 |
+
import type { ImageBlobs } from "./shared/types";
|
| 13 |
+
|
| 14 |
+
export interface ImageEditorEvents {
|
| 15 |
+
change: never;
|
| 16 |
+
error: string;
|
| 17 |
+
input: never;
|
| 18 |
+
edit: never;
|
| 19 |
+
drag: never;
|
| 20 |
+
apply: never;
|
| 21 |
+
upload: never;
|
| 22 |
+
clear: never;
|
| 23 |
+
select: SelectData;
|
| 24 |
+
share: ShareData;
|
| 25 |
+
clear_status: LoadingStatus;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
export interface ImageEditorProps {
|
| 29 |
+
border_region?: number;
|
| 30 |
+
webcam_options?: WebcamOptions;
|
| 31 |
+
value?: EditorData | null;
|
| 32 |
+
buttons?: string[] | null;
|
| 33 |
+
height?: number;
|
| 34 |
+
width?: number;
|
| 35 |
+
_selectable?: boolean;
|
| 36 |
+
sources?: Source[];
|
| 37 |
+
placeholder?: string | undefined;
|
| 38 |
+
brush?: Brush;
|
| 39 |
+
eraser?: Eraser;
|
| 40 |
+
transforms?: Transform[];
|
| 41 |
+
layers?: LayerOptions;
|
| 42 |
+
server?: {
|
| 43 |
+
accept_blobs: (a: any) => void;
|
| 44 |
+
};
|
| 45 |
+
canvas_size?: [number, number];
|
| 46 |
+
fixed_canvas?: boolean;
|
| 47 |
+
full_history?: CommandNode | null;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// export let border_region = 0;
|
| 51 |
+
// export let webcam_options: WebcamOptions;
|
| 52 |
+
// export let value: EditorData | null = {
|
| 53 |
+
// background: null,
|
| 54 |
+
// layers: [],
|
| 55 |
+
// composite: null,
|
| 56 |
+
// };
|
| 57 |
+
// export let buttons: string[] | null = null;
|
| 58 |
+
// export let height = 350;
|
| 59 |
+
// export let width: number | undefined;
|
| 60 |
+
|
| 61 |
+
// export let _selectable = false;
|
| 62 |
+
// export let sources: Source[] = [];
|
| 63 |
+
// export let placeholder: string | undefined;
|
| 64 |
+
// export let brush: Brush;
|
| 65 |
+
// export let eraser: Eraser;
|
| 66 |
+
// export let transforms: Transform[] = [];
|
| 67 |
+
// export let layers: LayerOptions;
|
| 68 |
+
// export let attached_events: string[] = [];
|
| 69 |
+
// export let server: {
|
| 70 |
+
// accept_blobs: (a: any) => void;
|
| 71 |
+
// };
|
| 72 |
+
// export let canvas_size: [number, number];
|
| 73 |
+
// export let fixed_canvas = false;
|
| 74 |
+
// export let full_history: CommandNode | null = null;
|