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 πŸ“–

fix(editor): draw the editor preview with the shared renderer

Luis Ortiz5340c9a1a2a2
diff --git a/src/core/screenshot/draw.ts b/src/core/screenshot/draw.ts
new file mode 100644
index 0000000..f3ac945
--- /dev/null
+++ b/src/core/screenshot/draw.ts
@@ -0,0 +1,147 @@
+import type { Annotation, ArrowEnd } from './types';
+import { FONT_FAMILIES, FONT_SIZES, LINE_HEIGHTS, LINE_WIDTHS } from './types';
+
+export type Ctx = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
+
+export const TARGET_STROKE = 3.5;
+export const TARGET_RADIUS = 12;
+
+export function drawRoundedRect(ctx: Ctx, x: number, y: number, w: number, h: number, r: number) {
+ ctx.beginPath();
+ ctx.moveTo(x + r, y);
+ ctx.arcTo(x + w, y, x + w, y + h, r);
+ ctx.arcTo(x + w, y + h, x, y + h, r);
+ ctx.arcTo(x, y + h, x, y, r);
+ ctx.arcTo(x, y, x + w, y, r);
+ ctx.closePath();
+}
+
+function drawArrowEnd(ctx: Ctx, x1: number, y1: number, x2: number, y2: number, width: number, end: ArrowEnd) {
+ if (end === 'none') return;
+ const angle = Math.atan2(y2 - y1, x2 - x1);
+ const size = width * 4;
+ ctx.save();
+ ctx.translate(x2, y2);
+ ctx.rotate(angle);
+ ctx.beginPath();
+ switch (end) {
+ case 'bar':
+ ctx.moveTo(0, -size / 2);
+ ctx.lineTo(0, size / 2);
+ ctx.stroke();
+ break;
+ case 'arrow':
+ ctx.moveTo(-size, -size / 2);
+ ctx.lineTo(0, 0);
+ ctx.lineTo(-size, size / 2);
+ ctx.stroke();
+ break;
+ case 'arrow-solid':
+ ctx.moveTo(0, 0);
+ ctx.lineTo(-size, -size / 2);
+ ctx.lineTo(-size, size / 2);
+ ctx.closePath();
+ ctx.fill();
+ break;
+ case 'circle':
+ ctx.arc(0, 0, size / 2, 0, Math.PI * 2);
+ ctx.stroke();
+ break;
+ case 'circle-solid':
+ ctx.arc(0, 0, size / 2, 0, Math.PI * 2);
+ ctx.fill();
+ break;
+ case 'square':
+ ctx.rect(-size / 2, -size / 2, size, size);
+ ctx.stroke();
+ break;
+ case 'square-solid':
+ ctx.rect(-size / 2, -size / 2, size, size);
+ ctx.fill();
+ break;
+ }
+ ctx.restore();
+}
+
+export function drawAnnotation(ctx: Ctx, a: Annotation, originX: number, originY: number) {
+ ctx.save();
+ switch (a.type) {
+ case 'box':
+ ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'ms'];
+ if (a.fill && a.fill !== 'transparent') {
+ ctx.fillStyle = a.fill;
+ drawRoundedRect(ctx, a.x, a.y, a.w, a.h, a.radius ?? 0);
+ ctx.fill();
+ }
+ if (ctx.lineWidth > 0) {
+ ctx.strokeStyle = a.color;
+ drawRoundedRect(ctx, a.x, a.y, a.w, a.h, a.radius ?? 0);
+ ctx.stroke();
+ }
+ break;
+ case 'ellipse':
+ ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'ms'];
+ ctx.beginPath();
+ ctx.ellipse(a.x + a.w / 2, a.y + a.h / 2, Math.abs(a.w / 2), Math.abs(a.h / 2), 0, 0, Math.PI * 2);
+ if (a.fill && a.fill !== 'transparent') {
+ ctx.fillStyle = a.fill;
+ ctx.fill();
+ }
+ ctx.strokeStyle = a.color;
+ ctx.stroke();
+ break;
+ case 'target':
+ ctx.strokeStyle = a.color;
+ ctx.lineWidth = TARGET_STROKE;
+ if (a.border === 'dashed') ctx.setLineDash([8, 5]);
+ drawRoundedRect(ctx, a.x, a.y, a.w, a.h, TARGET_RADIUS);
+ ctx.stroke();
+ break;
+ case 'arrow': {
+ const w = LINE_WIDTHS[a.lineWidth ?? 'ms'];
+ ctx.strokeStyle = a.color;
+ ctx.fillStyle = a.color;
+ ctx.lineWidth = w;
+ ctx.lineCap = 'round';
+ ctx.beginPath();
+ ctx.moveTo(a.x1, a.y1);
+ ctx.lineTo(a.x2, a.y2);
+ ctx.stroke();
+ drawArrowEnd(ctx, a.x1, a.y1, a.x2, a.y2, w, a.end ?? 'arrow-solid');
+ break;
+ }
+ case 'text': {
+ const px = a.fontSize ? FONT_SIZES[a.fontSize] : a.size;
+ const family = FONT_FAMILIES[a.fontFamily ?? 'sans-serif'];
+ const style = a.fontStyle === 'italic' ? 'italic ' : '';
+ const weight = a.fontStyle === 'bold' ? 700 : 500;
+ ctx.fillStyle = a.color;
+ ctx.font = `${style}${weight} ${px}px ${family}`;
+ const lh = px * LINE_HEIGHTS[a.lineHeight ?? 'md'];
+ a.text.split('\n').forEach((line, i) => {
+ ctx.fillText(line, a.x, a.y + i * lh);
+ });
+ break;
+ }
+ case 'freehand':
+ ctx.strokeStyle = a.color;
+ ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'md'];
+ ctx.lineJoin = 'round';
+ ctx.lineCap = 'round';
+ ctx.beginPath();
+ ctx.moveTo(a.points[0], a.points[1]);
+ for (let i = 2; i < a.points.length; i += 2) ctx.lineTo(a.points[i], a.points[i + 1]);
+ ctx.stroke();
+ break;
+ case 'redact':
+ if (a.style === 'solid') {
+ ctx.fillStyle = '#1E1B4B';
+ ctx.fillRect(a.x, a.y, a.w, a.h);
+ } else {
+ ctx.filter = 'blur(12px)';
+ ctx.drawImage(ctx.canvas, a.x - originX, a.y - originY, a.w, a.h, a.x, a.y, a.w, a.h);
+ }
+ break;
+ }
+ ctx.restore();
+}
diff --git a/src/core/screenshot/render.ts b/src/core/screenshot/render.ts
index e77f5d4..85f3600 100644
--- a/src/core/screenshot/render.ts
+++ b/src/core/screenshot/render.ts
@@ -1,171 +1,12 @@
import type { Screenshot } from '@/core/guides/types';
+import { drawAnnotation } from './draw';
import { resolveTarget, resolveViewport } from './geometry';
-import type { Annotation, ArrowEnd } from './types';
-import { FONT_FAMILIES, FONT_SIZES, LINE_HEIGHTS, LINE_WIDTHS } from './types';
-
-const TARGET_STROKE = 3.5;
-const TARGET_RADIUS = 12;
interface RenderOptions {
format?: 'image/webp' | 'image/jpeg' | 'image/png';
quality?: number;
}
-function drawRoundedRect(
- ctx: OffscreenCanvasRenderingContext2D,
- x: number,
- y: number,
- w: number,
- h: number,
- r: number,
-) {
- ctx.beginPath();
- ctx.moveTo(x + r, y);
- ctx.arcTo(x + w, y, x + w, y + h, r);
- ctx.arcTo(x + w, y + h, x, y + h, r);
- ctx.arcTo(x, y + h, x, y, r);
- ctx.arcTo(x, y, x + w, y, r);
- ctx.closePath();
-}
-
-function drawArrowEnd(
- ctx: OffscreenCanvasRenderingContext2D,
- x1: number,
- y1: number,
- x2: number,
- y2: number,
- width: number,
- end: ArrowEnd,
-) {
- if (end === 'none') return;
- const angle = Math.atan2(y2 - y1, x2 - x1);
- const size = width * 4;
- ctx.save();
- ctx.translate(x2, y2);
- ctx.rotate(angle);
- ctx.beginPath();
- switch (end) {
- case 'bar':
- ctx.moveTo(0, -size / 2);
- ctx.lineTo(0, size / 2);
- ctx.stroke();
- break;
- case 'arrow':
- ctx.moveTo(-size, -size / 2);
- ctx.lineTo(0, 0);
- ctx.lineTo(-size, size / 2);
- ctx.stroke();
- break;
- case 'arrow-solid':
- ctx.moveTo(0, 0);
- ctx.lineTo(-size, -size / 2);
- ctx.lineTo(-size, size / 2);
- ctx.closePath();
- ctx.fill();
- break;
- case 'circle':
- ctx.arc(0, 0, size / 2, 0, Math.PI * 2);
- ctx.stroke();
- break;
- case 'circle-solid':
- ctx.arc(0, 0, size / 2, 0, Math.PI * 2);
- ctx.fill();
- break;
- case 'square':
- ctx.rect(-size / 2, -size / 2, size, size);
- ctx.stroke();
- break;
- case 'square-solid':
- ctx.rect(-size / 2, -size / 2, size, size);
- ctx.fill();
- break;
- }
- ctx.restore();
-}
-
-function drawAnnotation(ctx: OffscreenCanvasRenderingContext2D, a: Annotation, originX: number, originY: number) {
- ctx.save();
- switch (a.type) {
- case 'box':
- ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'ms'];
- if (a.fill && a.fill !== 'transparent') {
- ctx.fillStyle = a.fill;
- drawRoundedRect(ctx, a.x, a.y, a.w, a.h, a.radius ?? 0);
- ctx.fill();
- }
- if (ctx.lineWidth > 0) {
- ctx.strokeStyle = a.color;
- drawRoundedRect(ctx, a.x, a.y, a.w, a.h, a.radius ?? 0);
- ctx.stroke();
- }
- break;
- case 'ellipse':
- ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'ms'];
- ctx.beginPath();
- ctx.ellipse(a.x + a.w / 2, a.y + a.h / 2, Math.abs(a.w / 2), Math.abs(a.h / 2), 0, 0, Math.PI * 2);
- if (a.fill && a.fill !== 'transparent') {
- ctx.fillStyle = a.fill;
- ctx.fill();
- }
- ctx.strokeStyle = a.color;
- ctx.stroke();
- break;
- case 'target':
- ctx.strokeStyle = a.color;
- ctx.lineWidth = TARGET_STROKE;
- if (a.border === 'dashed') ctx.setLineDash([8, 5]);
- drawRoundedRect(ctx, a.x, a.y, a.w, a.h, TARGET_RADIUS);
- ctx.stroke();
- break;
- case 'arrow': {
- const w = LINE_WIDTHS[a.lineWidth ?? 'ms'];
- ctx.strokeStyle = a.color;
- ctx.fillStyle = a.color;
- ctx.lineWidth = w;
- ctx.lineCap = 'round';
- ctx.beginPath();
- ctx.moveTo(a.x1, a.y1);
- ctx.lineTo(a.x2, a.y2);
- ctx.stroke();
- drawArrowEnd(ctx, a.x1, a.y1, a.x2, a.y2, w, a.end ?? 'arrow-solid');
- break;
- }
- case 'text': {
- const px = a.fontSize ? FONT_SIZES[a.fontSize] : a.size;
- const family = FONT_FAMILIES[a.fontFamily ?? 'sans-serif'];
- const style = a.fontStyle === 'italic' ? 'italic ' : '';
- const weight = a.fontStyle === 'bold' ? 700 : 500;
- ctx.fillStyle = a.color;
- ctx.font = `${style}${weight} ${px}px ${family}`;
- const lh = px * LINE_HEIGHTS[a.lineHeight ?? 'md'];
- a.text.split('\n').forEach((line, i) => {
- ctx.fillText(line, a.x, a.y + i * lh);
- });
- break;
- }
- case 'freehand':
- ctx.strokeStyle = a.color;
- ctx.lineWidth = LINE_WIDTHS[a.lineWidth ?? 'md'];
- ctx.lineJoin = 'round';
- ctx.lineCap = 'round';
- ctx.beginPath();
- ctx.moveTo(a.points[0], a.points[1]);
- for (let i = 2; i < a.points.length; i += 2) ctx.lineTo(a.points[i], a.points[i + 1]);
- ctx.stroke();
- break;
- case 'redact':
- if (a.style === 'solid') {
- ctx.fillStyle = '#1E1B4B';
- ctx.fillRect(a.x, a.y, a.w, a.h);
- } else {
- ctx.filter = 'blur(12px)';
- ctx.drawImage(ctx.canvas, a.x - originX, a.y - originY, a.w, a.h, a.x, a.y, a.w, a.h);
- }
- break;
- }
- ctx.restore();
-}
-
export async function renderScreenshot(screenshot: Screenshot, opts: RenderOptions = {}): Promise<Blob> {
const { format = 'image/webp', quality = 0.85 } = opts;
const viewport = resolveViewport(screenshot);
diff --git a/src/ui/shared/AnnotationEditor.tsx b/src/ui/shared/AnnotationEditor.tsx
index 1f7ace2..5403e91 100644
--- a/src/ui/shared/AnnotationEditor.tsx
+++ b/src/ui/shared/AnnotationEditor.tsx
@@ -23,6 +23,7 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react
import { i18n } from '#imports';
import { updateScreenshotEdits } from '@/core/guides/service';
import type { Screenshot, ScreenshotBounds } from '@/core/guides/types';
+import { drawAnnotation } from '@/core/screenshot/draw';
import {
annotationBounds,
cropTo,
@@ -148,69 +149,6 @@ function hitHandle(b: ScreenshotBounds, x: number, y: number, radius: number): H
return null;
}
-function drawShape(ctx: CanvasRenderingContext2D, a: Annotation, alpha: number) {
- ctx.save();
- ctx.globalAlpha = alpha;
- switch (a.type) {
- case 'box':
- ctx.strokeStyle = a.color;
- ctx.lineWidth = 3;
- ctx.strokeRect(a.x, a.y, a.w, a.h);
- break;
- case 'target':
- ctx.strokeStyle = a.color;
- ctx.lineWidth = 3.5;
- if (a.border === 'dashed') ctx.setLineDash([8, 5]);
- ctx.beginPath();
- ctx.roundRect(a.x, a.y, a.w, a.h, 12);
- ctx.stroke();
- break;
- case 'arrow': {
- const head = 14;
- const angle = Math.atan2(a.y2 - a.y1, a.x2 - a.x1);
- ctx.strokeStyle = a.color;
- ctx.fillStyle = a.color;
- ctx.lineWidth = 3;
- ctx.beginPath();
- ctx.moveTo(a.x1, a.y1);
- ctx.lineTo(a.x2, a.y2);
- ctx.stroke();
- ctx.beginPath();
- ctx.moveTo(a.x2, a.y2);
- ctx.lineTo(a.x2 - head * Math.cos(angle - Math.PI / 6), a.y2 - head * Math.sin(angle - Math.PI / 6));
- ctx.lineTo(a.x2 - head * Math.cos(angle + Math.PI / 6), a.y2 - head * Math.sin(angle + Math.PI / 6));
- ctx.closePath();
- ctx.fill();
- break;
- }
- case 'text':
- ctx.fillStyle = a.color;
- ctx.font = `600 ${a.size}px Poppins, sans-serif`;
- ctx.fillText(a.text, a.x, a.y);
- break;
- case 'freehand':
- ctx.strokeStyle = a.color;
- ctx.lineWidth = 4;
- ctx.lineJoin = 'round';
- ctx.lineCap = 'round';
- ctx.beginPath();
- ctx.moveTo(a.points[0], a.points[1]);
- for (let i = 2; i < a.points.length; i += 2) ctx.lineTo(a.points[i], a.points[i + 1]);
- ctx.stroke();
- break;
- case 'redact':
- if (a.style === 'solid') {
- ctx.fillStyle = '#1E1B4B';
- ctx.fillRect(a.x, a.y, a.w, a.h);
- } else {
- ctx.filter = 'blur(12px)';
- ctx.drawImage(ctx.canvas, a.x, a.y, a.w, a.h, a.x, a.y, a.w, a.h);
- }
- break;
- }
- ctx.restore();
-}
-
export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }: AnnotationEditorProps) {
const [activeTool, setActiveTool] = useState<EditorTool>('box');
const [annotations, setAnnotations] = useState<Annotation[]>(() => {
@@ -350,8 +288,13 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
- for (const a of annotations) drawShape(ctx, a, 1);
- if (draft) drawShape(ctx, draft, 0.7);
+ for (const a of annotations) drawAnnotation(ctx, a, 0, 0);
+ if (draft) {
+ ctx.save();
+ ctx.globalAlpha = 0.7;
+ drawAnnotation(ctx, draft, 0, 0);
+ ctx.restore();
+ }
const scale = getScale();
@@ -1015,66 +958,6 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
</select>
</label>
)}
- {activeTool === 'text' && (
- <>
- <label className="flex flex-col items-center gap-1 text-[10px] font-semibold text-muted-foreground">
- {i18n.t('annotationEditor.font')}
- <select
- value={fontFamily}
- onChange={(e) => setTextProp({ fontFamily: e.target.value as FontFamily })}
- className="h-7 rounded-lg border border-border bg-card px-1.5 text-[11px] text-foreground outline-none"
- >
- {FONT_FAMILY_ORDER.map((f) => (
- <option key={f} value={f}>
- {f}
- </option>
- ))}
- </select>
- </label>
- <label className="flex flex-col items-center gap-1 text-[10px] font-semibold text-muted-foreground">
- {i18n.t('annotationEditor.fontStyle')}
- <select
- value={fontStyle}
- onChange={(e) => setTextProp({ fontStyle: e.target.value as FontStyleName })}
- className="h-7 rounded-lg border border-border bg-card px-1.5 text-[11px] text-foreground outline-none"
- >
- {FONT_STYLE_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.style_${f}`)}
- </option>
- ))}
- </select>
- </label>
- <label className="flex flex-col items-center gap-1 text-[10px] font-semibold text-muted-foreground">
- {i18n.t('annotationEditor.fontSize')}
- <select
- value={fontSize}
- onChange={(e) => setTextProp({ fontSize: e.target.value as FontSizeName })}
- className="h-7 rounded-lg border border-border bg-card px-1.5 text-[11px] text-foreground outline-none"
- >
- {FONT_SIZE_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.size_${f}`)}
- </option>
- ))}
- </select>
- </label>
- <label className="flex flex-col items-center gap-1 text-[10px] font-semibold text-muted-foreground">
- {i18n.t('annotationEditor.lineHeight')}
- <select
- value={lineHeight}
- onChange={(e) => setTextProp({ lineHeight: e.target.value as LineHeightName })}
- className="h-7 rounded-lg border border-border bg-card px-1.5 text-[11px] text-foreground outline-none"
- >
- {LINE_HEIGHT_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.height_${f}`)}
- </option>
- ))}
- </select>
- </label>
- </>
- )}
</div>
)}