Open source CapCut alternative.
TypeScript
"use client";
import { Menu as MenuPrimitive } from "@base-ui/react/menu";
import { ChevronDown, Minus, Plus, RotateCcw, X } from "lucide-react";
import { useTranslations } from "next-intl";
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import {
flatSpeedCurve,
retimeOf,
SPEED_CURVE_MAX,
SPEED_CURVE_MIN,
SPEED_CURVE_PRESETS,
speedCurveOf,
speedCurvePresetOf,
type SpeedNode,
} from "@donkeycut/effects-kit";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { PICKED_RING } from "@/cut/lib/assetPick";
import { InfoTip } from "@/cut/components/panelBits";
import { startDrag } from "@/cut/lib/drag";
import {
playheadAt,
setSkim,
skimAt,
subscribePlayhead,
} from "@/cut/lib/playhead";
import { panelState, usePanelStore } from "@/cut/lib/panelState";
import { useEditor } from "@/cut/lib/store";
import type { VideoClip } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
/**
* The speed curve editor: a strip floating over the timeline's top edge, the
* full width of the editor, over whatever sits above it. It draws one
* clip's rate across its footage. Left to right is the clip's trimmed source;
* up and down is the rate on a log scale, 1× through the middle. Nodes are
* the curve's control points: drag one to move it, click the line to add one
* where it was clicked, double-click a node to take it away. A click off the
* line moves the playhead there, and hovering the graph skims the preview,
* so the moment under the pointer is on screen before a node is placed.
* Touching the line or a node leaves the playhead where it is.
*
* Edits draft locally and commit on release through the store's one curve
* setter, so every gesture is one undo step and every surface reads the
* same map.
*/
const GRAPH_H = 116;
const PAD_X = 14;
const PAD_Y = 10;
/** Pixels within which a drag lands on a beat or the 1× line. */
const SNAP_PX = 6;
/** Pixels within which a press counts as a press on the line. */
const LINE_HIT_PX = 8;
/** Closest two nodes may sit, source seconds. */
const MIN_GAP = 1 / 60;
const FRAME = 1 / 30;
/** The grid: the ends carry a label, 1× is the solid line through the
* middle, and the lines halfway between, on the log scale, are dashed. */
const RATE_LINES = [10, Math.sqrt(10), 1, 1 / Math.sqrt(10), 0.1];
const LABELED = new Set([10, 0.1]);
const LOG_MIN = Math.log10(SPEED_CURVE_MIN);
const LOG_MAX = Math.log10(SPEED_CURVE_MAX);
const clampRate = (r: number) =>
Math.min(SPEED_CURVE_MAX, Math.max(SPEED_CURVE_MIN, r));
/** A rate as people say it: 2×, 0.5×, 0.25×. */
const fmtRate = (r: number) => `${+r.toFixed(2)}×`;
const fmtSec = (s: number) => `${s.toFixed(1)}s`;
export function SpeedCurveStrip() {
// The strip shows for the selected clip when that clip's curve is open.
// Every clip keeps its own state, so a selection elsewhere hides the strip
// and a return to the clip brings it back.
const selectedId = useEditor((s) =>
s.selection?.kind === "clip" ? s.selection.id : null,
);
const clipId = usePanelStore((s) =>
selectedId && s.items[selectedId]?.speedCurve ? selectedId : null,
);
const clip = useEditor((s) =>
clipId ? s.clips.find((c) => c.id === clipId) : undefined,
);
if (!clip) return null;
return <Strip key={clip.id} clip={clip} />;
}
function Strip({ clip }: { clip: VideoClip }) {
const t = useTranslations("timeline.speedCurve");
// A preset's name and hint by its id; the kit's list keeps its English for
// the model.
const presetText = (id: string, part: "label" | "hint") =>
t(`presets.${id}.${part}` as Parameters<typeof t>[0]);
const asset = useEditor((s) => s.assets.find((a) => a.id === clip.assetId));
const picked = useEditor((s) =>
s.selectedKey?.track === "speed" && s.selectedKey.id === clip.id
? s.selectedKey.t
: null,
);
const [draft, setDraft] = useState<SpeedNode[] | null>(null);
const nodes = useMemo(
() => draft ?? speedCurveOf(clip) ?? flatSpeedCurve(clip),
[draft, clip],
);
const rt = useMemo(
() =>
retimeOf({
in: clip.in,
out: clip.out,
speedCurve: nodes,
reverse: clip.reverse,
}),
[clip.in, clip.out, nodes, clip.reverse],
);
const boxRef = useRef<HTMLDivElement | null>(null);
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
const box = boxRef.current;
if (!box) return;
const ro = new ResizeObserver(([e]) => setWidth(e.contentRect.width));
ro.observe(box);
setWidth(box.clientWidth);
return () => ro.disconnect();
}, []);
const innerW = Math.max(1, width - PAD_X * 2);
const innerH = GRAPH_H - PAD_Y * 2;
// Left to right is the clip's trimmed source, `in` to `out`. Nodes live in
// source seconds, so a node holds its place while another one is dragged:
// changing a rate changes how long the footage plays, never where the
// footage is on the graph.
const span = Math.max(1e-6, clip.out - clip.in);
const xOf = (src: number) => PAD_X + ((src - clip.in) / span) * innerW;
const srcOf = (x: number) =>
clip.in + Math.max(0, Math.min(1, (x - PAD_X) / innerW)) * span;
const yOf = (rate: number) =>
PAD_Y +
(1 - (Math.log10(clampRate(rate)) - LOG_MIN) / (LOG_MAX - LOG_MIN)) *
innerH;
const rateOf = (y: number) =>
clampRate(
Math.pow(10, LOG_MIN + (1 - (y - PAD_Y) / innerH) * (LOG_MAX - LOG_MIN)),
);
const beats = useMemo(
() =>
(asset?.beats?.beats ?? []).filter((b) => b >= clip.in && b <= clip.out),
[asset?.beats, clip.in, clip.out],
);
// The curve, and the area between it and the 1× line wherever it dips
// under — the footage smoothing works on, shaded when smoothing is on so
// the button's reach is on the graph.
const { curvePath, slowPath } = useMemo(() => {
if (innerW <= 1) return { curvePath: "", slowPath: "" };
const steps = Math.max(2, Math.min(400, Math.round(innerW / 2)));
// Sampled every couple of pixels, plus every node's own x, so the line
// passes through each node exactly however steep it turns there.
const xs: number[] = [];
for (let i = 0; i <= steps; i++) xs.push(PAD_X + (i / steps) * innerW);
for (const n of nodes) xs.push(xOf(n[0]));
xs.sort((a, b) => a - b);
const pts: string[] = [];
const under: string[] = [];
const yOne = yOf(1);
for (const x of xs) {
const y = yOf(rt.rateAtSrc(srcOf(x)));
pts.push(`${x.toFixed(1)},${y.toFixed(1)}`);
under.push(`${x.toFixed(1)},${Math.max(y, yOne).toFixed(1)}`);
}
const x0 = PAD_X.toFixed(1);
const x1 = (PAD_X + innerW).toFixed(1);
return {
curvePath: `M${pts.join("L")}`,
slowPath: `M${under.join("L")}L${x1},${yOne.toFixed(1)}L${x0},${yOne.toFixed(1)}Z`,
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [rt, nodes, clip.in, clip.out, innerW, innerH]);
// The playhead and the skimmer, mapped through the clip's map onto the
// graph's axis. One DOM write per line per move.
const playheadRef = useRef<HTMLDivElement | null>(null);
const skimRef = useRef<HTMLDivElement | null>(null);
const geom = useRef({ xOf, rt, start: clip.start, len: rt.len });
const paintLine = (el: HTMLDivElement | null, t: number | null) => {
if (!el) return;
const g = geom.current;
const tLocal = t === null ? -1 : t - g.start;
if (tLocal < -1e-6 || tLocal > g.len + 1e-6) {
el.style.opacity = "0";
return;
}
el.style.opacity = "1";
el.style.left = `${g.xOf(g.rt.srcAt(tLocal))}px`;
};
const paintLines = () => {
paintLine(playheadRef.current, playheadAt());
paintLine(skimRef.current, skimAt());
};
// The playhead stays on the frame it was on. It is a timeline time, and
// every edit to the curve moves the footage under it; so when the map
// changes, the source second the playhead was over under the old map is
// found again under the new one and the playhead is put there. A play in
// progress keeps its clock.
const lastRt = useRef(rt);
useEffect(() => {
const prev = lastRt.current;
lastRt.current = rt;
if (prev === rt || useEditor.getState().playing) return;
const tLocal = playheadAt() - clip.start;
if (tLocal < -1e-6 || tLocal > prev.len + 1e-6) return;
const src = prev.srcAt(tLocal);
const next = clip.start + Math.max(0, Math.min(rt.len, rt.tAt(src)));
if (Math.abs(next - playheadAt()) > 1e-4) useEditor.getState().seek(next);
}, [rt, clip.start]);
// The map and the geometry change with every draft and resize; the lines
// read the newest through the ref and repaint after each render.
useEffect(() => {
geom.current = { xOf, rt, start: clip.start, len: rt.len };
paintLines();
});
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => subscribePlayhead(paintLines), []);
// The skimmer belongs to the pointer over the graph; the strip closing
// leaves it off.
useEffect(() => () => setSkim(null), []);
const pick = (src: number | null) => {
useEditor.setState({
selectedKey:
src === null
? null
: { kind: "clip", id: clip.id, t: src, track: "speed" },
});
};
const commit = (next: SpeedNode[]) => {
setDraft(null);
useEditor.getState().setClipSpeedCurve(clip.id, next);
};
/** Where a node may sit between its neighbours. */
const bounds = (list: SpeedNode[], i: number) => ({
lo: i > 0 ? list[i - 1][0] + MIN_GAP : clip.in,
hi: i < list.length - 1 ? list[i + 1][0] - MIN_GAP : clip.out,
});
const insertAt = (
src: number,
rate: number,
list: SpeedNode[],
): SpeedNode[] => {
const next: SpeedNode[] = [...list, [src, clampRate(rate)]];
next.sort((a, b) => a[0] - b[0]);
return next;
};
const addAtPlayhead = () => {
const tLocal = Math.max(0, Math.min(rt.len, playheadAt() - clip.start));
const src = Math.min(clip.out, Math.max(clip.in, rt.srcAt(tLocal)));
if (nodes.some((n) => Math.abs(n[0] - src) < MIN_GAP)) return;
const next = insertAt(src, rt.rateAtSrc(src), nodes);
pick(src);
commit(next);
};
const removePicked = () => {
if (picked === null || nodes.length <= 1) return;
const next = nodes.filter((n) => Math.abs(n[0] - picked) > 1e-6);
if (next.length === nodes.length) return;
pick(null);
commit(next);
};
const nudge = (dSrc: number, rateScale: number) => {
if (picked === null) return;
const i = nodes.findIndex((n) => Math.abs(n[0] - picked) < 1e-6);
if (i < 0) return;
const { lo, hi } = bounds(nodes, i);
const src = Math.min(hi, Math.max(lo, nodes[i][0] + dSrc));
const rate = clampRate(nodes[i][1] * rateScale);
const next = nodes.map((n, j) =>
j === i ? ([src, rate] as SpeedNode) : n,
);
pick(src);
commit(next);
};
const srcAtX = (x: number) => Math.min(clip.out, Math.max(clip.in, srcOf(x)));
/** Whether a graph point sits on the drawn line. A steep segment is nearly
* vertical, so the line is sampled across the hit width, and the press
* counts when any sample is within reach. */
const onLine = (x: number, y: number) => {
for (let dx = -LINE_HIT_PX; dx <= LINE_HIT_PX; dx += 2) {
const sx = x + dx;
if (sx < PAD_X || sx > PAD_X + innerW) continue;
if (Math.abs(yOf(rt.rateAtSrc(srcAtX(sx))) - y) <= LINE_HIT_PX)
return true;
}
return false;
};
const graphPoint = (e: {
clientX: number;
clientY: number;
currentTarget: Element;
}) => {
const rect = e.currentTarget.getBoundingClientRect();
return { x: e.clientX - rect.left, y: e.clientY - rect.top };
};
// A press on the line adds a node there; a press anywhere else in the
// strip moves the playhead, and dragging from there scrubs.
const scrubFrom = (e: React.PointerEvent, x: number) => {
pick(null);
setSkim(null);
const seekAt = (px: number) =>
useEditor.getState().seek(clip.start + rt.tAt(srcAtX(px)));
seekAt(x);
startDrag(e, { onMove: (dx) => seekAt(x + dx) });
};
const onGraphPointerDown = (e: React.PointerEvent<SVGSVGElement>) => {
if (e.button !== 0) return;
const { x, y } = graphPoint(e);
// The line beside a node belongs to the node's handle, so a press there
// scrubs like any other.
if (
onLine(x, y) &&
!nodes.some((n) => Math.abs(xOf(n[0]) - x) < SNAP_PX * 2)
) {
const src = srcAtX(x);
const next = insertAt(src, rt.rateAtSrc(src), nodes);
pick(src);
commit(next);
return;
}
scrubFrom(e, x);
};
const onHeaderPointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
if (e.button !== 0 || (e.target as Element).closest("button")) return;
scrubFrom(e, e.clientX - e.currentTarget.getBoundingClientRect().left);
};
// Hovering skims the preview to the moment under the pointer, the way the
// timeline does; a held button or a running cut leaves the picture to the
// playhead. The cursor says what a press would do.
const onGraphPointerMove = (e: React.PointerEvent<SVGSVGElement>) => {
const { x, y } = graphPoint(e);
e.currentTarget.style.cursor = onLine(x, y) ? "crosshair" : "default";
if (e.buttons || useEditor.getState().playing) return setSkim(null);
setSkim(clip.start + rt.tAt(srcAtX(x)));
};
const onGraphPointerLeave = () => setSkim(null);
const onCapPointerDown = (e: React.PointerEvent) => {
const box = boxRef.current;
if (!box) return;
const left = box.getBoundingClientRect().left;
const x0 = e.clientX - left;
setSkim(null);
startDrag(e, {
cursor: () => "ew-resize",
onMove: (dx) =>
useEditor.getState().seek(clip.start + rt.tAt(srcAtX(x0 + dx))),
});
};
const onNodePointerDown = (e: React.PointerEvent, i: number) => {
const node = nodes[i];
pick(node[0]);
const x0 = xOf(node[0]);
const y0 = yOf(node[1]);
const { lo, hi } = bounds(nodes, i);
let live: SpeedNode[] = nodes;
startDrag(e, {
cursor: () => "grabbing",
onMove: (dx, dy, ev) => {
let x = x0 + dx;
let y = y0 + dy;
if (!ev.altKey) {
const beat = beats.find((b) => Math.abs(xOf(b) - x) <= SNAP_PX);
if (beat !== undefined) x = xOf(beat);
if (Math.abs(yOf(1) - y) <= SNAP_PX) y = yOf(1);
}
const src = Math.min(hi, Math.max(lo, srcOf(x)));
const rate = rateOf(Math.min(GRAPH_H - PAD_Y, Math.max(PAD_Y, y)));
live = nodes.map((n, j) => (j === i ? ([src, rate] as SpeedNode) : n));
setDraft(live);
pick(src);
},
onUp: (_dx, _dy, moved) => {
if (moved) commit(live);
},
});
};
const onKeyDown = (e: React.KeyboardEvent) => {
const big = e.shiftKey;
switch (e.key) {
case "ArrowLeft":
nudge(-(big ? FRAME * 10 : FRAME), 1);
break;
case "ArrowRight":
nudge(big ? FRAME * 10 : FRAME, 1);
break;
case "ArrowUp":
nudge(0, big ? 1.25 : 1.05);
break;
case "ArrowDown":
nudge(0, 1 / (big ? 1.25 : 1.05));
break;
case "Delete":
case "Backspace":
removePicked();
break;
case "Escape":
panelState.set(clip.id, "speedCurve", false);
break;
default:
return;
}
e.preventDefault();
e.stopPropagation();
};
const pickedRate =
picked === null
? null
: nodes.find((n) => Math.abs(n[0] - picked) < 1e-6)?.[1];
// The node under a drag, for the rate tag beside it.
const dragging =
draft && picked !== null
? (draft.find((n) => Math.abs(n[0] - picked) < 1e-6) ?? null)
: null;
// The picker names the preset the curve is; a curve that is none of them
// is "Custom", drawn as itself through the span.
const presetId = speedCurvePresetOf(nodes, clip.in, clip.out);
const preset = SPEED_CURVE_PRESETS.find((p) => p.id === presetId);
const shape = useMemo<SpeedNode[]>(
() =>
preset?.shape ??
nodes.map(([at, r]): SpeedNode => [(at - clip.in) / span, r]),
[preset, nodes, clip.in, span],
);
return (
<div
className="absolute inset-x-0 bottom-full z-40 flex min-w-0 flex-col border-t border-border bg-muted shadow-[0_-6px_16px_rgba(0,0,0,0.08)] outline-none"
tabIndex={0}
onKeyDown={onKeyDown}
data-testid="speed-curve-strip"
>
<div
className="flex h-10 items-center gap-1.5 px-2 pt-2 text-[11px]"
onPointerDown={onHeaderPointerDown}
>
{pickedRate != null && (
<span className="tabular-nums text-foreground/70">
{t("node", { rate: fmtRate(pickedRate), time: fmtSec(rt.tAt(picked!)) })}
</span>
)}
<span className="flex-1" />
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button
variant="outline"
size="xs"
title={preset ? presetText(preset.id, "hint") : t("pickPreset")}
className="gap-1.5 pl-1.5 font-normal"
/>
}
>
<Sparkline shape={shape} />
<span>{preset ? presetText(preset.id, "label") : t("custom")}</span>
<ChevronDown className="size-3 text-muted-foreground" />
</DropdownMenuTrigger>
<DropdownMenuContent
side="top"
align="center"
sideOffset={8}
className="overflow-visible p-0"
>
{/* The pad is the picked tile's ring and its offset, drawn outside
the tile. */}
<div className="grid grid-cols-2 gap-2 p-2">
{SPEED_CURVE_PRESETS.map((p) => (
<DropdownMenuItem
key={p.id}
title={presetText(p.id, "hint")}
className={cn(
"relative block h-20 w-40 overflow-hidden rounded-xl border border-border bg-muted/40 p-0 focus:bg-muted",
p.id === presetId && PICKED_RING,
)}
onClick={() => {
pick(null);
setDraft(null);
useEditor.getState().setClipSpeedPreset(clip.id, p.id);
}}
>
<span className="absolute inset-x-2.5 top-2.5">
<Sparkline shape={p.shape} w={140} h={48} grid />
</span>
<span className="absolute bottom-1.5 left-2.5 text-[11.5px] font-medium">
{presetText(p.id, "label")}
</span>
</DropdownMenuItem>
))}
</div>
<MenuPrimitive.Arrow className="absolute -bottom-2 left-1/2 -translate-x-1/2">
<svg width="20" height="10" viewBox="0 0 20 10" className="block">
<path d="M0 0 L10 10 L20 0" className="fill-popover" />
<path
d="M0 0 L10 10 L20 0"
fill="none"
className="stroke-foreground/10"
/>
</svg>
</MenuPrimitive.Arrow>
</DropdownMenuContent>
</DropdownMenu>
<Button
variant="outline"
size="xs"
className={cn(
"font-normal",
clip.smoothSlow && "border-[#0a84ff]/50 bg-[#0a84ff]/10 hover:bg-[#0a84ff]/15",
)}
aria-pressed={!!clip.smoothSlow}
onClick={() => useEditor.getState().setClipSmoothSlow(clip.id, !clip.smoothSlow)}
>
{t("smoothSlow")}
</Button>
<InfoTip label={t("smoothSlow")} side="top">
{t("smoothSlowInfo")}
</InfoTip>
<span className="mx-1 h-4 w-px bg-border" />
<IconButton title={t("addNode")} onClick={addAtPlayhead}>
<Plus className="size-3.5" />
</IconButton>
<IconButton
title={t("removeNode")}
disabled={picked === null || nodes.length <= 1}
onClick={removePicked}
>
<Minus className="size-3.5" />
</IconButton>
<IconButton
title={t("flatten")}
onClick={() => {
pick(null);
setDraft(null);
// Back to a plain clip at 1×: the curve goes with the rate, so the
// timeline's rail and badge go with it.
useEditor.getState().setClipSpeed(clip.id, 1);
}}
>
<RotateCcw className="size-3.5" />
</IconButton>
<IconButton
title={t("close", { key: "Esc" })}
onClick={() => panelState.set(clip.id, "speedCurve", false)}
>
<X className="size-3.5" />
</IconButton>
</div>
<div
ref={boxRef}
className="relative select-none overflow-hidden"
style={{ height: GRAPH_H }}
>
<svg
className="absolute inset-0 h-full w-full"
onPointerDown={onGraphPointerDown}
onPointerMove={onGraphPointerMove}
onPointerLeave={onGraphPointerLeave}
>
{RATE_LINES.map((r) => (
<g key={r}>
<line
x1={PAD_X}
x2={PAD_X + innerW}
y1={yOf(r)}
y2={yOf(r)}
stroke="currentColor"
strokeOpacity={r === 1 ? 0.45 : 0.18}
strokeDasharray={r === 1 ? undefined : "2 3"}
/>
{LABELED.has(r) && (
<text
x={PAD_X + 3}
y={yOf(r) - 2}
fontSize={9}
fill="currentColor"
fillOpacity={0.85}
>
{fmtRate(r)}
</text>
)}
</g>
))}
{beats.map((b) => (
<line
key={b}
x1={xOf(b)}
x2={xOf(b)}
y1={PAD_Y}
y2={GRAPH_H - PAD_Y}
stroke="#ff9f0a"
strokeOpacity={0.5}
/>
))}
{clip.smoothSlow && <path d={slowPath} fill="#0a84ff" fillOpacity={0.14} />}
<path d={curvePath} fill="none" stroke="#0a84ff" strokeWidth={2} />
{nodes.map((n, i) => {
const isPicked = picked !== null && Math.abs(n[0] - picked) < 1e-6;
return (
<circle
key={i}
cx={xOf(n[0])}
cy={yOf(n[1])}
r={isPicked ? 6 : 5}
fill="white"
stroke={isPicked ? "#0a84ff" : "rgba(0,0,0,0.5)"}
strokeWidth={isPicked ? 2 : 1}
className="cursor-grab"
onPointerDown={(e) => {
e.stopPropagation();
onNodePointerDown(e, i);
}}
onDoubleClick={(e) => {
e.stopPropagation();
if (nodes.length <= 1) return;
pick(null);
commit(nodes.filter((_, j) => j !== i));
}}
/>
);
})}
{dragging && (
<RateTag
x={xOf(dragging[0])}
y={yOf(dragging[1])}
rate={dragging[1]}
boxW={width}
/>
)}
</svg>
<div
ref={skimRef}
className="pointer-events-none absolute top-0 h-full w-px bg-foreground/30"
style={{ opacity: 0 }}
/>
<div
ref={playheadRef}
className="pointer-events-none absolute top-0 h-full w-[1.5px] bg-[#0a84ff] shadow-[0_0_8px_rgba(10,132,255,0.6)]"
style={{ opacity: 0 }}
>
{/* The timeline playhead's grab cap: drag it to scrub. */}
<div
className="pointer-events-auto absolute top-0 left-[-4.5px] h-3 w-2.5 cursor-ew-resize rounded-t-[3px] bg-[#0a84ff] [clip-path:polygon(0_0,100%_0,100%_58%,50%_100%,0_58%)]"
onPointerDown={onCapPointerDown}
/>
</div>
</div>
</div>
);
}
/** A curve at a glance: the 1× line and the rate through the span, on the
* same log scale as the graph, drawn by the same evaluator. */
function Sparkline({
shape,
w = 32,
h = 16,
grid,
}: {
shape: SpeedNode[];
w?: number;
h?: number;
/** Draw the graph's dashed ends around the 1× line, for a tile. */
grid?: boolean;
}) {
const pad = 2;
const yOf = (rate: number) =>
pad +
(1 - (Math.log10(clampRate(rate)) - LOG_MIN) / (LOG_MAX - LOG_MIN)) *
(h - pad * 2);
const d = useMemo(() => {
const rt = retimeOf({ in: 0, out: 1, speedCurve: shape });
const steps = Math.max(32, Math.round(w / 2));
const pts: string[] = [];
for (let i = 0; i <= steps; i++) {
const x = 1 + (i / steps) * (w - 2);
pts.push(`${x.toFixed(1)},${yOf(rt.rateAtSrc(i / steps)).toFixed(1)}`);
}
return `M${pts.join("L")}`;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [shape, w, h]);
return (
// Sized inline: a menu item squares every svg inside it to an icon, and
// an inline size outranks that rule.
<svg
viewBox={`0 0 ${w} ${h}`}
style={{ width: w, height: h }}
className="shrink-0 overflow-visible"
aria-hidden
>
{grid &&
[SPEED_CURVE_MAX, SPEED_CURVE_MIN].map((r) => (
<line
key={r}
x1={0}
x2={w}
y1={yOf(r)}
y2={yOf(r)}
stroke="currentColor"
strokeOpacity={0.2}
strokeDasharray="2 3"
/>
))}
<line
x1={0}
x2={w}
y1={yOf(1)}
y2={yOf(1)}
stroke="currentColor"
strokeOpacity={0.3}
/>
<path
d={d}
fill="none"
stroke="#0a84ff"
strokeWidth={grid ? 2 : 1.5}
strokeLinejoin="round"
strokeLinecap="round"
/>
</svg>
);
}
/** The rate beside a node while it is dragged: a dark label on whichever
* side has room, held inside the graph. */
function RateTag({
x,
y,
rate,
boxW,
}: {
x: number;
y: number;
rate: number;
boxW: number;
}) {
const label = fmtRate(rate);
const w = label.length * 6.5 + 10;
const h = 16;
const gap = 10;
const left = x + gap + w <= boxW ? x + gap : x - gap - w;
const cy = Math.min(GRAPH_H - h / 2, Math.max(h / 2, y));
return (
<g className="pointer-events-none">
<rect
x={left}
y={cy - h / 2}
width={w}
height={h}
rx={4}
className="fill-foreground/60"
/>
<text
x={left + w / 2}
y={cy + 3.5}
textAnchor="middle"
fontSize={10}
fontWeight={600}
className="fill-background tabular-nums"
>
{label}
</text>
</g>
);
}
function IconButton({
title,
disabled,
onClick,
children,
}: {
title: string;
disabled?: boolean;
onClick: () => void;
children: React.ReactNode;
}) {
// The strip is the muted grey, so the ghost hover reads against it.
return (
<Button
variant="ghost"
size="icon-xs"
title={title}
disabled={disabled}
onClick={onClick}
className="hover:bg-foreground/10 dark:hover:bg-foreground/15"
>
{children}
</Button>
);
}