aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlbin Larsson <mail@albinlarsson.com>2018-05-15 04:23:27 +0200
committerAlbin Larsson <mail@albinlarsson.com>2018-05-15 13:27:55 +0200
commit90d5b48845661ce99a204354f93fbbbc7a19f100 (patch)
treea18276677fa398f6f28e8d18aa92d0989cc4362e /src
parent765c01e83dc4173d778538061a82e1973a8574f0 (diff)
downloadplyr-90d5b48845661ce99a204354f93fbbbc7a19f100.tar.lz
plyr-90d5b48845661ce99a204354f93fbbbc7a19f100.tar.xz
plyr-90d5b48845661ce99a204354f93fbbbc7a19f100.zip
Add async method to utils for loading/checking images
Diffstat (limited to 'src')
-rw-r--r--src/js/utils.js15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/js/utils.js b/src/js/utils.js
index ebfb9c96..0cd332dd 100644
--- a/src/js/utils.js
+++ b/src/js/utils.js
@@ -119,6 +119,21 @@ const utils = {
});
},
+ // 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.
+ 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});
+ });
+ },
+
// Load an external script
loadScript(url) {
return new Promise((resolve, reject) => {