Created
November 25, 2020 12:39
-
-
Save colinkiama/9ed9c2e1224741b0cc83bd3fcce3f2e0 to your computer and use it in GitHub Desktop.
Parallax emoji confetti example
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
<script> | |
import { onMount } from 'svelte'; | |
let characters = ['🥳', '🎉', '✨']; | |
let confetti = new Array(100).fill() | |
.map((_, i) => { | |
return { | |
character: characters[i % characters.length], | |
x: Math.random() * 100, | |
y: -20 - Math.random() * 100, | |
r: 0.1 + Math.random() * 1 | |
}; | |
}) | |
.sort((a, b) => a.r - b.r); | |
onMount(() => { | |
let frame; | |
function loop() { | |
frame = requestAnimationFrame(loop); | |
confetti = confetti.map(emoji => { | |
emoji.y += 0.7 * emoji.r; | |
if (emoji.y > 120) emoji.y = -20; | |
return emoji; | |
}); | |
} | |
loop(); | |
return () => cancelAnimationFrame(frame); | |
}); | |
</script> | |
<style> | |
:global(body) { | |
overflow: hidden; | |
} | |
span { | |
position: absolute; | |
font-size: 5vw; | |
} | |
</style> | |
{#each confetti as c} | |
<span style="left: {c.x}%; top: {c.y}%; transform: scale({c.r})">{c.character}</span> | |
{/each} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment