Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Make the floating webcam preview resizable

Aniruddha Mandal34a19b257047
diff --git a/src/components/launch/LaunchWindow.module.css b/src/components/launch/LaunchWindow.module.css
index e632b99d..195938fd 100644
--- a/src/components/launch/LaunchWindow.module.css
+++ b/src/components/launch/LaunchWindow.module.css
@@ -153,8 +153,6 @@
position: fixed;
right: 32px;
bottom: 120px;
- height: 288px;
- width: 288px;
overflow: hidden;
border-radius: 24px;
background: var(--launch-surface);
@@ -171,6 +169,36 @@
cursor: grabbing;
}
+.recordingWebcamPreviewResizeHandle {
+ position: absolute;
+ width: 20px;
+ height: 20px;
+}
+
+.recordingWebcamPreviewResizeHandle[data-resize-handle="nw"] {
+ top: 0;
+ left: 0;
+ cursor: nwse-resize;
+}
+
+.recordingWebcamPreviewResizeHandle[data-resize-handle="ne"] {
+ top: 0;
+ right: 0;
+ cursor: nesw-resize;
+}
+
+.recordingWebcamPreviewResizeHandle[data-resize-handle="sw"] {
+ bottom: 0;
+ left: 0;
+ cursor: nesw-resize;
+}
+
+.recordingWebcamPreviewResizeHandle[data-resize-handle="se"] {
+ bottom: 0;
+ right: 0;
+ cursor: nwse-resize;
+}
+
.recordingWebcamPreviewVideo {
display: block;
height: 100%;
diff --git a/src/components/launch/LaunchWindow.tsx b/src/components/launch/LaunchWindow.tsx
index 634fec26..a20dd7a3 100644
--- a/src/components/launch/LaunchWindow.tsx
+++ b/src/components/launch/LaunchWindow.tsx
@@ -21,7 +21,10 @@ import { useScreenRecorder } from "../../hooks/useScreenRecorder";
import { useVideoDevices } from "../../hooks/useVideoDevices";
import { Button } from "../ui/button";
import { HudInteractionContext } from "./contexts/HudInteractionContext";
-import { canToggleFloatingWebcamPreview } from "./floatingWebcamPreview";
+import {
+ canToggleFloatingWebcamPreview,
+ WEBCAM_PREVIEW_RESIZE_HANDLES,
+} from "./floatingWebcamPreview";
import { useHudBarDrag } from "./hooks/useHudBarDrag";
import { useLaunchHudInteractionState } from "./hooks/useLaunchHudInteractionState";
import { useLaunchWindowActions } from "./hooks/useLaunchWindowActions";
@@ -116,12 +119,16 @@ function LaunchWindowContent() {
setShowFloatingWebcamPreview,
showRecordingWebcamPreview,
webcamPreviewOffset,
+ webcamPreviewSize,
recordingWebcamPreviewContainerRef,
isWebcamPreviewDraggingRef,
webcamPreviewDragStartRef,
handleWebcamPreviewPointerDown,
handleWebcamPreviewPointerMove,
handleWebcamPreviewPointerUp,
+ handleWebcamPreviewResizePointerDown,
+ handleWebcamPreviewResizePointerMove,
+ handleWebcamPreviewResizePointerUp,
setWebcamPreviewNode,
setRecordingWebcamPreviewNode,
} = useWebcamPreviewOverlay({
@@ -506,6 +513,8 @@ function LaunchWindowContent() {
title={t("recording.webcam")}
style={{
transform: `translate(${webcamPreviewOffset.x}px, ${webcamPreviewOffset.y}px)`,
+ width: webcamPreviewSize.width,
+ height: webcamPreviewSize.height,
}}
onMouseEnter={handleHudMouseEnter}
onMouseLeave={handleHudMouseLeave}
@@ -521,6 +530,17 @@ function LaunchWindowContent() {
playsInline
style={{ transform: "scaleX(-1)" }}
/>
+ {WEBCAM_PREVIEW_RESIZE_HANDLES.map((handle) => (
+ <div
+ key={handle}
+ data-resize-handle={handle}
+ className={styles.recordingWebcamPreviewResizeHandle}
+ onPointerDown={handleWebcamPreviewResizePointerDown}
+ onPointerMove={handleWebcamPreviewResizePointerMove}
+ onPointerUp={handleWebcamPreviewResizePointerUp}
+ onPointerCancel={handleWebcamPreviewResizePointerUp}
+ />
+ ))}
</div>
)}
</div>
diff --git a/src/components/launch/floatingWebcamPreview.test.ts b/src/components/launch/floatingWebcamPreview.test.ts
index 6e876f1d..89a738bb 100644
--- a/src/components/launch/floatingWebcamPreview.test.ts
+++ b/src/components/launch/floatingWebcamPreview.test.ts
@@ -3,6 +3,9 @@ import { describe, expect, it } from "vitest";
import {
canShowFloatingWebcamPreview,
canToggleFloatingWebcamPreview,
+ DEFAULT_WEBCAM_PREVIEW_SIZE,
+ parseWebcamPreviewSize,
+ resizeWebcamPreview,
} from "./floatingWebcamPreview";
describe("canShowFloatingWebcamPreview", () => {
@@ -24,3 +27,69 @@ describe("canToggleFloatingWebcamPreview", () => {
expect(canToggleFloatingWebcamPreview(false)).toBe(false);
});
});
+
+describe("parseWebcamPreviewSize", () => {
+ it("restores a stored size clamped to the allowed range", () => {
+ expect(parseWebcamPreviewSize('{"width":200,"height":150}')).toEqual({
+ width: 200,
+ height: 150,
+ });
+ expect(parseWebcamPreviewSize('{"width":10,"height":9000}')).toEqual({
+ width: 120,
+ height: 480,
+ });
+ });
+
+ it("falls back to the default size for missing or corrupt values", () => {
+ for (const stored of [null, "", "not json", "null", '{"width":"big"}']) {
+ expect(parseWebcamPreviewSize(stored)).toEqual(DEFAULT_WEBCAM_PREVIEW_SIZE);
+ }
+ });
+});
+
+describe("resizeWebcamPreview", () => {
+ const viewport = { width: 1440, height: 900 };
+ const start = { left: 1000, top: 400, right: 1288, bottom: 688 };
+
+ it("grows from the top-left corner without moving the anchored bottom-right edge", () => {
+ expect(resizeWebcamPreview("nw", start, -50, -20, viewport)).toEqual({
+ width: 338,
+ height: 308,
+ shiftX: 0,
+ shiftY: 0,
+ });
+ });
+
+ it("shifts the offset when resizing from the bottom-right so the top-left stays put", () => {
+ expect(resizeWebcamPreview("se", start, -100, -60, viewport)).toEqual({
+ width: 188,
+ height: 228,
+ shiftX: -100,
+ shiftY: -60,
+ });
+ });
+
+ it("enforces the minimum and maximum size", () => {
+ expect(resizeWebcamPreview("ne", start, -1000, 1000, viewport)).toMatchObject({
+ width: 120,
+ height: 120,
+ });
+ expect(resizeWebcamPreview("sw", start, -1000, 1000, viewport)).toMatchObject({
+ width: 480,
+ height: 480,
+ });
+ });
+
+ it("never pushes the dragged edge past the viewport", () => {
+ const nearCorner = { left: 20, top: 30, right: 308, bottom: 318 };
+ expect(resizeWebcamPreview("nw", nearCorner, -100, -100, viewport)).toMatchObject({
+ width: 308,
+ height: 318,
+ });
+ const nearEdge = { left: 1300, top: 700, right: 1420, bottom: 820 };
+ expect(resizeWebcamPreview("se", nearEdge, 200, 200, viewport)).toMatchObject({
+ width: 140,
+ height: 200,
+ });
+ });
+});
diff --git a/src/components/launch/floatingWebcamPreview.ts b/src/components/launch/floatingWebcamPreview.ts
index 5e421550..4612b313 100644
--- a/src/components/launch/floatingWebcamPreview.ts
+++ b/src/components/launch/floatingWebcamPreview.ts
@@ -10,3 +10,71 @@ export function canToggleFloatingWebcamPreview(
): boolean {
return hudOverlayMousePassthroughSupported !== false;
}
+
+export interface WebcamPreviewSize {
+ width: number;
+ height: number;
+}
+
+export type WebcamPreviewResizeHandle = "nw" | "ne" | "sw" | "se";
+
+export const WEBCAM_PREVIEW_RESIZE_HANDLES: WebcamPreviewResizeHandle[] = ["nw", "ne", "sw", "se"];
+export const WEBCAM_PREVIEW_SIZE_STORAGE_KEY = "recordly.webcam-preview-size";
+export const DEFAULT_WEBCAM_PREVIEW_SIZE: WebcamPreviewSize = { width: 288, height: 288 };
+const WEBCAM_PREVIEW_MIN_SIZE = 120;
+const WEBCAM_PREVIEW_MAX_SIZE = 480;
+
+function clampWebcamPreviewDimension(value: number, max = WEBCAM_PREVIEW_MAX_SIZE): number {
+ return Math.min(
+ Math.max(value, WEBCAM_PREVIEW_MIN_SIZE),
+ Math.min(max, WEBCAM_PREVIEW_MAX_SIZE),
+ );
+}
+
+export function parseWebcamPreviewSize(stored: string | null): WebcamPreviewSize {
+ try {
+ const { width, height } = JSON.parse(stored ?? "");
+ if (Number.isFinite(width) && Number.isFinite(height)) {
+ return {
+ width: clampWebcamPreviewDimension(width),
+ height: clampWebcamPreviewDimension(height),
+ };
+ }
+ } catch {
+ // Missing or corrupt preference: fall back to the default size.
+ }
+ return DEFAULT_WEBCAM_PREVIEW_SIZE;
+}
+
+/**
+ * The preview is anchored by its bottom-right corner (CSS right/bottom plus a translate
+ * offset), so growing it from an east/south handle also shifts the offset to keep the
+ * opposite edge in place. The dragged edge is never pushed past the viewport.
+ */
+export function resizeWebcamPreview(
+ handle: WebcamPreviewResizeHandle,
+ start: { left: number; top: number; right: number; bottom: number },
+ deltaX: number,
+ deltaY: number,
+ viewport: WebcamPreviewSize,
+): WebcamPreviewSize & { shiftX: number; shiftY: number } {
+ const growsEast = handle.endsWith("e");
+ const growsSouth = handle.startsWith("s");
+ const startWidth = start.right - start.left;
+ const startHeight = start.bottom - start.top;
+ const width = clampWebcamPreviewDimension(
+ startWidth + (growsEast ? deltaX : -deltaX),
+ growsEast ? viewport.width - start.left : start.right,
+ );
+ const height = clampWebcamPreviewDimension(
+ startHeight + (growsSouth ? deltaY : -deltaY),
+ growsSouth ? viewport.height - start.top : start.bottom,
+ );
+
+ return {
+ width,
+ height,
+ shiftX: growsEast ? width - startWidth : 0,
+ shiftY: growsSouth ? height - startHeight : 0,
+ };
+}
diff --git a/src/components/launch/hooks/useWebcamPreviewOverlay.ts b/src/components/launch/hooks/useWebcamPreviewOverlay.ts
index 3f914405..ed527e9a 100644
--- a/src/components/launch/hooks/useWebcamPreviewOverlay.ts
+++ b/src/components/launch/hooks/useWebcamPreviewOverlay.ts
@@ -1,5 +1,12 @@
import { type PointerEvent, useCallback, useEffect, useRef, useState } from "react";
-import { canShowFloatingWebcamPreview } from "../floatingWebcamPreview";
+import {
+ canShowFloatingWebcamPreview,
+ parseWebcamPreviewSize,
+ resizeWebcamPreview,
+ WEBCAM_PREVIEW_SIZE_STORAGE_KEY,
+ type WebcamPreviewResizeHandle,
+} from "../floatingWebcamPreview";
+import { shouldRestoreHudMousePassthroughAfterDrag } from "../hudMousePassthrough";
const WEBCAM_PREVIEW_DRAG_THRESHOLD = 6;
const DEFAULT_WEBCAM_PREVIEW_OFFSET = { x: 0, y: 0 };
@@ -41,6 +48,18 @@ export function useWebcamPreviewOverlay({
const [showFloatingWebcamPreview, setShowFloatingWebcamPreview] = useState(true);
const [webcamPreviewOffset, setWebcamPreviewOffset] = useState(DEFAULT_WEBCAM_PREVIEW_OFFSET);
const webcamPreviewOffsetRef = useRef(DEFAULT_WEBCAM_PREVIEW_OFFSET);
+ const [webcamPreviewSize, setWebcamPreviewSize] = useState(() =>
+ parseWebcamPreviewSize(localStorage.getItem(WEBCAM_PREVIEW_SIZE_STORAGE_KEY)),
+ );
+ const webcamPreviewResizeRef = useRef<{
+ pointerId: number;
+ handle: WebcamPreviewResizeHandle;
+ startX: number;
+ startY: number;
+ startRect: DOMRect;
+ startOffset: { x: number; y: number };
+ size: { width: number; height: number };
+ } | null>(null);
const webcamPreviewRef = useRef<HTMLVideoElement | null>(null);
const recordingWebcamPreviewRef = useRef<HTMLVideoElement | null>(null);
const recordingWebcamPreviewContainerRef = useRef<HTMLDivElement | null>(null);
@@ -190,6 +209,89 @@ export function useWebcamPreviewOverlay({
}
}, []);
+ const handleWebcamPreviewResizePointerDown = useCallback(
+ (event: PointerEvent<HTMLDivElement>) => {
+ const container = recordingWebcamPreviewContainerRef.current;
+ if (event.button !== 0 || !container) {
+ return;
+ }
+
+ // Keep the container's drag handler from treating this as a move.
+ event.preventDefault();
+ event.stopPropagation();
+ window.electronAPI?.hudOverlaySetIgnoreMouse?.(false);
+ isWebcamPreviewDraggingRef.current = true;
+ webcamPreviewResizeRef.current = {
+ pointerId: event.pointerId,
+ handle: event.currentTarget.dataset.resizeHandle as WebcamPreviewResizeHandle,
+ startX: event.clientX,
+ startY: event.clientY,
+ startRect: container.getBoundingClientRect(),
+ startOffset: webcamPreviewOffsetRef.current,
+ size: webcamPreviewSize,
+ };
+ event.currentTarget.setPointerCapture(event.pointerId);
+ },
+ [webcamPreviewSize],
+ );
+
+ const handleWebcamPreviewResizePointerMove = useCallback(
+ (event: PointerEvent<HTMLDivElement>) => {
+ const resizeState = webcamPreviewResizeRef.current;
+ const container = recordingWebcamPreviewContainerRef.current;
+ if (!resizeState || resizeState.pointerId !== event.pointerId || !container) {
+ return;
+ }
+
+ const { width, height, shiftX, shiftY } = resizeWebcamPreview(
+ resizeState.handle,
+ resizeState.startRect,
+ event.clientX - resizeState.startX,
+ event.clientY - resizeState.startY,
+ { width: window.innerWidth, height: window.innerHeight },
+ );
+ resizeState.size = { width, height };
+ webcamPreviewOffsetRef.current = {
+ x: resizeState.startOffset.x + shiftX,
+ y: resizeState.startOffset.y + shiftY,
+ };
+ container.style.width = `${width}px`;
+ container.style.height = `${height}px`;
+ container.style.transform = `translate(${webcamPreviewOffsetRef.current.x}px, ${webcamPreviewOffsetRef.current.y}px)`;
+ },
+ [],
+ );
+
+ const handleWebcamPreviewResizePointerUp = useCallback(
+ (event: PointerEvent<HTMLDivElement>) => {
+ const resizeState = webcamPreviewResizeRef.current;
+ if (!resizeState || resizeState.pointerId !== event.pointerId) {
+ return;
+ }
+
+ webcamPreviewResizeRef.current = null;
+ isWebcamPreviewDraggingRef.current = false;
+ setWebcamPreviewSize(resizeState.size);
+ setWebcamPreviewOffset({ ...webcamPreviewOffsetRef.current });
+ localStorage.setItem(WEBCAM_PREVIEW_SIZE_STORAGE_KEY, JSON.stringify(resizeState.size));
+ if (event.currentTarget.hasPointerCapture(event.pointerId)) {
+ event.currentTarget.releasePointerCapture(event.pointerId);
+ }
+ const previewBounds =
+ recordingWebcamPreviewContainerRef.current?.getBoundingClientRect();
+ if (
+ shouldRestoreHudMousePassthroughAfterDrag(
+ previewBounds ?? null,
+ event.clientX,
+ event.clientY,
+ )
+ ) {
+ window.electronAPI?.hudOverlaySetIgnoreMouse?.(true);
+ }
+ },
+ [],
+ );
+
const attachPreviewStreamToNode = useCallback((videoElement: HTMLVideoElement | null) => {
const previewStream = previewStreamRef.current;
if (!videoElement || !previewStream || videoElement.srcObject === previewStream) {
@@ -294,12 +396,16 @@ export function useWebcamPreviewOverlay({
showFloatingWebcamPreview,
setShowFloatingWebcamPreview,
webcamPreviewOffset,
+ webcamPreviewSize,
recordingWebcamPreviewContainerRef,
isWebcamPreviewDraggingRef,
webcamPreviewDragStartRef,
handleWebcamPreviewPointerDown,
handleWebcamPreviewPointerMove,
handleWebcamPreviewPointerUp,
+ handleWebcamPreviewResizePointerDown,
+ handleWebcamPreviewResizePointerMove,
+ handleWebcamPreviewResizePointerUp,
setWebcamPreviewNode,
setRecordingWebcamPreviewNode,
showRecordingWebcamPreview,
diff --git a/tests/ui/hud-layout.spec.ts b/tests/ui/hud-layout.spec.ts
index 8116374d..178dd73d 100644
--- a/tests/ui/hud-layout.spec.ts
+++ b/tests/ui/hud-layout.spec.ts
@@ -108,3 +108,57 @@ test("New recording mode starts the webcam with an editor still open and release
.poll(() => preview.evaluate((video: HTMLVideoElement) => video.srcObject === null))
.toBe(true);
});
+
+test("floating webcam preview resizes from its corners and remembers the size", async ({
+ page,
+}) => {
+ await installDesktopBridge(page);
+ await page.addInitScript(() => {
+ window.electronAPI.getRecordingPreferences = async () => ({
+ success: true,
+ microphoneEnabled: false,
+ webcamEnabled: true,
+ systemAudioEnabled: false,
+ });
+ navigator.mediaDevices.getUserMedia = async () => {
+ const canvas = document.createElement("canvas");
+ canvas.getContext("2d")!.fillRect(0, 0, 320, 320);
+ return canvas.captureStream(24);
+ };
+ navigator.mediaDevices.enumerateDevices = async () => [];
+ });
+ await page.goto("/?windowType=hud-overlay");
+ const preview = page.locator("[data-resize-handle]").first().locator("..");
+ await expect(preview).toBeVisible();
+ const before = (await preview.boundingBox())!;
+ expect([before.width, before.height]).toEqual([288, 288]);
+
+ const dragHandle = async (handle: string, dx: number, dy: number) => {
+ const box = (await preview.locator(`[data-resize-handle="${handle}"]`).boundingBox())!;
+ await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
+ await page.mouse.down();
+ await page.mouse.move(box.x + box.width / 2 + dx, box.y + box.height / 2 + dy, {
+ steps: 5,
+ });
+ await page.mouse.up();
+ };
+
+ // Top-left grows the preview while the bottom-right corner stays anchored.
+ await dragHandle("nw", -60, -40);
+ const grown = (await preview.boundingBox())!;
+ expect([grown.width, grown.height]).toEqual([348, 328]);
+ expect(grown.x + grown.width).toBeCloseTo(before.x + before.width);
+ expect(grown.y + grown.height).toBeCloseTo(before.y + before.height);
+
+ // Bottom-right shrinks the preview while the top-left corner stays put, down to the minimum.
+ await dragHandle("se", -500, -500);
+ const shrunk = (await preview.boundingBox())!;
+ expect([shrunk.width, shrunk.height]).toEqual([120, 120]);
+ expect(shrunk.x).toBeCloseTo(grown.x);
+ expect(shrunk.y).toBeCloseTo(grown.y);
+
+ await page.reload();
+ await expect(preview).toBeVisible();
+ const restored = (await preview.boundingBox())!;
+ expect([restored.width, restored.height]).toEqual([120, 120]);
+});