π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
feat(screenshot): resolve render viewport from bounds or explicit edits
Luis Ortiz7250f980e9c4
diff --git a/src/core/screenshot/__tests__/geometry.test.ts b/src/core/screenshot/__tests__/geometry.test.tsnew file mode 100644index 0000000..a0ad652--- /dev/null+++ b/src/core/screenshot/__tests__/geometry.test.ts@@ -0,0 +1,52 @@+import { describe, expect, it } from 'vitest';+import type { Screenshot } from '@/core/guides/types';+import { resolveViewport } from '@/core/screenshot/geometry';++function makeScreenshot(overrides: Partial<Screenshot> = {}): Screenshot {+ return {+ id: 'ss-1',+ stepId: 'step-1',+ blob: new Blob(['x']),+ mimeType: 'image/png',+ width: 1000,+ height: 800,+ ...overrides,+ };+}++describe('resolveViewport', () => {+ it('returns the whole image when there are no bounds and no edits', () => {+ expect(resolveViewport(makeScreenshot())).toEqual({ x: 0, y: 0, width: 1000, height: 800 });+ });++ it('prefers an explicit viewport over the bounds default', () => {+ const s = makeScreenshot({+ bounds: { x: 10, y: 10, width: 50, height: 50 },+ edits: { viewport: { x: 100, y: 200, width: 300, height: 240 } },+ });+ expect(resolveViewport(s)).toEqual({ x: 100, y: 200, width: 300, height: 240 });+ });++ it('pads around bounds and keeps the image aspect ratio', () => {+ const s = makeScreenshot({ bounds: { x: 400, y: 300, width: 200, height: 100 }, pixelRatio: 1 });+ const v = resolveViewport(s);+ expect(v.width / v.height).toBeCloseTo(1000 / 800, 5);+ expect(v.width).toBeGreaterThan(200);+ });++ it('clamps the viewport inside the image', () => {+ const s = makeScreenshot({ bounds: { x: 0, y: 0, width: 40, height: 40 }, pixelRatio: 1 });+ const v = resolveViewport(s);+ expect(v.x).toBeGreaterThanOrEqual(0);+ expect(v.y).toBeGreaterThanOrEqual(0);+ expect(v.x + v.width).toBeLessThanOrEqual(1000);+ expect(v.y + v.height).toBeLessThanOrEqual(800);+ });++ it('never exceeds the image size', () => {+ const s = makeScreenshot({ bounds: { x: 0, y: 0, width: 990, height: 790 }, pixelRatio: 1 });+ const v = resolveViewport(s);+ expect(v.width).toBeLessThanOrEqual(1000);+ expect(v.height).toBeLessThanOrEqual(800);+ });+});diff --git a/src/core/screenshot/geometry.ts b/src/core/screenshot/geometry.tsnew file mode 100644index 0000000..85165ca--- /dev/null+++ b/src/core/screenshot/geometry.ts@@ -0,0 +1,45 @@+import type { Screenshot, ScreenshotBounds } from '@/core/guides/types';++const PAD_RATIO = 0.3;++export function clamp(val: number, min: number, max: number): number {+ return Math.max(min, Math.min(max, val));+}++export function resolveViewport(screenshot: Screenshot): ScreenshotBounds {+ const imgW = screenshot.width;+ const imgH = screenshot.height;+ const explicit = screenshot.edits?.viewport;+ if (explicit) return explicit;++ const bounds = screenshot.bounds;+ if (!bounds) return { x: 0, y: 0, width: imgW, height: imgH };++ const dpr = screenshot.pixelRatio || 1;+ const bx = bounds.x * dpr;+ const by = bounds.y * dpr;+ const bw = bounds.width * dpr;+ const bh = bounds.height * dpr;++ const imgAspect = imgW / imgH;+ const elAspect = bw / bh;++ let visW = bw + PAD_RATIO * imgW;+ let visH = bh + PAD_RATIO * imgH;++ if (elAspect > 1) {+ visH = visW / imgAspect;+ } else if (elAspect < 1) {+ visW = visH * imgAspect;+ }++ visW = Math.min(visW, imgW);+ visH = Math.min(visH, imgH);++ return {+ x: clamp(bx + bw / 2 - visW / 2, 0, imgW - visW),+ y: clamp(by + bh / 2 - visH / 2, 0, imgH - visH),+ width: visW,+ height: visH,+ };+}