Skip to content

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

feat: add movement duration validation and warning in ZoomFragmentEditor, enhance fragment resizing logic in ZoomFragmentTrackItem

CristianOlivera1c87767eba71f
diff --git a/app/components/ui/editor/ZoomFragmentEditor.tsx b/app/components/ui/editor/ZoomFragmentEditor.tsx
index a07e6ce..bf31052 100644
--- a/app/components/ui/editor/ZoomFragmentEditor.tsx
+++ b/app/components/ui/editor/ZoomFragmentEditor.tsx
@@ -1,5 +1,5 @@
"use client";
-import { useRef, useMemo, useEffect } from "react";
+import { useRef, useMemo, useEffect, useState } from "react";
import { Icon } from "@iconify/react";
import { useTranslations } from "next-intl";
import { SliderControl } from "../../../../components/ui/SliderControl";
@@ -100,8 +100,32 @@ export function ZoomFragmentEditor({
const holdBounds = useMemo(() => getFragmentHoldBounds(fragment), [fragment]);
const holdDuration = Math.max(0, holdBounds.end - holdBounds.start);
const isTooShort = holdDuration < MIN_MOVEMENT_TRACK_DURATION;
+ const [showTooShortWarning, setShowTooShortWarning] = useState(false);
const canAddMovement = hasMovementSpaceAvailable(sortedMovements, holdBounds.start, holdBounds.end);
+ useEffect(() => {
+ if (!showTooShortWarning) return;
+ const id = setTimeout(() => setShowTooShortWarning(false), 3000);
+ return () => clearTimeout(id);
+ }, [showTooShortWarning]);
+
+ const handleMovementToggleClick = () => {
+ if (isTooShort) {
+ setShowTooShortWarning(true);
+ return;
+ }
+ onToggleMovement(!movementEnabled);
+ };
+
+ const minAllowedSpeed = useMemo(() => {
+ if (!movementEnabled) return 1;
+ const SPEED_MIN_MS = 150;
+ const SPEED_MAX_MS = 2000;
+ const fragmentDurationSec = Math.max(0, fragment.endTime - fragment.startTime);
+ const maxAllowedTransitionMs = (fragmentDurationSec - MIN_MOVEMENT_TRACK_DURATION) * 1000;
+ return Math.max(1, Math.min(10, 1 + (9 * (SPEED_MAX_MS - Math.max(SPEED_MIN_MS, Math.min(SPEED_MAX_MS, maxAllowedTransitionMs)))) / (SPEED_MAX_MS - SPEED_MIN_MS)));
+ }, [movementEnabled, fragment.startTime, fragment.endTime]);
+
const handleToggle3D = () => {
const enabling = !(fragment.enable3D ?? false);
if (enabling && (fragment.perspective3DAngleX === undefined || fragment.perspective3DAngleY === undefined)) {
@@ -172,13 +196,13 @@ export function ZoomFragmentEditor({
<div className={isTooShort ? "opacity-50 cursor-not-allowed" : ""}>
<Toggle
checked={movementEnabled}
- onChange={() => !isTooShort && onToggleMovement(!movementEnabled)}
+ onChange={handleMovementToggleClick}
activeColor="bg-emerald-500"
/>
</div>
</div>
- {isTooShort ? (
+ {isTooShort && showTooShortWarning ? (
<div className="p-3 bg-amber-500/10 border border-amber-500/20 squircle-element">
<div className="flex items-center gap-2 text-xs text-amber-600 dark:text-amber-400">
<span>{t("movement.tooShort")}</span>
@@ -259,7 +283,7 @@ export function ZoomFragmentEditor({
)}
<SliderControl icon="mdi:magnify-plus-outline" label={t("sliders.zoomLevel")} value={fragment.zoomLevel} min={1} max={10} step={0.1} onChange={(value) => onUpdate({ zoomLevel: value })} />
- <SliderControl icon="mdi:speedometer" label={t("sliders.transitionSpeed")} value={fragment.speed} min={1} max={10} step={0.1} onChange={(value) => onUpdate({ speed: value })} />
+ <SliderControl icon="mdi:speedometer" label={t("sliders.transitionSpeed")} value={Math.max(minAllowedSpeed, fragment.speed)} min={minAllowedSpeed} max={10} step={0.1} onChange={(value) => onUpdate({ speed: value })} />
<div className="h-px bg-border" />
diff --git a/app/components/ui/editor/ZoomFragmentTrackItem.tsx b/app/components/ui/editor/ZoomFragmentTrackItem.tsx
index 4848966..8fe33ae 100644
--- a/app/components/ui/editor/ZoomFragmentTrackItem.tsx
+++ b/app/components/ui/editor/ZoomFragmentTrackItem.tsx
@@ -3,7 +3,8 @@
import { useState, useRef, useCallback, useMemo, useEffect } from "react";
import { motion, useMotionValue } from "framer-motion";
import type { ZoomFragment } from "@/types/zoom.types";
-import { zoomLevelToFactor } from "@/types/zoom.types";
+import { zoomLevelToFactor, speedToTransitionMs } from "@/types/zoom.types";
+import { MIN_MOVEMENT_TRACK_DURATION } from "./ZoomMovementTrackItem";
const MIN_FRAGMENT_DURATION = 0.5;
@@ -80,6 +81,12 @@ export function ZoomFragmentTrackItem({
return { minStart, maxEnd };
}, [otherFragments, fragment.startTime, fragment.endTime, videoDuration, contentDuration]);
+ const minDurationSeconds = useMemo(() => {
+ if (!fragment.movementEnabled) return MIN_FRAGMENT_DURATION;
+ const transitionSec = speedToTransitionMs(fragment.speed) / 1000;
+ return Math.max(MIN_FRAGMENT_DURATION, MIN_MOVEMENT_TRACK_DURATION + transitionSec);
+ }, [fragment.movementEnabled, fragment.speed]);
+
const handleDrag = useCallback((e: MouseEvent | TouchEvent | PointerEvent, info: { delta: { x: number } }) => {
if (contentWidth === 0 || videoDuration === 0) return;
@@ -123,7 +130,7 @@ export function ZoomFragmentTrackItem({
let newX = currentX + info.delta.x;
let newWidth = currentWidth - info.delta.x;
- const minWidth = timeToPixels(MIN_FRAGMENT_DURATION);
+ const minWidth = timeToPixels(minDurationSeconds);
if (newWidth < minWidth) {
newWidth = minWidth;
newX = currentX + currentWidth - minWidth;
@@ -131,14 +138,14 @@ export function ZoomFragmentTrackItem({
const minX = timeToPixels(boundaries.minStart);
if (newX < minX) {
- const diff = minX - newX;
+ const allowedShrink = currentX + currentWidth - minX;
newX = minX;
- newWidth = currentWidth - diff;
+ newWidth = Math.max(minWidth, Math.min(newWidth, allowedShrink));
}
fragmentX.set(newX);
fragmentWidth.set(newWidth);
- }, [contentWidth, videoDuration, fragmentX, fragmentWidth, boundaries, timeToPixels]);
+ }, [contentWidth, videoDuration, fragmentX, fragmentWidth, boundaries, minDurationSeconds, timeToPixels]);
const handleResizeEndDrag = useCallback((e: MouseEvent | TouchEvent | PointerEvent, info: { delta: { x: number } }) => {
if (contentWidth === 0 || videoDuration === 0) return;
@@ -147,15 +154,13 @@ export function ZoomFragmentTrackItem({
let newWidth = currentWidth + info.delta.x;
- const minWidth = timeToPixels(MIN_FRAGMENT_DURATION);
- newWidth = Math.max(minWidth, newWidth);
-
const currentX = fragmentX.get();
const maxWidth = timeToPixels(boundaries.maxEnd) - currentX;
- newWidth = Math.min(newWidth, maxWidth);
+ const minWidth = timeToPixels(minDurationSeconds);
+ newWidth = Math.max(minWidth, Math.min(newWidth, Math.max(minWidth, maxWidth)));
fragmentWidth.set(newWidth);
- }, [contentWidth, videoDuration, fragmentWidth, fragmentX, boundaries, timeToPixels]);
+ }, [contentWidth, videoDuration, fragmentWidth, fragmentX, boundaries, minDurationSeconds, timeToPixels]);
const handleResizeStart = useCallback((handle: 'start' | 'end') => {
setIsResizing(handle);