Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): join full-page screenshot captures without a seam on HiDPI

Elian Doran932cb9a85d32
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.ts
index aa3e72941c..ad41a8cfe7 100644
--- a/apps/web-clipper/entrypoints/background/index.spec.ts
+++ b/apps/web-clipper/entrypoints/background/index.spec.ts
@@ -570,7 +570,7 @@ describe("background", () => {
await run(onCommand("saveFullPageScreenshot"));
expect(pageSteps()).toEqual([ undefined, 0, "first", 0, "rest", 500, 1000, "restore", 300 ]);
- expect(offscreen.request).toEqual({ viewportHeight: 500, pageHeight: 1200 });
+ expect(offscreen.request).toEqual({ pageHeight: 1200, devicePixelRatio: 1 });
expect(offscreen.slices).toEqual([ { dataUrl: PNG, y: 0 }, { dataUrl: PNG, y: 500 }, { dataUrl: PNG, y: 700 } ]);
const intervals = captureTimes.slice(1).map((time, index) => time - (captureTimes[index] ?? 0));
expect(intervals.every((interval) => interval >= 550)).toBe(true);
@@ -604,7 +604,7 @@ describe("background", () => {
offscreen.slices = [];
simulatePage({ viewportHeight: 2000, pageHeight: 100000, devicePixelRatio: 4 });
await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));
- expect(offscreen.request).toEqual({ viewportHeight: 2000, pageHeight: 15000 });
+ expect(offscreen.request).toEqual({ pageHeight: 15000, devicePixelRatio: 4 });
expect(sliceTops()).toEqual([ 0, 2000, 4000, 6000, 8000, 10000, 12000, 14000 ]);
expect(await lastToast()).toMatchObject({
message: "Screenshot has been saved to Trilium, but the page was too long to capture in full."
@@ -616,7 +616,7 @@ describe("background", () => {
await run(onCommand("saveFullPageScreenshot"));
- expect(offscreen.request).toEqual({ viewportHeight: 515, pageHeight: 1100 });
+ expect(offscreen.request).toEqual({ pageHeight: 1100, devicePixelRatio: 1 });
expect(sliceTops()).toEqual([ 0, 515, 585 ]);
});
@@ -664,7 +664,7 @@ describe("background", () => {
await run(sendRuntimeMessage({ name: "save-full-page-screenshot" }));
expect(runtimeSendMessage).not.toHaveBeenCalled();
- expect(backgroundStitcher.created).toHaveBeenCalledWith({ viewportHeight: 500, pageHeight: 500 });
+ expect(backgroundStitcher.created).toHaveBeenCalledWith({ pageHeight: 500, devicePixelRatio: 1 });
expect(backgroundStitcher.add).toHaveBeenCalledWith({ dataUrl: PNG, y: 0 });
expect(lastPayload()).toMatchObject({ images: [ { dataUrl: CROPPED_PNG } ] });
});
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index 5484b2ddd8..b839051b3f 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -178,7 +178,7 @@ export default defineBackground(() => {
// Measured once the page is prepared: without its scrollbars, it can lay out differently.
const page = await scrollTab(tabId, start.scrollX, 0);
const height = Math.min(page.pageHeight, MAX_SCREENSHOT_HEIGHT / page.devicePixelRatio);
- const stitcher = await createStitcher({ viewportHeight: page.viewportHeight, pageHeight: height });
+ const stitcher = await createStitcher({ pageHeight: height, devicePixelRatio: page.devicePixelRatio });
let position = page;
let lastCaptureAt = 0;
diff --git a/apps/web-clipper/entrypoints/offscreen/index.spec.ts b/apps/web-clipper/entrypoints/offscreen/index.spec.ts
index dda8bb902f..7016c33bdf 100644
--- a/apps/web-clipper/entrypoints/offscreen/index.spec.ts
+++ b/apps/web-clipper/entrypoints/offscreen/index.spec.ts
@@ -68,7 +68,7 @@ describe("offscreen document", () => {
});
it("stitches a screenshot one message at a time, handing out the images one by one", async () => {
- const request = { viewportHeight: 10, pageHeight: 30 };
+ const request = { pageHeight: 30, devicePixelRatio: 1 };
const slice = { dataUrl: "a", y: 0 };
stitcher.finish.mockReturnValue([ "part-0", CROPPED_PNG ]);
@@ -81,7 +81,7 @@ describe("offscreen document", () => {
});
it("answers why stitching failed, also when no screenshot was begun", async () => {
- await sendMessage({ type: "STITCH_BEGIN", request: { viewportHeight: 10, pageHeight: 10 } });
+ await sendMessage({ type: "STITCH_BEGIN", request: { pageHeight: 10, devicePixelRatio: 1 } });
stitcher.add.mockRejectedValueOnce(new Error("Too tall"));
await expect(sendMessage({ type: "STITCH_ADD", slice: { dataUrl: "a", y: 0 } }))
.resolves.toEqual({ error: "Too tall" });
diff --git a/apps/web-clipper/screenshot.spec.ts b/apps/web-clipper/screenshot.spec.ts
index e2d9cfafa3..1bd6535b3e 100644
--- a/apps/web-clipper/screenshot.spec.ts
+++ b/apps/web-clipper/screenshot.spec.ts
@@ -11,7 +11,7 @@ describe("ScreenshotStitcher", () => {
it("draws each capture where it was taken, scaled to device pixels", async () => {
stubImages({ naturalWidth: 1600, naturalHeight: 1000 });
const canvas = stubCanvas();
- const stitcher = new ScreenshotStitcher({ viewportHeight: 500, pageHeight: 1200 });
+ const stitcher = new ScreenshotStitcher({ pageHeight: 1200, devicePixelRatio: 2 });
for (const y of [ 0, 500, 700 ]) {
await stitcher.add({ dataUrl: `slice-${y}`, y });
@@ -24,10 +24,28 @@ describe("ScreenshotStitcher", () => {
]);
});
+ it("places captures by the pixel ratio, which a capture's rounded height would get wrong", async () => {
+ // A 701 px viewport at 150% is 1,051.5 device pixels, captured as 1,052 rows.
+ stubImages({ naturalWidth: 600, naturalHeight: 1052 });
+ const canvas = stubCanvas();
+ const stitcher = new ScreenshotStitcher({ pageHeight: 2200, devicePixelRatio: 1.5 });
+
+ for (const y of [ 0, 701, 1402, 1499 ]) {
+ await stitcher.add({ dataUrl: `slice-${y}`, y });
+ }
+ stitcher.finish();
+
+ expect(canvas.sizes()).toEqual([ [ 600, 3300 ] ]);
+ expect(canvas.events).toEqual([
+ "draw 0: slice-0 at 0", "draw 0: slice-701 at 1052", "draw 0: slice-1402 at 2103",
+ "draw 0: slice-1499 at 2249", "encode 0"
+ ]);
+ });
+
it("splits a long page into images, encoding each before the next is drawn", async () => {
stubImages({ naturalWidth: 900, naturalHeight: 3000 });
const canvas = stubCanvas();
- const stitcher = new ScreenshotStitcher({ viewportHeight: 1000, pageHeight: 6000 });
+ const stitcher = new ScreenshotStitcher({ pageHeight: 6000, devicePixelRatio: 3 });
for (const y of [ 0, 1000, 2000, 3000, 4000, 5000 ]) {
await stitcher.add({ dataUrl: `slice-${y}`, y });
@@ -49,7 +67,7 @@ describe("ScreenshotStitcher", () => {
it("leaves out the part of the last capture that reaches back into an image already encoded", async () => {
stubImages({ naturalWidth: 100, naturalHeight: 1500 });
const canvas = stubCanvas();
- const stitcher = new ScreenshotStitcher({ viewportHeight: 1500, pageHeight: 9400 });
+ const stitcher = new ScreenshotStitcher({ pageHeight: 9400, devicePixelRatio: 1 });
for (const y of [ 0, 1500, 3000, 4500, 6000, 7500, 7900 ]) {
await stitcher.add({ dataUrl: `slice-${y}`, y });
@@ -65,7 +83,7 @@ describe("ScreenshotStitcher", () => {
it("ignores what a capture shows below the height it was given", async () => {
stubImages({ naturalWidth: 100, naturalHeight: 1000 });
const canvas = stubCanvas();
- const stitcher = new ScreenshotStitcher({ viewportHeight: 1000, pageHeight: MAX_PART_HEIGHT });
+ const stitcher = new ScreenshotStitcher({ pageHeight: MAX_PART_HEIGHT, devicePixelRatio: 1 });
await stitcher.add({ dataUrl: "slice", y: 7500 });
@@ -74,7 +92,7 @@ describe("ScreenshotStitcher", () => {
});
it("fails without captures, without a canvas, or on a capture that cannot be read", async () => {
- const request = { viewportHeight: 100, pageHeight: 100 };
+ const request = { pageHeight: 100, devicePixelRatio: 1 };
stubImages({ naturalWidth: 100, naturalHeight: 100 });
expect(() => new ScreenshotStitcher(request).finish()).toThrow("There is no screenshot to stitch.");
diff --git a/apps/web-clipper/screenshot.ts b/apps/web-clipper/screenshot.ts
index 6f6d43a6db..e93f3a0957 100644
--- a/apps/web-clipper/screenshot.ts
+++ b/apps/web-clipper/screenshot.ts
@@ -5,10 +5,10 @@ export interface ScreenshotSlice {
}
export interface StitchRequest {
- /** The height of the page's viewport, in CSS pixels. */
- viewportHeight: number;
/** The height of the page to stitch, in CSS pixels. */
pageHeight: number;
+ /** The page's `devicePixelRatio`: the device pixels of a capture per CSS pixel, zoom included. */
+ devicePixelRatio: number;
}
/** How the background script has the offscreen document stitch a screenshot, one step a message. */
@@ -37,24 +37,20 @@ export const MAX_SCREENSHOT_HEIGHT = 60_000;
*/
export class ScreenshotStitcher {
- private scale = 0;
private width = 0;
- private height = 0;
+ private readonly height: number;
private part: { index: number, canvas: HTMLCanvasElement, context: CanvasRenderingContext2D } | undefined;
private readonly parts: string[] = [];
- constructor(private readonly request: StitchRequest) {}
+ constructor(private readonly request: StitchRequest) {
+ this.height = Math.round(request.pageHeight * request.devicePixelRatio);
+ }
async add({ dataUrl, y }: ScreenshotSlice) {
const image = await loadImage(dataUrl);
- if (!this.scale) {
- // The captures are in device pixels, which include the page zoom and the display's scaling.
- this.scale = image.naturalHeight / this.request.viewportHeight;
- this.width = image.naturalWidth;
- this.height = Math.round(this.request.pageHeight * this.scale);
- }
+ this.width ||= image.naturalWidth;
- const top = Math.round(y * this.scale);
+ const top = Math.round(y * this.request.devicePixelRatio);
const bottom = Math.min(top + image.naturalHeight, this.height);
for (let index = Math.floor(top / MAX_PART_HEIGHT); index * MAX_PART_HEIGHT < bottom; index++) {
// The last capture overlaps the one before it, which already drew any part it reaches back into.