Skip to content

Instantly share code, notes, and snippets.

@basilleaf
Created February 10, 2021 21:07
Show Gist options
  • Save basilleaf/2ffa31eacd83f5e97546c2abbcc97c48 to your computer and use it in GitHub Desktop.
Save basilleaf/2ffa31eacd83f5e97546c2abbcc97c48 to your computer and use it in GitHub Desktop.
loading images in background
// 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