Skip to content

Commit 0ad498a

Browse files
committed
feat: add lines background animation
1 parent c4b796d commit 0ad498a

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/components/Lines.svelte

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<script>
2+
import { onMount } from "svelte";
3+
4+
const { height = $bindable(0), width = $bindable(0), ...rest } = $props();
5+
6+
let perHeight = $derived(Math.floor(height / 4));
7+
let perWidth = $derived(Math.floor(width / 4));
8+
9+
let halfHeight = $derived(perHeight * 2);
10+
11+
/** @type {HTMLCanvasElement} */
12+
let canvas;
13+
/** @type {number} */
14+
let frame;
15+
16+
let offset = $state(0);
17+
18+
onMount(() => {
19+
const context = canvas.getContext("2d");
20+
context.strokeStyle = "white";
21+
22+
function animate() {
23+
context.clearRect(0, 0, width, height);
24+
25+
context.setLineDash([width, width]);
26+
offset = offset - 5;
27+
context.lineDashOffset = offset;
28+
29+
context.beginPath();
30+
31+
context.moveTo(0, halfHeight);
32+
context.lineTo(perWidth, halfHeight);
33+
context.lineTo(perWidth * 2, perHeight);
34+
context.lineTo(perWidth * 3, halfHeight + perHeight);
35+
36+
context.lineTo(width, halfHeight);
37+
context.stroke();
38+
frame = requestAnimationFrame(animate);
39+
}
40+
41+
animate();
42+
43+
return () => cancelAnimationFrame(frame);
44+
});
45+
</script>
46+
47+
<canvas bind:this={canvas} {height} {width} class="fixed mx-auto"></canvas>

src/routes/+layout.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<script>
22
import "$styles/index.css";
33
import Navbar from "$components/Navbar.svelte";
4+
import Lines from "$components/Lines.svelte";
45
56
let { children } = $props();
7+
8+
let innerHeight = $state(0);
9+
let innerWidth = $state(0);
610
</script>
711

812
<svelte:head>
913
<link rel="icon" href="/favicon.png" />
1014
</svelte:head>
1115

16+
<svelte:window bind:innerHeight bind:innerWidth />
17+
1218
<Navbar />
1319

1420
<main class="grow bg-neutral-950 p-4 py-16 text-white">
15-
<div class="mx-auto w-full max-w-3xl animate-up">
21+
<Lines width={innerWidth - 50} height={innerHeight} />
22+
<div class="relative mx-auto w-full max-w-3xl animate-up">
1623
{@render children()}
1724
</div>
1825
</main>

0 commit comments

Comments
 (0)