Skip to content

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

feat(ui): draggable floating camera preview during recording

Cris15853da2807b
diff --git a/app/components/ui/FloatingCameraPreview.tsx b/app/components/ui/FloatingCameraPreview.tsx
new file mode 100644
index 0000000..13c43f5
--- /dev/null
+++ b/app/components/ui/FloatingCameraPreview.tsx
@@ -0,0 +1,128 @@
+"use client";
+
+import { useCallback, useEffect, useRef, useState } from "react";
+import { Icon } from "@iconify/react";
+import type { CameraConfig } from "@/types/camera.types";
+
+interface Props {
+ stream: MediaStream;
+ config: CameraConfig;
+ onConfigChange: (partial: Partial<CameraConfig>) => void;
+}
+
+function shapeRadius(shape: CameraConfig["shape"]): string {
+ if (shape === "circle") return "50%";
+ if (shape === "rounded") return "22%";
+ return "4%";
+}
+
+export default function FloatingCameraPreview({ stream, config, onConfigChange }: Props) {
+ const videoRef = useRef<HTMLVideoElement>(null);
+ const containerRef = useRef<HTMLDivElement>(null);
+ const dragStateRef = useRef<{
+ pointerId: number;
+ startX: number;
+ startY: number;
+ startPosX: number;
+ startPosY: number;
+ } | null>(null);
+ const [isDragging, setIsDragging] = useState(false);
+
+ useEffect(() => {
+ const el = videoRef.current;
+ if (!el) return;
+ el.srcObject = stream;
+ el.play().catch(() => undefined);
+ }, [stream]);
+
+ const onPointerDown = useCallback(
+ (e: React.PointerEvent<HTMLDivElement>) => {
+ if (e.button !== 0) return;
+ const target = e.currentTarget;
+ target.setPointerCapture(e.pointerId);
+ dragStateRef.current = {
+ pointerId: e.pointerId,
+ startX: e.clientX,
+ startY: e.clientY,
+ startPosX: config.position.x,
+ startPosY: config.position.y,
+ };
+ setIsDragging(true);
+ },
+ [config.position.x, config.position.y]
+ );
+
+ const onPointerMove = useCallback(
+ (e: React.PointerEvent<HTMLDivElement>) => {
+ const state = dragStateRef.current;
+ if (!state || state.pointerId !== e.pointerId) return;
+ const dx = (e.clientX - state.startX) / window.innerWidth;
+ const dy = (e.clientY - state.startY) / window.innerHeight;
+ const nextX = Math.min(1, Math.max(0, state.startPosX + dx));
+ const nextY = Math.min(1, Math.max(0, state.startPosY + dy));
+ onConfigChange({
+ position: { x: nextX, y: nextY },
+ corner: "custom",
+ });
+ },
+ [onConfigChange]
+ );
+
+ const onPointerUp = useCallback(
+ (e: React.PointerEvent<HTMLDivElement>) => {
+ const state = dragStateRef.current;
+ if (!state || state.pointerId !== e.pointerId) return;
+ e.currentTarget.releasePointerCapture(e.pointerId);
+ dragStateRef.current = null;
+ setIsDragging(false);
+ },
+ []
+ );
+
+ const size = Math.min(window.innerWidth, window.innerHeight) * config.size;
+ const radius = shapeRadius(config.shape);
+
+ return (
+ <div
+ ref={containerRef}
+ onPointerDown={onPointerDown}
+ onPointerMove={onPointerMove}
+ onPointerUp={onPointerUp}
+ onPointerCancel={onPointerUp}
+ className={`fixed pointer-events-auto select-none z-[60] group ${
+ isDragging ? "cursor-grabbing" : "cursor-grab"
+ }`}
+ style={{
+ left: `calc(${config.position.x * 100}vw - ${size / 2}px)`,
+ top: `calc(${config.position.y * 100}vh - ${size / 2}px)`,
+ width: size,
+ height: size,
+ transition: isDragging ? "none" : "left 120ms ease, top 120ms ease",
+ touchAction: "none",
+ }}
+ aria-label="Vista previa de cámara (arrastra para mover)"
+ >
+ <video
+ ref={videoRef}
+ autoPlay
+ muted
+ playsInline
+ className="size-full object-cover shadow-[0_10px_40px_rgba(0,0,0,0.55)] ring-1 ring-white/20"
+ style={{
+ borderRadius: radius,
+ transform: config.mirror ? "scaleX(-1)" : undefined,
+ }}
+ />
+
+ <div
+ className="pointer-events-none absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center bg-black/30"
+ style={{ borderRadius: radius }}
+ >
+ <div className="flex items-center gap-1.5 px-2 py-1 rounded-full bg-black/60 text-white text-[10px] font-medium">
+ <Icon icon="solar:hand-move-bold" className="size-3.5" />
+ Arrastrar
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/app/components/ui/RecordingOverlay.tsx b/app/components/ui/RecordingOverlay.tsx
index 9c9dbba..75113a3 100644
--- a/app/components/ui/RecordingOverlay.tsx
+++ b/app/components/ui/RecordingOverlay.tsx
@@ -1,12 +1,27 @@
"use client";
import { useRecording } from "@/hooks/RecordingContext";
+import FloatingCameraPreview from "./FloatingCameraPreview";
export default function RecordingOverlay() {
- const { state, countdown, recordingTime, stopRecording, isCountdown, isRecording, isProcessing } = useRecording();
+ const {
+ state,
+ countdown,
+ recordingTime,
+ stopRecording,
+ isCountdown,
+ isRecording,
+ isProcessing,
+ cameraStream,
+ cameraConfig,
+ updateCameraConfig,
+ } = useRecording();
if (state === "idle") return null;
+ const showFloatingCamera =
+ (isCountdown || isRecording) && cameraStream && cameraConfig?.enabled;
+
const formatTime = (seconds: number) => {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
@@ -15,6 +30,13 @@ export default function RecordingOverlay() {
return (
<div className="fixed inset-0 z-9999 pointer-events-none">
+ {showFloatingCamera && cameraStream && cameraConfig && (
+ <FloatingCameraPreview
+ stream={cameraStream}
+ config={cameraConfig}
+ onConfigChange={updateCameraConfig}
+ />
+ )}
{isCountdown && (
<div className="absolute inset-0 bg-[#000B13]/95 backdrop-blur-md flex items-center justify-center z-50 pointer-events-auto">
<div className="flex flex-col items-center scale-110">