π¬ 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 annotation hit-testing and drag transforms
Luis Ortiz44b57ecceac2
diff --git a/src/core/screenshot/__tests__/geometry.test.ts b/src/core/screenshot/__tests__/geometry.test.tsindex f02f540..eead8f5 100644--- a/src/core/screenshot/__tests__/geometry.test.ts+++ b/src/core/screenshot/__tests__/geometry.test.ts@@ -1,6 +1,16 @@import { describe, expect, it } from 'vitest';import type { Screenshot } from '@/core/guides/types';-import { cropTo, panBy, resolveViewport, zoomBy } from '@/core/screenshot/geometry';+import {+ annotationBounds,+ cropTo,+ hitTest,+ moveAnnotation,+ panBy,+ resizeAnnotation,+ resolveViewport,+ zoomBy,+} from '@/core/screenshot/geometry';+import type { Annotation } from '@/core/screenshot/types';function makeScreenshot(overrides: Partial<Screenshot> = {}): Screenshot {return {@@ -119,3 +129,70 @@ describe('cropTo', () => {});});});++const box: Annotation = { id: 'a', type: 'box', x: 100, y: 100, w: 200, h: 100, color: '#4F46E5' };+const arrow: Annotation = { id: 'b', type: 'arrow', x1: 10, y1: 10, x2: 110, y2: 60, color: '#4F46E5' };+const stroke: Annotation = { id: 'c', type: 'freehand', points: [0, 0, 50, 80, 20, 40], color: '#4F46E5' };++describe('annotationBounds', () => {+ it('returns the rect for a box', () => {+ expect(annotationBounds(box)).toEqual({ x: 100, y: 100, width: 200, height: 100 });+ });++ it('returns the span of an arrow regardless of direction', () => {+ expect(annotationBounds({ ...arrow, x1: 110, y1: 60, x2: 10, y2: 10 })).toEqual({+ x: 10,+ y: 10,+ width: 100,+ height: 50,+ });+ });++ it('returns the bounding box of a freehand stroke', () => {+ expect(annotationBounds(stroke)).toEqual({ x: 0, y: 0, width: 50, height: 80 });+ });+});++describe('hitTest', () => {+ it('returns the topmost annotation under the point', () => {+ const under: Annotation = { ...box, id: 'under' };+ const over: Annotation = { ...box, id: 'over' };+ expect(hitTest([under, over], 150, 150)?.id).toBe('over');+ });++ it('returns null when nothing is under the point', () => {+ expect(hitTest([box], 5, 5)).toBeNull();+ });++ it('hits a thin arrow via its padded bounds', () => {+ expect(hitTest([arrow], 60, 35)?.id).toBe('b');+ });+});++describe('moveAnnotation', () => {+ it('translates a box', () => {+ expect(moveAnnotation(box, 10, -5)).toMatchObject({ x: 110, y: 95, w: 200, h: 100 });+ });++ it('translates both ends of an arrow', () => {+ expect(moveAnnotation(arrow, 5, 5)).toMatchObject({ x1: 15, y1: 15, x2: 115, y2: 65 });+ });++ it('translates every point of a freehand stroke', () => {+ expect(moveAnnotation(stroke, 10, 10)).toMatchObject({ points: [10, 10, 60, 90, 30, 50] });+ });+});++describe('resizeAnnotation', () => {+ it('resizes a box from the south-east handle', () => {+ expect(resizeAnnotation(box, 'se', 50, 20)).toMatchObject({ x: 100, y: 100, w: 250, h: 120 });+ });++ it('resizes a box from the north-west handle by moving its origin', () => {+ expect(resizeAnnotation(box, 'nw', 20, 10)).toMatchObject({ x: 120, y: 110, w: 180, h: 90 });+ });++ it('leaves a freehand stroke unchanged', () => {+ expect(resizeAnnotation(stroke, 'se', 10, 10)).toEqual(stroke);+ });+});diff --git a/src/core/screenshot/geometry.ts b/src/core/screenshot/geometry.tsindex 9354e03..cd49275 100644--- a/src/core/screenshot/geometry.ts+++ b/src/core/screenshot/geometry.ts@@ -1,4 +1,5 @@import type { Screenshot, ScreenshotBounds } from '@/core/guides/types';+import type { Annotation } from './types';const PAD_RATIO = 0.3;@@ -45,6 +46,7 @@ export function resolveViewport(screenshot: Screenshot): ScreenshotBounds {}const MIN_VIEWPORT = 40;+const MIN_ANNOTATION = 8;interface ImageSize {width: number;@@ -88,3 +90,67 @@ export function cropTo(rect: ScreenshotBounds, img: ImageSize): ScreenshotBoundsheight: clamp(height0, 0, img.height - y),};}++const HIT_PAD = 8;++export type Handle = 'nw' | 'ne' | 'sw' | 'se';++export function annotationBounds(a: Annotation): ScreenshotBounds {+ switch (a.type) {+ case 'box':+ case 'redact':+ return { x: a.x, y: a.y, width: a.w, height: a.h };+ case 'arrow':+ return {+ x: Math.min(a.x1, a.x2),+ y: Math.min(a.y1, a.y2),+ width: Math.abs(a.x2 - a.x1),+ height: Math.abs(a.y2 - a.y1),+ };+ case 'text':+ return { x: a.x, y: a.y - a.size, width: a.text.length * a.size * 0.6, height: a.size * 1.4 };+ case 'freehand': {+ const xs = a.points.filter((_, i) => i % 2 === 0);+ const ys = a.points.filter((_, i) => i % 2 === 1);+ const x = Math.min(...xs);+ const y = Math.min(...ys);+ return { x, y, width: Math.max(...xs) - x, height: Math.max(...ys) - y };+ }+ }+}++export function hitTest(annotations: Annotation[], x: number, y: number): Annotation | null {+ for (let i = annotations.length - 1; i >= 0; i--) {+ const b = annotationBounds(annotations[i]);+ if (x >= b.x - HIT_PAD && x <= b.x + b.width + HIT_PAD && y >= b.y - HIT_PAD && y <= b.y + b.height + HIT_PAD) {+ return annotations[i];+ }+ }+ return null;+}++export function moveAnnotation(a: Annotation, dx: number, dy: number): Annotation {+ switch (a.type) {+ case 'box':+ case 'redact':+ case 'text':+ return { ...a, x: a.x + dx, y: a.y + dy };+ case 'arrow':+ return { ...a, x1: a.x1 + dx, y1: a.y1 + dy, x2: a.x2 + dx, y2: a.y2 + dy };+ case 'freehand':+ return { ...a, points: a.points.map((p, i) => (i % 2 === 0 ? p + dx : p + dy)) };+ }+}++export function resizeAnnotation(a: Annotation, handle: Handle, dx: number, dy: number): Annotation {+ if (a.type !== 'box' && a.type !== 'redact') return a;+ const left = handle === 'nw' || handle === 'sw';+ const top = handle === 'nw' || handle === 'ne';+ return {+ ...a,+ x: left ? a.x + dx : a.x,+ y: top ? a.y + dy : a.y,+ w: Math.max(MIN_ANNOTATION, left ? a.w - dx : a.w + dx),+ h: Math.max(MIN_ANNOTATION, top ? a.h - dy : a.h + dy),+ };+}