Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Anchor annotations to recording coordinates

webadderallb2cac9aafb52
diff --git a/src/components/video-editor/AnnotationOverlay.tsx b/src/components/video-editor/AnnotationOverlay.tsx
index dd96d32a..c194e9a5 100644
--- a/src/components/video-editor/AnnotationOverlay.tsx
+++ b/src/components/video-editor/AnnotationOverlay.tsx
@@ -4,17 +4,39 @@ import { cn } from "@/lib/utils";
import { getArrowComponent } from "./ArrowSvgs";
import { type AnnotationRegion, BASE_PREVIEW_WIDTH, BLUR_ANNOTATION_STRENGTH } from "./types";
+type Rect = {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+};
+
+type SceneTransform = {
+ scale: number;
+ x: number;
+ y: number;
+};
+
interface AnnotationOverlayProps {
annotation: AnnotationRegion;
isSelected: boolean;
containerWidth: number;
containerHeight: number;
+ recordingRect: Rect;
+ sceneTransform: SceneTransform;
onPositionChange: (id: string, position: { x: number; y: number }) => void;
onSizeChange: (id: string, size: { width: number; height: number }) => void;
onClick: (id: string) => void;
zIndex: number;
isSelectedBoost: boolean; // Boost z-index when selected for easy editing
- scale?: number;
+}
+
+function clampPercent(value: number) {
+ if (!Number.isFinite(value)) {
+ return 0;
+ }
+
+ return Math.min(100, Math.max(0, value));
}
export function AnnotationOverlay({
@@ -22,20 +44,55 @@ export function AnnotationOverlay({
isSelected,
containerWidth,
containerHeight,
+ recordingRect,
+ sceneTransform,
onPositionChange,
onSizeChange,
onClick,
zIndex,
isSelectedBoost,
- scale = 1,
}: AnnotationOverlayProps) {
- const x = (annotation.position.x / 100) * containerWidth;
- const y = (annotation.position.y / 100) * containerHeight;
- const width = (annotation.size.width / 100) * containerWidth;
- const height = (annotation.size.height / 100) * containerHeight;
+ const safeRecordingRect =
+ recordingRect.width > 0 && recordingRect.height > 0
+ ? recordingRect
+ : { x: 0, y: 0, width: containerWidth, height: containerHeight };
+ const sceneX = safeRecordingRect.x + (annotation.position.x / 100) * safeRecordingRect.width;
+ const sceneY = safeRecordingRect.y + (annotation.position.y / 100) * safeRecordingRect.height;
+ const sceneWidth = (annotation.size.width / 100) * safeRecordingRect.width;
+ const sceneHeight = (annotation.size.height / 100) * safeRecordingRect.height;
+ const x = sceneX * sceneTransform.scale + sceneTransform.x;
+ const y = sceneY * sceneTransform.scale + sceneTransform.y;
+ const width = sceneWidth * sceneTransform.scale;
+ const height = sceneHeight * sceneTransform.scale;
+ const sizeScale = safeRecordingRect.width / BASE_PREVIEW_WIDTH;
+ const blurScaleFactor = sizeScale * sceneTransform.scale;
const isDraggingRef = useRef(false);
+ const screenRectToRecordingPercent = (rect: Rect) => {
+ const nextSceneX = (rect.x - sceneTransform.x) / sceneTransform.scale;
+ const nextSceneY = (rect.y - sceneTransform.y) / sceneTransform.scale;
+ const nextSceneWidth = rect.width / sceneTransform.scale;
+ const nextSceneHeight = rect.height / sceneTransform.scale;
+
+ return {
+ position: {
+ x: clampPercent(
+ ((nextSceneX - safeRecordingRect.x) / Math.max(1, safeRecordingRect.width)) *
+ 100,
+ ),
+ y: clampPercent(
+ ((nextSceneY - safeRecordingRect.y) / Math.max(1, safeRecordingRect.height)) *
+ 100,
+ ),
+ },
+ size: {
+ width: clampPercent((nextSceneWidth / Math.max(1, safeRecordingRect.width)) * 100),
+ height: clampPercent((nextSceneHeight / Math.max(1, safeRecordingRect.height)) * 100),
+ },
+ };
+ };
+
const renderArrow = () => {
const direction = annotation.figureData?.arrowDirection || "right";
const color = annotation.figureData?.color || "#2563EB";
@@ -50,7 +107,7 @@ export function AnnotationOverlay({
case "text":
return (
<div
- className="w-full h-full flex items-center p-2 overflow-hidden"
+ className="w-full h-full flex items-center overflow-hidden"
style={{
justifyContent:
annotation.style.textAlign === "left"
@@ -59,13 +116,14 @@ export function AnnotationOverlay({
? "flex-end"
: "center",
alignItems: "center",
+ padding: `${8 * sceneTransform.scale}px`,
}}
>
<span
style={{
color: annotation.style.color,
backgroundColor: annotation.style.backgroundColor,
- fontSize: `${annotation.style.fontSize}px`,
+ fontSize: `${annotation.style.fontSize * sceneTransform.scale}px`,
fontFamily: annotation.style.fontFamily,
fontWeight: annotation.style.fontWeight,
fontStyle: annotation.style.fontStyle,
@@ -76,7 +134,7 @@ export function AnnotationOverlay({
boxDecorationBreak: "clone",
WebkitBoxDecorationBreak: "clone",
padding: "0.1em 0.2em",
- borderRadius: "4px",
+ borderRadius: `${4 * sceneTransform.scale}px`,
lineHeight: "1.4",
}}
>
@@ -118,9 +176,8 @@ export function AnnotationOverlay({
);
case "blur": {
- const previewScaleFactor = containerWidth / BASE_PREVIEW_WIDTH;
const currentBlurStrength = annotation.blurIntensity ?? BLUR_ANNOTATION_STRENGTH;
- const blurPx = currentBlurStrength * previewScaleFactor;
+ const blurPx = currentBlurStrength * blurScaleFactor;
const blurStyle = `blur(${blurPx}px)`;
return (
@@ -130,7 +187,7 @@ export function AnnotationOverlay({
backdropFilter: blurStyle,
WebkitBackdropFilter: blurStyle,
backgroundColor: annotation.blurColor || "transparent",
- borderRadius: `${(annotation.style.borderRadius ?? 0) * previewScaleFactor}px`,
+ borderRadius: `${(annotation.style.borderRadius ?? 0) * blurScaleFactor}px`,
}}
/>
);
@@ -145,14 +202,12 @@ export function AnnotationOverlay({
<Rnd
position={{ x, y }}
size={{ width, height }}
- scale={scale}
onDragStart={() => {
isDraggingRef.current = true;
}}
onDragStop={(_e, d) => {
- const xPercent = (d.x / containerWidth) * 100;
- const yPercent = (d.y / containerHeight) * 100;
- onPositionChange(annotation.id, { x: xPercent, y: yPercent });
+ const next = screenRectToRecordingPercent({ x: d.x, y: d.y, width, height });
+ onPositionChange(annotation.id, next.position);
// Reset dragging flag after a short delay to prevent click event
setTimeout(() => {
@@ -160,12 +215,14 @@ export function AnnotationOverlay({
}, 100);
}}
onResizeStop={(_e, _direction, ref, _delta, position) => {
- const xPercent = (position.x / containerWidth) * 100;
- const yPercent = (position.y / containerHeight) * 100;
- const widthPercent = (ref.offsetWidth / containerWidth) * 100;
- const heightPercent = (ref.offsetHeight / containerHeight) * 100;
- onPositionChange(annotation.id, { x: xPercent, y: yPercent });
- onSizeChange(annotation.id, { width: widthPercent, height: heightPercent });
+ const next = screenRectToRecordingPercent({
+ x: position.x,
+ y: position.y,
+ width: ref.offsetWidth,
+ height: ref.offsetHeight,
+ });
+ onPositionChange(annotation.id, next.position);
+ onSizeChange(annotation.id, next.size);
}}
onClick={() => {
if (isDraggingRef.current) return;
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index f99a03a2..415212a6 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -2809,7 +2809,6 @@ export default function VideoEditor() {
}
setAutoCaptions(result.cues);
- setAutoCaptionSettings((prev) => ({ ...prev, enabled: true }));
toast.success(result.message || `Generated ${result.cues.length} captions`);
} catch (error) {
toast.error(getErrorMessage(error));
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index 729c2939..829ad795 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -206,6 +206,13 @@ type SceneTransformState = {
y: number;
};
+type AnnotationRecordingRect = {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+};
+
function createPlaybackAnimationState(): PlaybackAnimationState {
return {
scale: 1,
@@ -513,6 +520,13 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
x: 0,
y: 0,
});
+ const [annotationRecordingRect, setAnnotationRecordingRect] =
+ useState<AnnotationRecordingRect>({
+ x: 0,
+ y: 0,
+ width: 0,
+ height: 0,
+ });
useEffect(() => {
let framesSignature = getRegisteredFramesSignature();
@@ -1140,6 +1154,23 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
renderWidth: result.maskRect.width * renderResolution,
renderHeight: result.maskRect.height * renderResolution,
};
+ setAnnotationRecordingRect((current) => {
+ if (
+ Math.abs(current.x - result.maskRect.x) < 0.1 &&
+ Math.abs(current.y - result.maskRect.y) < 0.1 &&
+ Math.abs(current.width - result.maskRect.width) < 0.1 &&
+ Math.abs(current.height - result.maskRect.height) < 0.1
+ ) {
+ return current;
+ }
+
+ return {
+ x: result.maskRect.x,
+ y: result.maskRect.y,
+ width: result.maskRect.width,
+ height: result.maskRect.height,
+ };
+ });
cropBoundsRef.current = result.cropBounds;
// Sync extension cursor effects canvas resolution with renderer
@@ -3331,14 +3362,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
</div>
</div>
) : null}
- <div
- className="absolute inset-0"
- style={{
- pointerEvents: "none",
- transform: `translate(${annotationSceneTransform.x}px, ${annotationSceneTransform.y}px) scale(${annotationSceneTransform.scale})`,
- transformOrigin: "top left",
- }}
- >
+ <div className="absolute inset-0" style={{ pointerEvents: "none" }}>
{(() => {
const filtered = (annotationRegions || []).filter((annotation) => {
if (
@@ -3383,6 +3407,8 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
isSelected={annotation.id === selectedAnnotationId}
containerWidth={overlayRef.current?.clientWidth || 800}
containerHeight={overlayRef.current?.clientHeight || 600}
+ recordingRect={annotationRecordingRect}
+ sceneTransform={annotationSceneTransform}
onPositionChange={(id, position) =>
onAnnotationPositionChange?.(id, position)
}
@@ -3392,7 +3418,6 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
onClick={handleAnnotationClick}
zIndex={annotation.zIndex}
isSelectedBoost={annotation.id === selectedAnnotationId}
- scale={annotationSceneTransform.scale}
/>
));
})()}
diff --git a/src/lib/exporter/annotationRenderer.ts b/src/lib/exporter/annotationRenderer.ts
index bb06e605..773803fb 100644
--- a/src/lib/exporter/annotationRenderer.ts
+++ b/src/lib/exporter/annotationRenderer.ts
@@ -14,6 +14,13 @@ interface AnnotationSceneTransform {
y: number;
}
+interface AnnotationCoordinateRect {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+}
+
function transformAnnotationRect(
rect: { x: number; y: number; width: number; height: number },
sceneTransform?: AnnotationSceneTransform,
@@ -357,20 +364,22 @@ export async function renderAnnotations(
scaleFactor: number = 1.0,
assets?: AnnotationRenderAssets,
sceneTransform?: AnnotationSceneTransform,
+ coordinateRect?: AnnotationCoordinateRect,
): Promise<void> {
const activeAnnotations = annotations.filter(
(ann) => currentTimeMs >= ann.startMs && currentTimeMs <= ann.endMs,
);
const sortedAnnotations = [...activeAnnotations].sort((a, b) => a.zIndex - b.zIndex);
+ const annotationRect = coordinateRect ?? { x: 0, y: 0, width: canvasWidth, height: canvasHeight };
for (const annotation of sortedAnnotations) {
const rect = transformAnnotationRect(
{
- x: (annotation.position.x / 100) * canvasWidth,
- y: (annotation.position.y / 100) * canvasHeight,
- width: (annotation.size.width / 100) * canvasWidth,
- height: (annotation.size.height / 100) * canvasHeight,
+ x: annotationRect.x + (annotation.position.x / 100) * annotationRect.width,
+ y: annotationRect.y + (annotation.position.y / 100) * annotationRect.height,
+ width: (annotation.size.width / 100) * annotationRect.width,
+ height: (annotation.size.height / 100) * annotationRect.height,
},
sceneTransform,
);
diff --git a/src/lib/exporter/frameRenderer.ts b/src/lib/exporter/frameRenderer.ts
index 439cb4ae..4ee42e6a 100644
--- a/src/lib/exporter/frameRenderer.ts
+++ b/src/lib/exporter/frameRenderer.ts
@@ -1549,6 +1549,7 @@ export class FrameRenderer {
scaleFactor,
undefined,
temporalSnapshot.sceneTransform,
+ this.layoutCache?.maskRect,
);
}
@@ -1739,6 +1740,7 @@ export class FrameRenderer {
x: this.animationState.x,
y: this.animationState.y,
},
+ this.layoutCache?.maskRect,
);
}
diff --git a/src/lib/exporter/modernFrameRenderer.ts b/src/lib/exporter/modernFrameRenderer.ts
index ed6d0768..028dcbad 100644
--- a/src/lib/exporter/modernFrameRenderer.ts
+++ b/src/lib/exporter/modernFrameRenderer.ts
@@ -1591,6 +1591,7 @@ export class FrameRenderer {
x: this.animationState.x,
y: this.animationState.y,
},
+ this.layoutCache?.maskRect,
);
this.drawCaptionOverlay(context);
@@ -1614,10 +1615,16 @@ export class FrameRenderer {
);
for (const annotation of annotations) {
- const x = (annotation.position.x / 100) * this.config.width;
- const y = (annotation.position.y / 100) * this.config.height;
- const width = (annotation.size.width / 100) * this.config.width;
- const height = (annotation.size.height / 100) * this.config.height;
+ const annotationRect = this.layoutCache?.maskRect ?? {
+ x: 0,
+ y: 0,
+ width: this.config.width,
+ height: this.config.height,
+ };
+ const x = annotationRect.x + (annotation.position.x / 100) * annotationRect.width;
+ const y = annotationRect.y + (annotation.position.y / 100) * annotationRect.height;
+ const width = (annotation.size.width / 100) * annotationRect.width;
+ const height = (annotation.size.height / 100) * annotationRect.height;
if (width <= 0 || height <= 0) {
continue;