Skip to content

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

feat: add aspect ratio handling in VideoCanvas and utility function for numeric aspect ratio

CristianOlivera184ff2718fbdb
diff --git a/.gitignore b/.gitignore
index f1aef74..5aef776 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,4 +43,5 @@ next-env.d.ts
supabase-setup.sql
-BACKEND_SPEC.md
\ No newline at end of file
+BACKEND_SPEC.md
+BACKEND_TESTING.md
diff --git a/app/components/ui/editor/MockupMenu.tsx b/app/components/ui/editor/MockupMenu.tsx
index 78fda6c..cabac95 100644
--- a/app/components/ui/editor/MockupMenu.tsx
+++ b/app/components/ui/editor/MockupMenu.tsx
@@ -131,11 +131,10 @@ export function MockupMenu({
<button
key={cat.id}
onClick={() => handleCategoryChange(cat.id)}
- className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-[10px] font-medium uppercase tracking-wider transition-all ${
- selectedCategory === cat.id
+ className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-[10px] font-medium uppercase tracking-wider transition-all ${selectedCategory === cat.id
? "bg-blue-500/20 text-blue-400 border border-blue-500/40"
: "bg-white/5 text-white/50 hover:text-white/70 border border-transparent hover:border-white/10"
- }`}
+ }`}
>
<Icon icon={cat.icon} width="12" />
<span>{cat.label}</span>
@@ -145,16 +144,14 @@ export function MockupMenu({
</div>
<div className="relative overflow-y-auto custom-scrollbar overflow-x-hidden min-h-62.5">
<div
- className={`absolute inset-0 w-full transition-all duration-300 ease-out z-10 ${
- gridLoaded ? "opacity-0 blur-md pointer-events-none scale-105" : "opacity-100 blur-0 scale-100"
- }`}
+ className={`absolute inset-0 w-full transition-all duration-300 ease-out z-10 ${gridLoaded ? "opacity-0 blur-md pointer-events-none scale-105" : "opacity-100 blur-0 scale-100"
+ }`}
>
<MockupGridSkeleton />
</div>
<div
- className={`p-3 grid grid-cols-3 gap-2 transition-all duration-300 ease-out ${
- !gridLoaded ? "opacity-0 scale-95 pointer-events-none" : "opacity-100 scale-100"
- }`}
+ className={`p-3 grid grid-cols-3 gap-2 transition-all duration-300 ease-out ${!gridLoaded ? "opacity-0 scale-95 pointer-events-none" : "opacity-100 scale-100"
+ }`}
>
{filteredMockups.map((mockup) => {
const categoryConfig = MOCKUP_CATEGORIES.find((c) => c.id === mockup.category);
@@ -163,9 +160,8 @@ export function MockupMenu({
<button
key={mockup.id}
onClick={() => handleMockupSelect(mockup.id)}
- className={`group relative w-full h-28 squircle-element border-2 overflow-hidden shadow-lg transition-all active:scale-95 ${
- isActive ? "border-blue-500 ring-2 ring-blue-500/50" : "border-neutral-800 hover:border-white/20"
- }`}
+ className={`group relative w-full h-28 squircle-element border-2 overflow-hidden shadow-lg transition-all active:scale-95 ${isActive ? "border-blue-500 ring-2 ring-blue-500/50" : "border-neutral-800 hover:border-white/20"
+ }`}
>
<div
className="absolute inset-0 bg-cover bg-center bg-no-repeat transition-transform duration-500 group-hover:scale-105"
@@ -229,17 +225,17 @@ export function MockupMenu({
)}
{features.hasDarkMode && (
- <fieldset className="flex items-center justify-between">
- <legend className="flex items-center gap-2 text-[11px] text-white/55">
+ <fieldset className="flex items-center justify-between w-full gap-4">
+ <legend className="float-left flex items-center gap-2 text-[11px] text-white/55 whitespace-nowrap">
<Icon icon="ph:moon-bold" width="14" aria-hidden="true" />
<span>{t("darkMode.label")}</span>
</legend>
+
<div className="flex items-center gap-1 p-0.5 rounded-lg bg-white/6 border border-white/[0.07]" role="group" aria-label={t("darkMode.label")}>
<button
onClick={() => handleDarkModeChange(true)}
- className={`flex items-center gap-1 px-2 py-1 rounded-md text-[10px] transition-colors ${
- mockupConfig?.darkMode ? "bg-[#09090B] border border-white/10 text-white/70" : "text-white/30 hover:text-white/50"
- }`}
+ className={`flex items-center gap-1 px-2 py-1 rounded-md text-[10px] transition-colors ${mockupConfig?.darkMode ? "bg-[#09090B] border border-white/10 text-white/70" : "text-white/30 hover:text-white/50"
+ }`}
aria-pressed={mockupConfig?.darkMode}
aria-label={t("darkMode.dark")}
>
@@ -247,11 +243,10 @@ export function MockupMenu({
</button>
<button
onClick={() => handleDarkModeChange(false)}
- className={`flex items-center gap-1 px-2 py-1 rounded-md text-[10px] transition-colors ${
- !mockupConfig?.darkMode
+ className={`flex items-center gap-1 px-2 py-1 rounded-md text-[10px] transition-colors ${!mockupConfig?.darkMode
? "bg-[#09090B] border border-white/10 text-white/70"
: "text-white/30 hover:text-white/50"
- }`}
+ }`}
aria-pressed={!mockupConfig?.darkMode}
aria-label={t("darkMode.light")}
>
@@ -259,6 +254,7 @@ export function MockupMenu({
</button>
</div>
</fieldset>
+
)}
{features.hasFrameColor && (
@@ -269,11 +265,10 @@ export function MockupMenu({
<button
key={color}
onClick={() => handleFrameColorChange(color)}
- className={`aspect-square squircle-element cursor-pointer hover:ring-2 transition shadow-sm ring-white/60 border border-white/10 ${
- mockupConfig?.frameColor?.toLowerCase() === color.toLowerCase()
+ className={`aspect-square squircle-element cursor-pointer hover:ring-2 transition shadow-sm ring-white/60 border border-white/10 ${mockupConfig?.frameColor?.toLowerCase() === color.toLowerCase()
? "ring-2 ring-white/90 shadow-lg shadow-white"
: "border-white/10 border-transparent hover:border-white/30"
- }`}
+ }`}
style={{ backgroundColor: color }}
aria-label={t("frameColor.ariaLabel", { color })}
/>
@@ -285,7 +280,6 @@ export function MockupMenu({
className="absolute inset-0 opacity-0 cursor-pointer w-full h-full"
onChange={(e) => handleFrameColorChange(e.target.value)}
value={mockupConfig?.frameColor || "#ffffff"}
- aria-label={t("frameColor.customLabel")}
/>
</label>
</div>
diff --git a/app/components/ui/editor/VideoCanvas.tsx b/app/components/ui/editor/VideoCanvas.tsx
index a9c9695..eb8a831 100644
--- a/app/components/ui/editor/VideoCanvas.tsx
+++ b/app/components/ui/editor/VideoCanvas.tsx
@@ -6,7 +6,7 @@ import type { ImageElement, SvgElement } from "@/types/canvas-elements.types";
import { getCameraLayout } from "@/types/camera.types";
import { ASPECT_RATIO_DIMENSIONS } from "@/types";
import { getWallpaperUrl } from "@/lib/wallpaper.utils";
-import { drawRoundedRect, drawRoundedRectBottomOnly, calculateScaledPadding, applyCanvasBackground, getAspectRatioStyle, getMaxWidth, Corner, getCornerStyle, getNearestCorner } from "@/lib/canvas.utils";
+import { drawRoundedRect, drawRoundedRectBottomOnly, calculateScaledPadding, applyCanvasBackground, getAspectRatioStyle, getAspectRatioNumber, Corner, getCornerStyle, getNearestCorner } from "@/lib/canvas.utils";
import { drawMockupToCanvas } from "@/lib/mockup-canvas.utils";
import { speedToTransitionMs, ZOOM_EASING, calculateZoomPhaseState, zoomLevelToFactor } from "@/types/zoom.types";
import type { ZoomFragment } from "@/types/zoom.types";
@@ -227,6 +227,39 @@ export const VideoCanvas = forwardRef<VideoCanvasHandle, VideoCanvasProps>(funct
// Camera overlay refs / state
const cameraVideoRef = useRef<HTMLVideoElement | null>(null);
const previewContainerRef = useRef<HTMLDivElement>(null);
+ const canvasWrapperRef = useRef<HTMLDivElement>(null);
+
+ // Compute canvas dimensions to fill available space while maintaining aspect ratio
+ const [canvasDimensions, setCanvasDimensions] = useState<{ width: number; height: number } | null>(null);
+
+ useEffect(() => {
+ const wrapper = canvasWrapperRef.current;
+ if (!wrapper) return;
+
+ const arNumber = getAspectRatioNumber(aspectRatio, customAspectRatio ?? undefined);
+
+ const compute = (containerWidth: number, containerHeight: number) => {
+ if (containerWidth <= 0 || containerHeight <= 0) return;
+ const byHeight = { width: containerHeight * arNumber, height: containerHeight };
+ if (byHeight.width <= containerWidth) {
+ setCanvasDimensions(byHeight);
+ } else {
+ setCanvasDimensions({ width: containerWidth, height: containerWidth / arNumber });
+ }
+ };
+
+ const observer = new ResizeObserver(([entry]) => {
+ const { width, height } = entry.contentRect;
+ compute(width, height);
+ });
+
+ observer.observe(wrapper);
+ const rect = wrapper.getBoundingClientRect();
+ compute(rect.width, rect.height);
+
+ return () => observer.disconnect();
+ }, [aspectRatio, customAspectRatio]);
+
const cameraDragRef = useRef<{
pointerId: number;
startX: number;
@@ -1454,7 +1487,7 @@ export const VideoCanvas = forwardRef<VideoCanvasHandle, VideoCanvasProps>(funct
<div className="flex items-stretch min-h-0 min-w-0 w-full h-full justify-center gap-0">
{/* Preview visual - contenedor con tamaño dinámico según aspect ratio */}
- <div className="flex-1 flex items-center justify-center min-h-0 min-w-0">
+ <div ref={canvasWrapperRef} className="flex-1 flex items-center justify-center min-h-0 min-w-0 mx-4">
<div
ref={previewContainerRef}
className={`relative shrink-0 overflow-hidden transition-all duration-300 ${mediaType === "image" && imageUrl
@@ -1463,10 +1496,10 @@ export const VideoCanvas = forwardRef<VideoCanvasHandle, VideoCanvasProps>(funct
}`}
style={{
aspectRatio: getAspectRatioStyle(aspectRatio, customAspectRatio ?? undefined),
- maxWidth: getMaxWidth(aspectRatio, customAspectRatio ?? undefined),
- width: '100%',
- height: 'auto',
- maxHeight: '100%',
+ ...(canvasDimensions
+ ? { width: `${canvasDimensions.width}px`, height: `${canvasDimensions.height}px` }
+ : { width: '100%', height: 'auto', maxHeight: '100%' }
+ ),
containerType: 'size',
}}
onClick={(e) => {
diff --git a/lib/canvas.utils.ts b/lib/canvas.utils.ts
index ef4bf99..0b85189 100644
--- a/lib/canvas.utils.ts
+++ b/lib/canvas.utils.ts
@@ -61,6 +61,21 @@ export function getAspectRatioStyle(ratio: AspectRatio, customDimensions?: { wid
}
}
+// Numeric aspect ratio (width / height) for a given AspectRatio setting
+export function getAspectRatioNumber(ratio: AspectRatio, customDimensions?: { width: number; height: number }): number {
+ if ((ratio === "custom" || ratio === "auto") && customDimensions) {
+ return customDimensions.width / customDimensions.height;
+ }
+ switch (ratio) {
+ case "16:9": return 16 / 9;
+ case "9:16": return 9 / 16;
+ case "1:1": return 1;
+ case "4:3": return 4 / 3;
+ case "3:4": return 3 / 4;
+ default: return 16 / 9;
+ }
+}
+
// Función para obtener max-width basado en aspect ratio
export function getMaxWidth(ratio: AspectRatio, customDimensions?: { width: number; height: number }): string {
if ((ratio === "custom" || ratio === "auto") && customDimensions) {