anki/ts/image-occlusion/MaskEditor.svelte
Mani be1f889211
fixes: remove unfinished shapes, remove selectable and make shapes remain inside canvas (#2809)
* remove unfinished polygon and remove selectable for shapes in polygon mode

* make group and polygon position remain inside canvas area

* click through transparent area in grouped object

* add some shortcuts for basic usages

* tools button icon in center & switch mode border

* fix load svg image

* basic rtl support, panzoom have issues in rtl mode

* better zoom option both in ltr and rtl

* handle zoom event in mask editor

* add h button to handle toggle mask

* add more mime type

* use capital M (shift+m) for toggle mask

* allow io shortcuts in mask editor only

* make other shapes also remain in canvas bound area

* better zoom implementation, zoom from center
add zoom to resize event listener

* add a border to corner to handle blend of control

* add refresh button to go to  selection menu

* add tooltip to shortcuts and also add shortcut for other tools

* make opacity remain in same state when toggled on

* opacity for group/ungroup objects

* update shortcuts implementation
2023-11-24 14:06:40 +10:00

142 lines
3.5 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script context="module" lang="ts">
import { writable } from "svelte/store";
const changeSignal = writable(Symbol());
export function emitChangeSignal() {
changeSignal.set(Symbol());
}
</script>
<script lang="ts">
import type { PanZoom } from "panzoom";
import panzoom from "panzoom";
import { createEventDispatcher, onDestroy, onMount } from "svelte";
import type { IOMode } from "./lib";
import {
type ImageLoadedEvent,
setCanvasZoomRatio,
setupMaskEditor,
setupMaskEditorForEdit,
} from "./mask-editor";
import Toolbar from "./Toolbar.svelte";
import { MaskEditorAPI } from "./tools/api";
import { setCenterXForZoom } from "./tools/lib";
export let mode: IOMode;
const iconSize = 80;
let instance: PanZoom;
let innerWidth = 0;
const startingTool = mode.kind === "add" ? "draw-rectangle" : "cursor";
$: canvas = null;
$: {
globalThis.maskEditor = canvas ? new MaskEditorAPI(canvas) : null;
}
const dispatch = createEventDispatcher();
function onChange() {
dispatch("change", { canvas });
}
function onImageLoaded({ path, noteId }: ImageLoadedEvent) {
dispatch("image-loaded", { path, noteId });
}
$: $changeSignal, onChange();
function init(node) {
instance = panzoom(node, {
bounds: true,
maxZoom: 3,
minZoom: 0.1,
zoomDoubleClickSpeed: 1,
smoothScroll: false,
transformOrigin: { x: 0.5, y: 0.5 },
});
instance.pause();
globalThis.panzoom = instance;
if (mode.kind == "add") {
setupMaskEditor(mode.imagePath, instance, onChange, onImageLoaded).then(
(canvas1) => {
canvas = canvas1;
},
);
} else {
setupMaskEditorForEdit(mode.noteId, instance, onChange, onImageLoaded).then(
(canvas1) => {
canvas = canvas1;
},
);
}
}
onMount(() => {
window.addEventListener("resize", () => {
setCanvasZoomRatio(canvas, instance);
setCenterXForZoom(canvas);
});
});
onDestroy(() => {
window.removeEventListener("resize", () => {
setCanvasZoomRatio(canvas, instance);
setCenterXForZoom(canvas);
});
});
</script>
<Toolbar {canvas} {instance} {iconSize} activeTool={startingTool} />
<div class="editor-main" bind:clientWidth={innerWidth}>
<div class="editor-container" use:init>
<!-- svelte-ignore a11y-missing-attribute -->
<img id="image" />
<canvas id="canvas" />
</div>
</div>
<style lang="scss">
.editor-main {
position: absolute;
top: 42px;
left: 36px;
bottom: 2px;
right: 2px;
border: 1px solid var(--border);
overflow: auto;
padding-bottom: 100px;
}
:global([dir="rtl"]) .editor-main {
left: 2px;
right: 36px;
}
.editor-container {
width: 100%;
height: 100%;
position: relative;
direction: ltr;
}
#image {
position: absolute;
}
:global(.upper-canvas) {
border: 0.5px solid var(--border-strong);
border-width: thin;
}
:global(.canvas-container) {
position: absolute;
}
</style>