Skip to content

Instantly share code, notes, and snippets.

@BroFox86
Created November 22, 2023 10:57
Show Gist options
  • Save BroFox86/ab91ddf830071802587a5a040bf5da3f to your computer and use it in GitHub Desktop.
Save BroFox86/ab91ddf830071802587a5a040bf5da3f to your computer and use it in GitHub Desktop.
[Stop motion animation] №оы
/**
* Launches star animation.
* @param {Number} interval - Animation speed in MS.
* @param {String} target - Target selector.
*/
async function animateStar(interval, target) {
try {
const container = document.querySelector(target)
const image = document.createElement('img')
const paths = ['images/Star1.svg', 'images/Star2.svg', 'images/Star3.svg', 'images/Star4.svg', 'images/Star5.svg']
const starsCount = paths.length
let i = 0
await Promise.all(
(() => {
const array = []
for (const path of paths) {
array.push(loadImage(path))
}
return array
})(),
)
if (!container.children[0]) {
container.append(image)
} else {
console.log('rem')
container.children[0].remove()
container.append(image)
}
let timeoutID = setTimeout(function run() {
if (i === starsCount) {
clearInterval(timeoutID)
return
}
image.src = paths[i]
i++
timeoutID = setTimeout(run, interval)
}, interval)
} catch (error) {
console.log(`Error: ${error.message}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment