Create polished demo videos without editing skills. Mac/Windows/Linux
fix crop with zoom and camera frame
Siddharth98d3c3b6bd56
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsxindex 190ace1f..5309532e 100644--- a/src/components/video-editor/VideoPlayback.tsx+++ b/src/components/video-editor/VideoPlayback.tsx@@ -58,6 +58,8 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({const appRef = useRef<PIXI.Application | null>(null);const videoSpriteRef = useRef<PIXI.Sprite | null>(null);const videoContainerRef = useRef<PIXI.Container | null>(null);+ const cameraContainerRef = useRef<PIXI.Container | null>(null);+ const backgroundSpriteRef = useRef<PIXI.Sprite | null>(null);const timeUpdateAnimationRef = useRef<number | null>(null);const [pixiReady, setPixiReady] = useState(false);const [videoReady, setVideoReady] = useState(false);@@ -74,6 +76,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({const baseScaleRef = useRef(1);const baseOffsetRef = useRef({ x: 0, y: 0 });const baseMaskRef = useRef({ x: 0, y: 0, width: 0, height: 0 });+ const cropBoundsRef = useRef({ startX: 0, endX: 0, startY: 0, endY: 0 });const maskGraphicsRef = useRef<PIXI.Graphics | null>(null);const isPlayingRef = useRef(isPlaying);const isSeekingRef = useRef(false);@@ -115,8 +118,10 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({const videoSprite = videoSpriteRef.current;const maskGraphics = maskGraphicsRef.current;const videoElement = videoRef.current;+ const backgroundSprite = backgroundSpriteRef.current;+ const cameraContainer = cameraContainerRef.current;- if (!container || !app || !videoSprite || !maskGraphics || !videoElement) {+ if (!container || !app || !videoSprite || !maskGraphics || !videoElement || !cameraContainer) {return;}@@ -135,6 +140,18 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({baseScaleRef.current = result.baseScale;baseOffsetRef.current = result.baseOffset;baseMaskRef.current = result.maskRect;+ cropBoundsRef.current = result.cropBounds;++ // Size and position background to match stage+ if (backgroundSprite) {+ backgroundSprite.width = result.stageSize.width;+ backgroundSprite.height = result.stageSize.height;+ backgroundSprite.position.set(0, 0);+ }++ // Reset camera container to identity+ cameraContainer.scale.set(1);+ cameraContainer.position.set(0, 0);const selectedId = selectedZoomIdRef.current;const activeRegion = selectedId@@ -143,9 +160,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({updateOverlayForRegion(activeRegion);}- }, [updateOverlayForRegion, cropRegion]);-- const selectedZoom = useMemo(() => {+ }, [updateOverlayForRegion, cropRegion]); const selectedZoom = useMemo(() => {if (!selectedZoomId) return null;return zoomRegions.find((region) => region.id === selectedZoomId) ?? null;}, [zoomRegions, selectedZoomId]);@@ -264,7 +279,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({useEffect(() => {if (!pixiReady || !videoReady) return;layoutVideoContent();- }, [pixiReady, videoReady, layoutVideoContent, cropRegion]);+ }, [pixiReady, videoReady, layoutVideoContent, cropRegion, wallpaper]);useEffect(() => {if (!pixiReady || !videoReady) return;@@ -331,9 +346,15 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({appRef.current = app;container.appendChild(app.canvas);+ // Camera container - this will be scaled/positioned for zoom+ const cameraContainer = new PIXI.Container();+ cameraContainerRef.current = cameraContainer;+ app.stage.addChild(cameraContainer);++ // Video container - holds the masked video spriteconst videoContainer = new PIXI.Container();videoContainerRef.current = videoContainer;- app.stage.addChild(videoContainer);+ cameraContainer.addChild(videoContainer);setPixiReady(true);})();@@ -345,8 +366,10 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({app.destroy(true, { children: true, texture: true, textureSource: true });}appRef.current = null;+ cameraContainerRef.current = null;videoContainerRef.current = null;videoSpriteRef.current = null;+ backgroundSpriteRef.current = null;};}, []);@@ -358,6 +381,46 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({allowPlaybackRef.current = false;}, [videoPath]);+ // Load background into PIXI+ useEffect(() => {+ if (!pixiReady) return;+ const cameraContainer = cameraContainerRef.current;+ if (!cameraContainer || !wallpaper) return;++ // Remove old background if exists+ if (backgroundSpriteRef.current) {+ cameraContainer.removeChild(backgroundSpriteRef.current);+ backgroundSpriteRef.current.destroy({ texture: true });+ backgroundSpriteRef.current = null;+ }++ const isImageUrl = wallpaper?.startsWith('/wallpapers/') || wallpaper?.startsWith('http');+ if (!isImageUrl) return; // Skip if it's a solid color++ PIXI.Assets.load(wallpaper).then((texture) => {+ if (!cameraContainer) return;++ const backgroundSprite = new PIXI.Sprite(texture);+ backgroundSpriteRef.current = backgroundSprite;++ // Add background behind video container+ cameraContainer.addChildAt(backgroundSprite, 0);++ // Will be sized in layoutVideoContent+ layoutVideoContent();+ }).catch((err) => {+ console.error('Failed to load background:', err);+ });++ return () => {+ if (backgroundSpriteRef.current) {+ cameraContainer.removeChild(backgroundSpriteRef.current);+ backgroundSpriteRef.current.destroy({ texture: true });+ backgroundSpriteRef.current = null;+ }+ };+ }, [pixiReady, wallpaper]);+useEffect(() => {if (!pixiReady || !videoReady) return;@@ -460,19 +523,15 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({if (!app || !videoSprite || !videoContainer) return;const applyTransform = (motionIntensity: number) => {- const maskGraphics = maskGraphicsRef.current;- if (!maskGraphics) return;+ const cameraContainer = cameraContainerRef.current;+ if (!cameraContainer) return;const state = animationStateRef.current;applyZoomTransform({- videoSprite,- maskGraphics,+ cameraContainer,blurFilter: blurFilterRef.current,stageSize: stageSizeRef.current,- videoSize: videoSizeRef.current,- baseScale: baseScaleRef.current,- baseOffset: baseOffsetRef.current,baseMask: baseMaskRef.current,zoomScale: state.scale,focusX: state.focusX,@@ -569,13 +628,16 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(({return (<div className="relative aspect-video rounded-sm overflow-hidden" style={{ width: '100%' }}>- <div- className="absolute inset-0 bg-cover bg-center"- style={{- ...backgroundStyle,- filter: showBlur ? 'blur(2px)' : 'none',- }}- />+ {/* Fallback background for solid colors or when PIXI hasn't loaded */}+ {!isImageUrl && (+ <div+ className="absolute inset-0 bg-cover bg-center"+ style={{+ ...backgroundStyle,+ filter: showBlur ? 'blur(2px)' : 'none',+ }}+ />+ )}<divref={containerRef}className="absolute inset-0"diff --git a/src/components/video-editor/videoPlayback/layoutUtils.ts b/src/components/video-editor/videoPlayback/layoutUtils.tsindex 997e5502..4d6d1575 100644--- a/src/components/video-editor/videoPlayback/layoutUtils.ts+++ b/src/components/video-editor/videoPlayback/layoutUtils.ts@@ -17,7 +17,8 @@ interface LayoutResult {baseScale: number;baseOffset: { x: number; y: number };maskRect: { x: number; y: number; width: number; height: number };- cropOffset: { x: number; y: number };+ cropVideoBounds: { startX: number; endX: number; startY: number; endY: number };+ cropStageBounds: { startX: number; endX: number; startY: number; endY: number };}export function layoutVideoContent(params: LayoutParams): LayoutResult | null {@@ -47,7 +48,7 @@ export function layoutVideoContent(params: LayoutParams): LayoutResult | null {// Calculate the cropped dimensionsconst croppedVideoWidth = videoWidth * crop.width;const croppedVideoHeight = videoHeight * crop.height;-+// Calculate scale to fit the cropped area in the viewportconst maxDisplayWidth = width * VIEWPORT_SCALE;const maxDisplayHeight = height * VIEWPORT_SCALE;@@ -59,11 +60,14 @@ export function layoutVideoContent(params: LayoutParams): LayoutResult | null {);videoSprite.scale.set(scale);-- // Calculate display size of the full video at this scale+const fullVideoDisplayWidth = videoWidth * scale;const fullVideoDisplayHeight = videoHeight * scale;-++ const cropVideoStartX = crop.x * videoWidth;+ const cropVideoStartY = crop.y * videoHeight;+ const cropVideoEndX = cropVideoStartX + croppedVideoWidth;+ const cropVideoEndY = cropVideoStartY + croppedVideoHeight;// Calculate display size of just the cropped regionconst croppedDisplayWidth = croppedVideoWidth * scale;const croppedDisplayHeight = croppedVideoHeight * scale;@@ -89,16 +93,25 @@ export function layoutVideoContent(params: LayoutParams): LayoutResult | null {maskGraphics.clear();maskGraphics.roundRect(maskX, maskY, croppedDisplayWidth, croppedDisplayHeight, radius);maskGraphics.fill({ color: 0xffffff });+ const cropStageBounds = {+ startX: maskX,+ endX: maskX + croppedDisplayWidth,+ startY: maskY,+ endY: maskY + croppedDisplayHeight,+ };return {- stageSize: { width: croppedDisplayWidth, height: croppedDisplayHeight },+ stageSize: { width, height },videoSize: { width: croppedVideoWidth, height: croppedVideoHeight },baseScale: scale,baseOffset: { x: spriteX, y: spriteY },maskRect: { x: maskX, y: maskY, width: croppedDisplayWidth, height: croppedDisplayHeight },- cropOffset: {- x: crop.x * videoWidth,- y: crop.y * videoHeight,+ cropVideoBounds: {+ startX: cropVideoStartX,+ endX: cropVideoEndX,+ startY: cropVideoStartY,+ endY: cropVideoEndY,},+ cropStageBounds,};}diff --git a/src/components/video-editor/videoPlayback/zoomTransform.ts b/src/components/video-editor/videoPlayback/zoomTransform.tsindex a40fb4b5..a3f86463 100644--- a/src/components/video-editor/videoPlayback/zoomTransform.ts+++ b/src/components/video-editor/videoPlayback/zoomTransform.ts@@ -1,15 +1,10 @@import * as PIXI from 'pixi.js';interface TransformParams {- videoSprite: PIXI.Sprite;- maskGraphics: PIXI.Graphics;+ cameraContainer: PIXI.Container;blurFilter: PIXI.BlurFilter | null;stageSize: { width: number; height: number };- videoSize: { width: number; height: number };- baseScale: number;- baseOffset: { x: number; y: number };baseMask: { x: number; y: number; width: number; height: number };- cropOffset: { x: number; y: number };zoomScale: number;focusX: number;focusY: number;@@ -17,89 +12,48 @@ interface TransformParams {isPlaying: boolean;}-export function applyZoomTransform(params: TransformParams) {- const {- videoSprite,- maskGraphics,- blurFilter,- stageSize,- videoSize,- baseScale,- baseOffset,- baseMask,- cropOffset,- zoomScale,- focusX,- focusY,- motionIntensity,- isPlaying,- } = params;-+export function applyZoomTransform({+ cameraContainer,+ blurFilter,+ stageSize,+ baseMask,+ zoomScale,+ focusX,+ focusY,+ motionIntensity,+ isPlaying,+}: TransformParams) {if (- !stageSize.width ||- !stageSize.height ||- !videoSize.width ||- !videoSize.height ||- baseScale <= 0 ||+ stageSize.width <= 0 ||+ stageSize.height <= 0 ||baseMask.width <= 0 ||baseMask.height <= 0) {return;}- const focusStagePxX = baseMask.x + focusX * stageSize.width;- const focusStagePxY = baseMask.y + focusY * stageSize.height;- const stageCenterX = baseMask.x + stageSize.width / 2;- const stageCenterY = baseMask.y + stageSize.height / 2;-- const actualScale = baseScale * zoomScale;- videoSprite.scale.set(actualScale);-- // Keep the focus point centered in viewport after zoom transformation- const baseVideoX = baseOffset.x;- const baseVideoY = baseOffset.y;- const baseMaskX = baseMask.x;- const baseMaskY = baseMask.y;- const focusInVideoSpaceX = focusStagePxX - baseVideoX;- const focusInVideoSpaceY = focusStagePxY - baseVideoY;-- // Position formula: stageCenterX - focusInVideoSpace * zoomScale- let newVideoX = stageCenterX - focusInVideoSpaceX * zoomScale;- let newVideoY = stageCenterY - focusInVideoSpaceY * zoomScale;-- const cropStartX = cropOffset.x;- const cropStartY = cropOffset.y;- const cropEndX = cropOffset.x + videoSize.width;- const cropEndY = cropOffset.y + videoSize.height;- const maskWidth = baseMask.width;- const maskHeight = baseMask.height;+ // The focus point in stage coordinates (where the user clicked/selected)+ const focusStagePxX = focusX * stageSize.width;+ const focusStagePxY = focusY * stageSize.height;++ // Stage center (where we want the focus to end up after zoom)+ const stageCenterX = stageSize.width / 2;+ const stageCenterY = stageSize.height / 2;- const minVideoX = baseMaskX + maskWidth - cropEndX * actualScale;- const maxVideoX = baseMaskX - cropStartX * actualScale;- const minVideoY = baseMaskY + maskHeight - cropEndY * actualScale;- const maxVideoY = baseMaskY - cropStartY * actualScale;+ // Apply zoom scale to camera container+ cameraContainer.scale.set(zoomScale);- if (minVideoX <= maxVideoX) {- newVideoX = Math.max(minVideoX, Math.min(maxVideoX, newVideoX));- }-- if (minVideoY <= maxVideoY) {- newVideoY = Math.max(minVideoY, Math.min(maxVideoY, newVideoY));- }+ // Calculate camera position to keep focus point centered+ // After scaling, the focus point moves to (focusX * zoomScale, focusY * zoomScale)+ // We want it at stage center, so offset = center - (focus * scale)+ const cameraX = stageCenterX - focusStagePxX * zoomScale;+ const cameraY = stageCenterY - focusStagePxY * zoomScale;- videoSprite.position.set(newVideoX, newVideoY);+ cameraContainer.position.set(cameraX, cameraY);if (blurFilter) {const shouldBlur = isPlaying && motionIntensity > 0.0005;const motionBlur = shouldBlur ? Math.min(6, motionIntensity * 120) : 0;blurFilter.blur = motionBlur;}-- const maskX = baseMaskX;- const maskY = baseMaskY;- const radius = Math.min(maskWidth, maskHeight) * 0.02;-- maskGraphics.clear();- maskGraphics.roundRect(maskX, maskY, maskWidth, maskHeight, radius);- maskGraphics.fill({ color: 0xffffff });}