Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(editor): add autoTextColorAt for new text segments

Richie McIlroy89a7515a06d7
diff --git a/apps/desktop/src/routes/editor/text.ts b/apps/desktop/src/routes/editor/text.ts
index 96b23f6ef..a8cc1e449 100644
--- a/apps/desktop/src/routes/editor/text.ts
+++ b/apps/desktop/src/routes/editor/text.ts
@@ -1,3 +1,4 @@
+import type { CanvasControls } from "~/utils/socket";
import type { XY } from "~/utils/tauri";
// Font sizes are authored in pixels against a 1080p-tall reference frame;
@@ -22,6 +23,62 @@ export type TextSegment = {
fadeDuration: number;
};
+// Picks the starting colour for a new text segment by sampling the composited
+// preview frame around the box the text will occupy: black over light
+// backdrops (white web pages, light-mode apps), white otherwise. Video and
+// mid-tone content reads better with white, so the threshold leans that way.
+// Falls back to white when no frame has rendered yet.
+export function autoTextColorAt(
+ controls: Pick<CanvasControls, "drawLatestFrameToCanvas"> | null | undefined,
+ center: XY<number> = { x: 0.5, y: 0.5 },
+ size: XY<number> = { x: 0.1, y: 0.055 },
+): string {
+ const light = "#ffffff";
+ if (!controls) return light;
+ try {
+ const canvas = document.createElement("canvas");
+ if (!controls.drawLatestFrameToCanvas(canvas)) return light;
+ const ctx = canvas.getContext("2d");
+ if (!ctx || canvas.width <= 0 || canvas.height <= 0) return light;
+
+ // Sample generously beyond the initial box — the text grows from its
+ // center as the user types.
+ const halfW = Math.max(size.x * 1.5, 0.12);
+ const halfH = Math.max(size.y * 1.5, 0.07);
+ const x0 = Math.max(0, Math.floor((center.x - halfW) * canvas.width));
+ const x1 = Math.min(
+ canvas.width,
+ Math.ceil((center.x + halfW) * canvas.width),
+ );
+ const y0 = Math.max(0, Math.floor((center.y - halfH) * canvas.height));
+ const y1 = Math.min(
+ canvas.height,
+ Math.ceil((center.y + halfH) * canvas.height),
+ );
+ const width = x1 - x0;
+ const height = y1 - y0;
+ if (width <= 0 || height <= 0) return light;
+
+ const pixels = ctx.getImageData(x0, y0, width, height).data;
+ const pixelCount = width * height;
+ const stride = Math.max(1, Math.floor(pixelCount / 2048));
+ let total = 0;
+ let count = 0;
+ for (let i = 0; i < pixelCount; i += stride) {
+ const p = i * 4;
+ total +=
+ 0.299 * pixels[p] + 0.587 * pixels[p + 1] + 0.114 * pixels[p + 2];
+ count++;
+ }
+ if (count === 0) return light;
+
+ const luma = total / count / 255;
+ return luma > 0.6 ? "#000000" : light;
+ } catch {
+ return light;
+ }
+}
+
export const defaultTextSegment = (
start: number,
end: number,