你在这里面对XY and even -Z problem ,但每个人都可能有一个有用的答案,所以让我们深入研究。
X 。 请勿使用 canvas API 进行图片格式转换 。
canvas API 是有损的,无论你做什么,你都会从原始图像中丢失信息,即使你传递了无损图像,画布上绘制的图像也不会与原始图像相同。
如果您传递已经有损的格式(例如 JPEG),它甚至会添加原始图像中没有的信息:压缩伪影现在是原始位图的一部分,导出算法会将这些视为应保留的信息,从而使您的文件可能比您输入的 JPEG 文件大。
不知道你的用例,给你完美的建议有点困难,但一般来说,使版本中的不同格式最接近原始图像,一旦在浏览器中绘制,你至少已经太迟了三步。
现在,如果您对此图像进行一些处理,您可能确实想要导出结果。
但是您可能在这里不需要这个 Web Worker。是 。在您的描述中占用最大阻塞时间的应该是同步 toDataURL() 调用。
您应该始终使用异步且性能更高的 toBlob() 方法,而不是 API 中的这个历史错误。在 99% 的情况下,无论如何您都不需要数据 URL,几乎所有您想要对数据 URL 执行的操作都应该直接使用 Blob 完成。
使用这种方法,剩下的唯一繁重的同步操作就是在画布上绘画,除非你要缩小一些巨大的图像,否则这不应该占用 400 毫秒。
但无论如何,由于createImageBitmap 方法,您可以在最新的画布上使其变得更好,该方法允许您异步准备图像,以便图像的解码完成,所有需要做的只是一个 放像素 操作:
large.onclick = e => process('https://upload.wikimedia.org/wikipedia/commons/c/cf/Black_hole_-_Messier_87.jpg');
medium.onclick = e => process('https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Black_hole_-_Messier_87.jpg/1280px-Black_hole_-_Messier_87.jpg');
function process(url) {
convertToWebp(url)
.then(prepareDownload)
.catch(console.error);
}
async function convertToWebp(url) {
if(!supportWebpExport())
console.warn("your browser doesn't support webp export, will default to png");
let img = await loadImage(url);
if(typeof window.createImageBitmap === 'function') {
img = await createImageBitmap(img);
}
const ctx = get2DContext(img.width, img.height);
console.time('only sync part');
ctx.drawImage(img, 0,0);
console.timeEnd('only sync part');
return new Promise((res, rej) => {
ctx.canvas.toBlob( blob => {
if(!blob) rej(ctx.canvas);
res(blob);
}, 'image/webp');
});
}
// some helpers
function loadImage(url) {
return new Promise((res, rej) => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.src = url;
img.onload = e => res(img);
img.onerror = rej;
});
}
function get2DContext(width = 300, height=150) {
return Object.assign(
document.createElement('canvas'),
{width, height}
).getContext('2d');
}
function prepareDownload(blob) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'image.' + blob.type.replace('image/', '');
a.textContent = 'download';
document.body.append(a);
}
function supportWebpExport() {
return get2DContext(1,1).canvas
.toDataURL('image/webp')
.indexOf('image/webp') > -1;
}
<button id="large">convert large image (7,416 × 4,320 pixels)</button>
<button id="medium">convert medium image (1,280 × 746 pixels)</button>
Z 。要从 Web Worker 在 OffscreenCanvas 上绘制图像,您需要上面提到的 createImageBitmap。实际上,此方法生成的 ImageBitmap 对象是 drawImage() 和 texImage2D() (*) 可以接受的唯一 图像源 值,该值在 Workers 中可用(所有其他都是 DOM 元素)。
这个 ImageBitmap 是transferable ,所以你可以从主线程生成它,然后将它发送给你的 Worker,没有内存成本:
main.js
const img = new Image();
img.onload = e => {
createImageBitmap(img).then(bmp => {
// transfer it to your worker
worker.postMessage({
image: bmp // the key to retrieve it in `event.data`
},
[bmp] // transfer it
);
};
img.src = url;
另一种解决方案是直接从 Worker 中获取图像数据,并从获取的 Blob 中生成 ImageBitmap 对象:
worker.js
const blob = await fetch(url).then(r => r.blob());
const img = await createImageBitmap(blob);
ctx.drawImage(img,0,0);
请注意,如果您将主页面中的原始图像作为 Blob(例如来自 ),那么甚至不要直接使用 HTMLImageElement 的方式,也不要直接获取发送此 Blob 并从中生成 ImageBitmap。
*texImage2D 实际上接受更多的源图像格式,例如 TypedArrays 和 ImageData 对象,但是这些 TypedArrays 应该代表像素数据,就像 ImageData 一样,并且为了具有这个像素数据,您可能需要已经使用其他 图像源 格式之一在某处绘制了图像。