Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(export): project native cursor onto cropped rect, not mask rect

Etienne Lescot7f025c86e083
diff --git a/src/lib/cursor/nativeCursor.test.ts b/src/lib/cursor/nativeCursor.test.ts
index eb1ef5305..472ee6f39 100644
--- a/src/lib/cursor/nativeCursor.test.ts
+++ b/src/lib/cursor/nativeCursor.test.ts
@@ -4,6 +4,7 @@ import {
getNativeCursorClickBounceProgress,
getNativeCursorClickBounceScale,
hasNativeCursorRecordingData,
+ projectNativeCursorToLocal,
resolveInterpolatedNativeCursorFrame,
resolveNativeCursorRenderAsset,
} from "./nativeCursor";
@@ -217,3 +218,68 @@ describe("custom cursor themes", () => {
expect(rendered.id).toBe("pretty:text");
});
});
+
+describe("projectNativeCursorToLocal", () => {
+ const identityCrop = { x: 0, y: 0, width: 1, height: 1 };
+
+ it("maps a sample onto the supplied painted rectangle 1:1 with no crop", () => {
+ const point = projectNativeCursorToLocal({
+ cropRegion: identityCrop,
+ maskRect: { x: 100, y: 200, width: 1280, height: 720 },
+ sample: { timeMs: 0, cx: 0.25, cy: 0.5, visible: true },
+ });
+
+ expect(point?.x).toBeCloseTo(100 + 0.25 * 1280);
+ expect(point?.y).toBeCloseTo(200 + 0.5 * 720);
+ });
+
+ it("maps a sample into the cropped region of the painted rectangle", () => {
+ const point = projectNativeCursorToLocal({
+ cropRegion: { x: 0.25, y: 0.0, width: 0.5, height: 1.0 },
+ maskRect: { x: 0, y: 0, width: 1920, height: 1080 },
+ sample: { timeMs: 0, cx: 0.5, cy: 0.5, visible: true },
+ });
+
+ expect(point?.x).toBeCloseTo(0 + ((0.5 - 0.25) / 0.5) * 1920);
+ expect(point?.y).toBeCloseTo(0 + (0.5 / 1.0) * 1080);
+ });
+
+ it("projects onto the cropped (cover-letterboxed) painted rect, not the mask rect", () => {
+ const screenRect = { x: 0, y: 0, width: 1920, height: 1080 };
+ const croppedRect = { x: 0, y: -540, width: 1920, height: 2160 };
+
+ const point = projectNativeCursorToLocal({
+ cropRegion: { x: 0.0, y: 0.0, width: 0.5, height: 1.0 },
+ maskRect: croppedRect,
+ sample: { timeMs: 0, cx: 0.25, cy: 0.25, visible: true },
+ });
+
+ const wrong = screenRect.y + 0.25 * screenRect.height;
+ expect(wrong).toBe(270);
+
+ expect(point?.x).toBeCloseTo(croppedRect.x + ((0.25 - 0) / 0.5) * croppedRect.width);
+ expect(point?.y).toBeCloseTo(croppedRect.y + (0.25 / 1.0) * croppedRect.height);
+ expect(point?.y).toBe(0);
+ expect(point?.y).not.toBe(wrong);
+ });
+
+ it("returns null for a sample outside the cropped region", () => {
+ const point = projectNativeCursorToLocal({
+ cropRegion: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
+ maskRect: { x: 0, y: 0, width: 1920, height: 1080 },
+ sample: { timeMs: 0, cx: 0.1, cy: 0.5, visible: true },
+ });
+
+ expect(point).toBeNull();
+ });
+
+ it("returns null for a degenerate (zero-size) crop region", () => {
+ const point = projectNativeCursorToLocal({
+ cropRegion: { x: 0, y: 0, width: 0, height: 1 },
+ maskRect: { x: 0, y: 0, width: 1920, height: 1080 },
+ sample: { timeMs: 0, cx: 0.5, cy: 0.5, visible: true },
+ });
+
+ expect(point).toBeNull();
+ });
+});
diff --git a/src/lib/exporter/frameRenderer.ts b/src/lib/exporter/frameRenderer.ts
index d8f3c4312..30fd38575 100644
--- a/src/lib/exporter/frameRenderer.ts
+++ b/src/lib/exporter/frameRenderer.ts
@@ -126,6 +126,7 @@ interface LayoutCache {
baseScale: number;
baseOffset: { x: number; y: number };
maskRect: { x: number; y: number; width: number; height: number };
+ croppedRect: { x: number; y: number; width: number; height: number };
maskBorderRadius: number;
webcamRect: StyledRenderRect | null;
}
@@ -573,7 +574,7 @@ export class FrameRenderer {
const projectedPoint = projectNativeCursorToLocal({
cropRegion: this.config.cropRegion,
- maskRect: this.layoutCache.maskRect,
+ maskRect: this.layoutCache.croppedRect,
sample: displaySample,
});
if (!projectedPoint) {
@@ -761,6 +762,12 @@ export class FrameRenderer {
y: compositeLayout.screenRect.y + coverOffsetY - cropPixelY,
},
maskRect: compositeLayout.screenRect,
+ croppedRect: {
+ x: compositeLayout.screenRect.x + coverOffsetX,
+ y: compositeLayout.screenRect.y + coverOffsetY,
+ width: croppedDisplayWidth,
+ height: croppedDisplayHeight,
+ },
maskBorderRadius: scaledBorderRadius,
webcamRect: compositeLayout.webcamRect,
};