anki/ts/image-occlusion/shapes/base.ts

70 lines
2.1 KiB
TypeScript
Raw Normal View History

Various changes to I/O handling (#2513) * 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.
2023-05-31 05:45:12 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { fabric } from "fabric";
import { SHAPE_MASK_COLOR } from "../tools/lib";
import type { ConstructorParams, Size } from "../types";
import { floatToDisplay } from "./floats";
import { xFromNormalized, xToNormalized, yFromNormalized, yToNormalized } from "./position";
export type ShapeOrShapes = Shape | Shape[];
/** Defines a basic shape that can have its coordinates stored in either
absolute pixels (relative to a containing canvas), or in normalized 0-1
form. Can be converted to a fabric object, or to a format suitable for
storage in a cloze note.
*/
export class Shape {
left: number;
top: number;
fill: string = SHAPE_MASK_COLOR;
/** Whether occlusions from other cloze numbers should be shown on the
* question side.
*/
occludeInactive = false;
constructor(
{ left = 0, top = 0, fill = SHAPE_MASK_COLOR, occludeInactive = false }: ConstructorParams<Shape> = {},
) {
this.left = left;
this.top = top;
this.fill = fill;
this.occludeInactive = occludeInactive;
}
/** Format numbers and remove default values, for easier serialization to
* text.
*/
toDataForCloze(): ShapeDataForCloze {
return {
left: floatToDisplay(this.left),
top: floatToDisplay(this.top),
...(this.fill === SHAPE_MASK_COLOR ? {} : { fill: this.fill }),
...(this.occludeInactive ? { oi: "1" } : {}),
};
}
toFabric(size: Size): fabric.ForCloze {
this.makeAbsolute(size);
return new fabric.Object(this);
}
makeNormal(size: Size): void {
this.left = xToNormalized(size, this.left);
this.top = yToNormalized(size, this.top);
}
makeAbsolute(size: Size): void {
this.left = xFromNormalized(size, this.left);
this.top = yFromNormalized(size, this.top);
}
}
export interface ShapeDataForCloze {
left: string;
top: string;
fill?: string;
oi?: string;
}