Skip to content

🎥 Make videos programmatically with React

rotation control

Jonny Burger31e186d53df0
diff --git a/packages/core/src/sequence-field-schema.ts b/packages/core/src/sequence-field-schema.ts
index 8b2315b456..0b2a0e163a 100644
--- a/packages/core/src/sequence-field-schema.ts
+++ b/packages/core/src/sequence-field-schema.ts
@@ -13,7 +13,17 @@ export type BooleanFieldSchema = {
description?: string;
};
-export type SequenceFieldSchema = NumberFieldSchema | BooleanFieldSchema;
+export type RotationFieldSchema = {
+ type: 'rotation';
+ step?: number;
+ default: string | undefined;
+ description?: string;
+};
+
+export type SequenceFieldSchema =
+ | NumberFieldSchema
+ | BooleanFieldSchema
+ | RotationFieldSchema;
export type SequenceSchema = Record<string, SequenceFieldSchema>;
diff --git a/packages/light-leaks/src/LightLeak.tsx b/packages/light-leaks/src/LightLeak.tsx
index e0526d1892..a4b0e09d53 100644
--- a/packages/light-leaks/src/LightLeak.tsx
+++ b/packages/light-leaks/src/LightLeak.tsx
@@ -259,6 +259,12 @@ const lightLeakSchema = {
default: 1,
description: 'Opacity',
},
+ 'style.rotate': {
+ type: 'rotation',
+ step: 1,
+ default: '0deg',
+ description: 'Rotation',
+ },
} as const satisfies SequenceSchema;
const LightLeakInner: React.FC<
diff --git a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
index 51a4cba6dd..f516ef079e 100644
--- a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
@@ -125,6 +125,111 @@ const TimelineNumberField: React.FC<{
);
};
+const parseCssRotationToDegrees = (value: string): number => {
+ try {
+ const m = new DOMMatrix(`rotate(${value})`);
+ return Math.round(Math.atan2(m.b, m.a) * (180 / Math.PI) * 1e6) / 1e6;
+ } catch {
+ return 0;
+ }
+};
+
+const TimelineRotationField: React.FC<{
+ readonly field: SchemaFieldInfo;
+ readonly codeValue: unknown;
+ readonly canUpdate: boolean;
+ readonly onSave: (key: string, value: unknown) => Promise<void>;
+ readonly onDragValueChange: (key: string, value: unknown) => void;
+ readonly onDragEnd: () => void;
+}> = ({field, codeValue, canUpdate, onSave, onDragValueChange, onDragEnd}) => {
+ const [dragValue, setDragValue] = useState<number | null>(null);
+ const dragging = useRef(false);
+
+ const degrees = useMemo(
+ () => parseCssRotationToDegrees(String(codeValue ?? '0deg')),
+ [codeValue],
+ );
+
+ const onValueChange = useCallback(
+ (newVal: number) => {
+ dragging.current = true;
+ setDragValue(newVal);
+ onDragValueChange(field.key, `${newVal}deg`);
+ },
+ [onDragValueChange, field.key],
+ );
+
+ useEffect(() => {
+ setDragValue(null);
+ onDragEnd();
+ }, [field.currentValue, onDragEnd]);
+
+ const onValueChangeEnd = useCallback(
+ (newVal: number) => {
+ const newStr = `${newVal}deg`;
+ if (canUpdate && newStr !== codeValue) {
+ onSave(field.key, newStr).catch(() => {
+ setDragValue(null);
+ });
+ } else {
+ setDragValue(null);
+ }
+ },
+ [canUpdate, onSave, field.key, codeValue],
+ );
+
+ const onTextChange = useCallback(
+ (newVal: string) => {
+ if (canUpdate) {
+ const parsed = Number(newVal);
+ if (!Number.isNaN(parsed)) {
+ const newStr = `${parsed}deg`;
+ if (newStr !== codeValue) {
+ setDragValue(parsed);
+ onSave(field.key, newStr).catch(() => {
+ setDragValue(null);
+ });
+ }
+ }
+ }
+ },
+ [canUpdate, onSave, field.key, codeValue],
+ );
+
+ const step =
+ field.fieldSchema.type === 'rotation' ? (field.fieldSchema.step ?? 1) : 1;
+
+ const stepDecimals = useMemo(() => getDecimalPlaces(step), [step]);
+
+ const formatter = useCallback(
+ (v: number | string) => {
+ const num = Number(v);
+ const digits = Math.max(stepDecimals, getDecimalPlaces(num));
+ const formatted = digits === 0 ? String(num) : num.toFixed(digits);
+ return `${formatted}\u00B0`;
+ },
+ [stepDecimals],
+ );
+
+ return (
+ <InputDragger
+ type="number"
+ value={dragValue ?? degrees}
+ style={draggerStyle}
+ status="ok"
+ small
+ onValueChange={onValueChange}
+ onValueChangeEnd={onValueChangeEnd}
+ onTextChange={onTextChange}
+ min={-Infinity}
+ max={Infinity}
+ step={step}
+ formatter={formatter}
+ rightAlign={false}
+ />
+ );
+};
+
const TimelineBooleanField: React.FC<{
readonly field: SchemaFieldInfo;
readonly codeValue: unknown;
@@ -204,6 +309,21 @@ export const TimelineFieldValue: React.FC<{
);
}
+ if (field.typeName === 'rotation') {
+ return (
+ <span style={wrapperStyle}>
+ <TimelineRotationField
+ field={field}
+ codeValue={effectiveValue}
+ canUpdate={canUpdate}
+ onSave={onSave}
+ onDragValueChange={onDragValueChange}
+ onDragEnd={onDragEnd}
+ />
+ </span>
+ );
+ }
+
if (field.typeName === 'boolean') {
return (
<span style={wrapperStyle}>
diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.ts
index 375902aa5b..cfc4f56551 100644
--- a/packages/studio/src/helpers/timeline-layout.ts
+++ b/packages/studio/src/helpers/timeline-layout.ts
@@ -9,7 +9,7 @@ export const TIMELINE_TRACK_EXPANDED_HEIGHT = 100;
export const SCHEMA_FIELD_ROW_HEIGHT = 22;
export const UNSUPPORTED_FIELD_ROW_HEIGHT = 22;
-const SUPPORTED_SCHEMA_TYPES = new Set(['number', 'boolean']);
+const SUPPORTED_SCHEMA_TYPES = new Set(['number', 'boolean', 'rotation']);
export type SchemaFieldInfo = {
key: string;