π¬ 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): add zoom, pan and crop viewport transforms
Luis Ortiz86c5c7ba0d42
diff --git a/src/core/screenshot/__tests__/geometry.test.ts b/src/core/screenshot/__tests__/geometry.test.tsindex a0ad652..f02f540 100644--- a/src/core/screenshot/__tests__/geometry.test.ts+++ b/src/core/screenshot/__tests__/geometry.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';import type { Screenshot } from '@/core/guides/types';-import { resolveViewport } from '@/core/screenshot/geometry';+import { cropTo, panBy, resolveViewport, zoomBy } from '@/core/screenshot/geometry';function makeScreenshot(overrides: Partial<Screenshot> = {}): Screenshot {return {@@ -50,3 +50,72 @@ describe('resolveViewport', () => {expect(v.height).toBeLessThanOrEqual(800);});});++const IMG = { width: 1000, height: 800 };+const FULL = { x: 0, y: 0, width: 1000, height: 800 };++describe('zoomBy', () => {+ it('shrinks the viewport around its centre when zooming in', () => {+ const v = zoomBy(FULL, 2, IMG);+ expect(v.width).toBe(500);+ expect(v.height).toBe(400);+ expect(v.x + v.width / 2).toBeCloseTo(500, 5);+ expect(v.y + v.height / 2).toBeCloseTo(400, 5);+ });++ it('never grows past the image', () => {+ expect(zoomBy(FULL, 0.5, IMG)).toEqual(FULL);+ });++ it('stays inside the image when zooming out from a corner', () => {+ const v = zoomBy({ x: 0, y: 0, width: 200, height: 160 }, 0.5, IMG);+ expect(v.x).toBeGreaterThanOrEqual(0);+ expect(v.x + v.width).toBeLessThanOrEqual(1000);+ });++ it('clamps to a minimum viewport size', () => {+ let v = FULL;+ for (let i = 0; i < 20; i++) v = zoomBy(v, 2, IMG);+ expect(v.width).toBeGreaterThan(0);+ expect(v.height).toBeGreaterThan(0);+ });+});++describe('panBy', () => {+ it('translates the viewport', () => {+ const v = panBy({ x: 100, y: 100, width: 500, height: 400 }, 50, -20, IMG);+ expect(v).toEqual({ x: 150, y: 80, width: 500, height: 400 });+ });++ it('clamps at the image edges instead of scrolling past them', () => {+ const v = panBy({ x: 0, y: 0, width: 500, height: 400 }, -100, -100, IMG);+ expect(v.x).toBe(0);+ expect(v.y).toBe(0);+ });++ it('clamps at the far edges', () => {+ const v = panBy({ x: 500, y: 400, width: 500, height: 400 }, 100, 100, IMG);+ expect(v.x).toBe(500);+ expect(v.y).toBe(400);+ });+});++describe('cropTo', () => {+ it('accepts a free aspect ratio', () => {+ expect(cropTo({ x: 10, y: 20, width: 100, height: 900 }, IMG)).toEqual({+ x: 10,+ y: 20,+ width: 100,+ height: 780,+ });+ });++ it('normalises a rect dragged up and to the left', () => {+ expect(cropTo({ x: 300, y: 200, width: -100, height: -50 }, IMG)).toEqual({+ x: 200,+ y: 150,+ width: 100,+ height: 50,+ });+ });+});diff --git a/src/core/screenshot/geometry.ts b/src/core/screenshot/geometry.tsindex 85165ca..9354e03 100644--- a/src/core/screenshot/geometry.ts+++ b/src/core/screenshot/geometry.ts@@ -43,3 +43,48 @@ export function resolveViewport(screenshot: Screenshot): ScreenshotBounds {height: visH,};}++const MIN_VIEWPORT = 40;++interface ImageSize {+ width: number;+ height: number;+}++function fitRect(rect: ScreenshotBounds, img: ImageSize): ScreenshotBounds {+ const width = Math.min(rect.width, img.width);+ const height = Math.min(rect.height, img.height);+ return {+ width,+ height,+ x: clamp(rect.x, 0, img.width - width),+ y: clamp(rect.y, 0, img.height - height),+ };+}++export function zoomBy(viewport: ScreenshotBounds, factor: number, img: ImageSize): ScreenshotBounds {+ const cx = viewport.x + viewport.width / 2;+ const cy = viewport.y + viewport.height / 2;+ const width = clamp(viewport.width / factor, MIN_VIEWPORT, img.width);+ const height = clamp(viewport.height / factor, MIN_VIEWPORT, img.height);+ return fitRect({ x: cx - width / 2, y: cy - height / 2, width, height }, img);+}++export function panBy(viewport: ScreenshotBounds, dx: number, dy: number, img: ImageSize): ScreenshotBounds {+ return fitRect({ ...viewport, x: viewport.x + dx, y: viewport.y + dy }, img);+}++export function cropTo(rect: ScreenshotBounds, img: ImageSize): ScreenshotBounds {+ const x0 = rect.width < 0 ? rect.x + rect.width : rect.x;+ const y0 = rect.height < 0 ? rect.y + rect.height : rect.y;+ const width0 = Math.abs(rect.width);+ const height0 = Math.abs(rect.height);+ const x = clamp(x0, 0, img.width);+ const y = clamp(y0, 0, img.height);+ return {+ x,+ y,+ width: clamp(width0, 0, img.width - x),+ height: clamp(height0, 0, img.height - y),+ };+}