Skip to content

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

feat: enhance zoom functionality with movement support and initial state management

CristianOlivera12a1d98845325
diff --git a/app/components/ui/home/HeroEditorPreview.tsx b/app/components/ui/home/HeroEditorPreview.tsx
index c3aeed8..bda2a06 100644
--- a/app/components/ui/home/HeroEditorPreview.tsx
+++ b/app/components/ui/home/HeroEditorPreview.tsx
@@ -5,7 +5,7 @@ import { Icon } from "@iconify/react";
import { motion, AnimatePresence } from "framer-motion";
import { useTranslations } from "next-intl";
import { calculateSmoothZoom } from "@/lib/canvas.utils";
-import { zoomLevelToFactor, type ZoomFragment } from "@/types/zoom.types";
+import { zoomLevelToFactor, type ZoomFragment, type ZoomMovement } from "@/types/zoom.types";
import { formatTime } from "@/lib";
import {
ACCENT,
@@ -27,6 +27,7 @@ import {
ZOOM_FRAGMENT_DURATIONS,
clamp,
getInitialZoomFragments,
+ getInitialZoomMovements,
hashSeed,
organicDrift,
} from "@/lib/editor-preview-hero.utils";
@@ -506,6 +507,7 @@ export default function HeroEditorPreview() {
const [clipRange, setClipRange] = useState<{ start: number; end: number }>({ start: 0, end: 0 });
const [fragments, setFragments] = useState<ZoomFragment[]>([]);
+ const [zoomMovements, setZoomMovements] = useState<ZoomMovement[]>([]);
const [blurPercent, setBlurPercent] = useState(0);
const [paddingPercent, setPaddingPercent] = useState(0);
@@ -578,12 +580,22 @@ export default function HeroEditorPreview() {
setClipRange({ start: 0, end: Math.max(CLIP_MIN_DURATION, duration) });
- setFragments(getInitialZoomFragments(duration, ZOOM_FRAGMENT_DURATIONS));
+ const initialFragments = getInitialZoomFragments(duration, ZOOM_FRAGMENT_DURATIONS);
+ setFragments(initialFragments);
+ setZoomMovements(getInitialZoomMovements(initialFragments));
}, [duration]);
const handleFragmentChange = useCallback((id: string, next: { start: number; end: number }) => {
setFragments((prev) => prev.map((f) => (f.id === id ? { ...f, startTime: next.start, endTime: next.end } : f)));
- }, []);
+ if (id === "zMid") {
+ setZoomMovements((prev) => {
+ const deltaStart = next.start - (fragments.find((f) => f.id === id)?.startTime ?? next.start);
+ return prev.map((m) =>
+ m.zoomFragmentId === id ? { ...m, startTime: m.startTime + deltaStart, endTime: m.endTime + deltaStart } : m
+ );
+ });
+ }
+ }, [fragments]);
useEffect(() => {
if (HERO_WALLPAPERS.length === 0) return;
@@ -635,7 +647,7 @@ export default function HeroEditorPreview() {
setCurrentTime(clamped);
}, []);
- const smoothZoom = useMemo(() => calculateSmoothZoom(currentTime, fragments), [currentTime, fragments]);
+ const smoothZoom = useMemo(() => calculateSmoothZoom(currentTime, fragments, zoomMovements), [currentTime, fragments, zoomMovements]);
const activeFragment = useMemo(
() => fragments.find((f) => currentTime >= f.startTime && currentTime <= f.endTime) ?? null,
diff --git a/components/ui/HeroEditorPreviewComponents.tsx b/components/ui/HeroEditorPreviewComponents.tsx
index 0c867c1..8b5ac79 100644
--- a/components/ui/HeroEditorPreviewComponents.tsx
+++ b/components/ui/HeroEditorPreviewComponents.tsx
@@ -211,11 +211,13 @@ export const TimelineZoomContent = memo(function TimelineZoomContent({
duration,
isSelected,
zoomLabel,
+ hasMovement,
}: {
zoomLevel: number;
duration: number;
isSelected?: boolean;
zoomLabel: string;
+ hasMovement?: boolean;
}) {
return (
<div
@@ -284,6 +286,7 @@ export const ZoomFragmentRangeItem = memo(function ZoomFragmentRangeItem({
duration={fragment.endTime - fragment.startTime}
isSelected={isSelected}
zoomLabel={zoomLabel}
+ hasMovement={!!fragment.movementEnabled}
/>
</DraggableRange>
);
diff --git a/lib/editor-preview-hero.utils.ts b/lib/editor-preview-hero.utils.ts
index d3e7b23..55eb394 100644
--- a/lib/editor-preview-hero.utils.ts
+++ b/lib/editor-preview-hero.utils.ts
@@ -1,4 +1,4 @@
-import { ZoomFragment } from "@/types";
+import { ZoomFragment, ZoomMovement, speedToTransitionMs } from "@/types";
import { WALLPAPER_CATEGORIES } from "./wallpaper.catalog";
import { useTranslations } from "next-intl";
@@ -40,30 +40,77 @@ export const SLIDER_MAX = 30;
export function getInitialZoomFragments(
duration: number,
- durations: { z1: number; z2: number }
+ durations: { z1: number; z2: number; zMid?: number }
): ZoomFragment[] {
if (duration <= 0) return [];
const dur1 = Math.min(durations.z1, duration * 0.4);
const dur2 = Math.min(durations.z2, duration * 0.3);
- const f1Start = Math.min(duration * 0.1, Math.max(0, duration - dur1));
- const f2Start = Math.min(
- Math.max(f1Start + dur1, duration * 0.8),
- Math.max(0, duration - dur2)
- );
+ const durMid = Math.min(durations.zMid ?? 2.6, duration * 0.28);
+
+ if (duration < dur1 + durMid + dur2 + 0.6) {
+ const f1Start = Math.min(duration * 0.1, Math.max(0, duration - dur1));
+ const f2Start = Math.min(
+ Math.max(f1Start + dur1, duration * 0.8),
+ Math.max(0, duration - dur2)
+ );
+ return [
+ {
+ id: "z1",
+ startTime: f1Start,
+ endTime: Math.min(duration, f1Start + dur1),
+ zoomLevel: 4,
+ speed: 5,
+ focusX: 15,
+ focusY: 15,
+ },
+ {
+ id: "z2",
+ startTime: f2Start,
+ endTime: Math.min(duration, f2Start + dur2),
+ zoomLevel: 4.5,
+ speed: 5,
+ focusX: 90,
+ focusY: 90,
+ },
+ ];
+ }
+
+ const f1Start = Math.min(duration * 0.08, Math.max(0, duration - dur1 - durMid - dur2 - 0.8));
+ const f1End = Math.min(duration, f1Start + dur1);
+ const f2End = Math.min(duration, Math.max(f1End + durMid + 0.4, duration * 0.92));
+ const f2Start = Math.max(f1End + 0.2, f2End - dur2);
+ const gapStart = f1End + 0.15;
+ const gapEnd = f2Start - 0.15;
+ const availableGap = Math.max(0, gapEnd - gapStart);
+ const midDur = Math.min(durMid, availableGap > 0 ? availableGap : durMid);
+ const centeredStart = gapStart + Math.max(0, (availableGap - midDur) / 2);
+ const midStart = Math.max(gapStart, centeredStart - 0.75);
+ const midEnd = midStart + midDur;
+
return [
{
id: "z1",
startTime: f1Start,
- endTime: Math.min(duration, f1Start + dur1),
+ endTime: f1End,
zoomLevel: 4,
speed: 5,
focusX: 15,
focusY: 15,
},
+ {
+ id: "zMid",
+ startTime: Math.max(f1End + 0.1, midStart),
+ endTime: Math.min(f2Start - 0.1, midEnd),
+ zoomLevel: 5,
+ speed: 5,
+ focusX: 90,
+ focusY: 60,
+ movementEnabled: true,
+ },
{
id: "z2",
startTime: f2Start,
- endTime: Math.min(duration, f2Start + dur2),
+ endTime: Math.min(duration, f2End),
zoomLevel: 4.5,
speed: 5,
focusX: 90,
@@ -72,6 +119,29 @@ export function getInitialZoomFragments(
];
}
+export function getInitialZoomMovements(fragments: ZoomFragment[]): ZoomMovement[] {
+ const mid = fragments.find((f) => f.id === "zMid");
+ if (!mid) return [];
+ const totalDuration = mid.endTime - mid.startTime;
+ if (totalDuration <= 0.5) return [];
+ const transitionSec = Math.min(speedToTransitionMs(mid.speed) / 1000, totalDuration / 2);
+ const holdStart = mid.startTime + transitionSec;
+ const holdEnd = mid.endTime - transitionSec;
+ if (holdEnd <= holdStart + 0.2) return [];
+ const inset = Math.min(0.35, (holdEnd - holdStart) * 0.18);
+ return [
+ {
+ id: "zm-mid-1",
+ zoomFragmentId: "zMid",
+ name: "Pan",
+ startTime: holdStart + inset,
+ endTime: holdEnd - inset * 0.5,
+ focusX: 85,
+ focusY: 80,
+ },
+ ];
+}
+
export function formatTime(seconds: number): string {
if (!Number.isFinite(seconds) || seconds < 0) seconds = 0;
const m = Math.floor(seconds / 60);
@@ -106,4 +176,4 @@ export type TFunc = ReturnType<typeof useTranslations>;
export const DRAG_CLAMP_PCT = 16;
export const DRAG_EASE_FACTOR = 0.86;
-export const ZOOM_FRAGMENT_DURATIONS = { z1: 3.2, z2: 1.6 };
\ No newline at end of file
+export const ZOOM_FRAGMENT_DURATIONS = { z1: 3.2, zMid: 3.0, z2: 1.6 };
\ No newline at end of file