Skip to content

Commit 8ed33f1

Browse files
committed
fix: improve foam particle generation and prevent memory leaks in sky gradient buffer
1 parent 9883fab commit 8ed33f1

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

src/animations/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ describe('OceanWaves', () => {
408408

409409
## File Naming Conventions
410410

411-
```
411+
```text
412412
oceanWaves.ts # Business logic utilities
413413
oceanWaves.test.ts # Unit tests for utilities
414414
OceanWaves.svelte # Svelte component (PascalCase)

src/animations/oceanWaves.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ describe('getFoamColor', () => {
281281

282282
describe('generateFoamParticles', () => {
283283
// Mock Perlin noise function with predictable values
284-
const mockNoise = (x: number, y: number, z?: number): number => {
284+
const mockNoise = (x: number, y: number, z: number): number => {
285285
// Simple deterministic function for testing
286-
return (Math.sin(x * 10 + (y || 0) * 5 + (z || 0)) + 1) / 2; // Returns 0-1
286+
return (Math.sin(x * 10 + y * 5 + z) + 1) / 2; // Returns 0-1
287287
};
288288

289289
test('returns array of foam particles', () => {

src/animations/oceanWaves.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,17 @@ export function generateFoamParticles(
313313
const offsetY =
314314
(noiseFunc(
315315
x * FOAM_NOISE_X_SCALE + particle * FOAM_NOISE_Y_OFFSET,
316-
time * FOAM_NOISE_TIME_SCALE
316+
time * FOAM_NOISE_TIME_SCALE,
317+
layer * FOAM_NOISE_LAYER_OFFSET + particle
317318
) - 0.5) * FOAM_VERTICAL_SPREAD;
318319

319320
// Vary particle size (small particles)
320321
const size =
321322
FOAM_MIN_SIZE +
322323
noiseFunc(
323324
particle * FOAM_NOISE_SIZE_OFFSET,
324-
time * FOAM_NOISE_SIZE_TIME_SCALE
325+
time * FOAM_NOISE_SIZE_TIME_SCALE,
326+
layer * FOAM_NOISE_LAYER_OFFSET
325327
) * FOAM_MAX_SIZE_VARIATION;
326328

327329
// Create dissipation effect - particles further from center fade more

src/components/apps/OceanWaves.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@
184184
185185
// Create sky gradient as off-screen buffer (called once and on resize)
186186
function createSkyGradient(p: p5, w: number, h: number) {
187+
// Clean up existing buffer to prevent memory leaks
188+
if (skyGradientBuffer) {
189+
skyGradientBuffer.remove();
190+
skyGradientBuffer = null;
191+
}
187192
skyGradientBuffer = p.createGraphics(w, h);
188193
189194
// Create colors once outside the loop

0 commit comments

Comments
 (0)