Last active
September 15, 2018 10:39
-
-
Save josephrocca/79d88050b91df3702bb184f9a0bd5b82 to your computer and use it in GitHub Desktop.
scrap - image loading in webcomponent test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function(window, document, undefined) { | |
//document = document.currentScript.ownerDocument; | |
class ImageOnLoadExample extends HTMLElement { | |
constructor() { | |
super(); | |
this.attachShadow({mode: 'open'}); | |
this.shadowRoot.innerHTML = "<h1>ImageOnLoadExample WebComponent</h1>"; | |
} | |
async getImageWidthByUrl(imgUrl) { | |
const imgEl = document.createElement("img"); | |
let untilImgLoaded = new Promise((resolve, reject) => { | |
imgEl.onload = resolve; | |
imgEl.onerror = reject; | |
}); | |
imgEl.src = imgUrl; | |
await untilImgLoaded; | |
return imgEl.naturalWidth; | |
} | |
} | |
window.customElements.define('image-on-load-example', ImageOnLoadExample); | |
})(window, document); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment