Skip to content

Open source CapCut alternative.

TypeScript

feat(site): add triangle, diamond, star, heart and hexagon shapes [rebuild]

David Led32e634335d3
diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.ts
index 0543ef9e..fa0a6211 100644
--- a/site/packages/effects-kit/src/render.ts
+++ b/site/packages/effects-kit/src/render.ts
@@ -15,7 +15,15 @@ import {
import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys } from "./keys";
import type { LottieHandle } from "./lottie";
import { elementPlugin } from "./registry";
-import type { Overlay, ShapeOverlay, StickerOverlay, TextOverlay, TextShadowSpec } from "./types";
+import {
+ lineLikeShape,
+ type Overlay,
+ type ShapeKind,
+ type ShapeOverlay,
+ type StickerOverlay,
+ type TextOverlay,
+ type TextShadowSpec,
+} from "./types";
// Text metrics shared by DOM preview and canvas burn-in.
export const LINE_HEIGHT = 1.25;
@@ -121,16 +129,115 @@ export function shapeMetrics(o: ShapeOverlay, frame: PaintFrame) {
};
}
+/** A path consumer — a canvas context is one, and `shapePathD` builds SVG
+ * path data through the same calls, so both draw identical geometry. */
+export interface ShapePathSink {
+ moveTo(x: number, y: number): void;
+ lineTo(x: number, y: number): void;
+ bezierCurveTo(c1x: number, c1y: number, c2x: number, c2y: number, x: number, y: number): void;
+ closePath(): void;
+}
+
+/** Trace a polygon shape's outline into a w×h box whose top-left corner sits
+ * at (dx, dy). Covers the filled kinds beyond rect and ellipse. */
+export function tracePolyShape(
+ p: ShapePathSink,
+ kind: ShapeKind,
+ w: number,
+ h: number,
+ dx = 0,
+ dy = 0
+) {
+ const X = (f: number) => dx + f * w;
+ const Y = (f: number) => dy + f * h;
+ if (kind === "triangle") {
+ p.moveTo(X(0.5), Y(0));
+ p.lineTo(X(1), Y(1));
+ p.lineTo(X(0), Y(1));
+ } else if (kind === "diamond") {
+ p.moveTo(X(0.5), Y(0));
+ p.lineTo(X(1), Y(0.5));
+ p.lineTo(X(0.5), Y(1));
+ p.lineTo(X(0), Y(0.5));
+ } else if (kind === "hexagon") {
+ // Lucide's hexagon stands on a point: vertices top and bottom, straight
+ // vertical sides.
+ p.moveTo(X(0.5), Y(0));
+ p.lineTo(X(1), Y(0.25));
+ p.lineTo(X(1), Y(0.75));
+ p.lineTo(X(0.5), Y(1));
+ p.lineTo(X(0), Y(0.75));
+ p.lineTo(X(0), Y(0.25));
+ } else if (kind === "star") {
+ // Lucide's star: the inner ring at half the outer radius so the points
+ // read full-bodied, and rounded corners — a soft round in each inner
+ // notch, a slight one at each tip. Each corner is cut short along its
+ // two edges and bridged by a cubic whose controls sit on the vertex.
+ const pts: [number, number][] = [];
+ for (let i = 0; i < 10; i++) {
+ const r = i % 2 ? 0.5 : 1;
+ const a = -Math.PI / 2 + (i * Math.PI) / 5;
+ pts.push([0.5 + 0.5 * r * Math.cos(a), 0.5 + 0.5 * r * Math.sin(a)]);
+ }
+ for (let i = 0; i < 10; i++) {
+ const [px, py] = pts[(i + 9) % 10];
+ const [cx, cy] = pts[i];
+ const [nx, ny] = pts[(i + 1) % 10];
+ const cut = i % 2 ? 0.35 : 0.15;
+ const ex = cx + (px - cx) * cut;
+ const ey = cy + (py - cy) * cut;
+ const lx = cx + (nx - cx) * cut;
+ const ly = cy + (ny - cy) * cut;
+ if (i === 0) p.moveTo(X(ex), Y(ey));
+ else p.lineTo(X(ex), Y(ey));
+ p.bezierCurveTo(X(cx), Y(cy), X(cx), Y(cy), X(lx), Y(ly));
+ }
+ } else if (kind === "heart") {
+ // Lucide's heart, normalized to the unit box (its arcs as cubics):
+ // straight sides down to the tip, round quarter-circle lobes on top.
+ p.moveTo(X(0.5), Y(1));
+ p.lineTo(X(0.15), Y(0.611));
+ p.bezierCurveTo(X(0.075), Y(0.531), X(0), Y(0.433), X(0), Y(0.306));
+ p.bezierCurveTo(X(0), Y(0.137), X(0.123), Y(0), X(0.275), Y(0));
+ p.bezierCurveTo(X(0.363), Y(0), X(0.425), Y(0.028), X(0.5), Y(0.111));
+ p.bezierCurveTo(X(0.575), Y(0.028), X(0.637), Y(0), X(0.725), Y(0));
+ p.bezierCurveTo(X(0.877), Y(0), X(1), Y(0.137), X(1), Y(0.306));
+ p.bezierCurveTo(X(1), Y(0.433), X(0.925), Y(0.531), X(0.85), Y(0.611));
+ }
+ p.closePath();
+}
+
+/** A polygon shape as SVG path data for a w×h box, for the DOM preview. */
+export function shapePathD(kind: ShapeKind, w: number, h: number): string {
+ const parts: string[] = [];
+ const n = (v: number) => String(+v.toFixed(2));
+ tracePolyShape(
+ {
+ moveTo: (x, y) => parts.push(`M${n(x)} ${n(y)}`),
+ lineTo: (x, y) => parts.push(`L${n(x)} ${n(y)}`),
+ bezierCurveTo: (c1x, c1y, c2x, c2y, x, y) =>
+ parts.push(`C${n(c1x)} ${n(c1y)} ${n(c2x)} ${n(c2y)} ${n(x)} ${n(y)}`),
+ closePath: () => parts.push("Z"),
+ },
+ kind,
+ w,
+ h
+ );
+ return parts.join(" ");
+}
+
function paintShape(ctx: CanvasRenderingContext2D, o: ShapeOverlay, frame: PaintFrame) {
const m = shapeMetrics(o, frame);
const alpha = ctx.globalAlpha;
ctx.fillStyle = o.fill;
- if (o.shape === "rect" || o.shape === "ellipse") {
+ if (!lineLikeShape(o.shape)) {
ctx.beginPath();
if (o.shape === "rect") {
ctx.roundRect(m.cx - m.w / 2, m.cy - m.h / 2, m.w, m.h, m.radius);
- } else {
+ } else if (o.shape === "ellipse") {
ctx.ellipse(m.cx, m.cy, m.w / 2, m.h / 2, 0, 0, Math.PI * 2);
+ } else {
+ tracePolyShape(ctx, o.shape, m.w, m.h, m.cx - m.w / 2, m.cy - m.h / 2);
}
ctx.globalAlpha = alpha * (o.fillOpacity ?? 1);
ctx.fill();
@@ -138,6 +245,7 @@ function paintShape(ctx: CanvasRenderingContext2D, o: ShapeOverlay, frame: Paint
if (o.stroke && m.strokeWidth > 0) {
ctx.strokeStyle = o.stroke.color;
ctx.lineWidth = m.strokeWidth;
+ ctx.lineJoin = "round";
ctx.stroke();
}
return;
@@ -388,7 +496,7 @@ export async function measureElementBounds(
if (kind === "shape") {
const s = overlay as ShapeOverlay;
const m = shapeMetrics(s, frame);
- const lineLike = s.shape === "line" || s.shape === "arrow";
+ const lineLike = lineLikeShape(s.shape);
return {
cx,
cy,
diff --git a/site/packages/effects-kit/src/types.ts b/site/packages/effects-kit/src/types.ts
index 8fd1c9d0..5b6bc81c 100644
--- a/site/packages/effects-kit/src/types.ts
+++ b/site/packages/effects-kit/src/types.ts
@@ -90,7 +90,23 @@ export interface TextOverlay extends OverlayBase {
highlightText?: string;
}
-export type ShapeKind = "rect" | "ellipse" | "line" | "arrow";
+export type ShapeKind =
+ | "rect"
+ | "ellipse"
+ | "triangle"
+ | "diamond"
+ | "star"
+ | "heart"
+ | "hexagon"
+ | "line"
+ | "arrow";
+
+/** Shapes drawn as a stroke across the element box: `h` is the stroke
+ * thickness and rotation gives them their direction. Every other kind is a
+ * filled outline with the box as its bounds. */
+export function lineLikeShape(k: ShapeKind): boolean {
+ return k === "line" || k === "arrow";
+}
export interface ShapeOverlay extends OverlayBase {
kind: "shape";
diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsx
index 813064e0..2b1dfc71 100644
--- a/site/src/cut/components/Inspector.tsx
+++ b/site/src/cut/components/Inspector.tsx
@@ -25,6 +25,7 @@ import {
hasOverlayKeys,
KEY_EPSILON,
keyIndexAt,
+ lineLikeShape,
poseAt,
type OverlayKey,
OVERLAY_ANIM_DEFAULT_SECONDS,
@@ -2013,7 +2014,7 @@ function ShapePanel({ overlay: o }: { overlay: ShapeOverlay }) {
const fillCk = useSliderCheckpoint();
const radiusCk = useSliderCheckpoint();
const strokeCk = useSliderCheckpoint();
- const boxShape = o.shape === "rect" || o.shape === "ellipse";
+ const boxShape = !lineLikeShape(o.shape);
return (
<>
<PanelTitle>{SHAPE_LABELS[o.shape]}</PanelTitle>
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index c8df19d2..fa9a6edf 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -14,7 +14,7 @@ import {
subtitleLaneCount,
trackPos,
} from "@/cut/lib/subtitles";
-import { evalOverlayFrame, hasOverlayKeys, isOverlayAnimated, resolveShadow, shapeMetrics, type LottieHandle } from "@donkeycut/effects-kit";
+import { evalOverlayFrame, hasOverlayKeys, isOverlayAnimated, lineLikeShape, resolveShadow, shapeMetrics, shapePathD, type LottieHandle } from "@donkeycut/effects-kit";
import {
LINE_HEIGHT,
PLATE_PAD_X,
@@ -809,7 +809,7 @@ function ShapeView({
scale: number;
}) {
const m = shapeMetrics(o, { width: stageWidth, height: stageHeight, scale });
- if (o.shape === "line" || o.shape === "arrow") {
+ if (lineLikeShape(o.shape)) {
const h = Math.max(m.thickness, m.headHalf * 2);
const mid = h / 2;
return (
@@ -851,7 +851,7 @@ function ShapeView({
stroke={o.stroke?.color}
strokeWidth={m.strokeWidth || undefined}
/>
- ) : (
+ ) : o.shape === "ellipse" ? (
<ellipse
cx={m.w / 2}
cy={m.h / 2}
@@ -862,6 +862,15 @@ function ShapeView({
stroke={o.stroke?.color}
strokeWidth={m.strokeWidth || undefined}
/>
+ ) : (
+ <path
+ d={shapePathD(o.shape, m.w, m.h)}
+ fill={o.fill}
+ fillOpacity={o.fillOpacity ?? 1}
+ stroke={o.stroke?.color}
+ strokeWidth={m.strokeWidth || undefined}
+ strokeLinejoin="round"
+ />
)}
</svg>
);
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index d9942c5a..e0f2e346 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -4,6 +4,7 @@ import {
groupRemap,
KEY_EPSILON,
keyAt,
+ lineLikeShape,
removeKeyAt,
upsertKey,
type EffectId,
@@ -1819,7 +1820,7 @@ export const useEditor = create<EditorState>((baseSet, get) => {
const frame = frameOf(get().aspect);
// A square-reading default box whatever the aspect; lines/arrows are a
// wider box whose height is the stroke thickness.
- const w = shape === "line" || shape === "arrow" ? 0.42 : 0.3;
+ const w = lineLikeShape(shape) ? 0.42 : 0.3;
const h =
shape === "line"
? 6 / frame.h
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index 1bf370d7..05fd54d7 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -712,6 +712,11 @@ export const isEffectOverlay = (o: Overlay): o is EffectOverlay => o.kind === "e
export const SHAPE_LABELS: Record<ShapeKind, string> = {
rect: "Rectangle",
ellipse: "Ellipse",
+ triangle: "Triangle",
+ diamond: "Diamond",
+ star: "Star",
+ heart: "Heart",
+ hexagon: "Hexagon",
line: "Line",
arrow: "Arrow",
};