aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/loadImage.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-11-11 11:05:09 +1100
committerSam Potts <sam@potts.es>2018-11-11 11:05:09 +1100
commitb7b2e3c0aa0749eed53ae91230082cb0482e1f28 (patch)
treef073bde14df6459419323dd6570b2549b8d26c41 /src/js/utils/loadImage.js
parent3e0a91141822758094b2cbd5f0ecdd8ce4142b5f (diff)
parent2c8a337f265f3f84133bc674f3836802588c0c13 (diff)
downloadplyr-b7b2e3c0aa0749eed53ae91230082cb0482e1f28.tar.lz
plyr-b7b2e3c0aa0749eed53ae91230082cb0482e1f28.tar.xz
plyr-b7b2e3c0aa0749eed53ae91230082cb0482e1f28.zip
Merge branch 'develop' into css-variables
# Conflicts: # demo/dist/demo.css # demo/dist/demo.js # demo/dist/demo.js.map # demo/dist/demo.min.js # demo/dist/demo.min.js.map # dist/plyr.css # dist/plyr.js # dist/plyr.js.map # dist/plyr.min.js # dist/plyr.min.js.map # dist/plyr.polyfilled.js # dist/plyr.polyfilled.js.map # dist/plyr.polyfilled.min.js # dist/plyr.polyfilled.min.js.map # gulpfile.js # src/sass/components/captions.scss # src/sass/components/control.scss
Diffstat (limited to 'src/js/utils/loadImage.js')
-rw-r--r--src/js/utils/loadImage.js19
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 });
+ });
+}