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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
import type { MaskDefinition, RectangleMaskParams } from "@/lib/masks/types";
import {
BOX_LIKE_MASK_PARAMS,
buildBoxMaskInteraction,
computeBoxMaskParamUpdate,
getBoxLikeGeometry,
getDefaultSquareMaskParams,
getStrokeOffset,
rotatePoint,
} from "./box-like";
const STAR_INNER_RADIUS_RATIO = 0.45;
const STAR_VERTEX_COUNT = 10;
function buildStarPath({
centerX,
centerY,
halfWidth,
halfHeight,
rotationRad,
}: {
centerX: number;
centerY: number;
halfWidth: number;
halfHeight: number;
rotationRad: number;
}): Path2D {
const path = new Path2D();
for (let index = 0; index < STAR_VERTEX_COUNT; index++) {
const isOuterVertex = index % 2 === 0;
const radiusX = isOuterVertex
? halfWidth
: halfWidth * STAR_INNER_RADIUS_RATIO;
const radiusY = isOuterVertex
? halfHeight
: halfHeight * STAR_INNER_RADIUS_RATIO;
const angle = (index * Math.PI) / 5 - Math.PI / 2;
const point = rotatePoint({
x: centerX + radiusX * Math.cos(angle),
y: centerY + radiusY * Math.sin(angle),
centerX,
centerY,
rotationRad,
});
if (index === 0) {
path.moveTo(point.x, point.y);
} else {
path.lineTo(point.x, point.y);
}
}
path.closePath();
return path;
}
function buildOverlayStarPath({
width,
height,
}: {
width: number;
height: number;
}): string {
const centerX = width / 2;
const centerY = height / 2;
const halfWidth = width / 2;
const halfHeight = height / 2;
const segments: string[] = [];
for (let index = 0; index < STAR_VERTEX_COUNT; index++) {
const isOuterVertex = index % 2 === 0;
const radiusX = isOuterVertex
? halfWidth
: halfWidth * STAR_INNER_RADIUS_RATIO;
const radiusY = isOuterVertex
? halfHeight
: halfHeight * STAR_INNER_RADIUS_RATIO;
const angle = (index * Math.PI) / 5 - Math.PI / 2;
const x = centerX + radiusX * Math.cos(angle);
const y = centerY + radiusY * Math.sin(angle);
segments.push(`${index === 0 ? "M" : "L"} ${x},${y}`);
}
return `${segments.join(" ")} Z`;
}
export const starMaskDefinition: MaskDefinition<RectangleMaskParams> = {
type: "star",
name: "Star",
features: {
hasPosition: true,
hasRotation: true,
sizeMode: "width-height",
},
params: BOX_LIKE_MASK_PARAMS,
interaction: buildBoxMaskInteraction({
sizeMode: "width-height",
buildOverlayPath({ width, height }) {
return buildOverlayStarPath({ width, height });
},
}),
buildDefault(context) {
return {
type: "star",
params: getDefaultSquareMaskParams(context),
};
},
computeParamUpdate: computeBoxMaskParamUpdate,
renderer: {
buildPath({ resolvedParams, width, height }) {
const params = resolvedParams as RectangleMaskParams;
const { centerX, centerY, maskWidth, maskHeight, rotationRad } =
getBoxLikeGeometry({ params, width, height });
return buildStarPath({
centerX,
centerY,
halfWidth: maskWidth / 2,
halfHeight: maskHeight / 2,
rotationRad,
});
},
buildStrokePath({ resolvedParams, width, height }) {
const params = resolvedParams as RectangleMaskParams;
const { centerX, centerY, maskWidth, maskHeight, rotationRad } =
getBoxLikeGeometry({ params, width, height });
const offset = getStrokeOffset({
strokeAlign: params.strokeAlign,
strokeWidth: params.strokeWidth,
});
return buildStarPath({
centerX,
centerY,
halfWidth: Math.max(maskWidth / 2 + offset, 1),
halfHeight: Math.max(maskHeight / 2 + offset, 1),
rotationRad,
});
},
},
};
|