perf: implement background thumbnail generation and inter-post preloading
Key changes: - Added Web Worker for background image thumbnailing with a 1MiB threshold to optimize CPU/memory usage. - Implemented a serial task queue for memory-safe high-res image processing, preventing OOM crashes. - Added inter-post preloading in the modal for seamless 'Previous/Next' navigation. - Refined scanning UI with double-buffering and a dark background to completely eliminate white flashes. - Renamed project to 'instaarchive-viewer' in package.json. - Fixed 'Open image in new tab' by denylisting /archives and /api in PWA config.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Thumbnail Generation Worker
|
||||
* Uses OffscreenCanvas and createImageBitmap for high-performance,
|
||||
* background-thread image resizing.
|
||||
*/
|
||||
|
||||
self.onmessage = async (e: MessageEvent) => {
|
||||
const { id, blob, width } = e.data;
|
||||
|
||||
try {
|
||||
// 1. Create a bitmap from the blob (native browser decoding)
|
||||
// We resize it DURING the decode step for maximum efficiency
|
||||
const bitmap = await createImageBitmap(blob, {
|
||||
resizeWidth: width,
|
||||
resizeQuality: 'medium'
|
||||
});
|
||||
|
||||
// 2. Use OffscreenCanvas to draw the resized bitmap
|
||||
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error('Could not get OffscreenCanvas context');
|
||||
}
|
||||
|
||||
ctx.drawImage(bitmap, 0, 0);
|
||||
|
||||
// 3. Convert to a small JPEG blob
|
||||
const thumbnailBlob = await canvas.convertToBlob({
|
||||
type: 'image/jpeg',
|
||||
quality: 0.7
|
||||
});
|
||||
|
||||
// 4. Release bitmap memory
|
||||
bitmap.close();
|
||||
|
||||
// 5. Send result back
|
||||
self.postMessage({ id, blob: thumbnailBlob });
|
||||
} catch (err: any) {
|
||||
self.postMessage({ id, error: err.message });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user