@guinetik/gcanvas 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/demos/coordinates.html +698 -0
  2. package/demos/cube3d.html +23 -0
  3. package/demos/demos.css +17 -3
  4. package/demos/dino.html +42 -0
  5. package/demos/fluid-simple.html +22 -0
  6. package/demos/fluid.html +37 -0
  7. package/demos/gameobjects.html +626 -0
  8. package/demos/index.html +19 -7
  9. package/demos/js/blob.js +18 -5
  10. package/demos/js/coordinates.js +840 -0
  11. package/demos/js/cube3d.js +789 -0
  12. package/demos/js/dino.js +1420 -0
  13. package/demos/js/fluid-simple.js +253 -0
  14. package/demos/js/fluid.js +527 -0
  15. package/demos/js/gameobjects.js +176 -0
  16. package/demos/js/plane3d.js +256 -0
  17. package/demos/js/platformer.js +1579 -0
  18. package/demos/js/sphere3d.js +229 -0
  19. package/demos/js/sprite.js +473 -0
  20. package/demos/js/tde/accretiondisk.js +65 -12
  21. package/demos/js/tde/blackholescene.js +2 -2
  22. package/demos/js/tde/config.js +2 -2
  23. package/demos/js/tde/index.js +152 -27
  24. package/demos/js/tde/lensedstarfield.js +32 -25
  25. package/demos/js/tde/tdestar.js +78 -98
  26. package/demos/js/tde/tidalstream.js +24 -8
  27. package/demos/plane3d.html +24 -0
  28. package/demos/platformer.html +43 -0
  29. package/demos/sphere3d.html +24 -0
  30. package/demos/sprite.html +18 -0
  31. package/docs/README.md +230 -222
  32. package/docs/api/FluidSystem.md +173 -0
  33. package/docs/concepts/architecture-overview.md +204 -204
  34. package/docs/concepts/coordinate-system.md +384 -0
  35. package/docs/concepts/rendering-pipeline.md +279 -279
  36. package/docs/concepts/shapes-vs-gameobjects.md +187 -0
  37. package/docs/concepts/two-layer-architecture.md +229 -229
  38. package/docs/fluid-dynamics.md +99 -0
  39. package/docs/getting-started/first-game.md +354 -354
  40. package/docs/getting-started/installation.md +175 -157
  41. package/docs/modules/collision/README.md +2 -2
  42. package/docs/modules/fluent/README.md +6 -6
  43. package/docs/modules/game/README.md +303 -303
  44. package/docs/modules/isometric-camera.md +2 -2
  45. package/docs/modules/isometric.md +1 -1
  46. package/docs/modules/painter/README.md +328 -328
  47. package/docs/modules/particle/README.md +3 -3
  48. package/docs/modules/shapes/README.md +221 -221
  49. package/docs/modules/shapes/base/euclidian.md +123 -123
  50. package/docs/modules/shapes/base/shape.md +262 -262
  51. package/docs/modules/shapes/base/transformable.md +243 -243
  52. package/docs/modules/state/README.md +2 -2
  53. package/docs/modules/util/README.md +1 -1
  54. package/docs/modules/util/camera3d.md +3 -3
  55. package/docs/modules/util/scene3d.md +1 -1
  56. package/package.json +3 -1
  57. package/readme.md +19 -5
  58. package/src/collision/collision.js +75 -0
  59. package/src/game/game.js +11 -5
  60. package/src/game/index.js +2 -1
  61. package/src/game/objects/index.js +3 -0
  62. package/src/game/objects/platformer-scene.js +411 -0
  63. package/src/game/objects/scene.js +14 -0
  64. package/src/game/objects/sprite.js +529 -0
  65. package/src/game/pipeline.js +20 -16
  66. package/src/game/systems/FluidSystem.js +835 -0
  67. package/src/game/systems/index.js +11 -0
  68. package/src/game/ui/button.js +39 -18
  69. package/src/game/ui/cursor.js +14 -0
  70. package/src/game/ui/fps.js +12 -4
  71. package/src/game/ui/index.js +2 -0
  72. package/src/game/ui/stepper.js +549 -0
  73. package/src/game/ui/theme.js +123 -0
  74. package/src/game/ui/togglebutton.js +9 -3
  75. package/src/game/ui/tooltip.js +11 -4
  76. package/src/io/input.js +75 -45
  77. package/src/io/mouse.js +44 -19
  78. package/src/io/touch.js +35 -12
  79. package/src/math/fluid.js +507 -0
  80. package/src/math/index.js +2 -0
  81. package/src/mixins/anchor.js +17 -7
  82. package/src/motion/tweenetik.js +16 -0
  83. package/src/shapes/cube3d.js +599 -0
  84. package/src/shapes/index.js +3 -0
  85. package/src/shapes/plane3d.js +687 -0
  86. package/src/shapes/sphere3d.js +75 -6
  87. package/src/util/camera2d.js +315 -0
  88. package/src/util/camera3d.js +218 -12
  89. package/src/util/index.js +1 -0
  90. package/src/webgl/shaders/plane-shaders.js +332 -0
  91. package/src/webgl/shaders/sphere-shaders.js +4 -2
  92. package/types/fluent.d.ts +361 -0
  93. package/types/game.d.ts +303 -0
  94. package/types/index.d.ts +144 -5
  95. package/types/math.d.ts +361 -0
  96. package/types/motion.d.ts +271 -0
  97. package/types/particle.d.ts +373 -0
  98. package/types/shapes.d.ts +107 -9
  99. package/types/util.d.ts +353 -0
  100. package/types/webgl.d.ts +109 -0
  101. package/disk_example.png +0 -0
  102. package/tde.png +0 -0
