🎥 Make videos programmatically with React
Make Interactive.Path outlines follow the path geometry
Jonny Burger8d9cf652cb78
diff --git a/packages/canvas/src/canvas-outline-polygon.tsx b/packages/canvas/src/canvas-outline-polygon.tsxindex 94aeca2f94..947c4c1710 100644--- a/packages/canvas/src/canvas-outline-polygon.tsx+++ b/packages/canvas/src/canvas-outline-polygon.tsx@@ -46,6 +46,15 @@ export const CanvasOutlinePolygon = memo(() => outline.points.map((point) => `${point.x},${point.y}`).join(' '),[outline.points],);+ const pathPointsAttr = useMemo(+ () =>+ outline.pathPoints === null+ ? null+ : outline.pathPoints+ .map((point) => `${point.x},${point.y}`)+ .join(' '),+ [outline.pathPoints],+ );const onPointerEnter = useCallback(() => {if (!dragging) {onHoverChange(outline.key);@@ -58,23 +67,36 @@ export const CanvasOutlinePolygon = memo(}, [dragging, onHoverChange]);return (- <polygon- {...props}- ref={ref}- data-remotion-canvas-outline-key={outline.key}- data-remotion-directly-selected-outline={- directlySelected ? 'true' : undefined- }- points={points}- fill={fill}- stroke={stroke}- strokeOpacity={visible ? 1 : 0}- strokeWidth={2}- vectorEffect="non-scaling-stroke"- pointerEvents={interactive ? 'all' : 'none'}- onPointerEnter={onPointerEnter}- onPointerLeave={onPointerLeave}- />+ <g>+ <polygon+ {...props}+ ref={ref}+ data-remotion-canvas-outline-key={outline.key}+ data-remotion-directly-selected-outline={+ directlySelected ? 'true' : undefined+ }+ points={points}+ fill={fill}+ stroke={pathPointsAttr === null ? stroke : 'transparent'}+ strokeOpacity={visible ? 1 : 0}+ strokeWidth={2}+ vectorEffect="non-scaling-stroke"+ pointerEvents={interactive ? 'all' : 'none'}+ onPointerEnter={onPointerEnter}+ onPointerLeave={onPointerLeave}+ />+ {pathPointsAttr === null ? null : (+ <polyline+ points={pathPointsAttr}+ fill="none"+ stroke={stroke}+ strokeOpacity={visible ? 1 : 0}+ strokeWidth={2}+ vectorEffect="non-scaling-stroke"+ pointerEvents="none"+ />+ )}+ </g>);},),diff --git a/packages/canvas/src/outline-geometry.ts b/packages/canvas/src/outline-geometry.tsindex 733a15939f..4cabb2eed2 100644--- a/packages/canvas/src/outline-geometry.ts+++ b/packages/canvas/src/outline-geometry.ts@@ -24,6 +24,12 @@ export type CanvasOutline = {CanvasOutlinePoint,CanvasOutlinePoint,];+ /**+ * Samples along the actual geometry of an SVG path, in the same space as+ * `points` (already cropped). Null for non-path elements, or when the+ * geometry cannot be sampled.+ */+ readonly pathPoints: readonly CanvasOutlinePoint[] | null;};const mix = (from: number, to: number, progress: number): number => {diff --git a/packages/canvas/src/outline-measurement.ts b/packages/canvas/src/outline-measurement.tsindex a308772a4e..cd5b1d5f66 100644--- a/packages/canvas/src/outline-measurement.ts+++ b/packages/canvas/src/outline-measurement.ts@@ -5,7 +5,7 @@ import type {CanvasOutlinePoint,CanvasOutlineTarget,} from './outline-geometry';-import {getCanvasOutlinePoint} from './outline-geometry';+import {getCanvasOutlinePoint, getCanvasOutlineUv} from './outline-geometry';const rectToPoints = (elementRect: DOMRect,@@ -124,6 +124,81 @@ const getSvgSvgElementOutlinePoints = (});};+const isSvgPathElement = (element: Element): element is SVGPathElement => {+ const ownerSvgPathElement = element.ownerDocument.defaultView?.SVGPathElement;+ return (+ (typeof SVGPathElement !== 'undefined' &&+ element instanceof SVGPathElement) ||+ (ownerSvgPathElement !== undefined &&+ element instanceof ownerSvgPathElement)+ );+};++const pathSampleSpacing = 8;+const maxPathSamples = 400;++const getPathPoints = ({+ element,+ containerRect,+}: {+ readonly element: SVGPathElement;+ readonly containerRect: DOMRect;+}): readonly CanvasOutlinePoint[] | null => {+ const ctm = element.getScreenCTM();+ if (ctm === null) {+ return null;+ }++ const totalLength = element.getTotalLength();+ if (totalLength === 0) {+ return null;+ }++ const count = Math.min(+ maxPathSamples,+ Math.max(2, Math.ceil(totalLength / pathSampleSpacing) + 1),+ );+ const points: CanvasOutlinePoint[] = [];+ for (let i = 0; i < count; i++) {+ // Walk the length so each sample is equidistant along the geometry.+ const length = (totalLength * i) / (count - 1);+ const point = element.getPointAtLength(length);+ points.push({+ x: ctm.a * point.x + ctm.c * point.y + ctm.e - containerRect.left,+ y: ctm.b * point.x + ctm.d * point.y + ctm.f - containerRect.top,+ });+ }++ return points;+};++const cropPathPoints = (+ pathPoints: readonly CanvasOutlinePoint[],+ uncroppedPoints: CanvasOutline['uncroppedPoints'],+ crop: CanvasOutlineTarget['crop'],+): readonly CanvasOutlinePoint[] => {+ if (+ crop.left === 0 &&+ crop.right === 0 &&+ crop.top === 0 &&+ crop.bottom === 0+ ) {+ return pathPoints;+ }++ if (uncroppedPoints === null) {+ return pathPoints;+ }++ return pathPoints.map((point) => {+ const uv = getCanvasOutlineUv(uncroppedPoints, point);+ return getCanvasOutlinePoint(+ cropCanvasOutlinePoints(uncroppedPoints, crop),+ uv,+ );+ });+};+const getElementOutlinePoints = (element: Element,containerRect: DOMRect,@@ -216,6 +291,15 @@ export const measureCanvasOutlineTargets = (const points = cropCanvasOutlinePoints(uncroppedPoints, target.crop);const ownerHTMLElement = element.ownerDocument.defaultView?.HTMLElement;+ const pathPoints = isSvgPathElement(element)+ ? (() => {+ const sampled = getPathPoints({element, containerRect});+ return sampled === null+ ? null+ : cropPathPoints(sampled, uncroppedPoints, target.crop);+ })()+ : null;+outlines.push({key: target.key,dimensions:@@ -234,6 +318,7 @@ export const measureCanvasOutlineTargets = (: null,uncroppedPoints,points,+ pathPoints,});}@@ -277,6 +362,27 @@ export const canvasOutlinesAreEqual = (}}+ const aPathPoints = a[i].pathPoints;+ const bPathPoints = b[i].pathPoints;+ if ((aPathPoints === null) !== (bPathPoints === null)) {+ return false;+ }++ if (aPathPoints !== null && bPathPoints !== null) {+ if (aPathPoints.length !== bPathPoints.length) {+ return false;+ }++ for (let j = 0; j < aPathPoints.length; j++) {+ if (+ Math.abs(aPathPoints[j].x - bPathPoints[j].x) > 0.01 ||+ Math.abs(aPathPoints[j].y - bPathPoints[j].y) > 0.01+ ) {+ return false;+ }+ }+ }+for (let j = 0; j < a[i].points.length; j++) {if (Math.abs(a[i].points[j].x - b[i].points[j].x) > 0.01 ||diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex 72735430a3..fa9e1379c0 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -325,6 +325,7 @@ import {InteractiveHtmlElements,InteractiveSvgElements,} from './VisualModeTests/InteractiveComponents';+import {InteractivePaths} from './VisualModeTests/InteractivePaths';import {Issue9170} from './VisualModeTests/Issue9170';import {KeyframeDeleteFlash} from './VisualModeTests/KeyframeDeleteFlash';import {OutlineSelectionCases} from './VisualModeTests/OutlineSelectionCases';@@ -3145,6 +3146,14 @@ export const Index: React.FC = () => {fps={30}durationInFrames={90}/>+ <Composition+ id="interactive-paths"+ component={InteractivePaths}+ width={1080}+ height={1080}+ fps={30}+ durationInFrames={90}+ /><Compositionid="issue-9582-svg-paint-schema"component={SvgPaintSchema}diff --git a/packages/example/src/VisualModeTests/InteractivePaths.tsx b/packages/example/src/VisualModeTests/InteractivePaths.tsxnew file mode 100644index 0000000000..1571b097fb--- /dev/null+++ b/packages/example/src/VisualModeTests/InteractivePaths.tsx@@ -0,0 +1,103 @@+import React from 'react';+import {AbsoluteFill, Interactive, useCurrentFrame} from 'remotion';++const labelStyle: React.CSSProperties = {+ fontFamily: 'sans-serif',+ fontSize: 28,+ fill: '#64748b',+};++export const InteractivePaths: React.FC = () => {+ const frame = useCurrentFrame();+ const swing = Math.sin(frame / 10) * 90;++ return (+ <AbsoluteFill style={{backgroundColor: '#020617'}}>+ <Interactive.Svg width={1080} height={1080} viewBox="0 0 1080 1080">+ <text x={90} y={70} style={labelStyle}>+ Interactive.Path testbed+ </text>++ {/* Open bezier curve */}+ <Interactive.Path+ name="Open curve"+ d="M 90 210 C 250 60 430 90 530 230 S 710 420 890 300"+ fill="none"+ stroke="#facc15"+ strokeWidth={26}+ strokeLinecap="round"+ />+ <text x={90} y={470} style={labelStyle}>+ open curve+ </text>++ {/* Closed filled shape */}+ <Interactive.Path+ name="Closed triangle"+ d="M 870 350 L 1010 560 L 730 560 Z"+ fill="#7c3aed"+ fillOpacity={0.7}+ stroke="#c4b5fd"+ strokeWidth={12}+ strokeLinejoin="round"+ />+ <text x={790} y={640} style={labelStyle}>+ closed shape+ </text>++ {/* Multiple subpaths in one element */}+ <Interactive.Path+ name="Multi subpath"+ d="M 120 430 L 230 430 L 175 540 Z M 260 430 L 370 430 L 315 540 Z"+ fill="#22d3ee"+ fillOpacity={0.55}+ stroke="#67e8f9"+ strokeWidth={10}+ strokeLinejoin="round"+ />+ <text x={120} y={600} style={labelStyle}>+ multi subpath+ </text>++ {/* Animated path data */}+ <Interactive.Path+ name="Animated wave"+ d={`M 80 800 Q 250 ${710 + swing} 420 800 T 760 800 T 1040 800`}+ fill="none"+ stroke="#f472b6"+ strokeWidth={18}+ strokeLinecap="round"+ />+ <text x={80} y={880} style={labelStyle}>+ animated d+ </text>++ {/* Path inside a transformed group */}+ <Interactive.G name="Rotated group" transform="rotate(-14 420 980)">+ <Interactive.Path+ name="Group arc"+ d="M 250 1030 Q 420 860 590 1030"+ fill="none"+ stroke="#a3e635"+ strokeWidth={22}+ strokeLinecap="round"+ />+ </Interactive.G>+ <text x={650} y={1000} style={labelStyle}>+ inside rotated group+ </text>++ {/* Dashed stroke */}+ <Interactive.Path+ name="Dashed curve"+ d="M 700 700 C 780 770 950 800 1030 750"+ fill="none"+ stroke="#e879f9"+ strokeWidth={16}+ strokeDasharray="34 26"+ strokeLinecap="round"+ />+ </Interactive.Svg>+ </AbsoluteFill>+ );+};diff --git a/packages/studio/src/components/composition-drop-preview.ts b/packages/studio/src/components/composition-drop-preview.tsindex 5f034a2f13..ca51036702 100644--- a/packages/studio/src/components/composition-drop-preview.ts+++ b/packages/studio/src/components/composition-drop-preview.ts@@ -59,6 +59,7 @@ export const snapCompositionDropPosition = ({key: 'composition-drop-preview',dimensions: compositionDimensions,uncroppedPoints: null,+ pathPoints: null,points: [{x: left * scale, y: top * scale},{x: right * scale, y: top * scale},diff --git a/packages/studio/src/test/selected-outline-snap.test.ts b/packages/studio/src/test/selected-outline-snap.test.tsindex cf5e8126a7..59a0a562d5 100644--- a/packages/studio/src/test/selected-outline-snap.test.ts+++ b/packages/studio/src/test/selected-outline-snap.test.ts@@ -27,6 +27,7 @@ const makeOutline = ({key: 'outline',dimensions: {width, height},uncroppedPoints: null,+ pathPoints: null,points: [{x: left * scale, y: top * scale},{x: right * scale, y: top * scale},diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex faf67e7a03..6d7ec0d15d 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -3769,6 +3769,7 @@ test('Canvas outline rendering preserves unconstrained outline order', () => {key,dimensions: null,uncroppedPoints: null,+ pathPoints: null,points: [{x: 0, y: 0},{x: 10, y: 0},@@ -3816,6 +3817,7 @@ const makeTestOutline = ({key,dimensions: {width, height},uncroppedPoints: null,+ pathPoints: null,points: [{x: left, y: top},{x: left + width, y: top},