🎥 Make videos programmatically with React
Enable editing static SVG path points in Studio
Jonny Burgeree24dc2434c2
diff --git a/packages/core/src/Interactive.tsx b/packages/core/src/Interactive.tsxindex 3af596095e..b52b6bd880 100644--- a/packages/core/src/Interactive.tsx+++ b/packages/core/src/Interactive.tsx@@ -185,6 +185,16 @@ const interactiveSvgElementSchema = {...svgPaintSchema,} as const satisfies InteractivitySchema;+const interactiveSvgPathElementSchema = {+ ...interactiveSvgElementSchema,+ d: {+ type: 'svg-path',+ default: undefined,+ description: 'Path',+ keyframable: false,+ },+} as const satisfies InteractivitySchema;+const interactiveSvgStrokeElementSchema = {...interactiveElementSchema,...svgStrokeSchema,@@ -404,7 +414,11 @@ export const Interactive = {Nav: makeInteractiveTextElement('nav', '<Interactive.Nav>'),Ol: makeInteractiveTextElement('ol', '<Interactive.Ol>'),P: makeInteractiveTextElement('p', '<Interactive.P>'),- Path: makeInteractiveSvgElement('path', '<Interactive.Path>'),+ Path: makeInteractiveElement(+ 'path',+ '<Interactive.Path>',+ interactiveSvgPathElementSchema,+ ),Pre: makeInteractiveTextElement('pre', '<Interactive.Pre>'),Rect: makeInteractiveSvgElement('rect', '<Interactive.Rect>'),Section: makeInteractiveTextElement('section', '<Interactive.Section>'),diff --git a/packages/core/src/interactivity-schema.ts b/packages/core/src/interactivity-schema.tsindex 833a9a3241..01569d9ee0 100644--- a/packages/core/src/interactivity-schema.ts+++ b/packages/core/src/interactivity-schema.ts@@ -106,6 +106,13 @@ export type TextContentFieldSchema = {keyframable?: false;};+export type SvgPathFieldSchema = {+ type: 'svg-path';+ default: string | undefined;+ description?: string;+ keyframable?: false;+};+export type FontFamilyFieldSchema = {type: 'font-family';default: string | undefined;@@ -219,6 +226,7 @@ export type VisibleFieldSchema =| UvCoordinateFieldSchema| ColorFieldSchema| TextContentFieldSchema+ | SvgPathFieldSchema| FontFamilyFieldSchema| FontWeightFieldSchema| AssetFieldSchemadiff --git a/packages/paths/src/normalize-path.ts b/packages/paths/src/normalize-path.tsindex cb59ca7660..5804f9d34e 100644--- a/packages/paths/src/normalize-path.ts+++ b/packages/paths/src/normalize-path.ts@@ -19,8 +19,8 @@ export const normalizeInstructions = (moveX = instruction.x;moveY = instruction.y;} else if (instruction.type === 'm') {- moveX += instruction.dx;- moveY += instruction.dy;+ moveX = x + instruction.dx;+ moveY = y + instruction.dy;}if (diff --git a/packages/studio-shared/src/keyframe-clipboard-data.ts b/packages/studio-shared/src/keyframe-clipboard-data.tsindex 32a1a293a2..56fd187820 100644--- a/packages/studio-shared/src/keyframe-clipboard-data.ts+++ b/packages/studio-shared/src/keyframe-clipboard-data.ts@@ -21,6 +21,7 @@ const KEYFRAME_CLIPBOARD_FIELD_TYPE_SUPPORT = {'rotation-css': true,'rotation-degrees': true,scale: true,+ 'svg-path': false,'text-content': false,'transform-origin': true,translate: true,diff --git a/packages/studio-shared/src/keyframe-interpolation-function.ts b/packages/studio-shared/src/keyframe-interpolation-function.tsindex 1b4994b525..3e4317f114 100644--- a/packages/studio-shared/src/keyframe-interpolation-function.ts+++ b/packages/studio-shared/src/keyframe-interpolation-function.ts@@ -28,6 +28,7 @@ const KEYFRAME_FIELD_TYPE_SUPPORT = {'rotation-css': true,'rotation-degrees': true,scale: true,+ 'svg-path': false,'text-content': false,'transform-origin': true,translate: true,@@ -53,6 +54,7 @@ const KEYFRAME_FIELD_TYPE_INTERPOLATION = {'rotation-css': 'interpolate','rotation-degrees': 'infer',scale: 'interpolate',+ 'svg-path': 'unsupported','text-content': 'unsupported','transform-origin': 'interpolate',translate: 'interpolate',@@ -76,6 +78,7 @@ const KEYFRAME_FIELD_TYPE_OUTPUT_TYPE = {'rotation-css': 'rotate','rotation-degrees': null,scale: 'scale',+ 'svg-path': null,'text-content': null,'transform-origin': 'transform-origin',translate: 'translate',diff --git a/packages/studio-shared/src/schema-field-info.ts b/packages/studio-shared/src/schema-field-info.tsindex a54c2c23a7..532d5bad13 100644--- a/packages/studio-shared/src/schema-field-info.ts+++ b/packages/studio-shared/src/schema-field-info.ts@@ -232,6 +232,7 @@ const TIMELINE_SCHEMA_FIELD_TYPE_SUPPORT = {'rotation-css': true,'rotation-degrees': true,scale: true,+ 'svg-path': true,'text-content': true,'transform-origin': true,translate: true,diff --git a/packages/studio/src/components/SelectedOutlineOverlay.tsx b/packages/studio/src/components/SelectedOutlineOverlay.tsxindex 07f06b7c55..7d35ac2317 100644--- a/packages/studio/src/components/SelectedOutlineOverlay.tsx+++ b/packages/studio/src/components/SelectedOutlineOverlay.tsx@@ -366,6 +366,8 @@ const calculateOutlineTargets = ({const selectedForRotation = selectedRotationInfo?.sequenceKey === key;const selectedForUvHandles = selectedEffectsBySequenceKey.has(key);const fieldSchema = activeSchema?.[translateFieldKey];+ const pathFieldSchema = activeSchema?.d;+ const pathPropStatus = nodePropStatuses?.d;const propStatus = nodePropStatuses?.[translateFieldKey];const scaleFieldSchema = activeSchema?.[scaleFieldKey];const scalePropStatus = nodePropStatuses?.[scaleFieldKey];@@ -491,6 +493,18 @@ const calculateOutlineTargets = ({return [{...layoutTarget,+ pathDrag:+ previewInteractive &&+ controls !== null &&+ pathFieldSchema?.type === 'svg-path' &&+ pathPropStatus?.status === 'static' &&+ typeof pathPropStatus.codeValue === 'string'+ ? {+ clientId: connectedClientId,+ nodePath,+ schema: controls.schema,+ }+ : null,canCrop: previewInteractive && controls !== null && cropFields !== null,cropDrag: canCropDrag? {diff --git a/packages/studio/src/components/SelectedOutlinePathPoints.tsx b/packages/studio/src/components/SelectedOutlinePathPoints.tsxindex 3193851ace..b1d95e06b7 100644--- a/packages/studio/src/components/SelectedOutlinePathPoints.tsx+++ b/packages/studio/src/components/SelectedOutlinePathPoints.tsx@@ -1,11 +1,31 @@-import {normalizePath, parsePath} from '@remotion/paths';-import React, {useMemo} from 'react';+import {+ normalizePath,+ parsePath,+ serializeInstructions,+ type AbsoluteInstruction,+} from '@remotion/paths';+import React, {useContext, useMemo} from 'react';+import {Internals} from 'remotion';import {BLUE, WHITE} from '../helpers/colors';+import {+ isPointerSessionRelease,+ startCapturedPointerSession,+} from '../helpers/pointer-session';+import {showNotification} from './Notifications/NotificationCenter';import type {SelectedOutline} from './selected-outline-geometry';+import type {SelectedOutlinePathDragTarget} from './selected-outline-types';+import {saveSequenceProps} from './Timeline/save-sequence-prop';++type PathPointRole = 'end' | 'cp1' | 'cp2' | 'cp' | 'reflected-cp';export const SelectedOutlinePathPoints: React.FC<{readonly outline: SelectedOutline;-}> = ({outline}) => {+ readonly pathDrag: SelectedOutlinePathDragTarget;+ readonly onDraggingChange: (dragging: boolean) => void;+}> = ({outline, pathDrag, onDraggingChange}) => {+ const {setDragOverrides, clearDragOverrides, setPropStatuses} = useContext(+ Internals.VisualModeSettersContext,+ );const {points, connections} = useMemo(() => {if (outline.path === null) {return {points: [], connections: []};@@ -17,6 +37,8 @@ export const SelectedOutlinePathPoints: React.FC<{x: number;y: number;key: string;+ instructionIndex: number;+ role: PathPointRole;}[] = [];const pathConnections: {x1: number;@@ -63,11 +85,15 @@ export const SelectedOutlinePathPoints: React.FC<{x: instruction.cp1x,y: instruction.cp1y,key: `${instructionIndex}-cp1`,+ instructionIndex,+ role: 'cp1',},{x: instruction.cp2x,y: instruction.cp2y,key: `${instructionIndex}-cp2`,+ instructionIndex,+ role: 'cp2',},);previousCubicControl = {x: instruction.cp2x, y: instruction.cp2y};@@ -88,6 +114,8 @@ export const SelectedOutlinePathPoints: React.FC<{x: reflectedX,y: reflectedY,key: `${instructionIndex}-reflected-cp`,+ instructionIndex,+ role: 'reflected-cp',});}}@@ -103,6 +131,8 @@ export const SelectedOutlinePathPoints: React.FC<{x: instruction.cpx,y: instruction.cpy,key: `${instructionIndex}-cp`,+ instructionIndex,+ role: 'cp',});previousCubicControl = {x: instruction.cpx, y: instruction.cpy};previousQuadraticControl = null;@@ -127,6 +157,8 @@ export const SelectedOutlinePathPoints: React.FC<{x: instruction.cpx,y: instruction.cpy,key: `${instructionIndex}-cp`,+ instructionIndex,+ role: 'cp',});previousQuadraticControl = {x: instruction.cpx, y: instruction.cpy};previousCubicControl = null;@@ -159,6 +191,8 @@ export const SelectedOutlinePathPoints: React.FC<{x: control.x,y: control.y,key: `${instructionIndex}-reflected-cp`,+ instructionIndex,+ role: 'reflected-cp',});}@@ -183,7 +217,13 @@ export const SelectedOutlinePathPoints: React.FC<{subpathY = y;}- pathPoints.push({x, y, key: `${instructionIndex}-end`});+ pathPoints.push({+ x,+ y,+ key: `${instructionIndex}-end`,+ instructionIndex,+ role: 'end',+ });}const {a, b, c, d, e, f} = outline.path.matrix;@@ -194,7 +234,11 @@ export const SelectedOutlinePathPoints: React.FC<{return {points: pathPoints.map((point) => ({...transformPoint(point.x, point.y),+ localX: point.x,+ localY: point.y,key: point.key,+ instructionIndex: point.instructionIndex,+ role: point.role,})),connections: pathConnections.map((connection) => {const from = transformPoint(connection.x1, connection.y1);@@ -213,8 +257,180 @@ export const SelectedOutlinePathPoints: React.FC<{}}, [outline.path]);+ const onPointPointerDown = (+ event: React.PointerEvent<SVGCircleElement>,+ point: (typeof points)[number],+ ) => {+ if (event.button !== 0 || outline.path === null) {+ return;+ }++ const svg = event.currentTarget.ownerSVGElement;+ if (svg === null) {+ return;+ }++ const {a, b, c, d} = outline.path.matrix;+ const determinant = a * d - b * c;+ if (!Number.isFinite(determinant) || determinant === 0) {+ return;+ }++ let instructions: AbsoluteInstruction[];+ try {+ instructions = parsePath(+ normalizePath(outline.path.d),+ ) as AbsoluteInstruction[];+ } catch {+ return;+ }++ event.preventDefault();+ event.stopPropagation();+ onDraggingChange(true);+ const startX = event.clientX;+ const startY = event.clientY;+ const originalD = serializeInstructions(instructions);+ let lastD = originalD;++ startCapturedPointerSession({+ event,+ captureTarget: svg,+ onMove: (moveEvent) => {+ moveEvent.preventDefault();+ const deltaX = moveEvent.clientX - startX;+ const deltaY = moveEvent.clientY - startY;+ const localDeltaX =+ Math.round(((d * deltaX - c * deltaY) / determinant) * 100) / 100;+ const localDeltaY =+ Math.round(((a * deltaY - b * deltaX) / determinant) * 100) / 100;+ if (localDeltaX === 0 && localDeltaY === 0) {+ if (lastD !== originalD) {+ lastD = originalD;+ setDragOverrides(+ pathDrag.nodePath,+ 'd',+ Internals.makeStaticDragOverride(originalD),+ );+ }++ return;+ }++ const nextX =+ localDeltaX === 0+ ? point.localX+ : Math.round((point.localX + localDeltaX) * 100) / 100;+ const nextY =+ localDeltaY === 0+ ? point.localY+ : Math.round((point.localY + localDeltaY) * 100) / 100;++ const instruction = instructions[point.instructionIndex];+ if (instruction === undefined) {+ return;+ }++ let updated: AbsoluteInstruction;+ if (point.role === 'end') {+ if (instruction.type === 'H') {+ updated =+ nextY === point.localY+ ? {...instruction, x: nextX}+ : {type: 'L', x: nextX, y: nextY};+ } else if (instruction.type === 'V') {+ updated =+ nextX === point.localX+ ? {...instruction, y: nextY}+ : {type: 'L', x: nextX, y: nextY};+ } else if ('x' in instruction && 'y' in instruction) {+ updated = {...instruction, x: nextX, y: nextY};+ } else {+ return;+ }+ } else if (point.role === 'cp1' && instruction.type === 'C') {+ updated = {...instruction, cp1x: nextX, cp1y: nextY};+ } else if (point.role === 'cp2' && instruction.type === 'C') {+ updated = {...instruction, cp2x: nextX, cp2y: nextY};+ } else if (+ point.role === 'cp' &&+ (instruction.type === 'Q' || instruction.type === 'S')+ ) {+ updated = {...instruction, cpx: nextX, cpy: nextY};+ } else if (point.role === 'reflected-cp' && instruction.type === 'S') {+ updated = {+ type: 'C',+ cp1x: nextX,+ cp1y: nextY,+ cp2x: instruction.cpx,+ cp2y: instruction.cpy,+ x: instruction.x,+ y: instruction.y,+ };+ } else if (point.role === 'reflected-cp' && instruction.type === 'T') {+ updated = {+ type: 'Q',+ cpx: nextX,+ cpy: nextY,+ x: instruction.x,+ y: instruction.y,+ };+ } else {+ return;+ }++ const nextInstructions = instructions.slice();+ nextInstructions[point.instructionIndex] = updated;+ const nextD = serializeInstructions(nextInstructions);+ if (lastD === nextD) {+ return;+ }++ lastD = nextD;+ setDragOverrides(+ pathDrag.nodePath,+ 'd',+ Internals.makeStaticDragOverride(nextD),+ );+ },+ onEnd: (reason, endEvent) => {+ onDraggingChange(false);+ if (!isPointerSessionRelease(reason, endEvent) || lastD === originalD) {+ clearDragOverrides(pathDrag.nodePath);+ return;+ }++ saveSequenceProps({+ changes: [+ {+ fileName: pathDrag.nodePath.absolutePath,+ nodePath: pathDrag.nodePath,+ fieldKey: 'd',+ value: lastD,+ defaultValue: null,+ schema: pathDrag.schema,+ },+ ],+ addedKeyframes: null,+ movedKeyframes: null,+ setPropStatuses,+ clientId: pathDrag.clientId,+ undoLabel: 'Edit path point',+ redoLabel: 'Edit path point back',+ })+ .catch((error) => {+ showNotification(+ `Could not save path: ${error instanceof Error ? error.message : String(error)}`,+ 3000,+ );+ })+ .finally(() => clearDragOverrides(pathDrag.nodePath));+ },+ });+ };+return (- <g pointerEvents="none">+ <g>{connections.map((connection) => (<linekey={connection.key}@@ -224,18 +440,30 @@ export const SelectedOutlinePathPoints: React.FC<{y2={connection.y2}stroke={BLUE}strokeWidth={2}+ pointerEvents="none"/>))}{points.map((point) => (- <circle- key={point.key}- cx={point.x}- cy={point.y}- r={4.5}- fill={WHITE}- stroke={BLUE}- strokeWidth={2}- />+ <g key={point.key}>+ <circle+ cx={point.x}+ cy={point.y}+ r={10}+ fill="transparent"+ pointerEvents="all"+ onPointerDown={(event) => onPointPointerDown(event, point)}+ style={{cursor: 'crosshair'}}+ />+ <circle+ cx={point.x}+ cy={point.y}+ r={4.5}+ fill={WHITE}+ stroke={BLUE}+ strokeWidth={2}+ pointerEvents="none"+ />+ </g>))}</g>);diff --git a/packages/studio/src/components/SelectedOutlineRenderer.tsx b/packages/studio/src/components/SelectedOutlineRenderer.tsxindex b8aa7cbf19..e9f83fcd54 100644--- a/packages/studio/src/components/SelectedOutlineRenderer.tsx+++ b/packages/studio/src/components/SelectedOutlineRenderer.tsx@@ -264,14 +264,20 @@ const SelectedOutlineRendererUnmemoized: React.FC<{layoutTarget={targetsByKey.get(outline.key)}/>))}- {outlinesForRendering.map((outline) =>- outline.path !== null && targetsByKey.get(outline.key)?.selected ? (+ {outlinesForRendering.map((outline) => {+ const target = targetsByKey.get(outline.key);+ const pathDrag = target?.selected+ ? (getLatestOutlineTargetByKey(outline.key)?.pathDrag ?? null)+ : null;+ return outline.path !== null && pathDrag !== null ? (<SelectedOutlinePathPointskey={`${outline.key}-path-points`}outline={outline}+ pathDrag={pathDrag}+ onDraggingChange={onDraggingChange}/>- ) : null,- )}+ ) : null;+ })}{/* Render editing handles after all outline polygons so selected controls stay visible and hit-testable over unrelated sequences. */}{outlinesForEditingHandles.map((outline) => (<SelectedOutlineEditingHandlesdiff --git a/packages/studio/src/components/Timeline/TimelinePrimitiveFieldValue.tsx b/packages/studio/src/components/Timeline/TimelinePrimitiveFieldValue.tsxindex bb95a44180..328cda9b53 100644--- a/packages/studio/src/components/Timeline/TimelinePrimitiveFieldValue.tsx+++ b/packages/studio/src/components/Timeline/TimelinePrimitiveFieldValue.tsx@@ -219,7 +219,7 @@ export const TimelinePrimitiveFieldValue: React.FC<{);}- if (field.typeName === 'text-content') {+ if (field.typeName === 'text-content' || field.typeName === 'svg-path') {return (<TimelineTextContentFieldeffectiveValue={effectiveValue}diff --git a/packages/studio/src/components/Timeline/TimelineTextContentField.tsx b/packages/studio/src/components/Timeline/TimelineTextContentField.tsxindex 28a5ba25e0..50770b615f 100644--- a/packages/studio/src/components/Timeline/TimelineTextContentField.tsx+++ b/packages/studio/src/components/Timeline/TimelineTextContentField.tsx@@ -1,4 +1,5 @@-import React, {useCallback, useRef} from 'react';+import {parsePath} from '@remotion/paths';+import React, {useCallback, useRef, useState} from 'react';import type {CanUpdateSequencePropStatusStatic,SequencePropsSubscriptionKey,@@ -26,6 +27,19 @@ const textAreaStyle: React.CSSProperties = {} satisfies React.CSSProperties & {fieldSizing: 'content'}),};+const isValidPathData = (value: string): boolean => {+ if (value.trim() === '') {+ return true;+ }++ try {+ parsePath(value);+ return true;+ } catch {+ return false;+ }+};+export const TimelineTextContentField: React.FC<{readonly field: SchemaFieldInfo;readonly effectiveValue: unknown;@@ -44,6 +58,7 @@ export const TimelineTextContentField: React.FC<{propStatus,}) => {const inputRef = useRef<HTMLTextAreaElement>(null);+ const [valid, setValid] = useState(true);const currentValue = String(effectiveValue ?? '');const draftRef = useRef({dirty: false,@@ -73,6 +88,11 @@ export const TimelineTextContentField: React.FC<{}const value = inputRef.current?.value ?? draftRef.current.value;+ if (field.typeName === 'svg-path' && !isValidPathData(value)) {+ latestRef.current.onDragEnd();+ return false;+ }+const savedValue = String(latestRef.current.codeValue ?? '');draftRef.current = {dirty: false,@@ -94,7 +114,7 @@ export const TimelineTextContentField: React.FC<{.catch(() => undefined);return true;- }, []);+ }, [field.typeName]);const setInputRef = useCallback((element: HTMLTextAreaElement | null) => {@@ -124,9 +144,13 @@ export const TimelineTextContentField: React.FC<{dirty: true,value,};- latestRef.current.onDragValueChange(value);+ const nextValid = field.typeName !== 'svg-path' || isValidPathData(value);+ setValid(nextValid);+ if (nextValid) {+ latestRef.current.onDragValueChange(value);+ }},- [],+ [field.typeName],);const onKeyDownCapture = useCallback(@@ -138,6 +162,7 @@ export const TimelineTextContentField: React.FC<{value: savedValue,};event.currentTarget.value = savedValue;+ setValid(true);latestRef.current.onDragEnd();event.currentTarget.blur();}@@ -149,7 +174,8 @@ export const TimelineTextContentField: React.FC<{<RemTextareakey={String(propStatus.codeValue ?? '')}ref={setInputRef}- status="ok"+ status={valid ? 'ok' : 'error'}+ aria-invalid={!valid}smalldefaultValue={currentValue}onBlur={commitPending}diff --git a/packages/studio/src/components/Timeline/timeline-field-display-utils.ts b/packages/studio/src/components/Timeline/timeline-field-display-utils.tsindex e3bc8d8038..2e893b81b3 100644--- a/packages/studio/src/components/Timeline/timeline-field-display-utils.ts+++ b/packages/studio/src/components/Timeline/timeline-field-display-utils.ts@@ -397,6 +397,7 @@ export const formatTimelineFieldValueForDisplay = ({);case 'text-content':+ case 'svg-path':return String(value);case 'asset':diff --git a/packages/studio/src/components/Timeline/timeline-field-row-layout.ts b/packages/studio/src/components/Timeline/timeline-field-row-layout.tsindex e0c9d17ad2..eb01a77268 100644--- a/packages/studio/src/components/Timeline/timeline-field-row-layout.ts+++ b/packages/studio/src/components/Timeline/timeline-field-row-layout.ts@@ -16,6 +16,7 @@ export const isTimelineFieldStacked = ({}) => {return (field.typeName === 'text-content' ||+ field.typeName === 'svg-path' ||(transform3DMode &&(field.typeName === 'translate' ||field.typeName === 'rotation-css' ||diff --git a/packages/studio/src/components/selected-outline-types.ts b/packages/studio/src/components/selected-outline-types.tsindex 6de0bf5386..fb05c765f2 100644--- a/packages/studio/src/components/selected-outline-types.ts+++ b/packages/studio/src/components/selected-outline-types.ts@@ -33,6 +33,7 @@ export type SelectedOutlineLayoutTarget = CanvasOutlineLayoutTarget & {export type SelectedOutlineTarget = SelectedOutlineLayoutTarget & {readonly canCrop: boolean;+ readonly pathDrag: SelectedOutlinePathDragTarget | null;readonly cropDrag: SelectedOutlineCropDragTarget | null;readonly drag: SelectedOutlineDragTarget | null;readonly scaleDrag: SelectedOutlineScaleDragTarget | null;@@ -40,6 +41,12 @@ export type SelectedOutlineTarget = SelectedOutlineLayoutTarget & {readonly transformOriginDrag: SelectedOutlineTransformOriginDragTarget | null;};+export type SelectedOutlinePathDragTarget = {+ readonly clientId: string;+ readonly nodePath: SequencePropsSubscriptionKey;+ readonly schema: InteractivitySchema;+};+export const cropFieldKeys = {left: 'cropLeft',right: 'cropRight',