Skip to content

Commit 3437678

Browse files
committed
fix: improved star animations
1 parent b0783ff commit 3437678

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"dev": "concurrently \"pnpm run build:projects\" \"vite dev --host\"",
6+
"dev": "vite dev --host",
7+
"dev:projects": "concurrently \"pnpm run build:projects\" \"vite dev --host\"",
78
"dev:open": "concurrently \"pnpm run build:projects\" \"vite dev --host --open\"",
89
"build": "pnpm run build:projects && vite build",
910
"preview": "vite preview",
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
<script lang="ts">
2-
import { onDestroy, onMount } from 'svelte';
3-
import { cubicOut } from 'svelte/easing';
4-
import { tweened } from 'svelte/motion';
2+
import { onMount } from 'svelte';
53
64
// Randomly move the star around by tweening its x and y coordinates
75
// in an offset from its original position, using a sine wave to
86
// create a smooth, natural motion
7+
let data = { x: 0, y: 0, size: 0, opacity: 0 };
98
10-
let xTween = tweened(0, { duration: 1000, easing: cubicOut });
11-
let yTween = tweened(0, { duration: 1000, easing: cubicOut });
9+
// Create a random offset
10+
const offset = Math.random() * 5;
1211
13-
// Randomly change the star's size and opacity
14-
let size = tweened(Math.random() * 20 + 10, { duration: 1000, easing: cubicOut });
15-
let opacityTween = tweened(Math.random() * 0.5 + 0.5, { duration: 1000, easing: cubicOut });
12+
// Create a random speed
13+
// 5 - 10
14+
let speed = 0;
1615
17-
// Perform the movement and size/opacity changes on mount
18-
let lastTimeout;
16+
// Signed directions
17+
let directions = [-1, 1];
1918
20-
onMount(() => {
21-
function update() {
22-
xTween.set(Math.random() * 20 - 10);
23-
yTween.set(Math.random() * 20 - 10);
24-
opacityTween.set(Math.random() * 0.5 + 0.5);
25-
size.set(Math.random() * 20 + 10);
26-
27-
lastTimeout = setTimeout(update, Math.random() * 1000 + 1000);
19+
const render = () => {
20+
// Update the completion status
21+
if (speed === 0 || data.opacity < 0.001) {
22+
speed = Math.random() * 15 + 5;
23+
directions = [Math.random() * 2 - 1, Math.random() * 2 - 1];
2824
}
2925
30-
update();
31-
});
32-
onDestroy(() => clearTimeout(lastTimeout));
26+
// Update the data
27+
data = {
28+
x: directions[0] * Math.sin(Date.now() / 1000 + offset) * speed,
29+
y: directions[1] * Math.sin(Date.now() / 1000 + offset) * speed,
30+
size: Math.sin(Date.now() / 1000 + offset) * speed + 20,
31+
opacity: Math.sin(Date.now() / 1000 + offset) * 0.5 + 0.5,
32+
};
33+
34+
// Request the next frame
35+
requestAnimationFrame(render);
36+
};
37+
38+
onMount(() => requestAnimationFrame(render));
3339
</script>
3440

3541
<div class="relative">
36-
<div class="absolute" style="left: {$xTween}px; top: {$yTween}px; width: {$size}px; height: {$size}px; opacity: {$opacityTween};">
42+
<div class="absolute" style="left: {data.x}px; top: {data.y}px; width: {data.size}px; height: {data.size}px; opacity: {data.opacity};">
3743
<slot />
3844
</div>
3945
</div>

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</div>
4040

4141
<!-- Highlight - Blue Star (Above "with") -->
42-
<div class="absolute top-[8.5rem] left-0">
42+
<div class="absolute top-[7.5rem] left-0">
4343
<TwinklingStar>
4444
<svg width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg">
4545
<path

0 commit comments

Comments
 (0)