mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
feat(studio): type an exact preview zoom and see a pixel grid at high zoom (#5708)
* feat(studio): type an exact preview zoom and see a pixel grid at high zoom Clicking the preview's zoom readout opens an input: Enter applies the typed percentage (clamped to the zoom limits, keeping the view's centre), Esc or blur cancels. Above 800% a one-pixel grid is drawn over the stage, one cell per composition pixel. It lives in Studio's DOM, so renders and snapshots never include it. The max preview zoom goes from 400% to 1600% so the 800% grid is reachable. Closes #5587 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(studio): move preview zoom reasoning from comments to the PR Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cc7a760f03
commit
25a61016d1
@@ -390,6 +390,78 @@ describe("NLEPreview", () => {
|
||||
view.cleanup();
|
||||
});
|
||||
|
||||
/** Opens the zoom readout's input, types `text` into it and presses `key`. */
|
||||
function typeZoom(view: ReturnType<typeof renderPreview>, text: string, key: string) {
|
||||
act(() => {
|
||||
view.host.querySelector<HTMLButtonElement>('[data-testid="preview-zoom-readout"]')!.click();
|
||||
});
|
||||
const input = view.host.querySelector<HTMLInputElement>(
|
||||
'[data-testid="preview-zoom-input"]',
|
||||
)!;
|
||||
act(() => {
|
||||
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(
|
||||
input,
|
||||
text,
|
||||
);
|
||||
input.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
});
|
||||
act(() => {
|
||||
input.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
|
||||
});
|
||||
act(() => vi.advanceTimersByTime(300));
|
||||
}
|
||||
const pixelGrid = (view: ReturnType<typeof renderPreview>) =>
|
||||
view.host.querySelector<HTMLElement>('[data-testid="preview-pixel-grid"]');
|
||||
|
||||
it("applies a typed zoom on Enter, clamped to the zoom limit, and closes the input", () => {
|
||||
const view = renderPreview();
|
||||
pinchIn(view, 10);
|
||||
typeZoom(view, "350%", "Enter");
|
||||
expect(view.stage.style.transform).toContain("scale(3.5)");
|
||||
expect(chip(view)?.textContent).toBe("Zoomed 350%·Fit");
|
||||
|
||||
typeZoom(view, "99999", "Enter");
|
||||
expect(view.stage.style.transform).toContain("scale(16)");
|
||||
expect(view.host.querySelector('[data-testid="preview-zoom-input"]')).toBeNull();
|
||||
view.cleanup();
|
||||
});
|
||||
|
||||
it("keeps the zoom when the typed value is cancelled with Esc or is not a number", () => {
|
||||
const view = renderPreview();
|
||||
pinchIn(view, 10);
|
||||
const before = view.stage.style.transform;
|
||||
|
||||
typeZoom(view, "500", "Escape");
|
||||
expect(view.stage.style.transform).toBe(before);
|
||||
expect(view.host.querySelector('[data-testid="preview-zoom-input"]')).toBeNull();
|
||||
|
||||
typeZoom(view, "abc", "Enter");
|
||||
expect(view.stage.style.transform).toBe(before);
|
||||
view.cleanup();
|
||||
});
|
||||
|
||||
it("draws a one-pixel grid above 800% over the stage, one cell per composition pixel", () => {
|
||||
const view = renderPreview(undefined, { compositionSizeHint: { width: 1920, height: 1080 } });
|
||||
pinchIn(view, 10);
|
||||
typeZoom(view, "800", "Enter");
|
||||
expect(pixelGrid(view)?.hidden).toBe(true);
|
||||
|
||||
typeZoom(view, "1000", "Enter");
|
||||
const grid = pixelGrid(view)!;
|
||||
expect(grid.hidden).toBe(false);
|
||||
expect(view.stage.contains(grid)).toBe(false);
|
||||
const stageWidth = parseFloat(view.stage.style.width);
|
||||
expect(parseFloat(grid.style.width)).toBeCloseTo(stageWidth * 10, 2);
|
||||
expect(parseFloat(grid.style.backgroundSize)).toBeCloseTo((stageWidth * 10) / 1920, 3);
|
||||
|
||||
act(() => {
|
||||
view.host.querySelector<HTMLButtonElement>('[data-testid="preview-zoom-fit"]')!.click();
|
||||
});
|
||||
act(() => vi.advanceTimersByTime(300));
|
||||
expect(pixelGrid(view)).toBeNull();
|
||||
view.cleanup();
|
||||
});
|
||||
|
||||
it("keeps a zoom only while the project is open: nothing is saved, and another project opens at Fit", () => {
|
||||
const view = renderPreview();
|
||||
pinchIn(view, 10);
|
||||
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
type PreviewZoomState,
|
||||
} from "./previewZoom";
|
||||
import { RULER_GUTTER_PX, usePreviewGuidesStore } from "../editor/previewGuidesStore";
|
||||
import { PreviewZoomOverlay, usePreviewNavigator } from "./PreviewZoomOverlay";
|
||||
import { PreviewZoomOverlay, usePreviewZoomOverlay } from "./PreviewZoomOverlay";
|
||||
import {
|
||||
PREVIEW_STAGE_INSET_PX,
|
||||
resolvePreviewStageSize,
|
||||
@@ -192,11 +192,13 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
const stageSizeRef = useRef(stageSize);
|
||||
stageSizeRef.current = stageSize;
|
||||
|
||||
const { draw: drawNavigator, setRegion: setNavigatorRegion } = usePreviewNavigator(
|
||||
const zoomOverlay = usePreviewZoomOverlay(
|
||||
viewportRef,
|
||||
stageSize,
|
||||
zoomRef,
|
||||
compositionSize?.width ?? hintWidth,
|
||||
);
|
||||
const { draw: drawZoomOverlay } = zoomOverlay;
|
||||
const writeTransform = useCallback(
|
||||
(state: PreviewZoomState) => {
|
||||
const stage = stageRef.current;
|
||||
@@ -205,9 +207,9 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
const px = toDomPrecision(state.panX);
|
||||
const py = toDomPrecision(state.panY);
|
||||
stage.style.transform = `translate3d(${px}px, ${py}px, 0) scale(${s})`;
|
||||
drawNavigator(state);
|
||||
drawZoomOverlay(state);
|
||||
},
|
||||
[drawNavigator],
|
||||
[drawZoomOverlay],
|
||||
);
|
||||
|
||||
const zoomProjectRef = useRef(projectId);
|
||||
@@ -551,11 +553,8 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
<PreviewZoomOverlay
|
||||
zoom={settledZoom}
|
||||
stageSize={stageSize}
|
||||
onFit={() => {
|
||||
applyZoom(DEFAULT_PREVIEW_ZOOM);
|
||||
viewportRef.current?.focus();
|
||||
}}
|
||||
navigatorRegionRef={setNavigatorRegion}
|
||||
onZoom={applyZoom}
|
||||
controls={zoomOverlay}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { useCallback, useEffect, useRef, type RefObject } from "react";
|
||||
import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
|
||||
import {
|
||||
DEFAULT_PREVIEW_ZOOM,
|
||||
isFitZoom,
|
||||
isPreviewAtFit,
|
||||
parsePreviewZoomInput,
|
||||
resolvePreviewPixelGrid,
|
||||
resolvePreviewTypedZoom,
|
||||
resolvePreviewVisibleRegion,
|
||||
toDomPrecision,
|
||||
type PreviewZoomState,
|
||||
@@ -21,7 +25,7 @@ function navigatorFrameSize(stage: { width: number; height: number }) {
|
||||
}
|
||||
|
||||
/** `draw` moves the navigator's highlight without a render, so it can follow every transform write. */
|
||||
export function usePreviewNavigator(
|
||||
function usePreviewNavigator(
|
||||
viewportRef: RefObject<HTMLDivElement | null>,
|
||||
stageSize: { width: number; height: number },
|
||||
zoomRef: RefObject<PreviewZoomState>,
|
||||
@@ -59,34 +63,176 @@ export function usePreviewNavigator(
|
||||
return { draw, setRegion };
|
||||
}
|
||||
|
||||
function ZoomPercentInput({
|
||||
zoomPercent,
|
||||
onSubmit,
|
||||
onDone,
|
||||
}: {
|
||||
zoomPercent: number;
|
||||
onSubmit: (zoomPercent: number) => void;
|
||||
onDone: () => void;
|
||||
}) {
|
||||
const [text, setText] = useState(() => String(Math.round(zoomPercent)));
|
||||
return (
|
||||
<input
|
||||
autoFocus
|
||||
onFocus={(event) => event.currentTarget.select()}
|
||||
value={text}
|
||||
onChange={(event) => setText(event.currentTarget.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter") {
|
||||
const next = parsePreviewZoomInput(text);
|
||||
if (next !== null) onSubmit(next);
|
||||
onDone();
|
||||
} else if (event.key === "Escape") {
|
||||
onDone();
|
||||
}
|
||||
}}
|
||||
onBlur={onDone}
|
||||
inputMode="decimal"
|
||||
aria-label="Zoom percentage"
|
||||
className="w-12 rounded bg-white/10 px-1 py-0.5 text-xs tabular-nums text-white outline-hidden focus:ring-1 focus:ring-studio-accent"
|
||||
data-testid="preview-zoom-input"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function usePreviewPixelGrid(
|
||||
viewportRef: RefObject<HTMLDivElement | null>,
|
||||
stageSize: { width: number; height: number },
|
||||
compositionWidth: number | undefined,
|
||||
zoomRef: RefObject<PreviewZoomState>,
|
||||
) {
|
||||
const gridRef = useRef<HTMLDivElement | null>(null);
|
||||
const layoutRef = useRef({ stageSize, compositionWidth });
|
||||
layoutRef.current = { stageSize, compositionWidth };
|
||||
const draw = useCallback(
|
||||
(state: PreviewZoomState) => {
|
||||
const grid = gridRef.current;
|
||||
if (!grid) return;
|
||||
const rect = viewportRef.current?.getBoundingClientRect();
|
||||
const { stageSize: stage, compositionWidth: width } = layoutRef.current;
|
||||
const box = rect && width ? resolvePreviewPixelGrid({ state, rect, stage, width }) : null;
|
||||
grid.hidden = !box;
|
||||
if (!box) return;
|
||||
grid.style.left = `${box.left}px`;
|
||||
grid.style.top = `${box.top}px`;
|
||||
grid.style.width = `${box.width}px`;
|
||||
grid.style.height = `${box.height}px`;
|
||||
grid.style.backgroundSize = `${box.cellPx}px ${box.cellPx}px`;
|
||||
},
|
||||
[viewportRef],
|
||||
);
|
||||
const setGrid = useCallback(
|
||||
(node: HTMLDivElement | null) => {
|
||||
gridRef.current = node;
|
||||
draw(zoomRef.current);
|
||||
},
|
||||
[draw, zoomRef],
|
||||
);
|
||||
useEffect(() => draw(zoomRef.current), [stageSize, compositionWidth, draw, zoomRef]);
|
||||
return { draw, setGrid };
|
||||
}
|
||||
|
||||
export type PreviewZoomOverlayControls = ReturnType<typeof usePreviewZoomOverlay>;
|
||||
|
||||
export function usePreviewZoomOverlay(
|
||||
viewportRef: RefObject<HTMLDivElement | null>,
|
||||
stageSize: { width: number; height: number },
|
||||
zoomRef: RefObject<PreviewZoomState>,
|
||||
compositionWidth: number | undefined,
|
||||
) {
|
||||
const { draw: drawNavigator, setRegion } = usePreviewNavigator(viewportRef, stageSize, zoomRef);
|
||||
const { draw: drawGrid, setGrid } = usePreviewPixelGrid(
|
||||
viewportRef,
|
||||
stageSize,
|
||||
compositionWidth,
|
||||
zoomRef,
|
||||
);
|
||||
const draw = useCallback(
|
||||
(state: PreviewZoomState) => {
|
||||
drawNavigator(state);
|
||||
drawGrid(state);
|
||||
},
|
||||
[drawNavigator, drawGrid],
|
||||
);
|
||||
return { viewportRef, draw, setRegion, setGrid };
|
||||
}
|
||||
|
||||
export function PreviewZoomOverlay({
|
||||
zoom,
|
||||
stageSize,
|
||||
onFit,
|
||||
navigatorRegionRef,
|
||||
onZoom,
|
||||
controls,
|
||||
}: {
|
||||
zoom: PreviewZoomState;
|
||||
stageSize: { width: number; height: number };
|
||||
onFit: () => void;
|
||||
navigatorRegionRef: (node: HTMLDivElement | null) => void;
|
||||
onZoom: (next: PreviewZoomState) => void;
|
||||
controls: PreviewZoomOverlayControls;
|
||||
}) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
if (isPreviewAtFit(zoom)) return null;
|
||||
const { viewportRef } = controls;
|
||||
const zoomTo = (zoomPercent: number) => {
|
||||
const rect = viewportRef.current?.getBoundingClientRect();
|
||||
onZoom(
|
||||
resolvePreviewTypedZoom({
|
||||
state: zoom,
|
||||
zoomPercent,
|
||||
viewportWidth: rect?.width ?? stageSize.width,
|
||||
viewportHeight: rect?.height ?? stageSize.height,
|
||||
contentWidth: stageSize.width,
|
||||
contentHeight: stageSize.height,
|
||||
}),
|
||||
);
|
||||
viewportRef.current?.focus();
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
ref={controls.setGrid}
|
||||
hidden
|
||||
aria-hidden="true"
|
||||
className="pointer-events-none absolute z-10"
|
||||
style={{
|
||||
backgroundImage:
|
||||
"linear-gradient(to right, var(--color-pixel-grid) 1px, transparent 1px), linear-gradient(to bottom, var(--color-pixel-grid) 1px, transparent 1px)",
|
||||
}}
|
||||
data-testid="preview-pixel-grid"
|
||||
/>
|
||||
<div
|
||||
className="absolute top-3 left-1/2 -translate-x-1/2 z-50 flex items-center gap-1.5 rounded-md py-1 pl-2.5 pr-1 text-xs text-white/80 bg-black/60 backdrop-blur-xs"
|
||||
data-testid="preview-zoom-chip"
|
||||
// The pane clears the timeline selection on a pointerdown outside the frame.
|
||||
onPointerDown={(event) => event.stopPropagation()}
|
||||
>
|
||||
<span className="tabular-nums">{zoomChipLabel(zoom.zoomPercent)}</span>
|
||||
{editing ? (
|
||||
<ZoomPercentInput
|
||||
zoomPercent={zoom.zoomPercent}
|
||||
onSubmit={zoomTo}
|
||||
onDone={() => setEditing(false)}
|
||||
/>
|
||||
) : (
|
||||
<button
|
||||
type="button"
|
||||
className="rounded px-1 py-0.5 tabular-nums hover:bg-white/10 transition-colors"
|
||||
onClick={() => setEditing(true)}
|
||||
aria-label="Type a zoom percentage"
|
||||
data-testid="preview-zoom-readout"
|
||||
>
|
||||
{zoomChipLabel(zoom.zoomPercent)}
|
||||
</button>
|
||||
)}
|
||||
<span aria-hidden="true" className="text-white/30">
|
||||
·
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded px-1.5 py-0.5 font-medium text-accent-ink hover:bg-white/10 transition-colors"
|
||||
onClick={onFit}
|
||||
onClick={() => {
|
||||
onZoom(DEFAULT_PREVIEW_ZOOM);
|
||||
viewportRef.current?.focus();
|
||||
}}
|
||||
aria-label="Fit the whole frame in view"
|
||||
data-testid="preview-zoom-fit"
|
||||
>
|
||||
@@ -100,7 +246,7 @@ export function PreviewZoomOverlay({
|
||||
>
|
||||
<div className="relative overflow-hidden bg-white/10" style={navigatorFrameSize(stageSize)}>
|
||||
<div
|
||||
ref={navigatorRegionRef}
|
||||
ref={controls.setRegion}
|
||||
className="absolute rounded-[1px] border border-studio-accent bg-studio-accent/15"
|
||||
data-testid="preview-zoom-navigator-region"
|
||||
/>
|
||||
|
||||
@@ -11,11 +11,16 @@ import {
|
||||
getNextPreviewZoomPercent,
|
||||
getPreviewWheelZoomPercent,
|
||||
ownsPreviewPanTarget,
|
||||
parsePreviewZoomInput,
|
||||
PREVIEW_PAN_OVERSCROLL_PX,
|
||||
PREVIEW_PAN_SURFACE_SELECTOR,
|
||||
PREVIEW_PIXEL_GRID_MIN_ZOOM_PERCENT,
|
||||
resolvePreviewPixelGrid,
|
||||
resolvePreviewTypedZoom,
|
||||
resolvePreviewVisibleRegion,
|
||||
resolvePreviewWheelPan,
|
||||
resolvePreviewWheelZoom,
|
||||
showsPreviewPixelGrid,
|
||||
toDomPrecision,
|
||||
} from "./previewZoom";
|
||||
|
||||
@@ -44,6 +49,99 @@ describe("clampPreviewZoomPercent", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("parsePreviewZoomInput", () => {
|
||||
it("reads a plain number or a percentage, with surrounding spaces", () => {
|
||||
expect(parsePreviewZoomInput("250")).toBe(250);
|
||||
expect(parsePreviewZoomInput(" 250 % ")).toBe(250);
|
||||
expect(parsePreviewZoomInput("137.5%")).toBe(137.5);
|
||||
});
|
||||
|
||||
it("clamps to the preview zoom bounds", () => {
|
||||
expect(parsePreviewZoomInput("5")).toBe(MIN_PREVIEW_ZOOM_PERCENT);
|
||||
expect(parsePreviewZoomInput("0")).toBe(MIN_PREVIEW_ZOOM_PERCENT);
|
||||
expect(parsePreviewZoomInput("99999")).toBe(MAX_PREVIEW_ZOOM_PERCENT);
|
||||
});
|
||||
|
||||
it("rejects anything that is not a percentage", () => {
|
||||
for (const text of ["", " ", "%", "abc", "-50", "1e3", "12px", "1,000", "50%%"]) {
|
||||
expect(parsePreviewZoomInput(text)).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("showsPreviewPixelGrid", () => {
|
||||
it("shows the grid only above 800%, which the zoom limit can reach", () => {
|
||||
expect(PREVIEW_PIXEL_GRID_MIN_ZOOM_PERCENT).toBe(800);
|
||||
expect(MAX_PREVIEW_ZOOM_PERCENT).toBeGreaterThan(PREVIEW_PIXEL_GRID_MIN_ZOOM_PERCENT);
|
||||
expect(showsPreviewPixelGrid(100)).toBe(false);
|
||||
expect(showsPreviewPixelGrid(800)).toBe(false);
|
||||
expect(showsPreviewPixelGrid(801)).toBe(true);
|
||||
expect(showsPreviewPixelGrid(MAX_PREVIEW_ZOOM_PERCENT)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolvePreviewPixelGrid", () => {
|
||||
const layout = {
|
||||
rect: { width: 1000, height: 600 },
|
||||
stage: { width: 960, height: 540 },
|
||||
width: 1920,
|
||||
};
|
||||
|
||||
it("covers the scaled, centred and panned stage with one cell per composition pixel", () => {
|
||||
const box = resolvePreviewPixelGrid({
|
||||
state: { zoomPercent: 1000, panX: 30, panY: -20 },
|
||||
...layout,
|
||||
});
|
||||
expect(box).toEqual({
|
||||
left: (1000 - 9600) / 2 + 30,
|
||||
top: (600 - 5400) / 2 - 20,
|
||||
width: 9600,
|
||||
height: 5400,
|
||||
cellPx: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it("draws nothing at or below the threshold or before the composition size is known", () => {
|
||||
const state = { zoomPercent: 800, panX: 0, panY: 0 };
|
||||
expect(resolvePreviewPixelGrid({ state, ...layout })).toBeNull();
|
||||
expect(
|
||||
resolvePreviewPixelGrid({ state: { ...state, zoomPercent: 1000 }, ...layout, width: 0 }),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolvePreviewTypedZoom", () => {
|
||||
const viewport = { viewportWidth: 1000, viewportHeight: 600 };
|
||||
|
||||
it("keeps the point at the centre of the view in place", () => {
|
||||
const next = resolvePreviewTypedZoom({
|
||||
state: { zoomPercent: 200, panX: 100, panY: -50 },
|
||||
zoomPercent: 400,
|
||||
...viewport,
|
||||
});
|
||||
expect(next).toEqual({ zoomPercent: 400, panX: 200, panY: -100 });
|
||||
});
|
||||
|
||||
it("clamps the zoom to its bounds", () => {
|
||||
const state = { zoomPercent: 200, panX: 0, panY: 0 };
|
||||
expect(resolvePreviewTypedZoom({ state, zoomPercent: 10, ...viewport }).zoomPercent).toBe(
|
||||
MIN_PREVIEW_ZOOM_PERCENT,
|
||||
);
|
||||
expect(resolvePreviewTypedZoom({ state, zoomPercent: 99999, ...viewport }).zoomPercent).toBe(
|
||||
MAX_PREVIEW_ZOOM_PERCENT,
|
||||
);
|
||||
});
|
||||
|
||||
it("clamps a pan that would leave the frame out of view", () => {
|
||||
const next = resolvePreviewTypedZoom({
|
||||
state: { zoomPercent: 400, panX: 1500, panY: 0 },
|
||||
zoomPercent: 200,
|
||||
...viewport,
|
||||
});
|
||||
expect(next.panX).toBe((1000 * 2 - 1000) / 2 + PREVIEW_PAN_OVERSCROLL_PX);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPreviewWheelZoomPercent", () => {
|
||||
it("zooms in on negative deltaY (scroll up / pinch out)", () => {
|
||||
expect(getPreviewWheelZoomPercent(-5, 100)).toBeGreaterThan(100);
|
||||
|
||||
@@ -5,7 +5,8 @@ export interface PreviewZoomState {
|
||||
}
|
||||
|
||||
export const MIN_PREVIEW_ZOOM_PERCENT = 25;
|
||||
export const MAX_PREVIEW_ZOOM_PERCENT = 400;
|
||||
export const MAX_PREVIEW_ZOOM_PERCENT = 1600;
|
||||
export const PREVIEW_PIXEL_GRID_MIN_ZOOM_PERCENT = 800;
|
||||
export const PREVIEW_PAN_SURFACE_SELECTOR = '[data-preview-pan-surface="true"]';
|
||||
export const PREVIEW_PAN_OVERSCROLL_PX = 48;
|
||||
export const DEFAULT_PREVIEW_ZOOM: PreviewZoomState = {
|
||||
@@ -32,6 +33,16 @@ export function clampPreviewZoomPercent(percent: number): number {
|
||||
return Math.min(MAX_PREVIEW_ZOOM_PERCENT, Math.max(MIN_PREVIEW_ZOOM_PERCENT, percent));
|
||||
}
|
||||
|
||||
export function parsePreviewZoomInput(text: string): number | null {
|
||||
const match = /^\s*(\d+(?:\.\d*)?|\.\d+)\s*%?\s*$/.exec(text);
|
||||
if (!match) return null;
|
||||
return clampPreviewZoomPercent(Number(match[1]));
|
||||
}
|
||||
|
||||
export function showsPreviewPixelGrid(zoomPercent: number): boolean {
|
||||
return zoomPercent > PREVIEW_PIXEL_GRID_MIN_ZOOM_PERCENT;
|
||||
}
|
||||
|
||||
export function canStartPreviewPan(button: number): boolean {
|
||||
return button === 1;
|
||||
}
|
||||
@@ -144,6 +155,29 @@ export function resolvePreviewWheelZoom(input: {
|
||||
};
|
||||
}
|
||||
|
||||
export function resolvePreviewTypedZoom(input: {
|
||||
state: PreviewZoomState;
|
||||
zoomPercent: number;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
contentWidth?: number;
|
||||
contentHeight?: number;
|
||||
}): PreviewZoomState {
|
||||
const oldZoom = clampPreviewZoomPercent(input.state.zoomPercent);
|
||||
const nextZoomPercent = clampPreviewZoomPercent(input.zoomPercent);
|
||||
const ratio = nextZoomPercent / oldZoom;
|
||||
const pan = clampPreviewPanForZoom(
|
||||
input.state.panX * ratio,
|
||||
input.state.panY * ratio,
|
||||
nextZoomPercent,
|
||||
input.viewportWidth,
|
||||
input.viewportHeight,
|
||||
input.contentWidth ?? input.viewportWidth,
|
||||
input.contentHeight ?? input.viewportHeight,
|
||||
);
|
||||
return { zoomPercent: nextZoomPercent, ...pan };
|
||||
}
|
||||
|
||||
export function resolvePreviewWheelPan(input: {
|
||||
state: PreviewZoomState;
|
||||
deltaX: number;
|
||||
@@ -169,6 +203,36 @@ export function resolvePreviewWheelPan(input: {
|
||||
};
|
||||
}
|
||||
|
||||
export interface PreviewPixelGridBox {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
height: number;
|
||||
cellPx: number;
|
||||
}
|
||||
|
||||
export function resolvePreviewPixelGrid(input: {
|
||||
state: PreviewZoomState;
|
||||
rect: { width: number; height: number };
|
||||
stage: { width: number; height: number };
|
||||
width: number;
|
||||
}): PreviewPixelGridBox | null {
|
||||
const zoomPercent = clampPreviewZoomPercent(input.state.zoomPercent);
|
||||
if (!showsPreviewPixelGrid(zoomPercent) || !(input.width > 0) || !(input.stage.width > 0)) {
|
||||
return null;
|
||||
}
|
||||
const scale = zoomPercent / 100;
|
||||
const width = input.stage.width * scale;
|
||||
const height = input.stage.height * scale;
|
||||
return {
|
||||
left: toDomPrecision((input.rect.width - width) / 2 + input.state.panX),
|
||||
top: toDomPrecision((input.rect.height - height) / 2 + input.state.panY),
|
||||
width: toDomPrecision(width),
|
||||
height: toDomPrecision(height),
|
||||
cellPx: toDomPrecision(width / input.width),
|
||||
};
|
||||
}
|
||||
|
||||
export interface PreviewVisibleRegion {
|
||||
left: number;
|
||||
top: number;
|
||||
|
||||
@@ -70,6 +70,8 @@
|
||||
--color-ring: var(--color-accent-ink);
|
||||
--color-selection: light-dark(oklch(0.5 0.13 240), #4ba3d2);
|
||||
--color-playhead: var(--color-text-0);
|
||||
/* Over the composition's own pixels, so mid-grey to read on light and dark content alike. */
|
||||
--color-pixel-grid: oklch(0.6 0 0 / 0.5);
|
||||
--color-danger: oklch(0.56 0.19 25);
|
||||
--color-danger-ink: light-dark(oklch(0.45 0.17 25), oklch(0.791 0.12 25));
|
||||
--color-warning-ink: light-dark(oklch(0.5 0.12 70), oklch(82.8% 0.189 84.429));
|
||||
|
||||
Reference in New Issue
Block a user