Skip to content

Commit 799558a

Browse files
committed
chore: staged changes
1 parent 1614d27 commit 799558a

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/packlets/playground/playground.svelte

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
1414
let balloons: Item[] = []
1515
16-
const addBalloon = (x: number, y = 0) => {
17-
const size = Math.floor(Math.random() * 300300) + 60
16+
const addBalloon = (
17+
x = Math.floor(Math.random() * window.innerWidth),
18+
y = 0
19+
) => {
20+
const size = Math.floor(Math.random() * 300) + 60
1821
1922
const item: Item = {
2023
id: (Math.random() + 1).toString(36).substring(7),
@@ -31,19 +34,38 @@
3134
}
3235
3336
const handleClick: MouseEventHandler<HTMLElement> = e => {
34-
addBalloon(e.pageX, -window.outerHeight + e.pageY)
37+
addBalloon(e.pageX, -window.innerHeight + e.pageY)
3538
}
3639
3740
onMount(() => {
3841
// randomly generate balloons
39-
const interval = setInterval(() => {
40-
addBalloon(Math.floor(Math.random() * window.outerWidth))
41-
addBalloon(Math.floor(Math.random() * window.outerWidth))
42-
addBalloon(Math.floor(Math.random() * window.outerWidth))
43-
addBalloon(Math.floor(Math.random() * window.outerWidth))
44-
}, 600)
45-
46-
return () => clearInterval(interval)
42+
const random = (amount: number) => {
43+
for (let i = 0; i < amount; i++)
44+
setTimeout(() => addBalloon(), Math.floor(Math.random() * 10000) % 1000)
45+
}
46+
47+
let interval = 0
48+
let balloonPerSet = 0
49+
50+
// monitor window size
51+
const handleResize = () => {
52+
const targetSize =
53+
window.innerWidth < 600 ? 3 : window.innerWidth < 1000 ? 4 : 5
54+
55+
if (balloonPerSet !== targetSize) {
56+
clearInterval(interval)
57+
balloonPerSet = targetSize
58+
interval = setInterval(() => random(balloonPerSet), 800)
59+
}
60+
}
61+
62+
window.addEventListener('resize', handleResize)
63+
handleResize()
64+
65+
return () => {
66+
clearInterval(interval)
67+
window.removeEventListener('resize', handleResize)
68+
}
4769
})
4870
</script>
4971

0 commit comments

Comments
 (0)