Last active
May 28, 2024 18:32
-
-
Save BroFox86/ab91ddf830071802587a5a040bf5da3f to your computer and use it in GitHub Desktop.
[Motion animation] #animation
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
/** | |
* 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