|
13 | 13 |
|
14 | 14 | let balloons: Item[] = []
|
15 | 15 |
|
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 |
18 | 21 |
|
19 | 22 | const item: Item = {
|
20 | 23 | id: (Math.random() + 1).toString(36).substring(7),
|
|
31 | 34 | }
|
32 | 35 |
|
33 | 36 | const handleClick: MouseEventHandler<HTMLElement> = e => {
|
34 |
| - addBalloon(e.pageX, -window.outerHeight + e.pageY) |
| 37 | + addBalloon(e.pageX, -window.innerHeight + e.pageY) |
35 | 38 | }
|
36 | 39 |
|
37 | 40 | onMount(() => {
|
38 | 41 | // 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 | + } |
47 | 69 | })
|
48 | 70 | </script>
|
49 | 71 |
|
|
0 commit comments