Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(desktop): zoom toward the click without magnifying past the pixels

Luis Ortiz3fc9333d9e05
diff --git a/AGENTS.md b/AGENTS.md
index 75bd6d2..9bef1f6 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -581,17 +581,28 @@ control the click landed on is the one that was there before. It is capped at 15
`null`, never an error β€” a slow or unresponsive foreign application costs a step its metadata, not
the recording.
-A desktop step shows its whole frame. `resolveViewport` zooms to `bounds` when a screenshot has
-them, padded by `PAD_RATIO` of the image but never more than `MAX_PAD_MULTIPLE` times the element β€”
-a cap that suits a browser page, where a control is a real fraction of the viewport, and fails on a
-screen grab, where a 30 px window button against 2560 px yields a crop a few hundred pixels wide that
-the card then stretches. So `screenshotForElement` takes `zoomToTarget` and the desktop sink passes
-false, which writes no `bounds` at all; `resolveViewport` falls back to the full image and
-`edits.target` still marks where the click landed. It is written into the data rather than branched
-on `client()`, so a desktop guide frames the same way in whatever opens it, exports included, and the
-extension keeps its zoom because `zoomToTarget` defaults to true. `check:pipeline` asserts the
-viewport equals the frame, and reverting the flag makes it report a 196 Γ— 196 crop of an 800 Γ— 600
-capture β€” the failure in its own numbers.
+A desktop step zooms toward the click, never into it. `screenshotForElement` takes a `zoom` mode:
+`element` writes `bounds` and is what the extension keeps, `click` writes an explicit
+`edits.viewport`, and `none` writes neither.
+
+The extension's rule cannot serve both. `resolveViewport` pads `bounds` by `PAD_RATIO` of the image
+but never beyond `MAX_PAD_MULTIPLE` times the element, which suits a page where a control is a real
+fraction of the viewport and collapses on a screen grab: a 30 px window button against 2560 px gives
+a crop a few hundred pixels wide, which the card then stretches. Magnification past 1:1 cannot look
+good, because the detail is not in the file.
+
+So `clickZoomViewport` sizes the region from the frame rather than the control β€” 55% of its width,
+floored at 1100 px and capped at the frame β€” centred on the click and clamped inside. A 2560 Γ— 1440
+capture yields 1408 Γ— 792, about 1.8Γ—, which still lands at or below the size it is displayed at; a
+1200 px window yields 1100, barely a zoom; anything at or under 1100 px yields the whole frame. Zoom
+appears only where there are spare pixels to spend on it.
+
+It goes in `edits.viewport` rather than `bounds` because `resolveViewport` returns that verbatim, so
+the region is exactly what was computed rather than what the padding rule makes of it, and
+`resolveFrameViewport` gives the video exporter the same starting frame before it eases toward
+`edits.target`. The mode is written into the data rather than branched on `client()`, so a desktop
+guide frames the same way in whatever opens it, exports included, and nothing shared changes for the
+extension. `check:pipeline` asserts the viewport sits inside the frame at between 1Γ— and 2Γ—.
`targetRect` decides what the dashed target in the screenshot encloses. The control's own rectangle
wins when there is one, which is the whole point of reading the accessibility tree; it falls back to
diff --git a/apps/desktop/src/renderer/capture-sink.ts b/apps/desktop/src/renderer/capture-sink.ts
index bf2dd5d..2e54d4a 100644
--- a/apps/desktop/src/renderer/capture-sink.ts
+++ b/apps/desktop/src/renderer/capture-sink.ts
@@ -48,7 +48,7 @@ export class DesktopCaptureSink implements CaptureSink {
height: data.image.height,
cursor: data.cursor ?? null,
targetColor,
- zoomToTarget: false,
+ zoom: 'click',
},
meta,
);
diff --git a/apps/desktop/src/renderer/check-pipeline.ts b/apps/desktop/src/renderer/check-pipeline.ts
index 6ca794b..d85ec0a 100644
--- a/apps/desktop/src/renderer/check-pipeline.ts
+++ b/apps/desktop/src/renderer/check-pipeline.ts
@@ -88,17 +88,22 @@ window.mimik.onRequest('mimik:check:verify', async (payload) => {
});
const viewport = shot ? resolveViewport(shot) : null;
+ const zoom = shot && viewport ? shot.width / viewport.width : 0;
results.push({
- name: 'a desktop step shows its whole frame',
+ name: 'a desktop step zooms without upscaling',
ok:
shot !== undefined &&
shot.bounds === undefined &&
viewport !== null &&
- viewport.width === shot.width &&
- viewport.height === shot.height &&
+ viewport.x >= 0 &&
+ viewport.y >= 0 &&
+ viewport.x + viewport.width <= shot.width &&
+ viewport.y + viewport.height <= shot.height &&
+ zoom >= 1 &&
+ zoom <= 2 &&
(shot.edits?.target?.width ?? 0) > 0,
detail: shot
- ? `viewport ${viewport?.width} Γ— ${viewport?.height} of ${shot.width} Γ— ${shot.height}, target still drawn`
+ ? `${zoom.toFixed(2)}x β€” viewport ${viewport?.width} Γ— ${viewport?.height} of ${shot.width} Γ— ${shot.height}, inside the frame`
: 'no screenshot',
});
diff --git a/packages/core/src/screenshot/__tests__/record.test.ts b/packages/core/src/screenshot/__tests__/record.test.ts
index c294975..4c3bb43 100644
--- a/packages/core/src/screenshot/__tests__/record.test.ts
+++ b/packages/core/src/screenshot/__tests__/record.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import type { ElementMeta } from '@/core/guides/types';
import { resolveViewport } from '@/core/screenshot/geometry';
-import { screenshotForElement } from '@/core/screenshot/record';
+import { clickZoomViewport, screenshotForElement } from '@/core/screenshot/record';
const meta: ElementMeta = {
textContent: null,
@@ -41,10 +41,37 @@ describe('screenshotForElement', () => {
expect(screenshotForElement({ ...bytes, cursor: null }, meta).edits?.cursor).toBeNull();
});
- it('drops bounds when the target should not be zoomed to, keeping the target itself', () => {
- const shot = screenshotForElement({ ...bytes, zoomToTarget: false }, meta);
+ it('zooms to the click instead of the element, keeping the target itself', () => {
+ const shot = screenshotForElement({ ...bytes, zoom: 'click' }, meta);
expect(shot.bounds).toBeUndefined();
expect(shot.edits?.target).toMatchObject({ x: 20, y: 40, width: 200, height: 80 });
+ expect(resolveViewport({ ...shot, blob: new Blob() })).toEqual(shot.edits?.viewport);
+ });
+
+ it('writes no viewport at all when zoom is off', () => {
+ const shot = screenshotForElement({ ...bytes, zoom: 'none' }, meta);
+ expect(shot.bounds).toBeUndefined();
+ expect(shot.edits?.viewport).toBeUndefined();
expect(resolveViewport({ ...shot, blob: new Blob() })).toEqual({ x: 0, y: 0, width: 800, height: 600 });
});
});
+
+describe('clickZoomViewport', () => {
+ it('zooms a large screen but never past what its pixels support', () => {
+ const big = clickZoomViewport(2560, 1440, { x: 1280, y: 720 });
+ expect(big.width).toBe(1408);
+ expect(big.height).toBe(792);
+ expect(2560 / big.width).toBeCloseTo(1.82, 2);
+ });
+
+ it('barely zooms a window-sized frame and not at all a small one', () => {
+ expect(clickZoomViewport(1200, 800, { x: 600, y: 400 }).width).toBe(1100);
+ expect(clickZoomViewport(800, 600, { x: 400, y: 300 })).toEqual({ x: 0, y: 0, width: 800, height: 600 });
+ });
+
+ it('centres on the click and stays inside the frame', () => {
+ expect(clickZoomViewport(2560, 1440, { x: 1600, y: 900 })).toMatchObject({ x: 896, y: 504 });
+ expect(clickZoomViewport(2560, 1440, { x: 10, y: 10 })).toMatchObject({ x: 0, y: 0 });
+ expect(clickZoomViewport(2560, 1440, { x: 2550, y: 1430 })).toMatchObject({ x: 1152, y: 648 });
+ });
+});
diff --git a/packages/core/src/screenshot/record.ts b/packages/core/src/screenshot/record.ts
index 608baf9..cebb0af 100644
--- a/packages/core/src/screenshot/record.ts
+++ b/packages/core/src/screenshot/record.ts
@@ -1,6 +1,11 @@
-import type { ElementMeta, StoredScreenshot } from '@/core/guides/types';
+import type { ElementMeta, ScreenshotBounds, StoredScreenshot } from '@/core/guides/types';
import { type CursorMark, DEFAULT_TARGET_COLOR } from '@/core/screenshot/types';
+export type ZoomMode = 'element' | 'click' | 'none';
+
+const CLICK_ZOOM_FRACTION = 0.55;
+const MIN_CLICK_ZOOM_WIDTH = 1100;
+
export interface ScreenshotBytes {
id: string;
stepId: string;
@@ -11,21 +16,40 @@ export interface ScreenshotBytes {
src?: string;
cursor?: CursorMark | null;
targetColor?: string;
- zoomToTarget?: boolean;
+ zoom?: ZoomMode;
+}
+
+function clamp(value: number, min: number, max: number): number {
+ return Math.max(min, Math.min(max, value));
+}
+
+export function clickZoomViewport(width: number, height: number, click: { x: number; y: number }): ScreenshotBounds {
+ const visWidth = Math.min(width, Math.max(width * CLICK_ZOOM_FRACTION, MIN_CLICK_ZOOM_WIDTH));
+ const visHeight = Math.min(height, (visWidth * height) / width);
+ return {
+ x: clamp(click.x - visWidth / 2, 0, width - visWidth),
+ y: clamp(click.y - visHeight / 2, 0, height - visHeight),
+ width: visWidth,
+ height: visHeight,
+ };
}
export function screenshotForElement(bytes: ScreenshotBytes, meta: ElementMeta): StoredScreenshot {
- const { cursor, targetColor, zoomToTarget = true, ...rest } = bytes;
+ const { cursor, targetColor, zoom = 'element', ...rest } = bytes;
const ratio = meta.devicePixelRatio;
+ const click = meta.clickPoint ?? { x: meta.rect.x + meta.rect.width / 2, y: meta.rect.y + meta.rect.height / 2 };
return {
...rest,
- ...(zoomToTarget
+ ...(zoom === 'element'
? { bounds: { x: meta.rect.x, y: meta.rect.y, width: meta.rect.width, height: meta.rect.height } }
: {}),
pixelRatio: ratio,
clickPoint: meta.clickPoint,
edits: {
...(cursor === undefined ? {} : { cursor }),
+ ...(zoom === 'click'
+ ? { viewport: clickZoomViewport(rest.width, rest.height, { x: click.x * ratio, y: click.y * ratio }) }
+ : {}),
target: {
x: meta.rect.x * ratio,
y: meta.rect.y * ratio,