Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor mask blur logic for screenshot editor

Richie McIlroy6dcb6f2dec1b
diff --git a/apps/desktop/src/routes/screenshot-editor/AnnotationConfig.tsx b/apps/desktop/src/routes/screenshot-editor/AnnotationConfig.tsx
index 754914fce..fe4ef91e6 100644
--- a/apps/desktop/src/routes/screenshot-editor/AnnotationConfig.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/AnnotationConfig.tsx
@@ -1,5 +1,5 @@
import { Popover } from "@kobalte/core/popover";
-import { createMemo, For, Show } from "solid-js";
+import { createMemo, Show } from "solid-js";
import { Portal } from "solid-js/web";
import { BACKGROUND_COLORS, hexToRgb, RgbInput, rgbToHex } from "./ColorPicker";
import { type Annotation, useScreenshotEditorContext } from "./context";
@@ -30,12 +30,13 @@ export function AnnotationConfig() {
<Show when={selected()}>
{(ann) => {
const type = ann().type;
+ const isMask = type === "mask";
const maskType = () => ann().maskType ?? "blur";
const maskLevel = () => ann().maskLevel ?? 16;
return (
<Portal>
<div class="fixed left-1/2 -translate-x-1/2 top-20 z-[100] flex items-center gap-3 p-2 rounded-xl border border-gray-3 bg-gray-1/90 backdrop-blur-md shadow-xl animate-in fade-in slide-in-from-top-2">
- <Show when={type !== "mask"}>
+ <Show when={!isMask}>
<div class="flex flex-col gap-1">
<span class="text-[10px] text-gray-11 font-medium uppercase tracking-wider">
{type === "text" ? "Color" : "Stroke"}
@@ -47,7 +48,7 @@ export function AnnotationConfig() {
</div>
</Show>
- <Show when={type !== "text" && type !== "mask"}>
+ <Show when={type !== "text" && !isMask}>
<div class="flex flex-col gap-1 w-24">
<span class="text-[10px] text-gray-11 font-medium uppercase tracking-wider flex justify-between">
Width <span>{ann().strokeWidth}px</span>
@@ -76,7 +77,7 @@ export function AnnotationConfig() {
</div>
</Show>
- <Show when={type !== "mask"}>
+ <Show when={!isMask}>
<div class="flex flex-col gap-1 w-24">
<span class="text-[10px] text-gray-11 font-medium uppercase tracking-wider flex justify-between">
Opacity <span>{Math.round(ann().opacity * 100)}%</span>
diff --git a/apps/desktop/src/routes/screenshot-editor/ExportDialog.tsx b/apps/desktop/src/routes/screenshot-editor/ExportDialog.tsx
index 65858297e..e089bfbca 100644
--- a/apps/desktop/src/routes/screenshot-editor/ExportDialog.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/ExportDialog.tsx
@@ -80,6 +80,48 @@ export function ExportDialog() {
}
};
+ const blurRegion = (
+ ctx: CanvasRenderingContext2D,
+ source: HTMLCanvasElement,
+ startX: number,
+ startY: number,
+ regionWidth: number,
+ regionHeight: number,
+ level: number,
+ ) => {
+ const scale = Math.max(2, Math.round(level / 4));
+ const temp = document.createElement("canvas");
+ temp.width = Math.max(1, Math.floor(regionWidth / scale));
+ temp.height = Math.max(1, Math.floor(regionHeight / scale));
+ const tempCtx = temp.getContext("2d");
+ if (!tempCtx) return;
+
+ tempCtx.imageSmoothingEnabled = true;
+ tempCtx.drawImage(
+ source,
+ startX,
+ startY,
+ regionWidth,
+ regionHeight,
+ 0,
+ 0,
+ temp.width,
+ temp.height,
+ );
+
+ ctx.drawImage(
+ temp,
+ 0,
+ 0,
+ temp.width,
+ temp.height,
+ startX,
+ startY,
+ regionWidth,
+ regionHeight,
+ );
+ };
+
const applyMaskAnnotations = (
ctx: CanvasRenderingContext2D,
source: HTMLCanvasElement,
@@ -136,23 +178,7 @@ export function ExportDialog() {
continue;
}
- ctx.save();
- ctx.beginPath();
- ctx.rect(startX, startY, regionWidth, regionHeight);
- ctx.clip();
- ctx.filter = `blur(${level}px)`;
- ctx.drawImage(
- source,
- startX,
- startY,
- regionWidth,
- regionHeight,
- startX,
- startY,
- regionWidth,
- regionHeight,
- );
- ctx.restore();
+ blurRegion(ctx, source, startX, startY, regionWidth, regionHeight, level);
}
ctx.filter = "none";
};
diff --git a/apps/desktop/src/routes/screenshot-editor/Preview.tsx b/apps/desktop/src/routes/screenshot-editor/Preview.tsx
index a0362d96d..f8086e3af 100644
--- a/apps/desktop/src/routes/screenshot-editor/Preview.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/Preview.tsx
@@ -217,6 +217,48 @@ export function Preview(props: { zoom: number; setZoom: (z: number) => void }) {
let maskCanvasRef: HTMLCanvasElement | undefined;
+ const blurRegion = (
+ ctx: CanvasRenderingContext2D,
+ source: HTMLCanvasElement,
+ startX: number,
+ startY: number,
+ regionWidth: number,
+ regionHeight: number,
+ level: number,
+ ) => {
+ const scale = Math.max(2, Math.round(level / 4));
+ const temp = document.createElement("canvas");
+ temp.width = Math.max(1, Math.floor(regionWidth / scale));
+ temp.height = Math.max(1, Math.floor(regionHeight / scale));
+ const tempCtx = temp.getContext("2d");
+ if (!tempCtx) return;
+
+ tempCtx.imageSmoothingEnabled = true;
+ tempCtx.drawImage(
+ source,
+ startX,
+ startY,
+ regionWidth,
+ regionHeight,
+ 0,
+ 0,
+ temp.width,
+ temp.height,
+ );
+
+ ctx.drawImage(
+ temp,
+ 0,
+ 0,
+ temp.width,
+ temp.height,
+ startX,
+ startY,
+ regionWidth,
+ regionHeight,
+ );
+ };
+
const renderMaskOverlays = () => {
const frameData = latestFrame();
if (!maskCanvasRef) return;
@@ -308,23 +350,15 @@ export function Preview(props: { zoom: number; setZoom: (z: number) => void }) {
continue;
}
- ctx.save();
- ctx.beginPath();
- ctx.rect(startX, startY, regionWidth, regionHeight);
- ctx.clip();
- ctx.filter = `blur(${level}px)`;
- ctx.drawImage(
+ blurRegion(
+ ctx,
source,
startX,
startY,
regionWidth,
regionHeight,
- startX,
- startY,
- regionWidth,
- regionHeight,
+ level,
);
- ctx.restore();
}
ctx.filter = "none";