Skip to content

Commit 7595232

Browse files
committed
refactor: use translate3d for gpu acceleration
1 parent 6ae7619 commit 7595232

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Diff for: src/packlets/front/balloon.svelte

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
export let id: string
1111
export let size: number = 150
1212
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
1515
1616
const colors = [
1717
'text-น้ำครั่ง',
@@ -27,7 +27,8 @@
2727
const color = colors[Math.floor(Math.random() * colors.length)]
2828
2929
// 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)
3132
let mountedAt = performance.now()
3233
3334
const frame = () => {
@@ -37,10 +38,10 @@
3738
const distance = 0.5 * acceleration * time ** 2
3839
3940
// calculate new height based on initial height and distance, would fly up
40-
currentHeight = initialHeight ? initialHeight - distance : -size + distance
41+
currentHeight = currentHeight + distance
4142
4243
// stop when it overshoots viewport height
43-
if (currentHeight > window.innerHeight) {
44+
if ((-currentHeight - (size * 2)) > window.innerHeight) {
4445
ended = true
4546
}
4647
@@ -60,10 +61,12 @@
6061
if (ended)
6162
dispatch('end', id)
6263
}
64+
65+
$: computedStyle = `width:${size}px;height:${size}px;transform:translate3d(${currentWidth}px,${currentHeight}px, 0);`
6366
</script>
6467

6568
{#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} />
6770
{:else}
68-
<Outer class="absolute {color}" style="width:{size}px;height:{size}px;left:{horizontal};bottom:{currentHeight};" />
71+
<Outer class="absolute {color}" style={computedStyle} />
6972
{/if}

Diff for: src/packlets/front/playground.svelte

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
id: string
88
size: number
99
horizontal: number
10-
initialHeight?: number
10+
vertical?: number
1111
}
1212
1313
let balloons: Item[] = []
@@ -23,7 +23,7 @@
2323
id: (Math.random() + 1).toString(36).substring(7),
2424
size,
2525
horizontal: e.pageX - (size / 2),
26-
initialHeight: window.outerHeight - e.pageY - size
26+
vertical: -window.outerHeight + e.pageY + size
2727
}
2828
2929
balloons = [...balloons, item]
@@ -32,12 +32,12 @@
3232

3333
<section class="h-screen flex justify-center items-center flex-col relative overflow-hidden" on:mousedown={handleClick} role="none">
3434
<slot />
35-
{#each balloons as { id, size, horizontal, initialHeight } (`balloon-${id}`)}
35+
{#each balloons as { id, size, horizontal, vertical } (`balloon-${id}`)}
3636
<Balloon
3737
id={id}
3838
size={size}
3939
horizontal={horizontal}
40-
initialHeight={initialHeight}
40+
vertical={vertical}
4141
on:end={handleEnd}
4242
/>
4343
{/each}

0 commit comments

Comments
 (0)