Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

refactor: comment out unused cursor-related code and improve zoom calculations in canvas utilities

CristianOlivera19452165489e3
diff --git a/app/(editor)/editor/page.tsx b/app/(editor)/editor/page.tsx
index db41782..5feaf39 100644
--- a/app/(editor)/editor/page.tsx
+++ b/app/(editor)/editor/page.tsx
@@ -13,8 +13,8 @@ import type { ExportQuality, Tool, BackgroundTab, VideoCanvasHandle, BackgroundC
import type { TrimRange } from "@/types/timeline.types";
import type { MockupConfig } from "@/types/mockup.types";
import type { EditorState } from "@/types/editor-state.types";
-import type { CursorConfig, CursorRecordingData } from "@/types/cursor.types";
-import { DEFAULT_CURSOR_CONFIG, EMPTY_CURSOR_DATA } from "@/types/cursor.types";
+//import type { CursorConfig, CursorRecordingData } from "@/types/cursor.types";
+// import { DEFAULT_CURSOR_CONFIG, EMPTY_CURSOR_DATA } from "@/types/cursor.types";
import { createInitialEditorState } from "@/types/editor-state.types";
import { DEFAULT_MOCKUP_CONFIG, getMockupDefaultConfig } from "@/types/mockup.types";
import type { CanvasElement } from "@/types/canvas-elements.types";
@@ -201,8 +201,9 @@ export default function Editor() {
const [selectedAudioTrackId, setSelectedAudioTrackId] = useState<string | null>(null);
// Cursor state
- const [cursorConfig, setCursorConfig] = useState<CursorConfig>(DEFAULT_CURSOR_CONFIG);
- const [cursorData, setCursorData] = useState<CursorRecordingData>(EMPTY_CURSOR_DATA);
+ // Cursor state
+ // const [cursorConfig, setCursorConfig] = useState<CursorConfig>(DEFAULT_CURSOR_CONFIG);
+ // const [cursorData, setCursorData] = useState<CursorRecordingData>(EMPTY_CURSOR_DATA);
const [isRecordedVideo, setIsRecordedVideo] = useState<boolean>(false);
// Audio trim modal state
@@ -609,9 +610,9 @@ export default function Editor() {
}, []);
// Cursor config change handler
- const handleCursorConfigChange = useCallback((config: Partial<CursorConfig>) => {
+ /* const handleCursorConfigChange = useCallback((config: Partial<CursorConfig>) => {
setCursorConfig(prev => ({ ...prev, ...config }));
- }, []);
+ }, []);*/
const handleSelectAudioTrack = useCallback((trackId: string | null) => {
setSelectedAudioTrackId(trackId);
@@ -764,15 +765,15 @@ export default function Editor() {
// Load cursor data if available (only for recorded videos)
if ('isRecordedVideo' in videoToLoad && videoToLoad.isRecordedVideo) {
setIsRecordedVideo(true);
- if ('cursorData' in videoToLoad && videoToLoad.cursorData) {
+ /*if ('cursorData' in videoToLoad && videoToLoad.cursorData) {
setCursorData(videoToLoad.cursorData);
} else {
setCursorData(EMPTY_CURSOR_DATA);
- }
+ }*/
} else {
// Uploaded video - no cursor data
setIsRecordedVideo(false);
- setCursorData(EMPTY_CURSOR_DATA);
+ //setCursorData(EMPTY_CURSOR_DATA);
}
// Clear undo/redo history when loading a new video
@@ -1334,10 +1335,10 @@ export default function Editor() {
videoDuration={videoDuration}
videoHasAudioTrack={videoHasAudioTrack}
// Cursor props
- cursorConfig={cursorConfig}
+ /*cursorConfig={cursorConfig}
cursorData={cursorData}
isRecordedVideo={isRecordedVideo}
- onCursorConfigChange={handleCursorConfigChange}
+ onCursorConfigChange={handleCursorConfigChange}*/
/>
</Suspense>
</motion.div>
@@ -1420,8 +1421,8 @@ export default function Editor() {
onElementUpdate={updateCanvasElement}
onElementSelect={selectCanvasElement}
// Cursor overlay props
- cursorConfig={cursorConfig}
- cursorData={cursorData}
+ /*cursorConfig={cursorConfig}
+ cursorData={cursorData}*/
onEnded={() => {
setIsPlaying(false);
justEndedRef.current = true;
@@ -1549,10 +1550,10 @@ export default function Editor() {
videoDuration={videoDuration}
videoHasAudioTrack={videoHasAudioTrack}
// Cursor props
- cursorConfig={cursorConfig}
+ /*cursorConfig={cursorConfig}
cursorData={cursorData}
isRecordedVideo={isRecordedVideo}
- onCursorConfigChange={handleCursorConfigChange}
+ onCursorConfigChange={handleCursorConfigChange}*/
/>
<Suspense fallback={null}>
diff --git a/app/components/ui/editor/VideoCanvas.tsx b/app/components/ui/editor/VideoCanvas.tsx
index c674385..5ccb1cf 100644
--- a/app/components/ui/editor/VideoCanvas.tsx
+++ b/app/components/ui/editor/VideoCanvas.tsx
@@ -540,55 +540,40 @@ export const VideoCanvas = forwardRef<VideoCanvasHandle, VideoCanvasProps>(funct
const zoomCenterY = canvasHeight / 2;
if (zoomState.scale !== 1 || zoomState.rotateX !== 0 || zoomState.rotateY !== 0) {
- const focusOffsetX = (50 - zoomState.focusX) / 100 * canvasWidth * (zoomState.scale - 1) * 2;
- const focusOffsetY = (50 - zoomState.focusY) / 100 * canvasHeight * (zoomState.scale - 1) * 2;
- ctx.translate(zoomCenterX + focusOffsetX, zoomCenterY + focusOffsetY);
+ // Punto de enfoque en píxeles
+ const focusPxX = (zoomState.focusX / 100) * canvasWidth;
+ const focusPxY = (zoomState.focusY / 100) * canvasHeight;
- // 3D solo si hay rotación — NO tocar el ctx si no hay 3D
+ // 1. Trasladar al punto de enfoque (centro de toda la transformación)
+ ctx.translate(focusPxX, focusPxY);
+
+ // 2. Aplicar 3D centrado en el punto de enfoque
if (zoomState.perspective > 0 && (zoomState.rotateX !== 0 || zoomState.rotateY !== 0)) {
const rotateXRad = (zoomState.rotateX * Math.PI) / 180;
const rotateYRad = (zoomState.rotateY * Math.PI) / 180;
- const d = zoomState.perspective; // 600px
-
- // Punto de enfoque en píxeles (origen de la perspectiva)
- const focusPxX = (zoomState.focusX / 100) * canvasWidth;
- const focusPxY = (zoomState.focusY / 100) * canvasHeight;
- // Trasladar al punto de enfoque para que la perspectiva parta desde ahí
- ctx.translate(focusPxX, focusPxY);
-
- // Perspectiva real usando tangente (igual que CSS perspective)
- // CSS perspective: un punto a distancia d del plano, rotado θ grados
- // produce escala = d / (d - z) donde z = dist * sin(θ)
const tanY = Math.tan(rotateYRad);
const tanX = Math.tan(rotateXRad);
-
- // Matriz de perspectiva 2D aproximada a CSS perspective + rotateX/Y:
- // - skewX/Y crean el efecto trapezoidal
- // - scaleX/Y compensan la profundidad
- // Valores sin perspectiveFactor para que el efecto sea igual de fuerte
const scaleX = 1 / Math.sqrt(1 + tanY * tanY);
const scaleY = 1 / Math.sqrt(1 + tanX * tanX);
const skewX = tanY * scaleX;
const skewY = tanX * scaleY;
- ctx.transform(
- scaleX, // a
- skewY, // b
- skewX, // c
- scaleY, // d
- 0, // e
- 0 // f
- );
-
- // Volver al origen
- ctx.translate(-focusPxX, -focusPxY);
+ ctx.transform(scaleX, skewY, skewX, scaleY, 0, 0);
}
- // Zoom siempre se aplica (con o sin 3D)
+ // 3. Aplicar zoom: escalar desde el punto de enfoque
+ // El offset mueve el contenido para que focusX/Y quede centrado en pantalla
+ const offsetX = (zoomCenterX - focusPxX) * (zoomState.scale - 1);
+ const offsetY = (zoomCenterY - focusPxY) * (zoomState.scale - 1);
ctx.scale(zoomState.scale, zoomState.scale);
- ctx.translate(-zoomCenterX, -zoomCenterY);
+
+ // 4. Volver al sistema de coordenadas del canvas, ajustando por zoom y offset de enfoque
+ ctx.translate(
+ -focusPxX + offsetX / zoomState.scale,
+ -focusPxY + offsetY / zoomState.scale
+ );
}
const backgroundImage = (shouldShowCustomImage || shouldShowUnsplashOverride) ? customImageRef.current : (shouldShowWallpaper ? wallpaperImageRef.current : null);
diff --git a/lib/canvas.utils.ts b/lib/canvas.utils.ts
index 3d295e6..2b1221a 100644
--- a/lib/canvas.utils.ts
+++ b/lib/canvas.utils.ts
@@ -1,5 +1,5 @@
import { AspectRatio } from "@/types/editor.types";
-import { ZoomStateCanvas, ZoomFragment, calculateZoomPhaseState } from "@/types/zoom.types";
+import { ZoomStateCanvas, ZoomFragment, calculateZoomPhaseState, zoomLevelToFactor, speedToTransitionMs, easeOutQuart } from "@/types/zoom.types";
export function drawRoundedRect(
ctx: CanvasRenderingContext2D,
x: number,
@@ -212,6 +212,13 @@ export interface ZoomStateCanvasExport extends ZoomStateCanvas {
perspective: number;
}
+/**
+ * Calculate smooth zoom for canvas export.
+ *
+ * IMPORTANT: Two different zoom behaviors:
+ * 1. Simple zoom (no 3D, no movement): Entry inside fragment, Exit AFTER fragment ends
+ * 2. Advanced zoom (3D or movement): 3-phase system (Entry → Hold → Exit) all within fragment
+ */
export function calculateSmoothZoom(
frameTime: number,
zoomFragments: ZoomFragment[]
@@ -233,18 +240,83 @@ export function calculateSmoothZoom(
f => frameTime >= f.startTime && frameTime <= f.endTime
);
+ // Find previous fragment for exit transition (only used for simple zoom)
+ const previousFragment = sortedFragments
+ .filter(f => f.endTime < frameTime)
+ .sort((a, b) => b.endTime - a.endTime)[0];
+
+ // Helper to check if fragment uses advanced features
+ const isAdvancedZoom = (f: ZoomFragment) => f.enable3D || f.movementEnabled;
+
if (activeFragment) {
- // Use calculateZoomPhaseState with forExport=true for animated scale
- const phaseState = calculateZoomPhaseState(activeFragment, frameTime, true);
-
- return {
- scale: phaseState.scale,
- focusX: phaseState.focusX,
- focusY: phaseState.focusY,
- rotateX: phaseState.rotateX,
- rotateY: phaseState.rotateY,
- perspective: phaseState.perspective,
- };
+ // Check if this fragment uses 3D or movement
+ if (isAdvancedZoom(activeFragment)) {
+ // ADVANCED ZOOM: Use 3-phase system (entry, hold, exit all within fragment)
+ const phaseState = calculateZoomPhaseState(activeFragment, frameTime, true);
+ return {
+ scale: phaseState.scale,
+ focusX: phaseState.focusX,
+ focusY: phaseState.focusY,
+ rotateX: phaseState.rotateX,
+ rotateY: phaseState.rotateY,
+ perspective: phaseState.perspective,
+ };
+ } else {
+ // SIMPLE ZOOM: Entry transition inside fragment, no exit inside (exit happens after)
+ const transitionMs = speedToTransitionMs(activeFragment.speed);
+ const transitionSec = transitionMs / 1000;
+ const targetScale = zoomLevelToFactor(activeFragment.zoomLevel);
+ const timeIntoFragment = frameTime - activeFragment.startTime;
+
+ let scale: number;
+ if (timeIntoFragment < transitionSec) {
+ // Entry: animate from 1 to target
+ const progress = Math.min(1, timeIntoFragment / transitionSec);
+ const easedProgress = easeOutQuart(progress);
+ scale = 1 + (targetScale - 1) * easedProgress;
+ } else {
+ // Hold: maintain target scale (no exit inside fragment for simple zoom)
+ scale = targetScale;
+ }
+
+ return {
+ scale,
+ focusX: activeFragment.focusX,
+ focusY: activeFragment.focusY,
+ rotateX: 0,
+ rotateY: 0,
+ perspective: 0,
+ };
+ }
+ }
+
+ // Not inside any fragment - check exit transition from previous fragment
+ if (previousFragment) {
+ if (isAdvancedZoom(previousFragment)) {
+ // ADVANCED ZOOM: Exit already happened inside fragment, return default
+ return DEFAULT_STATE;
+ } else {
+ // SIMPLE ZOOM: Exit happens AFTER fragment ends
+ const exitTransitionMs = speedToTransitionMs(previousFragment.speed);
+ const exitTransitionSec = exitTransitionMs / 1000;
+ const timeSinceEnd = frameTime - previousFragment.endTime;
+
+ if (timeSinceEnd < exitTransitionSec) {
+ const progress = Math.min(1, timeSinceEnd / exitTransitionSec);
+ const easedProgress = easeOutQuart(progress);
+ const targetScale = zoomLevelToFactor(previousFragment.zoomLevel);
+ const scale = targetScale - (targetScale - 1) * easedProgress;
+
+ return {
+ scale,
+ focusX: previousFragment.focusX,
+ focusY: previousFragment.focusY,
+ rotateX: 0,
+ rotateY: 0,
+ perspective: 0,
+ };
+ }
+ }
}
return DEFAULT_STATE;