summaryrefslogtreecommitdiff
path: root/apps/web/src/masks/definitions/ellipse.ts
blob: f57d7444377f9729d3932656f8a697c7b1f5c14c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { MaskDefinition } from "@/masks/types";
import {
	BOX_LIKE_MASK_PARAMS,
	buildBoxMaskInteraction,
	computeBoxMaskParamUpdate,
	getBoxLikeGeometry,
	getDefaultSquareMaskParams,
	getStrokeOffset,
} from "./box-like";

export const ellipseMaskDefinition: MaskDefinition<"ellipse"> = {
	type: "ellipse",
	name: "Ellipse",
	features: {
		hasPosition: true,
		hasRotation: true,
		sizeMode: "width-height",
	},
	params: BOX_LIKE_MASK_PARAMS,
	interaction: buildBoxMaskInteraction({
		sizeMode: "width-height",
		buildOverlayPath({ width, height }) {
			const rx = Math.max((width - 1) / 2, 0);
			const ry = Math.max((height - 1) / 2, 0);
			const cx = width / 2;
			const cy = height / 2;
			return `M ${cx},${cy - ry} A ${rx},${ry} 0 1,1 ${cx},${cy + ry} A ${rx},${ry} 0 1,1 ${cx},${cy - ry} Z`;
		},
	}),
	buildDefault(context) {
		return {
			type: "ellipse",
			params: getDefaultSquareMaskParams(context),
		};
	},
	computeParamUpdate: computeBoxMaskParamUpdate,
	renderer: {
		body: {
			kind: "fillPath",
			buildPath({ resolvedParams, width, height }) {
				const params = resolvedParams;
				const { centerX, centerY, maskWidth, maskHeight, rotationRad } =
					getBoxLikeGeometry({ params, width, height });
				const path = new Path2D();
				path.ellipse(
					centerX,
					centerY,
					maskWidth / 2,
					maskHeight / 2,
					rotationRad,
					0,
					Math.PI * 2,
				);
				return path;
			},
		},
		stroke: {
			kind: "strokeFromPath",
			buildStrokePath({ resolvedParams, width, height }) {
				const params = resolvedParams;
				const { centerX, centerY, maskWidth, maskHeight, rotationRad } =
					getBoxLikeGeometry({ params, width, height });
				const offset = getStrokeOffset({
					strokeAlign: params.strokeAlign,
					strokeWidth: params.strokeWidth,
				});
				const path = new Path2D();
				path.ellipse(
					centerX,
					centerY,
					Math.max(1, maskWidth / 2 + offset),
					Math.max(1, maskHeight / 2 + offset),
					rotationRad,
					0,
					Math.PI * 2,
				);
				return path;
			},
		},
	},
};