c828a2eb6f
* Refactor: Add index to shapes package * Add shape draw callback API to setupImageCloze * Expose IO drawing API, switch away from image cloze naming We currently use "image occlusion" in most places, but some references to "image cloze" still remain. For consistency's sake and to make it easier to quickly find IO-related code, this commit replaces all remaining references to "image cloze", only maintaining those required for backwards compatibility with existing note types. * Add cloze ordinal to shapes * Do not mutate original shapes during (de)normalization Mutating shapes would be a recipe for trouble when combined with IO API use by external consumers. (makeNormal(makeAbsolute(makeNormal())) is not idempotent, and keeping track of the original state would introduce additional complexity with no discernible performance benefit or otherwise.) * Tweak IO API, allowing modifications to ShapeProperties * Tweak drawShape parameters * Switch method order For consistency with previous implementation * Run Rust formatters * Simplify position (de)normalization --------- Co-authored-by: Glutanimate <glutanimate@users.noreply.github.com>
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
// 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 type { ConstructorParams, Size } from "../types";
|
|
import type { ShapeDataForCloze } from "./base";
|
|
import { Shape } from "./base";
|
|
import { floatToDisplay } from "./floats";
|
|
import { xFromNormalized, xToNormalized, yFromNormalized, yToNormalized } from "./position";
|
|
|
|
export class Polygon extends Shape {
|
|
points: Point[];
|
|
|
|
constructor({ points = [], ...rest }: ConstructorParams<Polygon> = {}) {
|
|
super(rest);
|
|
this.points = points;
|
|
}
|
|
|
|
toDataForCloze(): PolygonDataForCloze {
|
|
return {
|
|
...super.toDataForCloze(),
|
|
points: this.points.map(({ x, y }) => `${floatToDisplay(x)},${floatToDisplay(y)}`).join(" "),
|
|
};
|
|
}
|
|
|
|
toFabric(size: Size): fabric.Polygon {
|
|
const absolute = this.toAbsolute(size);
|
|
return new fabric.Polygon(absolute.points, absolute);
|
|
}
|
|
|
|
toNormal(size: Size): Polygon {
|
|
const points: Point[] = [];
|
|
this.points.forEach((p) => {
|
|
points.push({
|
|
x: xToNormalized(size, p.x),
|
|
y: yToNormalized(size, p.y),
|
|
});
|
|
});
|
|
return new Polygon({
|
|
...this,
|
|
...super.normalPosition(size),
|
|
points,
|
|
});
|
|
}
|
|
|
|
toAbsolute(size: Size): Polygon {
|
|
const points: Point[] = [];
|
|
this.points.forEach((p) => {
|
|
points.push({
|
|
x: xFromNormalized(size, p.x),
|
|
y: yFromNormalized(size, p.y),
|
|
});
|
|
});
|
|
return new Polygon({
|
|
...this,
|
|
...super.absolutePosition(size),
|
|
points,
|
|
});
|
|
}
|
|
}
|
|
|
|
interface PolygonDataForCloze extends ShapeDataForCloze {
|
|
// "x1,y1 x2,y2 ...""
|
|
points: string;
|
|
}
|
|
|
|
export class Point {
|
|
x = 0;
|
|
y = 0;
|
|
|
|
constructor({ x = 0, y = 0 }: ConstructorParams<Point> = {}) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
}
|