7f6c410ca5
* Store coordinates as ratios of full size * Use single definition for cappedCanvasSize() * Move I/O review code into ts/image-occlusion A bit simpler when it's all in one place. * Reduce number precision, and round to whole pixels >>> n=10000 >>> for i in range(1, int(n)): assert i == round(float("%0.4f" % (i/n))*n) * Minor typing tweak So, it turns out that typing is mostly broken in ts/image-occlusion. We're importing from fabric which is a js file without types, so types like fabric.Canvas are resolving to any. I first tried switching to `@types/fabric`, which introduced a slew of typing errors. Wasted a few hours trying to address them, before deciding to give up on it, since the types were not complete. Then found fabric has a 6.0 beta that introduces typing, and spent some time with that, but ran into some new issues as it still seems to be a work in progress. I think we're probably best off waiting until it's out and stabilized before sinking more effort into this. * Refactor (de)serialization of occlusions To make the code easier to follow/maintain, cloze deletions are now decoded/ encoded into simple data classes, which can then be converted to Fabric objects and back. The data objects handle converting from absolute/normal positions, and producing values suitable for writing to text (eg truncated floats). Various other changes: - Polygon points are now stored as 'x,y x2,y2 ...' instead of JSON in cloze divs, as that makes the handling consistent with reading from cloze deletion text. - Fixed the reviewer not showing updated placement when a polygon was moved. - Disabled rotation controls in the editor, since we don't support rotation during review. - Renamed hideInactive to occludeInactive, as it wasn't clear whether the former meant to hide the occlusions, or keep them (hiding the content). It's stored as 'oi=1' in the cloze text. * Increase canvas size limit, and double pixels when required. * Size canvas based on container size This results in sharper masks when the intrinsic image size is smaller than the container, and more legible ones when the container is smaller than the intrinsic image size. By using the container instead of the viewport, we account for margins, and when the pixel ratio is 1x, the canvas size and container size should match. * Disable zoom animation on editor load * Default to rectangle when adding new occlusions * Allow users to add/update notes directly from mask editing page * The mask editor needs to work with css pixels, not actual pixels The canvas and image were being scaled too large, which impacted performance.
133 lines
4.7 KiB
TypeScript
133 lines
4.7 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import * as tr from "@tslib/ftl";
|
|
import type { ImageOcclusion } from "@tslib/proto";
|
|
import { fabric } from "fabric";
|
|
import type { PanZoom } from "panzoom";
|
|
import protobuf from "protobufjs";
|
|
import { get } from "svelte/store";
|
|
|
|
import { optimumCssSizeForCanvas } from "./canvas-scale";
|
|
import { getImageForOcclusion, getImageOcclusionNote } from "./lib";
|
|
import { notesDataStore, tagsWritable, zoomResetValue } from "./store";
|
|
import Toast from "./Toast.svelte";
|
|
import { addShapesToCanvasFromCloze } from "./tools/add-from-cloze";
|
|
import { enableSelectable, moveShapeToCanvasBoundaries } from "./tools/lib";
|
|
import { undoRedoInit } from "./tools/tool-undo-redo";
|
|
import type { Size } from "./types";
|
|
|
|
export const setupMaskEditor = async (path: string, instance: PanZoom): Promise<fabric.Canvas> => {
|
|
const imageData = await getImageForOcclusion(path!);
|
|
const canvas = initCanvas();
|
|
|
|
// get image width and height
|
|
const image = document.getElementById("image") as HTMLImageElement;
|
|
image.src = getImageData(imageData.data!);
|
|
image.onload = function() {
|
|
const size = optimumCssSizeForCanvas({ width: image.width, height: image.height }, containerSize());
|
|
canvas.setWidth(size.width);
|
|
canvas.setHeight(size.height);
|
|
image.height = size.height;
|
|
image.width = size.width;
|
|
setCanvasZoomRatio(canvas, instance);
|
|
};
|
|
|
|
return canvas;
|
|
};
|
|
|
|
export const setupMaskEditorForEdit = async (noteId: number, instance: PanZoom): Promise<fabric.Canvas> => {
|
|
const clozeNoteResponse: ImageOcclusion.GetImageOcclusionNoteResponse = await getImageOcclusionNote(noteId);
|
|
if (clozeNoteResponse.error) {
|
|
new Toast({
|
|
target: document.body,
|
|
props: {
|
|
message: tr.notetypesErrorGettingImagecloze(),
|
|
type: "error",
|
|
},
|
|
}).$set({ showToast: true });
|
|
return;
|
|
}
|
|
|
|
const clozeNote = clozeNoteResponse.note!;
|
|
const canvas = initCanvas();
|
|
|
|
// get image width and height
|
|
const image = document.getElementById("image") as HTMLImageElement;
|
|
image.src = getImageData(clozeNote.imageData!);
|
|
image.onload = function() {
|
|
const size = optimumCssSizeForCanvas({ width: image.width, height: image.height }, containerSize());
|
|
canvas.setWidth(size.width);
|
|
canvas.setHeight(size.height);
|
|
image.height = size.height;
|
|
image.width = size.width;
|
|
|
|
setCanvasZoomRatio(canvas, instance);
|
|
addShapesToCanvasFromCloze(canvas, clozeNote.occlusions);
|
|
enableSelectable(canvas, true);
|
|
addClozeNotesToTextEditor(clozeNote.header, clozeNote.backExtra, clozeNote.tags);
|
|
};
|
|
|
|
return canvas;
|
|
};
|
|
|
|
const initCanvas = (): fabric.Canvas => {
|
|
const canvas = new fabric.Canvas("canvas");
|
|
tagsWritable.set([]);
|
|
globalThis.canvas = canvas;
|
|
// enables uniform scaling by default without the need for the Shift key
|
|
canvas.uniformScaling = false;
|
|
canvas.uniScaleKey = "none";
|
|
moveShapeToCanvasBoundaries(canvas);
|
|
undoRedoInit(canvas);
|
|
return canvas;
|
|
};
|
|
|
|
const getImageData = (imageData): string => {
|
|
const b64encoded = protobuf.util.base64.encode(
|
|
imageData,
|
|
0,
|
|
imageData.length,
|
|
);
|
|
return "data:image/png;base64," + b64encoded;
|
|
};
|
|
|
|
const setCanvasZoomRatio = (
|
|
canvas: fabric.Canvas,
|
|
instance: PanZoom,
|
|
): void => {
|
|
const zoomRatioW = (innerWidth - 40) / canvas.width!;
|
|
const zoomRatioH = (innerHeight - 100) / canvas.height!;
|
|
const zoomRatio = zoomRatioW < zoomRatioH ? zoomRatioW : zoomRatioH;
|
|
zoomResetValue.set(zoomRatio);
|
|
instance.zoomAbs(0, 0, zoomRatio);
|
|
};
|
|
|
|
const addClozeNotesToTextEditor = (header: string, backExtra: string, tags: string[]) => {
|
|
const noteFieldsData: { id: string; title: string; divValue: string; textareaValue: string }[] = get(
|
|
notesDataStore,
|
|
);
|
|
noteFieldsData[0].divValue = header;
|
|
noteFieldsData[1].divValue = backExtra;
|
|
noteFieldsData[0].textareaValue = header;
|
|
noteFieldsData[1].textareaValue = backExtra;
|
|
tagsWritable.set(tags);
|
|
|
|
noteFieldsData.forEach((note) => {
|
|
const divId = `${note.id}--div`;
|
|
const textAreaId = `${note.id}--textarea`;
|
|
const divElement = document.getElementById(divId)!;
|
|
const textAreaElement = document.getElementById(textAreaId)! as HTMLTextAreaElement;
|
|
divElement.innerHTML = note.divValue;
|
|
textAreaElement.value = note.textareaValue;
|
|
});
|
|
};
|
|
|
|
function containerSize(): Size {
|
|
const container = document.querySelector(".editor-main")!;
|
|
return {
|
|
width: container.clientWidth,
|
|
height: container.clientHeight,
|
|
};
|
|
}
|