add createImageBitmap polyfill

the changes i made to the image pipeline required stuff that needs a polyfill, so here it is!
This commit is contained in:
spaciecat 2019-09-14 23:11:31 +10:00
parent 5ba626b73b
commit 0f99d8e10b
2 changed files with 34 additions and 0 deletions

View File

@ -216,6 +216,7 @@
</html>
<!-- SCRIPTS -->
<script src="scripts/lib/createImageBitmap.js"></script>
<script src="scripts/lib/tickable_observer.js"></script>
<script src="scripts/lib/helpers.js"></script>
<script src="scripts/lib/rsvp.min.js"></script>

View File

@ -0,0 +1,33 @@
/*
* Safari and Edge polyfill for createImageBitmap
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap
*
* Support source image types Blob and ImageData.
*
* From: https://dev.to/nektro/createimagebitmap-polyfill-for-safari-and-edge-228
* Updated by Yoan Tournade <yoan@ytotech.com>
*/
if (!('createImageBitmap' in window)) {
window.createImageBitmap = async function (data) {
return new Promise((resolve,reject) => {
let dataURL;
if (data instanceof Blob) {
dataURL = URL.createObjectURL(data);
} else if (data instanceof ImageData) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = data.width;
canvas.height = data.height;
ctx.putImageData(data,0,0);
dataURL = canvas.toDataURL();
} else {
throw new Error('createImageBitmap does not handle the provided image source type');
}
const img = document.createElement('img');
img.addEventListener('load',function () {
resolve(this);
});
img.src = dataURL;
});
};
}