Skip to content

Instantly share code, notes, and snippets.

@josephrocca
Last active September 15, 2018 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrocca/79d88050b91df3702bb184f9a0bd5b82 to your computer and use it in GitHub Desktop.
Save josephrocca/79d88050b91df3702bb184f9a0bd5b82 to your computer and use it in GitHub Desktop.
scrap - image loading in webcomponent test
<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