anki/ts/image-occlusion/shapes/to-cloze.ts

162 lines
4.8 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 type { Canvas, Object as FabricObject } from "fabric";
import { fabric } from "fabric";
import { cloneDeep } from "lodash-es";
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
import type { Size } from "../types";
import type { Shape, ShapeOrShapes } from "./base";
import { Ellipse } from "./ellipse";
import { Polygon } from "./polygon";
import { Rectangle } from "./rectangle";
import { Text } from "./text";
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
export function exportShapesToClozeDeletions(occludeInactive: boolean): {
clozes: string;
noteCount: number;
} {
const shapes = baseShapesFromFabric();
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
let clozes = "";
let index = 0;
shapes.forEach((shapeOrShapes) => {
clozes += shapeOrShapesToCloze(shapeOrShapes, index, occludeInactive);
if (!(shapeOrShapes instanceof Text)) {
index++;
}
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
});
2023-12-10 03:55:47 +01:00
return { clozes, noteCount: index };
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
}
/** Gather all Fabric shapes, and convert them into BaseShapes or
* BaseShape[]s.
*/
export function baseShapesFromFabric(): ShapeOrShapes[] {
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
const canvas = globalThis.canvas as Canvas;
const activeObject = canvas.getActiveObject();
const selectionContainingMultipleObjects = activeObject instanceof fabric.ActiveSelection
&& (activeObject.size() > 1)
? activeObject
: null;
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
const objects = canvas.getObjects() as FabricObject[];
return objects
.map((object) => {
// If the object is in the active selection containing multiple objects,
// we need to calculate its x and y coordinates relative to the canvas.
const parent = selectionContainingMultipleObjects?.contains(object)
? selectionContainingMultipleObjects
: undefined;
return fabricObjectToBaseShapeOrShapes(
canvas,
object,
parent,
);
})
.filter((o): o is ShapeOrShapes => o !== null);
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
}
/** Convert a single Fabric object/group to one or more BaseShapes. */
function fabricObjectToBaseShapeOrShapes(
size: Size,
object: FabricObject,
parentObject?: FabricObject,
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
): ShapeOrShapes | null {
let shape: Shape;
// Prevents the original fabric object from mutating when a non-primitive
// property of a Shape mutates.
const cloned = cloneDeep(object);
if (parentObject) {
const scaling = parentObject.getObjectScaling();
cloned.width = cloned.width * scaling.scaleX;
cloned.height = cloned.height * scaling.scaleY;
}
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
switch (object.type) {
case "rect":
shape = new Rectangle(cloned);
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
break;
case "ellipse":
shape = new Ellipse(cloned);
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
break;
case "polygon":
shape = new Polygon(cloned);
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
break;
case "i-text":
shape = new Text(cloned);
break;
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
case "group":
return object._objects.map((child) => {
return fabricObjectToBaseShapeOrShapes(
size,
child,
object,
);
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
});
default:
return null;
}
if (parentObject) {
const newPosition = fabric.util.transformPoint(
{ x: shape.left, y: shape.top },
parentObject.calcTransformMatrix(),
);
shape.left = newPosition.x;
shape.top = newPosition.y;
}
shape = shape.toNormal(size);
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
return shape;
}
/** generate cloze data in form of
{{c1::image-occlusion:rect:top=.1:left=.23:width=.4:height=.5}} */
function shapeOrShapesToCloze(
shapeOrShapes: ShapeOrShapes,
index: number,
occludeInactive: boolean,
): string {
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
let text = "";
function addKeyValue(key: string, value: string) {
value = value.replace(":", "\\:");
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
text += `:${key}=${value}`;
}
let type: string;
if (Array.isArray(shapeOrShapes)) {
return shapeOrShapes
.map((shape) => shapeOrShapesToCloze(shape, index, occludeInactive))
.join("");
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
} else if (shapeOrShapes instanceof Rectangle) {
type = "rect";
} else if (shapeOrShapes instanceof Ellipse) {
type = "ellipse";
} else if (shapeOrShapes instanceof Polygon) {
type = "polygon";
} else if (shapeOrShapes instanceof Text) {
type = "text";
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
} else {
throw new Error("Unknown shape type");
}
for (const [key, value] of Object.entries(shapeOrShapes.toDataForCloze())) {
addKeyValue(key, value);
}
if (occludeInactive) {
addKeyValue("oi", "1");
}
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
let ordinal: number;
if (type === "text") {
ordinal = 0;
} else {
ordinal = index + 1;
}
shapeOrShapes.ordinal = ordinal;
text = `{{c${ordinal}::image-occlusion:${type}${text}}}<br>`;
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
return text;
}