-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from robertknight/chrome-image-clone-workaround
Convert ImageBitmap => ImageData on the main thread in all browsers
- Loading branch information
Showing
3 changed files
with
38 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @param {ImageBitmap} bitmap | ||
* @return {ImageData} | ||
*/ | ||
export function imageDataFromBitmap(bitmap) { | ||
/** @type {HTMLCanvasElement} */ | ||
let canvas; | ||
// @ts-expect-error - OffscreenCanvas API is missing | ||
if (typeof OffscreenCanvas !== "undefined") { | ||
// @ts-expect-error - OffscreenCanvas API is missing | ||
canvas = new OffscreenCanvas(bitmap.width, bitmap.height); | ||
} else if (typeof HTMLCanvasElement !== "undefined") { | ||
const canvasEl = document.createElement("canvas"); | ||
canvasEl.width = bitmap.width; | ||
canvasEl.height = bitmap.height; | ||
canvas = canvasEl; | ||
} else { | ||
throw new Error("No canvas implementation available"); | ||
} | ||
|
||
const context = /** @type {CanvasRenderingContext2D} */ ( | ||
canvas.getContext("2d") | ||
); | ||
context.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height); | ||
return context.getImageData(0, 0, bitmap.width, bitmap.height); | ||
} |