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: zoomed screenshot rendering with element-centered crop

Luis Ortizb034f88e6433
diff --git a/src/background/screenshot.ts b/src/background/screenshot.ts
index b6fc520..5f31886 100644
--- a/src/background/screenshot.ts
+++ b/src/background/screenshot.ts
@@ -2,7 +2,7 @@ import { db } from '../shared/db-schema';
import type { Screenshot, ElementMeta } from '../shared/types';
export async function captureAndStore(tabId: number, stepId: string): Promise<Screenshot> {
- const dataUrl = await chrome.tabs.captureVisibleTab(null, { format: 'jpeg', quality: 85 });
+ const dataUrl = await chrome.tabs.captureVisibleTab(null, { format: 'jpeg', quality: 90 });
const blob = await fetch(dataUrl).then(r => r.blob());
const screenshot: Screenshot = {
id: crypto.randomUUID(), stepId, blob, mimeType: 'image/jpeg', width: 0, height: 0,
@@ -22,43 +22,29 @@ export async function captureAnnotated(
): Promise<Screenshot> {
const dataUrl = await chrome.tabs.captureVisibleTab(null, {
format: 'jpeg',
- quality: 85,
+ quality: 90,
});
- const annotatedBlob = await drawHighlight(dataUrl, elementMeta);
- const img = await createImageBitmap(annotatedBlob);
+ const blob = await fetch(dataUrl).then(r => r.blob());
+ const img = await createImageBitmap(blob);
const screenshot: Screenshot = {
id: crypto.randomUUID(),
stepId,
- blob: annotatedBlob,
+ blob,
mimeType: 'image/jpeg',
width: img.width,
height: img.height,
+ bounds: {
+ x: elementMeta.rect.x,
+ y: elementMeta.rect.y,
+ width: elementMeta.rect.width,
+ height: elementMeta.rect.height,
+ },
+ pixelRatio: elementMeta.devicePixelRatio,
};
+ img.close();
await db.screenshots.add(screenshot);
return screenshot;
}
-
-export async function drawHighlight(dataUrl: string, meta: ElementMeta): Promise<Blob> {
- const img = await createImageBitmap(await fetch(dataUrl).then(r => r.blob()));
-
- const canvas = new OffscreenCanvas(img.width, img.height);
- const ctx = canvas.getContext('2d')!;
- ctx.drawImage(img, 0, 0);
-
- const dpr = meta.devicePixelRatio || 1;
- const x = meta.rect.x * dpr;
- const y = meta.rect.y * dpr;
- const w = meta.rect.width * dpr;
- const h = meta.rect.height * dpr;
-
- ctx.strokeStyle = '#2563EB';
- ctx.lineWidth = 3 * dpr;
- ctx.fillStyle = 'rgba(37, 99, 235, 0.15)';
- ctx.fillRect(x, y, w, h);
- ctx.strokeRect(x, y, w, h);
-
- return canvas.convertToBlob({ type: 'image/jpeg', quality: 0.85 });
-}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index cab6a8d..16edbc4 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -17,6 +17,13 @@ export interface Step {
screenshotId?: string;
}
+export interface ScreenshotBounds {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+}
+
export interface Screenshot {
id: string;
stepId: string;
@@ -24,6 +31,8 @@ export interface Screenshot {
mimeType: string;
width: number;
height: number;
+ bounds?: ScreenshotBounds;
+ pixelRatio?: number;
}
export interface Settings {
diff --git a/src/sidepanel/StepCard.tsx b/src/sidepanel/StepCard.tsx
index 07cd27e..2ad3629 100644
--- a/src/sidepanel/StepCard.tsx
+++ b/src/sidepanel/StepCard.tsx
@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react';
import { Trash2, GripVertical, EyeOff, Copy, Check } from 'lucide-react';
import type { Step, Screenshot } from '../shared/types';
+import ZoomScreenshot from './ZoomScreenshot';
interface DragHandleProps {
onDragStart: (e: React.DragEvent) => void;
@@ -103,11 +104,11 @@ export default function StepCard({
{step.index + 1}
</span>
<div className="flex-1 min-w-0">
- {objectUrl ? (
- <img
- src={objectUrl}
+ {screenshot ? (
+ <ZoomScreenshot
+ screenshot={screenshot}
alt={`Step ${step.index + 1} screenshot`}
- className="w-full rounded mb-2 border border-gray-100"
+ className="mb-2"
/>
) : (
<div className="w-full h-32 bg-gray-100 rounded flex items-center justify-center text-gray-400 text-sm mb-2">
diff --git a/src/sidepanel/ZoomScreenshot.tsx b/src/sidepanel/ZoomScreenshot.tsx
new file mode 100644
index 0000000..4262294
--- /dev/null
+++ b/src/sidepanel/ZoomScreenshot.tsx
@@ -0,0 +1,133 @@
+import { useState, useEffect, useRef } from 'react';
+import type { Screenshot } from '../shared/types';
+
+interface ZoomScreenshotProps {
+ screenshot: Screenshot;
+ className?: string;
+ alt?: string;
+}
+
+function clamp(val: number, min: number, max: number): number {
+ return Math.max(min, Math.min(max, val));
+}
+
+function drawRoundedRect(
+ ctx: OffscreenCanvasRenderingContext2D,
+ x: number, y: number, w: number, h: number, r: number
+) {
+ ctx.beginPath();
+ ctx.moveTo(x + r, y);
+ ctx.lineTo(x + w - r, y);
+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);
+ ctx.lineTo(x + w, y + h - r);
+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
+ ctx.lineTo(x + r, y + h);
+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);
+ ctx.lineTo(x, y + r);
+ ctx.quadraticCurveTo(x, y, x + r, y);
+ ctx.closePath();
+ ctx.stroke();
+}
+
+export default function ZoomScreenshot({ screenshot, className = '', alt = '' }: ZoomScreenshotProps) {
+ const [croppedUrl, setCroppedUrl] = useState<string | null>(null);
+ const prevUrlRef = useRef<string | null>(null);
+
+ useEffect(() => {
+ if (!screenshot.blob) return;
+
+ let cancelled = false;
+
+ (async () => {
+ const img = await createImageBitmap(screenshot.blob);
+ if (cancelled) { img.close(); return; }
+
+ const imgW = img.width;
+ const imgH = img.height;
+ const bounds = screenshot.bounds;
+ const dpr = screenshot.pixelRatio || 1;
+
+ const canvas = new OffscreenCanvas(imgW, imgH);
+ const ctx = canvas.getContext('2d')!;
+
+ if (bounds) {
+ const bx = bounds.x * dpr;
+ const by = bounds.y * dpr;
+ const bw = bounds.width * dpr;
+ const bh = bounds.height * dpr;
+
+ const PAD_RATIO = 0.3;
+ const padH = PAD_RATIO * imgW; // horizontal padding = 30% of image width
+ const padV = PAD_RATIO * imgH; // vertical padding = 30% of image height
+ const imgAspect = imgW / imgH;
+ const elAspect = bw / bh;
+
+ const cx = bx + bw / 2;
+ const cy = by + bh / 2;
+
+ let visW = bw + padH;
+ let visH = bh + padV;
+
+ if (elAspect > 1) {
+ visW = bw + padH;
+ visH = visW / imgAspect;
+ } else if (elAspect < 1) {
+ visH = bh + padV;
+ visW = visH * imgAspect;
+ }
+
+ visW = Math.min(visW, imgW);
+ visH = Math.min(visH, imgH);
+
+ const cropX = clamp(cx - visW / 2, 0, imgW - visW);
+ const cropY = clamp(cy - visH / 2, 0, imgH - visH);
+
+ ctx.drawImage(img, cropX, cropY, visW, visH, 0, 0, imgW, imgH);
+
+ const scaleX = imgW / visW;
+ const scaleY = imgH / visH;
+ ctx.strokeStyle = '#2563EB';
+ ctx.lineWidth = 10;
+ drawRoundedRect(
+ ctx,
+ (bx - cropX) * scaleX,
+ (by - cropY) * scaleY,
+ bw * scaleX,
+ bh * scaleY,
+ 20
+ );
+ } else {
+ ctx.drawImage(img, 0, 0, imgW, imgH);
+ }
+
+ img.close();
+ const blob = await canvas.convertToBlob({ type: 'image/webp', quality: 0.8 });
+ if (cancelled) return;
+
+ const url = URL.createObjectURL(blob);
+ if (prevUrlRef.current) URL.revokeObjectURL(prevUrlRef.current);
+ prevUrlRef.current = url;
+ setCroppedUrl(url);
+ })();
+
+ return () => { cancelled = true; };
+ }, [screenshot.blob, screenshot.bounds, screenshot.pixelRatio]);
+
+ useEffect(() => {
+ return () => {
+ if (prevUrlRef.current) URL.revokeObjectURL(prevUrlRef.current);
+ };
+ }, []);
+
+ if (!croppedUrl) {
+ return <div className={`bg-gray-100 rounded-lg animate-pulse h-32 ${className}`} />;
+ }
+
+ return (
+ <img
+ src={croppedUrl}
+ alt={alt}
+ className={`w-full rounded-lg border border-gray-200 ${className}`}
+ />
+ );
+}