diff options
| author | Maze Winther <mazewinther@gmail.com> | 2026-04-29 18:41:15 +0200 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2026-05-02 03:27:23 +0200 |
| commit | 79dcec8a2ec2f5cad9be95e5b52fbfa752fb6ac4 (patch) | |
| tree | 99ff9393c4a550669ebac921c6a0535437a39413 /apps/web/src/masks/builtin/definitions/star.ts | |
| parent | 97f8d1968f186028e41f83b04c0897130e6c69f4 (diff) | |
| download | opencut-79dcec8a2ec2f5cad9be95e5b52fbfa752fb6ac4.tar.gz | |
refactor: split builtin and freeform masks
Made-with: Cursor
Diffstat (limited to 'apps/web/src/masks/builtin/definitions/star.ts')
| -rw-r--r-- | apps/web/src/masks/builtin/definitions/star.ts | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/apps/web/src/masks/builtin/definitions/star.ts b/apps/web/src/masks/builtin/definitions/star.ts new file mode 100644 index 00000000..62e9cbc4 --- /dev/null +++ b/apps/web/src/masks/builtin/definitions/star.ts @@ -0,0 +1,146 @@ +import type { MaskDefinition } from "@/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<"star"> = { + 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: { + body: { + kind: "fillPath", + buildPath({ resolvedParams, width, height }) { + const params = resolvedParams; + const { centerX, centerY, maskWidth, maskHeight, rotationRad } = + getBoxLikeGeometry({ params, width, height }); + return buildStarPath({ + centerX, + centerY, + halfWidth: maskWidth / 2, + halfHeight: maskHeight / 2, + rotationRad, + }); + }, + }, + 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, + }); + return buildStarPath({ + centerX, + centerY, + halfWidth: Math.max(maskWidth / 2 + offset, 1), + halfHeight: Math.max(maskHeight / 2 + offset, 1), + rotationRad, + }); + }, + }, + }, +}; |
