Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix: restore native cursor preview and export

EtienneLescotd21e5eb34c63
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index 840101da9..432958a0a 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -28,6 +28,7 @@ import {
import {
hasNativeCursorRecordingData,
projectNativeCursorToLocal,
+ projectNativeCursorToStage,
resolveInterpolatedNativeCursorFrame,
resolveNativeCursorRenderAsset,
} from "@/lib/cursor/nativeCursor";
@@ -243,7 +244,6 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
const videoSpriteRef = useRef<Sprite | null>(null);
const videoContainerRef = useRef<Container | null>(null);
const cameraContainerRef = useRef<Container | null>(null);
- const cursorContainerRef = useRef<Container | null>(null);
const timeUpdateAnimationRef = useRef<number | null>(null);
const [pixiReady, setPixiReady] = useState(false);
const [videoReady, setVideoReady] = useState(false);
@@ -836,17 +836,6 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
videoContainerRef.current = videoContainer;
cameraContainer.addChild(videoContainer);
- // Cursor container - rendered above video
- const cursorContainer = new Container();
- cursorContainerRef.current = cursorContainer;
- cameraContainer.addChild(cursorContainer);
-
- const nativeCursorSprite = new Sprite(Texture.EMPTY);
- nativeCursorSprite.visible = false;
- nativeCursorSprite.eventMode = "none";
- nativeCursorSpriteRef.current = nativeCursorSprite;
- cursorContainer.addChild(nativeCursorSprite);
-
// Cursor overlay - rendered above the masked video
if (cursorOverlayEnabled) {
const cursorOverlay = new PixiCursorOverlay({
@@ -856,7 +845,6 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
clickBounce: cursorClickBounceRef.current,
});
cursorOverlayRef.current = cursorOverlay;
- cursorContainer.addChild(cursorOverlay.container);
}
setPixiReady(true);
@@ -871,6 +859,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
}
nativeCursorSpriteRef.current = null;
nativeCursorTextureIdRef.current = null;
+ nativeCursorImageIdRef.current = null;
if (app && app.renderer) {
app.destroy(true, {
children: true,
@@ -881,7 +870,6 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
appRef.current = null;
cameraContainerRef.current = null;
videoContainerRef.current = null;
- cursorContainerRef.current = null;
videoSpriteRef.current = null;
};
}, []);
@@ -920,9 +908,8 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
const video = videoRef.current;
const app = appRef.current;
const videoContainer = videoContainerRef.current;
- const cursorContainer = cursorContainerRef.current;
- if (!video || !app || !videoContainer || !cursorContainer) return;
+ if (!video || !app || !videoContainer) return;
if (video.videoWidth === 0 || video.videoHeight === 0) return;
const source = VideoSource.from(video);
@@ -942,8 +929,12 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
videoContainer.addChild(maskGraphics);
videoContainer.mask = maskGraphics;
maskGraphicsRef.current = maskGraphics;
+ const nativeCursorSprite = new Sprite(Texture.EMPTY);
+ nativeCursorSprite.visible = false;
+ nativeCursorSprite.eventMode = "none";
+ nativeCursorSpriteRef.current = nativeCursorSprite;
if (cursorOverlayRef.current) {
- cursorContainer.addChild(cursorOverlayRef.current.container);
+ videoContainer.addChild(cursorOverlayRef.current.container);
}
const cursorHighlightGraphics = new Graphics();
@@ -951,6 +942,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
videoContainer.addChild(cursorHighlightGraphics);
cursorHighlightGraphicsRef.current = cursorHighlightGraphics;
drawCursorHighlightGraphics(cursorHighlightGraphics, cursorHighlightRef.current);
+ videoContainer.addChild(nativeCursorSprite);
animationStateRef.current = {
scale: 1,
@@ -1019,6 +1011,12 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
cursorHighlightGraphicsRef.current.destroy();
cursorHighlightGraphicsRef.current = null;
}
+ if (nativeCursorSpriteRef.current) {
+ videoContainer.removeChild(nativeCursorSpriteRef.current);
+ nativeCursorSpriteRef.current.destroy();
+ nativeCursorSpriteRef.current = null;
+ nativeCursorTextureIdRef.current = null;
+ }
videoContainer.mask = null;
maskGraphicsRef.current = null;
if (blurFilterRef.current) {
@@ -1304,53 +1302,88 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
);
}
- // Update native cursor sprite in the same PIXI coordinate space as the video.
+ // Keep the native cursor preview in the same transformed coordinate space as PIXI.
const nativeCursorSprite = nativeCursorSpriteRef.current;
- if (nativeCursorSprite) {
- const videoContainerRc = videoContainerRef.current;
- if (hasNativeCursorRecordingRef.current && showCursorRef.current && videoContainerRc) {
+ const nativeCursorImage = nativeCursorImageRef.current;
+ const hideNativeCursorPreview = () => {
+ if (nativeCursorSprite) {
+ nativeCursorSprite.visible = false;
+ }
+ if (nativeCursorImage) {
+ nativeCursorImage.style.display = "none";
+ }
+ };
+ if (nativeCursorImage) {
+ if (hasNativeCursorRecordingRef.current && showCursorRef.current) {
const timeMs = currentTimeRef.current; // already in ms
const frame = resolveInterpolatedNativeCursorFrame(
cursorRecordingDataRef.current,
timeMs,
);
if (frame) {
- const projectedPoint = projectNativeCursorToLocal({
- cropRegion: cropRegionRef.current ?? { x: 0, y: 0, width: 1, height: 1 },
+ const cameraContainer = cameraContainerRef.current;
+ const videoContainer = videoContainerRef.current;
+ const cropRegionValue = cropRegionRef.current ?? { x: 0, y: 0, width: 1, height: 1 };
+ const projectedLocalPoint = projectNativeCursorToLocal({
+ cropRegion: cropRegionValue,
maskRect: baseMaskRef.current,
- videoContainerPosition: {
- x: videoContainerRc.x,
- y: videoContainerRc.y,
- },
sample: frame.sample,
});
- if (projectedPoint) {
+ const projectedStagePoint =
+ cameraContainer && videoContainer
+ ? projectNativeCursorToStage({
+ cameraContainer,
+ cropRegion: cropRegionValue,
+ maskRect: baseMaskRef.current,
+ videoContainerPosition: {
+ x: videoContainer.x,
+ y: videoContainer.y,
+ },
+ sample: frame.sample,
+ })
+ : null;
+ if (projectedLocalPoint && projectedStagePoint) {
const renderAsset = resolveNativeCursorRenderAsset(
frame.asset,
window.devicePixelRatio || 1,
frame.sample,
);
const scale = Math.max(0, cursorSizeRef.current);
- if (nativeCursorTextureIdRef.current !== renderAsset.id) {
- nativeCursorSprite.texture = Texture.from(renderAsset.imageDataUrl);
- nativeCursorTextureIdRef.current = renderAsset.id;
+ const transformedScale = scale * Math.abs(cameraContainer?.scale.x || 1);
+ if (nativeCursorImageIdRef.current !== renderAsset.id) {
+ nativeCursorImage.src = renderAsset.imageDataUrl;
+ nativeCursorImageIdRef.current = renderAsset.id;
+ }
+ nativeCursorImage.style.display = "block";
+ nativeCursorImage.style.width = `${renderAsset.width * transformedScale}px`;
+ nativeCursorImage.style.height = `${renderAsset.height * transformedScale}px`;
+ nativeCursorImage.style.transform = `translate3d(${
+ projectedStagePoint.x - renderAsset.hotspotX * transformedScale
+ }px, ${projectedStagePoint.y - renderAsset.hotspotY * transformedScale}px, 0)`;
+ if (nativeCursorSprite) {
+ nativeCursorSprite.visible = false;
+ if (nativeCursorTextureIdRef.current !== renderAsset.id) {
+ nativeCursorSprite.texture = Texture.from(renderAsset.imageDataUrl);
+ nativeCursorTextureIdRef.current = renderAsset.id;
+ }
+ nativeCursorSprite.position.set(
+ projectedLocalPoint.x - renderAsset.hotspotX * scale,
+ projectedLocalPoint.y - renderAsset.hotspotY * scale,
+ );
+ nativeCursorSprite.width = renderAsset.width * scale;
+ nativeCursorSprite.height = renderAsset.height * scale;
}
- nativeCursorSprite.position.set(
- projectedPoint.x - renderAsset.hotspotX * scale,
- projectedPoint.y - renderAsset.hotspotY * scale,
- );
- nativeCursorSprite.width = renderAsset.width * scale;
- nativeCursorSprite.height = renderAsset.height * scale;
- nativeCursorSprite.visible = true;
} else {
- nativeCursorSprite.visible = false;
+ hideNativeCursorPreview();
}
} else {
- nativeCursorSprite.visible = false;
+ hideNativeCursorPreview();
}
} else {
- nativeCursorSprite.visible = false;
+ hideNativeCursorPreview();
}
+ } else {
+ hideNativeCursorPreview();
}
const composite3D = composite3DRef.current;
@@ -1584,6 +1617,18 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
: "none",
}}
/>
+ <img
+ ref={nativeCursorImageRef}
+ alt=""
+ aria-hidden="true"
+ className="absolute left-0 top-0 select-none"
+ style={{
+ display: "none",
+ pointerEvents: "none",
+ transformOrigin: "0 0",
+ zIndex: 18,
+ }}
+ />
{webcamVideoPath &&
(() => {
const clipPath = getCssClipPath(webcamLayout?.maskShape ?? "rectangle");
diff --git a/src/lib/cursor/nativeCursor.ts b/src/lib/cursor/nativeCursor.ts
index 5c38c4d90..3b62cb854 100644
--- a/src/lib/cursor/nativeCursor.ts
+++ b/src/lib/cursor/nativeCursor.ts
@@ -29,12 +29,12 @@ export interface ActiveNativeCursorFrame {
interface ProjectNativeCursorOptions {
cropRegion: CropRegion;
maskRect: { x: number; y: number; width: number; height: number };
- videoContainerPosition: { x: number; y: number };
sample: CursorRecordingSample;
}
interface ProjectNativeCursorToStageOptions extends ProjectNativeCursorOptions {
cameraContainer: Container;
+ videoContainerPosition: { x: number; y: number };
}
function clamp(value: number, min: number, max: number) {
@@ -179,6 +179,15 @@ function getCroppedCursorPosition(sample: CursorRecordingSample, cropRegion: Cro
};
}
+function getNativeCursorMaskPoint(sample: CursorRecordingSample, cropRegion: CropRegion) {
+ const croppedPosition = getCroppedCursorPosition(sample, cropRegion);
+ if (!croppedPosition) {
+ return null;
+ }
+
+ return new Point(croppedPosition.cx, croppedPosition.cy);
+}
+
export function resolveActiveNativeCursorFrame(
recordingData: CursorRecordingData | null | undefined,
timeMs: number,
@@ -270,29 +279,32 @@ export function resolveInterpolatedNativeCursorFrame(
export function projectNativeCursorToLocal({
cropRegion,
maskRect,
- videoContainerPosition,
sample,
}: ProjectNativeCursorOptions) {
- const croppedPosition = getCroppedCursorPosition(sample, cropRegion);
- if (!croppedPosition) {
+ const maskPoint = getNativeCursorMaskPoint(sample, cropRegion);
+ if (!maskPoint) {
return null;
}
return new Point(
- videoContainerPosition.x + maskRect.x + croppedPosition.cx * maskRect.width,
- videoContainerPosition.y + maskRect.y + croppedPosition.cy * maskRect.height,
+ maskRect.x + maskPoint.x * maskRect.width,
+ maskRect.y + maskPoint.y * maskRect.height,
);
}
export function projectNativeCursorToStage({
cameraContainer,
+ videoContainerPosition,
...options
}: ProjectNativeCursorToStageOptions) {
const localPoint = projectNativeCursorToLocal(options);
if (!localPoint) {
return null;
}
- return cameraContainer.toGlobal(localPoint);
+
+ return cameraContainer.toGlobal(
+ new Point(localPoint.x + videoContainerPosition.x, localPoint.y + videoContainerPosition.y),
+ );
}
export function getNativeCursorDisplayMetrics(asset: NativeCursorAsset, deviceScaleFactor: number) {
diff --git a/src/lib/exporter/frameRenderer.ts b/src/lib/exporter/frameRenderer.ts
index a1e20ccd2..7f7513cc4 100644
--- a/src/lib/exporter/frameRenderer.ts
+++ b/src/lib/exporter/frameRenderer.ts
@@ -57,7 +57,7 @@ import {
type StyledRenderRect,
} from "@/lib/compositeLayout";
import {
- projectNativeCursorToStage,
+ projectNativeCursorToLocal,
resolveInterpolatedNativeCursorFrame,
resolveNativeCursorRenderAsset,
} from "@/lib/cursor/nativeCursor";
@@ -555,7 +555,7 @@ export class FrameRenderer {
}
private async drawNativeCursor(timeMs: number) {
- if (!this.compositeCtx || !this.cameraContainer || !this.videoContainer || !this.layoutCache) {
+ if (!this.foregroundCtx || !this.layoutCache) {
return;
}
@@ -571,14 +571,9 @@ export class FrameRenderer {
return;
}
- const projectedPoint = projectNativeCursorToStage({
- cameraContainer: this.cameraContainer,
+ const projectedPoint = projectNativeCursorToLocal({
cropRegion: this.config.cropRegion,
maskRect: this.layoutCache.maskRect,
- videoContainerPosition: {
- x: this.videoContainer.x,
- y: this.videoContainer.y,
- },
sample: activeNativeCursor.sample,
});
if (!projectedPoint) {
@@ -592,12 +587,15 @@ export class FrameRenderer {
);
const image = await this.getCursorImage(renderAsset);
const scale = Math.max(0, this.config.cursorScale ?? 1);
- this.compositeCtx.drawImage(
+ const appliedScale = this.animationState.appliedScale;
+ const canvasX = projectedPoint.x * appliedScale + this.animationState.x;
+ const canvasY = projectedPoint.y * appliedScale + this.animationState.y;
+ this.foregroundCtx.drawImage(
image,
- projectedPoint.x - renderAsset.hotspotX * scale,
- projectedPoint.y - renderAsset.hotspotY * scale,
- renderAsset.width * scale,
- renderAsset.height * scale,
+ canvasX - renderAsset.hotspotX * scale * appliedScale,
+ canvasY - renderAsset.hotspotY * scale * appliedScale,
+ renderAsset.width * scale * appliedScale,
+ renderAsset.height * scale * appliedScale,
);
}