Created
February 10, 2021 21:07
-
-
Save basilleaf/2ffa31eacd83f5e97546c2abbcc97c48 to your computer and use it in GitHub Desktop.
loading images in background
This file contains hidden or 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
// load in background js | |
const animatedGif = new Image() | |
animatedGif.src = animatedGifSrc | |
animatedGif.onload = () => { | |
this.setState({ mobileImgSrc: animatedGif.src }) | |
} | |
// load via xhr | |
const xhr = new XMLHttpRequest() | |
xhr.open("GET", animatedGifSrc, true) | |
xhr.responseType = "blob" | |
xhr.onload = () => { | |
if (xhr.status === 200) { | |
const blob = xhr.response | |
const url = window.URL.createObjectURL(blob) | |
this.setState({ mobileImgSrc: url }) | |
} | |
} | |
xhr.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment