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.
49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import type { Size } from "./types";
|
|
|
|
/**
|
|
* - Choose an appropriate size for the canvas based on the current container,
|
|
* so the masks are sharp and legible.
|
|
* - Safari doesn't allow canvas elements to be over 16M (4096x4096), so we need
|
|
* to ensure the canvas is smaller than that size.
|
|
* - Returns the size in actual pixels, not CSS size.
|
|
*/
|
|
export function optimumPixelSizeForCanvas(imageSize: Size, containerSize: Size): Size {
|
|
let { width, height } = imageSize;
|
|
|
|
const pixelScale = window.devicePixelRatio;
|
|
containerSize.width *= pixelScale;
|
|
containerSize.height *= pixelScale;
|
|
|
|
// Scale image dimensions to fit in container, retaining aspect ratio.
|
|
// We take the minimum of width/height scales, as that's the one that is
|
|
// potentially limiting the image from expanding.
|
|
const containerScale = Math.min(containerSize.width / imageSize.width, containerSize.height / imageSize.height);
|
|
width *= containerScale;
|
|
height *= containerScale;
|
|
|
|
const maximumPixels = 4096 * 4096;
|
|
const requiredPixels = width * height;
|
|
if (requiredPixels > maximumPixels) {
|
|
const shrinkScale = Math.sqrt(maximumPixels) / Math.sqrt(requiredPixels);
|
|
width *= shrinkScale;
|
|
height *= shrinkScale;
|
|
}
|
|
|
|
return {
|
|
width: Math.floor(width),
|
|
height: Math.floor(height),
|
|
};
|
|
}
|
|
|
|
/** See {@link optimumPixelSizeForCanvas()} */
|
|
export function optimumCssSizeForCanvas(imageSize: Size, containerSize: Size): Size {
|
|
const { width, height } = optimumPixelSizeForCanvas(imageSize, containerSize);
|
|
return {
|
|
width: width / window.devicePixelRatio,
|
|
height: height / window.devicePixelRatio,
|
|
};
|
|
}
|