export async function loadImage ( source ) { const img = new Image ( ) ; return new Promise < HTMLImageElement > ( ( resolve , reject ) => { img . onload = ( ) => { resolve ( img ) ; } ; img . onerror = ( ) => { reject ( new Error ( `Unable to load image '${ source }'` ) ) ; } ; img . src = source instanceof URL ? source . href : `assets/${ source }` ; } ) ; } export function createCanvas ( width , height ) { const canvas = document . createElement ( "canvas" ) ! ; canvas . width = width ; canvas . height = height ; return canvas ; } export function getRenderingContext ( canvas , contextId , options ? ) ; export function getRenderingContext ( canvas , contextId , options ? ) { const ctx = canvas . getContext ( contextId , options ) ; if ( ctx == null ) { throw new Error ( `Canvas doesn't support context with id '${ contextId }'` ) ; } return ctx ; } export function createContext2D ( width , height ) { return getRenderingContext ( createCanvas ( width , height ) , "2d" ) ; } export function getImageData ( image ) { const ctx = createContext2D ( image . width , image . height ) ; ctx . drawImage ( image , 0 , 0 , image . width , image . height ) ; return ctx . getImageData ( 0 , 0 , image . width , image . height ) ; }