Skip to content

Commit 89211aa

Browse files
committed
Add effectful cleanup
1 parent ae41324 commit 89211aa

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/App.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { main } from "./lava/app"
1+
import { startLavaAnimation } from "./lava/app"
22
import { createEffect } from 'solid-js';
33

44
function App() {
5-
createEffect(() => {
6-
main()
7-
})
5+
createEffect(() => startLavaAnimation())
86

97
return (
108
<>
11-
{/* Background */}
9+
{/* Background. Mounting point for lava animation */}
1210
<canvas id="background" class="fixed inset-0 w-screen h-screen" />
1311

1412
{/* Content */}

src/lava/app.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import * as TWGL from 'twgl.js';
33
import vertexShader from './glsl/vertex.glsl';
44
import fragmentShader from './glsl/fragment.glsl';
55

6-
export function main() {
6+
/**
7+
* Initializes and starts a WebGL-based lava animation in the background canvas.
8+
* The animation uses spherical coordinates to position the camera and renders
9+
* a full-screen quad with custom shaders.
10+
*
11+
* @returns {Function} A cleanup function that cancels the animation frame when called
12+
* @throws {Error} If WebGL context cannot be obtained or shader compilation fails
13+
*/
14+
export function startLavaAnimation() {
715
// Calculate camera position
816
// based on THREE.Vector3.setFromSphericalCoords(radius, phi, theta)
917
const DEG_TO_RAD = Math.PI / 180;
@@ -57,4 +65,7 @@ export function main() {
5765

5866
frameId = requestAnimationFrame(render);
5967
}
68+
69+
// Return cleanup function
70+
return () => cancelAnimationFrame(frameId);
6071
}

0 commit comments

Comments
 (0)