diff options
author | Sam Potts <sam@potts.es> | 2018-06-13 00:02:55 +1000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2018-06-13 00:02:55 +1000 |
commit | 392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 (patch) | |
tree | aedb56d3945eaa10bf74e61902e16c08fd24914a /src/js/utils/loadImage.js | |
parent | 840e31a693462e7ed9f7644a13a0187d9e9d93a9 (diff) | |
download | plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.lz plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.xz plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.zip |
Utils broken down into seperate files and exports
Diffstat (limited to 'src/js/utils/loadImage.js')
-rw-r--r-- | src/js/utils/loadImage.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/js/utils/loadImage.js b/src/js/utils/loadImage.js new file mode 100644 index 00000000..8acd2496 --- /dev/null +++ b/src/js/utils/loadImage.js @@ -0,0 +1,19 @@ +// ========================================================================== +// Load image avoiding xhr/fetch CORS issues +// Server status can't be obtained this way unfortunately, so this uses "naturalWidth" to determine if the image has loaded +// By default it checks if it is at least 1px, but you can add a second argument to change this +// ========================================================================== + +export default function loadImage(src, minWidth = 1) { + return new Promise((resolve, reject) => { + const image = new Image(); + + const handler = () => { + delete image.onload; + delete image.onerror; + (image.naturalWidth >= minWidth ? resolve : reject)(image); + }; + + Object.assign(image, { onload: handler, onerror: handler, src }); + }); +} |