|
10 | 10 | export let id: string
|
11 | 11 | export let size: number = 150
|
12 | 12 | export let horizontal: number = 0
|
13 |
| - export let initialHeight: number | undefined = undefined |
14 |
| - export let acceleration = -150 // px/s^2 |
| 13 | + export let vertical: number | undefined |
| 14 | + export let acceleration = -6 // px/s^2 |
15 | 15 |
|
16 | 16 | const colors = [
|
17 | 17 | 'text-น้ำครั่ง',
|
|
27 | 27 | const color = colors[Math.floor(Math.random() * colors.length)]
|
28 | 28 |
|
29 | 29 | // by default, if initialHeight is not provided, size will determine height position
|
30 |
| - let currentHeight = initialHeight ?? -size |
| 30 | + const currentWidth = horizontal + (size / 2) - window.innerWidth / 2 |
| 31 | + let currentHeight = (vertical ? vertical - (size / 2) : -size) + (window.innerHeight / 2) |
31 | 32 | let mountedAt = performance.now()
|
32 | 33 |
|
33 | 34 | const frame = () => {
|
|
37 | 38 | const distance = 0.5 * acceleration * time ** 2
|
38 | 39 |
|
39 | 40 | // calculate new height based on initial height and distance, would fly up
|
40 |
| - currentHeight = initialHeight ? initialHeight - distance : -size + distance |
| 41 | + currentHeight = currentHeight + distance |
41 | 42 |
|
42 | 43 | // stop when it overshoots viewport height
|
43 |
| - if (currentHeight > window.innerHeight) { |
| 44 | + if ((-currentHeight - (size * 2)) > window.innerHeight) { |
44 | 45 | ended = true
|
45 | 46 | }
|
46 | 47 |
|
|
60 | 61 | if (ended)
|
61 | 62 | dispatch('end', id)
|
62 | 63 | }
|
| 64 | +
|
| 65 | + $: computedStyle = `width:${size}px;height:${size}px;transform:translate3d(${currentWidth}px,${currentHeight}px, 0);` |
63 | 66 | </script>
|
64 | 67 |
|
65 | 68 | {#if variant === 'inner'}
|
66 |
| - <Inner class="absolute {color}" style="width:{size}px;height:{size}px;left:{horizontal};bottom:{currentHeight};" /> |
| 69 | + <Inner class="absolute {color}" style={computedStyle} /> |
67 | 70 | {:else}
|
68 |
| - <Outer class="absolute {color}" style="width:{size}px;height:{size}px;left:{horizontal};bottom:{currentHeight};" /> |
| 71 | + <Outer class="absolute {color}" style={computedStyle} /> |
69 | 72 | {/if}
|
0 commit comments