Skip to content

Commit 22f6666

Browse files
committed
docs: Add comprehensive JSDoc comments to the NeuralNetwork class and its methods for improved code documentation.
1 parent 2edb4cd commit 22f6666

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

src/neural-network.js

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
1+
/**
2+
* NeuralNetwork class creates an animated particle system that simulates a neural network
3+
* with interconnected nodes and animated pulses traveling between them.
4+
* Particles move with physics-based motion and respond to mouse interaction.
5+
*/
16
class NeuralNetwork {
7+
/**
8+
* Creates a new NeuralNetwork instance and initializes the animation.
9+
* Sets up canvas, particles, event listeners, and starts the animation loop.
10+
*
11+
* @constructor
12+
*/
213
constructor() {
14+
/** @type {HTMLCanvasElement} Canvas element for rendering the neural network */
315
this.canvas = document.getElementById('neural-network-bg');
16+
17+
/** @type {CanvasRenderingContext2D} 2D rendering context for drawing */
418
this.ctx = this.canvas.getContext('2d');
19+
20+
/** @type {Array<Particle>} Array of particle objects representing network nodes */
521
this.particles = [];
6-
this.pulses = []; // Array to store active pulses
22+
23+
/** @type {Array<Pulse>} Array of active pulse objects traveling between particles */
24+
this.pulses = [];
25+
26+
/** @type {number} Number of particles to render (calculated based on canvas area) */
727
this.particleCount = 100;
28+
29+
/** @type {number} Maximum distance for drawing connections between particles (in pixels) */
830
this.connectionDistance = 150;
31+
32+
/** @type {number} Maximum distance for mouse interaction with particles (in pixels) */
933
this.mouseDistance = 200;
1034

35+
/**
36+
* @type {{x: number|null, y: number|null}}
37+
* Current mouse position, null when mouse is outside canvas
38+
*/
1139
this.mouse = {
1240
x: null,
1341
y: null,
@@ -21,17 +49,44 @@ class NeuralNetwork {
2149
window.addEventListener('mouseout', () => this.handleMouseOut());
2250
}
2351

52+
/**
53+
* Initializes the neural network by setting canvas size and creating particles.
54+
* Called once during construction.
55+
*
56+
* @returns {void}
57+
*/
2458
init() {
2559
this.resize();
2660
this.createParticles();
2761
}
2862

63+
/**
64+
* Handles window resize events by adjusting canvas dimensions and recreating particles
65+
* to maintain consistent particle density across different screen sizes.
66+
*
67+
* @returns {void}
68+
*/
2969
resize() {
3070
this.canvas.width = window.innerWidth;
3171
this.canvas.height = window.innerHeight;
3272
this.createParticles(); // Recreate particles on resize to maintain density
3373
}
3474

75+
/**
76+
* Creates particles with random positions and velocities.
77+
* Particle count is calculated based on canvas area to maintain consistent density.
78+
* Each particle has position (x, y), velocity (vx, vy), size, and color.
79+
*
80+
* @typedef {Object} Particle
81+
* @property {number} x - X coordinate position
82+
* @property {number} y - Y coordinate position
83+
* @property {number} vx - X velocity component
84+
* @property {number} vy - Y velocity component
85+
* @property {number} size - Particle radius
86+
* @property {string} color - Particle color (hex format)
87+
*
88+
* @returns {void}
89+
*/
3590
createParticles() {
3691
this.particles = [];
3792
// Reduced density for a cleaner look (larger divisor)
@@ -51,16 +106,44 @@ class NeuralNetwork {
51106
}
52107
}
53108

109+
/**
110+
* Handles mouse move events and updates the mouse position for particle interaction.
111+
*
112+
* @param {MouseEvent} e - The mouse event object
113+
* @returns {void}
114+
*/
54115
handleMouseMove(e) {
55116
this.mouse.x = e.x;
56117
this.mouse.y = e.y;
57118
}
58119

120+
/**
121+
* Handles mouse out events by resetting mouse position to null,
122+
* disabling particle interaction when mouse leaves the window.
123+
*
124+
* @returns {void}
125+
*/
59126
handleMouseOut() {
60127
this.mouse.x = null;
61128
this.mouse.y = null;
62129
}
63130

131+
/**
132+
* Main animation loop that updates and renders all particles, connections, and pulses.
133+
* Handles particle physics (movement, boundary collision), mouse interaction,
134+
* connection drawing, pulse spawning, and pulse animation.
135+
* Uses requestAnimationFrame for smooth 60fps animation.
136+
*
137+
* @typedef {Object} Pulse
138+
* @property {number} x - Starting X coordinate
139+
* @property {number} y - Starting Y coordinate
140+
* @property {number} targetX - Target X coordinate
141+
* @property {number} targetY - Target Y coordinate
142+
* @property {number} progress - Animation progress (0 to 1)
143+
* @property {number} speed - Speed of pulse movement per frame
144+
*
145+
* @returns {void}
146+
*/
64147
animate() {
65148
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
66149

@@ -157,7 +240,11 @@ class NeuralNetwork {
157240
}
158241
}
159242

160-
// Initialize when DOM is loaded
243+
/**
244+
* Initializes the NeuralNetwork animation when the DOM is fully loaded.
245+
* This ensures all DOM elements (especially the canvas) are available before
246+
* attempting to create the neural network visualization.
247+
*/
161248
document.addEventListener('DOMContentLoaded', () => {
162249
new NeuralNetwork();
163250
});

0 commit comments

Comments
 (0)