package/docs/README.md CHANGED
@@ -1,222 +1,230 @@
1
- # GCanvas Documentation
2
-
3
- > A minimalist 2D canvas rendering library built for learning, expression, and creative coding.
4
-
5
- GCanvas is a modular 2D rendering and game framework built on top of the HTML5 Canvas API. Inspired by the simplicity of p5.js and the composability of game engines.
6
-
7
- ## Quick Navigation
8
-
9
- | Section | Description |
10
- |---------|-------------|
11
- | [Getting Started](./getting-started/installation.md) | Installation and first steps |
12
- | [Concepts](./concepts/architecture-overview.md) | Core architecture and design |
13
- | [Shapes Module](./modules/shapes/README.md) | Drawing primitives and hierarchy |
14
- | [Game Module](./modules/game/README.md) | Game loop and GameObjects |
15
- | [Particle Module](./modules/particle/README.md) | High-performance particle systems |
16
- | [Util Module](./modules/util/README.md) | Camera3D, Scene3D, layouts |
17
- | [Collision Module](./modules/collision/README.md) | Collision detection and management |
18
- | [State Module](./modules/state/README.md) | State machines for entities and games |
19
- | [Painter Module](./modules/painter/README.md) | Low-level canvas API |
20
-
21
- ## Architecture Overview
22
-
23
- ```
24
- ┌─────────────────────────────────────────────────────────────┐
25
- │ GCanvas │
26
- ├─────────────────────────────────────────────────────────────┤
27
- │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
28
- │ │ Shapes │ │ Game │ │ Painter │ │
29
- │ │ (Drawing) │ │ (Lifecycle) │ │ (Canvas API) │ │
30
- │ └─────────────┘ └─────────────┘ └─────────────────────┘ │
31
- ├─────────────────────────────────────────────────────────────┤
32
- │ ┌───────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
33
- │ │ Collision │ │ State │ │ Motion │ │ IO │ ... │
34
- │ │ (Physics) │ │ (FSM) │ │ (Anim) │ │ (Input) │ │
35
- │ └───────────┘ └─────────┘ └─────────┘ └─────────┘ │
36
- └─────────────────────────────────────────────────────────────┘
37
- ```
38
-
39
- GCanvas is organized into **12 core modules**:
40
-
41
- | Module | Purpose |
42
- |--------|---------|
43
- | **[shapes](./modules/shapes/README.md)** | 40+ drawable primitives and shape classes |
44
- | **[game](./modules/game/README.md)** | Core game loop, Pipeline, GameObjects, Scenes |
45
- | **[particle](./modules/particle/README.md)** | High-performance particle systems with pooling |
46
- | **[collision](./modules/collision/README.md)** | Collision detection algorithms and group management |
47
- | **[state](./modules/state/README.md)** | State machines with lifecycle callbacks |
48
- | **[painter](./modules/painter/README.md)** | Low-level canvas drawing API |
49
- | **[util](./modules/util/README.md)** | Camera3D, Scene3D, Layout, Position utilities |
50
- | **motion** | Animation with Tweenetik and Motion patterns |
51
- | **io** | Input handling (Mouse, Keyboard, Touch, Events) |
52
- | **math** | Random, Noise, Fractals, Patterns |
53
- | **mixins** | Draggable, Anchor behaviors |
54
- | **logger** | Debug logging system |
55
-
56
- ## Two-Layer Architecture
57
-
58
- GCanvas provides two complementary ways to work:
59
-
60
- ### Shape Layer (Declarative Drawing)
61
-
62
- For static visuals and simple graphics. Use shapes directly without a game loop:
63
-
64
- ```js
65
- import { Circle, Rectangle, Painter } from 'gcanvas';
66
-
67
- Painter.init(ctx);
68
-
69
- const circle = new Circle(100, { x: 200, y: 150, color: 'red' });
70
- circle.draw();
71
- ```
72
-
73
- ### Game Layer (Interactive Entities)
74
-
75
- For games, simulations, and interactive applications:
76
-
77
- ```js
78
- import { Game, Scene, GameObject, Circle } from 'gcanvas';
79
-
80
- class Player extends GameObject {
81
- constructor(game) {
82
- super(game);
83
- this.shape = new Circle(40, { color: 'blue' });
84
- this.enableInteractivity(this.shape);
85
- }
86
-
87
- update(dt) {
88
- // Game logic here
89
- }
90
-
91
- render() {
92
- this.shape.draw();
93
- }
94
- }
95
- ```
96
-
97
- [Learn more about the Two-Layer Architecture](./concepts/two-layer-architecture.md)
98
-
99
- ## The Rendering Pipeline
100
-
101
- Every visual element inherits from a chain of base classes:
102
-
103
- ```
104
- Euclidian ─── Position (x, y) and size (width, height)
105
-
106
- Geometry2d ─── Bounding boxes and constraints
107
-
108
- Traceable ─── Debug visualization
109
-
110
- Renderable ─── Visibility, opacity, shadows
111
-
112
- Transformable ─── Rotation and scaling
113
-
114
- Shape ─── Fill color, stroke, line styling
115
-
116
- [Circle, Rectangle, Star, Cube, ...] ─── Concrete implementations
117
- ```
118
-
119
- [Learn more about the Rendering Pipeline](./concepts/rendering-pipeline.md)
120
-
121
- ## Quick Start
122
-
123
- ### Installation
124
-
125
- ```bash
126
- git clone https://github.com/guinetik/gcanvas.git
127
- cd gcanvas
128
- npm install
129
- npm run dev
130
- ```
131
-
132
- ### Hello World
133
-
134
- ```html
135
- <canvas id="game"></canvas>
136
- <script type="module">
137
- import { Game, Scene, Rectangle, TextShape, Group } from 'gcanvas';
138
-
139
- class HelloWorld extends Game {
140
- init() {
141
- this.enableFluidSize();
142
- this.backgroundColor = 'black';
143
-
144
- const box = new Rectangle(200, 80, {
145
- color: '#111',
146
- stroke: '#0f0',
147
- lineWidth: 2
148
- });
149
-
150
- const label = new TextShape('Hello World!', {
151
- font: '18px monospace',
152
- color: '#0f0',
153
- align: 'center',
154
- baseline: 'middle'
155
- });
156
-
157
- const group = new Group({ x: this.width / 2, y: this.height / 2 });
158
- group.add(box);
159
- group.add(label);
160
-
161
- const scene = new Scene(this);
162
- scene.add(group);
163
- this.pipeline.add(scene);
164
- }
165
- }
166
-
167
- const game = new HelloWorld(document.getElementById('game'));
168
- game.start();
169
- </script>
170
- ```
171
-
172
- [Full getting started guide](./getting-started/hello-world.md)
173
-
174
- ## Features
175
-
176
- - **40+ Shape Primitives** - Circle, Rectangle, Star, Polygon, Heart, and more
177
- - **2.5D Shapes** - Cube, Cylinder, Sphere, Cone, Prism with pseudo-3D rendering
178
- - **Groups** - Composite shapes with collective transforms
179
- - **Transforms** - Rotation, scale, opacity, constraints
180
- - **Painter API** - Direct canvas control when needed
181
- - **GameObjects** - Interactive entities with lifecycle methods
182
- - **Scenes & Scene3D** - Hierarchical organization with optional 3D projection
183
- - **Camera3D** - Pseudo-3D projection with mouse-controlled rotation
184
- - **Particle Systems** - High-performance particles with object pooling and composable updaters
185
- - **Collision Detection** - AABB, circles, lines, sweep tests, and group management
186
- - **State Machines** - FSM with enter/update/exit lifecycle, timed transitions
187
- - **UI Components** - Button, ToggleButton, Cursor, Layout managers
188
- - **Motion System** - Stateless animation patterns (orbit, bounce, spiral...)
189
- - **Tweenetik** - Property-based tweening with easing
190
- - **Event System** - Mouse, touch, keyboard with unified input
191
- - **Zero Dependencies** - Pure JavaScript, works everywhere
192
-
193
- ## Demo
194
-
195
- See GCanvas in action: [gcanvas.guinetik.com](https://gcanvas.guinetik.com)
196
-
197
- Or run locally:
198
-
199
- ```bash
200
- npm run dev
201
- ```
202
-
203
- ## API Documentation
204
-
205
- - [Shapes Module](./modules/shapes/README.md) - All drawable primitives
206
- - [Game Module](./modules/game/README.md) - Game loop and objects
207
- - [Particle Module](./modules/particle/README.md) - Particle systems
208
- - [Util Module](./modules/util/README.md) - Camera3D, Scene3D, layouts
209
- - [Collision Module](./modules/collision/README.md) - Collision detection
210
- - [State Module](./modules/state/README.md) - State machines
211
- - [Painter Module](./modules/painter/README.md) - Canvas abstraction
212
-
213
- ## Learn More
214
-
215
- - [Architecture Overview](./concepts/architecture-overview.md)
216
- - [Rendering Pipeline](./concepts/rendering-pipeline.md)
217
- - [Game Lifecycle](./concepts/lifecycle.md)
218
- - [Installation Guide](./getting-started/installation.md)
219
-
220
- ---
221
-
222
- **Source:** [github.com/guinetik/gcanvas](https://github.com/guinetik/gcanvas)
1
+ # GCanvas Documentation
2
+
3
+ > A minimalist 2D canvas rendering library built for learning, expression, and creative coding.
4
+
5
+ GCanvas is a modular 2D rendering and game framework built on top of the HTML5 Canvas API. Inspired by the simplicity of p5.js and the composability of game engines.
6
+
7
+ ## Quick Navigation
8
+
9
+ | Section | Description |
10
+ |---------|-------------|
11
+ | [Getting Started](./getting-started/installation.md) | Installation and first steps |
12
+ | [Concepts](./concepts/architecture-overview.md) | Core architecture and design |
13
+ | [Shapes Module](./modules/shapes/README.md) | Drawing primitives and hierarchy |
14
+ | [Game Module](./modules/game/README.md) | Game loop and GameObjects |
15
+ | [Particle Module](./modules/particle/README.md) | High-performance particle systems |
16
+ | [Util Module](./modules/util/README.md) | Camera3D, Scene3D, layouts |
17
+ | [Collision Module](./modules/collision/README.md) | Collision detection and management |
18
+ | [State Module](./modules/state/README.md) | State machines for entities and games |
19
+ | [Painter Module](./modules/painter/README.md) | Low-level canvas API |
20
+
21
+ ## Architecture Overview
22
+
23
+ ```
24
+ ┌─────────────────────────────────────────────────────────────┐
25
+ │ GCanvas │
26
+ ├─────────────────────────────────────────────────────────────┤
27
+ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
28
+ │ │ Shapes │ │ Game │ │ Painter │ │
29
+ │ │ (Drawing) │ │ (Lifecycle) │ │ (Canvas API) │ │
30
+ │ └─────────────┘ └─────────────┘ └─────────────────────┘ │
31
+ ├─────────────────────────────────────────────────────────────┤
32
+ │ ┌───────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
33
+ │ │ Collision │ │ State │ │ Motion │ │ IO │ ... │
34
+ │ │ (Physics) │ │ (FSM) │ │ (Anim) │ │ (Input) │ │
35
+ │ └───────────┘ └─────────┘ └─────────┘ └─────────┘ │
36
+ └─────────────────────────────────────────────────────────────┘
37
+ ```
38
+
39
+ GCanvas is organized into **12 core modules**:
40
+
41
+ | Module | Purpose |
42
+ |--------|---------|
43
+ | **[shapes](./modules/shapes/README.md)** | 40+ drawable primitives and shape classes |
44
+ | **[game](./modules/game/README.md)** | Core game loop, Pipeline, GameObjects, Scenes |
45
+ | **[particle](./modules/particle/README.md)** | High-performance particle systems with pooling |
46
+ | **[collision](./modules/collision/README.md)** | Collision detection algorithms and group management |
47
+ | **[state](./modules/state/README.md)** | State machines with lifecycle callbacks |
48
+ | **[painter](./modules/painter/README.md)** | Low-level canvas drawing API |
49
+ | **[util](./modules/util/README.md)** | Camera3D, Scene3D, Layout, Position utilities |
50
+ | **motion** | Animation with Tweenetik and Motion patterns |
51
+ | **io** | Input handling (Mouse, Keyboard, Touch, Events) |
52
+ | **math** | Random, Noise, Fractals, Patterns |
53
+ | **mixins** | Draggable, Anchor behaviors |
54
+ | **logger** | Debug logging system |
55
+
56
+ ## Two-Layer Architecture
57
+
58
+ GCanvas provides two complementary ways to work:
59
+
60
+ ### Shape Layer (Declarative Drawing)
61
+
62
+ For static visuals and simple graphics. Use shapes directly without a game loop:
63
+
64
+ ```js
65
+ import { Circle, Rectangle, Painter } from '@guinetik/gcanvas';
66
+
67
+ Painter.init(ctx);
68
+
69
+ const circle = new Circle(100, { x: 200, y: 150, color: 'red' });
70
+ circle.draw();
71
+ ```
72
+
73
+ ### Game Layer (Interactive Entities)
74
+
75
+ For games, simulations, and interactive applications:
76
+
77
+ ```js
78
+ import { Game, Scene, GameObject, Circle } from '@guinetik/gcanvas';
79
+
80
+ class Player extends GameObject {
81
+ constructor(game) {
82
+ super(game);
83
+ this.shape = new Circle(40, { color: 'blue' });
84
+ this.enableInteractivity(this.shape);
85
+ }
86
+
87
+ update(dt) {
88
+ // Game logic here
89
+ }
90
+
91
+ render() {
92
+ this.shape.draw();
93
+ }
94
+ }
95
+ ```
96
+
97
+ [Learn more about the Two-Layer Architecture](./concepts/two-layer-architecture.md)
98
+
99
+ ## The Rendering Pipeline
100
+
101
+ Every visual element inherits from a chain of base classes:
102
+
103
+ ```
104
+ Euclidian ─── Position (x, y) and size (width, height)
105
+
106
+ Geometry2d ─── Bounding boxes and constraints
107
+
108
+ Traceable ─── Debug visualization
109
+
110
+ Renderable ─── Visibility, opacity, shadows
111
+
112
+ Transformable ─── Rotation and scaling
113
+
114
+ Shape ─── Fill color, stroke, line styling
115
+
116
+ [Circle, Rectangle, Star, Cube, ...] ─── Concrete implementations
117
+ ```
118
+
119
+ [Learn more about the Rendering Pipeline](./concepts/rendering-pipeline.md)
120
+
121
+ ## Quick Start
122
+
123
+ ### Installation
124
+
125
+ **NPM (Recommended):**
126
+
127
+ ```bash
128
+ npm install @guinetik/gcanvas
129
+ ```
130
+
131
+ **Or clone the repository:**
132
+
133
+ ```bash
134
+ git clone https://github.com/guinetik/gcanvas.git
135
+ cd gcanvas
136
+ npm install
137
+ npm run dev
138
+ ```
139
+
140
+ ### Hello World
141
+
142
+ ```html
143
+ <canvas id="game"></canvas>
144
+ <script type="module">
145
+ import { Game, Scene, Rectangle, TextShape, Group } from '@guinetik/gcanvas';
146
+
147
+ class HelloWorld extends Game {
148
+ init() {
149
+ this.enableFluidSize();
150
+ this.backgroundColor = 'black';
151
+
152
+ const box = new Rectangle(200, 80, {
153
+ color: '#111',
154
+ stroke: '#0f0',
155
+ lineWidth: 2
156
+ });
157
+
158
+ const label = new TextShape('Hello World!', {
159
+ font: '18px monospace',
160
+ color: '#0f0',
161
+ align: 'center',
162
+ baseline: 'middle'
163
+ });
164
+
165
+ const group = new Group({ x: this.width / 2, y: this.height / 2 });
166
+ group.add(box);
167
+ group.add(label);
168
+
169
+ const scene = new Scene(this);
170
+ scene.add(group);
171
+ this.pipeline.add(scene);
172
+ }
173
+ }
174
+
175
+ const game = new HelloWorld(document.getElementById('game'));
176
+ game.start();
177
+ </script>
178
+ ```
179
+
180
+ [Full getting started guide](./getting-started/hello-world.md)
181
+
182
+ ## Features
183
+
184
+ - **40+ Shape Primitives** - Circle, Rectangle, Star, Polygon, Heart, and more
185
+ - **2.5D Shapes** - Cube, Cylinder, Sphere, Cone, Prism with pseudo-3D rendering
186
+ - **Groups** - Composite shapes with collective transforms
187
+ - **Transforms** - Rotation, scale, opacity, constraints
188
+ - **Painter API** - Direct canvas control when needed
189
+ - **GameObjects** - Interactive entities with lifecycle methods
190
+ - **Scenes & Scene3D** - Hierarchical organization with optional 3D projection
191
+ - **Camera3D** - Pseudo-3D projection with mouse-controlled rotation
192
+ - **Particle Systems** - High-performance particles with object pooling and composable updaters
193
+ - **Collision Detection** - AABB, circles, lines, sweep tests, and group management
194
+ - **State Machines** - FSM with enter/update/exit lifecycle, timed transitions
195
+ - **UI Components** - Button, ToggleButton, Cursor, Layout managers
196
+ - **Motion System** - Stateless animation patterns (orbit, bounce, spiral...)
197
+ - **Tweenetik** - Property-based tweening with easing
198
+ - **Event System** - Mouse, touch, keyboard with unified input
199
+ - **Zero Dependencies** - Pure JavaScript, works everywhere
200
+
201
+ ## Demo
202
+
203
+ See GCanvas in action: [gcanvas.guinetik.com](https://gcanvas.guinetik.com)
204
+
205
+ Or run locally:
206
+
207
+ ```bash
208
+ npm run dev
209
+ ```
210
+
211
+ ## API Documentation
212
+
213
+ - [Shapes Module](./modules/shapes/README.md) - All drawable primitives
214
+ - [Game Module](./modules/game/README.md) - Game loop and objects
215
+ - [Particle Module](./modules/particle/README.md) - Particle systems
216
+ - [Util Module](./modules/util/README.md) - Camera3D, Scene3D, layouts
217
+ - [Collision Module](./modules/collision/README.md) - Collision detection
218
+ - [State Module](./modules/state/README.md) - State machines
219
+ - [Painter Module](./modules/painter/README.md) - Canvas abstraction
220
+
221
+ ## Learn More
222
+
223
+ - [Architecture Overview](./concepts/architecture-overview.md)
224
+ - [Rendering Pipeline](./concepts/rendering-pipeline.md)
225
+ - [Game Lifecycle](./concepts/lifecycle.md)
226
+ - [Installation Guide](./getting-started/installation.md)
227
+
228
+ ---
229
+
230
+ **Source:** [github.com/guinetik/gcanvas](https://github.com/guinetik/gcanvas)
@@ -0,0 +1,173 @@
1
+ # FluidSystem
2
+
3
+ High-level fluid simulation system built on `ParticleSystem`.
4
+
5
+ Integrates SPH physics, collision detection, and boundary handling into a cohesive, configurable fluid simulation.
6
+
7
+ ## Import
8
+
9
+ ```javascript
10
+ import { FluidSystem } from "gcanvas";
11
+ ```
12
+
13
+ ## Basic Usage
14
+
15
+ ```javascript
16
+ // Create a liquid simulation
17
+ const fluid = new FluidSystem(game, {
18
+ maxParticles: 500,
19
+ particleSize: 20,
20
+ bounds: { x: 50, y: 50, w: 700, h: 500 },
21
+ physics: 'liquid',
22
+ });
23
+
24
+ // Spawn particles
25
+ fluid.spawn(300);
26
+
27
+ // Add to game pipeline
28
+ game.pipeline.add(fluid);
29
+ ```
30
+
31
+ ## Constructor Options
32
+
33
+ | Option | Type | Default | Description |
34
+ |--------|------|---------|-------------|
35
+ | `maxParticles` | number | 500 | Maximum particle count |
36
+ | `particleSize` | number | 20 | Base particle diameter |
37
+ | `bounds` | Object | null | Containment bounds `{ x, y, w, h }` |
38
+ | `physics` | string | 'liquid' | Physics mode: `'liquid'`, `'gas'`, or `'blend'` |
39
+ | `gravity` | number | 200 | Gravity strength |
40
+ | `gravityEnabled` | boolean | true | Whether gravity is active |
41
+ | `damping` | number | 0.98 | Velocity damping per frame |
42
+ | `bounce` | number | 0.3 | Bounce factor on collision |
43
+ | `maxSpeed` | number | 400 | Maximum particle speed |
44
+
45
+ ### Fluid Sub-Options (`options.fluid`)
46
+
47
+ | Option | Type | Default | Description |
48
+ |--------|------|---------|-------------|
49
+ | `smoothingRadius` | number | particleSize * 2 | SPH smoothing radius |
50
+ | `restDensity` | number | 3.0 | Target density |
51
+ | `pressureStiffness` | number | 80 | Pressure force multiplier |
52
+ | `nearPressureStiffness` | number | 3 | Near pressure multiplier |
53
+ | `viscosity` | number | 0.005 | Viscosity strength |
54
+
55
+ ### Collision Sub-Options (`options.collision`)
56
+
57
+ | Option | Type | Default | Description |
58
+ |--------|------|---------|-------------|
59
+ | `enabled` | boolean | true | Enable particle collision |
60
+ | `strength` | number | 5000 | Collision repulsion strength |
61
+
62
+ ### Boundary Sub-Options (`options.boundary`)
63
+
64
+ | Option | Type | Default | Description |
65
+ |--------|------|---------|-------------|
66
+ | `enabled` | boolean | true | Enable boundary forces |
67
+ | `strength` | number | 4000 | Boundary repulsion strength |
68
+ | `radius` | number | particleSize * 0.8 | Boundary force range |
69
+
70
+ ## Methods
71
+
72
+ ### spawn(count, options?)
73
+
74
+ Spawn particles within the bounds or at a specified position.
75
+
76
+ ```javascript
77
+ // Spawn at bounds center (default)
78
+ fluid.spawn(200);
79
+
80
+ // Spawn at specific position
81
+ fluid.spawn(100, { x: 400, y: 300, spreadX: 50, spreadY: 50 });
82
+ ```
83
+
84
+ ### setBounds(bounds)
85
+
86
+ Update containment bounds.
87
+
88
+ ```javascript
89
+ fluid.setBounds({ x: 100, y: 100, w: 600, h: 400 });
90
+ ```
91
+
92
+ ### reset()
93
+
94
+ Reset all particles to initial spawn positions.
95
+
96
+ ```javascript
97
+ fluid.reset();
98
+ ```
99
+
100
+ ### toggleGravity()
101
+
102
+ Toggle gravity on/off. Returns new state.
103
+
104
+ ```javascript
105
+ const gravityOn = fluid.toggleGravity();
106
+ ```
107
+
108
+ ### setPhysicsMode(mode)
109
+
110
+ Switch physics mode.
111
+
112
+ ```javascript
113
+ fluid.setPhysicsMode('liquid'); // SPH fluid
114
+ fluid.setPhysicsMode('gas'); // Gas dynamics
115
+ fluid.setPhysicsMode(0.5); // 50% blend
116
+ ```
117
+
118
+ ## Properties
119
+
120
+ | Property | Type | Description |
121
+ |----------|------|-------------|
122
+ | `particles` | Array | Live particle array |
123
+ | `bounds` | Object | Current bounds `{ x, y, w, h }` |
124
+ | `gravityEnabled` | boolean | Gravity state |
125
+ | `physicsBlend` | number | 0 = liquid, 1 = gas |
126
+ | `config` | Object | Merged configuration |
127
+
128
+ ## Example: Complete Demo
129
+
130
+ ```javascript
131
+ import { Game, FluidSystem, FPSCounter } from "gcanvas";
132
+
133
+ class FluidDemo extends Game {
134
+ init() {
135
+ super.init();
136
+
137
+ // Create fluid system
138
+ this.fluid = new FluidSystem(this, {
139
+ maxParticles: 400,
140
+ particleSize: 25,
141
+ bounds: { x: 50, y: 50, w: this.width - 100, h: this.height - 100 },
142
+ physics: 'liquid',
143
+ gravity: 200,
144
+ });
145
+
146
+ this.fluid.spawn(400);
147
+ this.pipeline.add(this.fluid);
148
+ this.pipeline.add(new FPSCounter(this));
149
+
150
+ // Keyboard controls
151
+ window.addEventListener('keydown', (e) => {
152
+ if (e.key === 'g') this.fluid.toggleGravity();
153
+ if (e.key === 'r') this.fluid.reset();
154
+ });
155
+ }
156
+ }
157
+ ```
158
+
159
+ ## Integration with Existing Code
160
+
161
+ FluidSystem reuses existing modules:
162
+
163
+ - `ParticleSystem` - Base particle management
164
+ - `Collision.applyCircleSeparation()` - Particle-particle collision
165
+ - `computeFluidForces()` - SPH physics from `math/fluid.js`
166
+ - `computeGasForces()` - Gas dynamics from `math/fluid.js`
167
+
168
+ ## Performance Notes
169
+
170
+ - O(n²) collision detection is used for simplicity
171
+ - For >1000 particles, consider spatial hashing optimization
172
+ - The `blendMode: 'source-over'` default prevents washed-out colors
173
+