Skip to content

Commit

Permalink
feat: fetchImageBitmap - ultra fast image loading for TexImage2D
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Aug 14, 2020
1 parent f7082f7 commit 3267f4c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/textures/loaders/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,32 @@ export async function fetchCubeImages(urlPattern: string): Promise<HTMLImageElem
});
return Promise.all(fetchPromises);
}

export function fetchImageBitmap(url: string): Promise<ImageBitmap> {
return new Promise<ImageBitmap>((resolve, reject) => {
fetch(url)
.then((response: Response) => {
if (response.status === 200) {
return response.blob();
}
reject(`Unable to load resource with url ${url}`);
})

// Turn it into an ImageBitmap.
.then((blobData: Blob | undefined) => {
if (blobData !== undefined) {
return createImageBitmap(blobData);
}
})

// Post it back to main thread.
.then(
(imageBitmap) => {
return resolve(imageBitmap);
},
(err) => {
reject(err);
},
);
});
}

0 comments on commit 3267f4c

Please sign in to comment.