mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Add support for OffscreenCanvas in media optimization worker (#24074)
Back in c31772879b we introduced
SiteSetting.composer_ios_media_optimisation_image_enabled and
disabled media optimization on safari iOS because of performance
issues when rendering to canvas, and OffscreenCanvas support
was not yet available.
Safari now supports OffscreenCanvas, so now we can give this
another go, and also use OffscreenCanvas everywhere it is supported.
This commit is contained in:
@@ -40,10 +40,17 @@ function drawableToImageData(drawable) {
|
||||
sw = width,
|
||||
sh = height;
|
||||
|
||||
const offscreenCanvasSupported = typeof OffscreenCanvas !== "undefined";
|
||||
|
||||
// Make canvas same size as image
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
let canvas;
|
||||
if (offscreenCanvasSupported) {
|
||||
canvas = new OffscreenCanvas(width, height);
|
||||
} else {
|
||||
canvas = document.createElement("canvas");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
}
|
||||
|
||||
// Draw image onto canvas
|
||||
const ctx = canvas.getContext("2d");
|
||||
@@ -52,7 +59,11 @@ function drawableToImageData(drawable) {
|
||||
}
|
||||
ctx.drawImage(drawable, sx, sy, sw, sh, 0, 0, width, height);
|
||||
const imageData = ctx.getImageData(0, 0, width, height);
|
||||
canvas.remove();
|
||||
|
||||
if (!offscreenCanvasSupported) {
|
||||
canvas.remove();
|
||||
}
|
||||
|
||||
return imageData;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user