History

fix(web): sample the SnapShot band at the one frame aspect every surface uses

sampleSnapShotBottomColor accepted a frame aspect but cached by URL alone, so a second aspect would have read the first one's result. Every surface renders the capture in the same frame, so the parameter is gone and the per-URL cache is correct by construction.

Julius Marminge committed 2 months ago

Commit a534f651 parent 57a00a61 file · +7 −11Browse files

Unified · Split

apps/web/src/lib/snapShotOverlayTone.ts+7−11
@@ -107,7 +107,7 @@ export function coverCrop( · 106 unmodified lines
107107 return { x: 0, y: (height - cropHeight) / 2, width, height: cropHeight };
108108 }
109109
110−function readBottomBand(image: HTMLImageElement, frameAspect: number): RgbColor | null {
110+function readBottomBand(image: HTMLImageElement): RgbColor | null {
111111 const { naturalWidth: width, naturalHeight: height } = image;
112112 if (width === 0 || height === 0) return null;
113113 const canvas = document.createElement("canvas");
@@ -115,7 +115,7 @@ function readBottomBand(image: HTMLImageElement, frameAspect: number): RgbColor · 1 unmodified line
115115 canvas.height = SAMPLE_HEIGHT;
116116 const context = canvas.getContext("2d", { willReadFrequently: true });
117117 if (context === null) return null;
118− const crop = coverCrop(width, height, frameAspect);
118+ const crop = coverCrop(width, height, SNAP_SHOT_FRAME_ASPECT);
119119 const bandHeight = Math.max(1, crop.height * SAMPLE_BAND);
120120 // Drawing the band into a tiny canvas lets the rasterizer do the averaging.
121121 context.drawImage(
@@ -139,22 +139,18 @@ function readBottomBand(image: HTMLImageElement, frameAspect: number): RgbColor · 17 unmodified lines
139139
140140 /**
141141 * Average color of the bottom band of the region the thumbnail frame shows, or
142− * null when the image cannot be loaded or read. Results are memoized per URL
143− * so the surfaces that show the same capture decode it once.
142+ * null when the image cannot be loaded or read. Every surface renders the
143+ * capture in the same frame, so results are memoized per URL and the surfaces
144+ * that show the same capture decode it once.
144145 */
145−export function sampleSnapShotBottomColor(
146− src: string,
147− frameAspect = SNAP_SHOT_FRAME_ASPECT,
148−): Promise<RgbColor | null> {
146+export function sampleSnapShotBottomColor(src: string): Promise<RgbColor | null> {
149147 const cached = sampleCache.get(src);
150148 if (cached) return cached;
151149 const pending = new Promise<RgbColor | null>((resolve) => {
152150 const image = new Image();
153151 image.crossOrigin = "anonymous";
154152 image.decoding = "async";
155− image.addEventListener("load", () => resolve(readBottomBand(image, frameAspect)), {
156− once: true,
157− });
153+ image.addEventListener("load", () => resolve(readBottomBand(image)), { once: true });
158154 image.addEventListener("error", () => resolve(null), { once: true });
159155 image.src = src;
160156 });

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t