@@ -411,28 +411,18 @@ export const IMAGE_EMBED_SIZE = 1921;
411
411
/**
412
412
* Given a url, get the image data. The url can either start with `http` or `data:image`.
413
413
*/
414
- export function getImageDataFromURL ( url : string ) : Promise < ImageData > {
415
- return new Promise ( ( resolve , reject ) => {
416
- // Converts img to any, and later `as CanvasImageSource`, otherwise build complains
417
- const img : any = new Image ( ) ;
418
- img . crossOrigin = "anonymous" ; // Important for CORS
419
- img . onload = ( ) => {
420
- const canvas = document . createElement ( "canvas" ) ;
421
- const ctx = canvas . getContext ( "2d" ) ;
422
- if ( ! ctx ) {
423
- reject ( new Error ( "Could not get 2d context" ) ) ;
424
- return ;
425
- }
426
- canvas . width = img . width ;
427
- canvas . height = img . height ;
428
- ctx . drawImage ( img as CanvasImageSource , 0 , 0 ) ;
414
+ export async function getImageDataFromURL ( url : string ) : Promise < ImageData > {
415
+ const response = await fetch ( url , { mode : "cors" } ) ;
416
+ const img = await createImageBitmap ( await response . blob ( ) ) ;
417
+ const canvas = new OffscreenCanvas ( img . width , img . height ) ;
418
+ const ctx = canvas . getContext ( "2d" ) ;
419
+ if ( ! ctx ) {
420
+ throw new Error ( "Could not get 2d context" ) ;
421
+ }
422
+ ctx . drawImage ( img , 0 , 0 ) ;
429
423
430
- const imageData = ctx . getImageData ( 0 , 0 , img . width , img . height ) ;
431
- resolve ( imageData ) ;
432
- } ;
433
- img . onerror = ( ) => reject ( new Error ( "Failed to load image" ) ) ;
434
- img . src = url ;
435
- } ) ;
424
+ const imageData = ctx . getImageData ( 0 , 0 , img . width , img . height ) ;
425
+ return imageData ;
436
426
}
437
427
438
428
/**
0 commit comments