@guinetik/gcanvas 1.0.0 → 1.0.1
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.
- package/demos/fluid-simple.html +22 -0
- package/demos/fluid.html +37 -0
- package/demos/index.html +2 -0
- package/demos/js/blob.js +18 -5
- package/demos/js/fluid-simple.js +253 -0
- package/demos/js/fluid.js +527 -0
- package/demos/js/tde/accretiondisk.js +64 -11
- package/demos/js/tde/blackholescene.js +2 -2
- package/demos/js/tde/config.js +2 -2
- package/demos/js/tde/index.js +152 -27
- package/demos/js/tde/lensedstarfield.js +32 -25
- package/demos/js/tde/tdestar.js +78 -98
- package/demos/js/tde/tidalstream.js +23 -7
- package/docs/README.md +230 -222
- package/docs/api/FluidSystem.md +173 -0
- package/docs/concepts/architecture-overview.md +204 -204
- package/docs/concepts/rendering-pipeline.md +279 -279
- package/docs/concepts/two-layer-architecture.md +229 -229
- package/docs/fluid-dynamics.md +97 -0
- package/docs/getting-started/first-game.md +354 -354
- package/docs/getting-started/installation.md +175 -157
- package/docs/modules/collision/README.md +2 -2
- package/docs/modules/fluent/README.md +6 -6
- package/docs/modules/game/README.md +303 -303
- package/docs/modules/isometric-camera.md +2 -2
- package/docs/modules/isometric.md +1 -1
- package/docs/modules/painter/README.md +328 -328
- package/docs/modules/particle/README.md +3 -3
- package/docs/modules/shapes/README.md +221 -221
- package/docs/modules/shapes/base/euclidian.md +123 -123
- package/docs/modules/shapes/base/shape.md +262 -262
- package/docs/modules/shapes/base/transformable.md +243 -243
- package/docs/modules/state/README.md +2 -2
- package/docs/modules/util/README.md +1 -1
- package/docs/modules/util/camera3d.md +3 -3
- package/docs/modules/util/scene3d.md +1 -1
- package/package.json +3 -1
- package/readme.md +19 -5
- package/src/collision/collision.js +75 -0
- package/src/game/index.js +2 -1
- package/src/game/pipeline.js +3 -3
- package/src/game/systems/FluidSystem.js +835 -0
- package/src/game/systems/index.js +11 -0
- package/src/game/ui/button.js +39 -18
- package/src/game/ui/cursor.js +14 -0
- package/src/game/ui/fps.js +12 -4
- package/src/game/ui/index.js +2 -0
- package/src/game/ui/stepper.js +549 -0
- package/src/game/ui/theme.js +121 -0
- package/src/game/ui/togglebutton.js +9 -3
- package/src/game/ui/tooltip.js +11 -4
- package/src/math/fluid.js +507 -0
- package/src/math/index.js +2 -0
- package/src/mixins/anchor.js +17 -7
- package/src/motion/tweenetik.js +16 -0
- package/src/shapes/index.js +1 -0
- package/src/util/camera3d.js +218 -12
- package/types/fluent.d.ts +361 -0
- package/types/game.d.ts +303 -0
- package/types/index.d.ts +144 -5
- package/types/math.d.ts +361 -0
- package/types/motion.d.ts +271 -0
- package/types/particle.d.ts +373 -0
- package/types/shapes.d.ts +107 -9
- package/types/util.d.ts +353 -0
- package/types/webgl.d.ts +109 -0
- package/disk_example.png +0 -0
- package/tde.png +0 -0
|
@@ -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
|
+
|
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
# Architecture Overview
|
|
2
|
-
|
|
3
|
-
> High-level system architecture, module relationships, and design philosophy.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
GCanvas is a modular 2D rendering and game framework built on top of the HTML5 Canvas API. It follows a layered architecture where each module has a clear responsibility and minimal coupling to other modules.
|
|
8
|
-
|
|
9
|
-
The library is designed around three core principles:
|
|
10
|
-
|
|
11
|
-
1. **Modularity** - Use only what you need
|
|
12
|
-
2. **Composability** - Combine simple parts to build complex systems
|
|
13
|
-
3. **Zero Dependencies** - Pure JavaScript, works everywhere
|
|
14
|
-
|
|
15
|
-
## Module Architecture
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
┌─────────────────────────────────────────────────────────────────┐
|
|
19
|
-
│ GCanvas │
|
|
20
|
-
│ (index.js) │
|
|
21
|
-
├─────────────────────────────────────────────────────────────────┤
|
|
22
|
-
│ │
|
|
23
|
-
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
24
|
-
│ │ Core Modules │ │
|
|
25
|
-
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │ │
|
|
26
|
-
│ │ │ shapes/ │ │ game/ │ │ painter/ │ │ │
|
|
27
|
-
│ │ │ 40+ shapes │ │ Game loop │ │ Canvas API │ │ │
|
|
28
|
-
│ │ │ hierarchy │ │ Pipeline │ │ abstraction │ │ │
|
|
29
|
-
│ │ │ Group │ │ GameObject │ │ │ │ │
|
|
30
|
-
│ │ └─────────────┘ └─────────────┘ └─────────────────┘ │ │
|
|
31
|
-
│ └──────────────────────────────────────────────────────────┘ │
|
|
32
|
-
│ │
|
|
33
|
-
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
34
|
-
│ │ Support Modules │ │
|
|
35
|
-
│ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
|
|
36
|
-
│ │ │ motion │ │ io │ │ math │ │ util │ │ mixins │ │ │
|
|
37
|
-
│ │ │ Tween │ │ Events │ │ Random │ │ Layout │ │Draggable│ │ │
|
|
38
|
-
│ │ │ Easing │ │ Mouse │ │ Noise │ │Position│ │ Anchor │ │ │
|
|
39
|
-
│ │ │ Motion │ │ Keys │ │Fractals│ │ZIndex │ │ │ │ │
|
|
40
|
-
│ │ │Tweenetik│ │ Touch │ │Patterns│ │ Tasks │ │ │ │ │
|
|
41
|
-
│ │ └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ │ │
|
|
42
|
-
│ └──────────────────────────────────────────────────────────┘ │
|
|
43
|
-
│ │
|
|
44
|
-
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
45
|
-
│ │ Utility Modules │ │
|
|
46
|
-
│ │ ┌────────┐ │ │
|
|
47
|
-
│ │ │ logger │ │ │
|
|
48
|
-
│ │ │ Logger │ │ │
|
|
49
|
-
│ │ │Loggable│ │ │
|
|
50
|
-
│ │ │DebugTab│ │ │
|
|
51
|
-
│ │ └────────┘ │ │
|
|
52
|
-
│ └──────────────────────────────────────────────────────────┘ │
|
|
53
|
-
│ │
|
|
54
|
-
└─────────────────────────────────────────────────────────────────┘
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Module Responsibilities
|
|
58
|
-
|
|
59
|
-
### Core Modules
|
|
60
|
-
|
|
61
|
-
| Module | Responsibility | Key Classes |
|
|
62
|
-
|--------|---------------|-------------|
|
|
63
|
-
| **shapes** | Visual primitives and rendering | `Shape`, `Circle`, `Rectangle`, `Group`, `Transformable` |
|
|
64
|
-
| **game** | Game loop, lifecycle, object management | `Game`, `Pipeline`, `GameObject`, `Scene` |
|
|
65
|
-
| **painter** | Canvas context abstraction | `Painter`, `PainterShapes`, `PainterText` |
|
|
66
|
-
|
|
67
|
-
### Support Modules
|
|
68
|
-
|
|
69
|
-
| Module | Responsibility | Key Classes |
|
|
70
|
-
|--------|---------------|-------------|
|
|
71
|
-
| **motion** | Animation and tweening | `Motion`, `Tweenetik`, `Easing`, `Tween` |
|
|
72
|
-
| **io** | Input handling | `EventEmitter`, `Mouse`, `Keys`, `Touch`, `Input` |
|
|
73
|
-
| **math** | Mathematical utilities | `Random`, `Noise`, `Complex`, `Fractals`, `Patterns` |
|
|
74
|
-
| **util** | Layout and positioning | `Layout`, `Position`, `ZOrderedCollection` |
|
|
75
|
-
| **mixins** | Composable behaviors | `applyDraggable()`, `applyAnchor()` |
|
|
76
|
-
|
|
77
|
-
### Utility Modules
|
|
78
|
-
|
|
79
|
-
| Module | Responsibility | Key Classes |
|
|
80
|
-
|--------|---------------|-------------|
|
|
81
|
-
| **logger** | Debug and logging | `Logger`, `Loggable`, `DebugTab` |
|
|
82
|
-
|
|
83
|
-
## Data Flow
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
User Input Game Loop
|
|
87
|
-
│ │
|
|
88
|
-
▼ ▼
|
|
89
|
-
┌────────┐ events ┌────────┐
|
|
90
|
-
│ IO │ ──────────────────►│ Game │
|
|
91
|
-
└────────┘ └────────┘
|
|
92
|
-
│
|
|
93
|
-
┌──────────────┼──────────────┐
|
|
94
|
-
▼ ▼ ▼
|
|
95
|
-
┌──────────┐ ┌──────────┐ ┌──────────┐
|
|
96
|
-
│ Pipeline │ │ Tweenetik│ │ Painter │
|
|
97
|
-
│ update() │ │ update() │ │ clear() │
|
|
98
|
-
└──────────┘ └──────────┘ └──────────┘
|
|
99
|
-
│
|
|
100
|
-
▼
|
|
101
|
-
┌──────────┐
|
|
102
|
-
│GameObject│
|
|
103
|
-
│ update() │
|
|
104
|
-
│ render() │
|
|
105
|
-
└──────────┘
|
|
106
|
-
│
|
|
107
|
-
▼
|
|
108
|
-
┌──────────┐
|
|
109
|
-
│ Shape │
|
|
110
|
-
│ draw() │
|
|
111
|
-
└──────────┘
|
|
112
|
-
│
|
|
113
|
-
▼
|
|
114
|
-
┌──────────┐
|
|
115
|
-
│ Painter │
|
|
116
|
-
│ Canvas │
|
|
117
|
-
└──────────┘
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
## Design Patterns
|
|
121
|
-
|
|
122
|
-
### 1. Inheritance Chain (Shapes)
|
|
123
|
-
|
|
124
|
-
Shapes use a linear inheritance hierarchy where each class adds specific functionality:
|
|
125
|
-
|
|
126
|
-
```
|
|
127
|
-
Euclidian → Geometry2d → Traceable → Renderable → Transformable → Shape
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
See: [Rendering Pipeline](./rendering-pipeline.md)
|
|
131
|
-
|
|
132
|
-
### 2. Static Singleton (Painter)
|
|
133
|
-
|
|
134
|
-
`Painter` is a static utility class initialized once with the canvas context:
|
|
135
|
-
|
|
136
|
-
```js
|
|
137
|
-
Painter.init(ctx);
|
|
138
|
-
Painter.shapes.circle(100, 100, 50);
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### 3. Component Pattern (GameObject + Shape)
|
|
142
|
-
|
|
143
|
-
GameObjects compose shapes rather than inheriting from them:
|
|
144
|
-
|
|
145
|
-
```js
|
|
146
|
-
class Player extends GameObject {
|
|
147
|
-
constructor(game) {
|
|
148
|
-
super(game);
|
|
149
|
-
this.shape = new Circle(40, { color: 'blue' });
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### 4. Mixin Pattern (Behaviors)
|
|
155
|
-
|
|
156
|
-
Optional behaviors are applied via mixin functions:
|
|
157
|
-
|
|
158
|
-
```js
|
|
159
|
-
applyDraggable(gameObject, gameObject.shape);
|
|
160
|
-
applyAnchor(gameObject, { anchor: 'top-right' });
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### 5. Observer Pattern (Events)
|
|
164
|
-
|
|
165
|
-
The `EventEmitter` provides pub-sub event handling:
|
|
166
|
-
|
|
167
|
-
```js
|
|
168
|
-
game.events.on('click', (e) => { ... });
|
|
169
|
-
game.events.emit('custom-event', data);
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
## Entry Point
|
|
173
|
-
|
|
174
|
-
The main entry point (`src/index.js`) re-exports all modules:
|
|
175
|
-
|
|
176
|
-
```js
|
|
177
|
-
export * from "./util";
|
|
178
|
-
export * from "./math";
|
|
179
|
-
export * from "./logger";
|
|
180
|
-
export * from "./painter";
|
|
181
|
-
export * from "./shapes";
|
|
182
|
-
export * from "./io";
|
|
183
|
-
export * from "./game";
|
|
184
|
-
export * from "./motion";
|
|
185
|
-
export * from "./mixins";
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
This allows importing any component from the main package:
|
|
189
|
-
|
|
190
|
-
```js
|
|
191
|
-
import { Game, Circle, Painter, Motion } from 'gcanvas';
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
## Related
|
|
195
|
-
|
|
196
|
-
- [Two-Layer Architecture](./two-layer-architecture.md) - Shape vs Game layer
|
|
197
|
-
- [Rendering Pipeline](./rendering-pipeline.md) - Shape inheritance chain
|
|
198
|
-
- [Game Lifecycle](./lifecycle.md) - Update/render cycle
|
|
199
|
-
|
|
200
|
-
## See Also
|
|
201
|
-
|
|
202
|
-
- [Shapes Module](../modules/shapes/README.md)
|
|
203
|
-
- [Game Module](../modules/game/README.md)
|
|
204
|
-
- [Painter Module](../modules/painter/README.md)
|
|
1
|
+
# Architecture Overview
|
|
2
|
+
|
|
3
|
+
> High-level system architecture, module relationships, and design philosophy.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
GCanvas is a modular 2D rendering and game framework built on top of the HTML5 Canvas API. It follows a layered architecture where each module has a clear responsibility and minimal coupling to other modules.
|
|
8
|
+
|
|
9
|
+
The library is designed around three core principles:
|
|
10
|
+
|
|
11
|
+
1. **Modularity** - Use only what you need
|
|
12
|
+
2. **Composability** - Combine simple parts to build complex systems
|
|
13
|
+
3. **Zero Dependencies** - Pure JavaScript, works everywhere
|
|
14
|
+
|
|
15
|
+
## Module Architecture
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
19
|
+
│ GCanvas │
|
|
20
|
+
│ (index.js) │
|
|
21
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
22
|
+
│ │
|
|
23
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
24
|
+
│ │ Core Modules │ │
|
|
25
|
+
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │ │
|
|
26
|
+
│ │ │ shapes/ │ │ game/ │ │ painter/ │ │ │
|
|
27
|
+
│ │ │ 40+ shapes │ │ Game loop │ │ Canvas API │ │ │
|
|
28
|
+
│ │ │ hierarchy │ │ Pipeline │ │ abstraction │ │ │
|
|
29
|
+
│ │ │ Group │ │ GameObject │ │ │ │ │
|
|
30
|
+
│ │ └─────────────┘ └─────────────┘ └─────────────────┘ │ │
|
|
31
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
32
|
+
│ │
|
|
33
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
34
|
+
│ │ Support Modules │ │
|
|
35
|
+
│ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
|
|
36
|
+
│ │ │ motion │ │ io │ │ math │ │ util │ │ mixins │ │ │
|
|
37
|
+
│ │ │ Tween │ │ Events │ │ Random │ │ Layout │ │Draggable│ │ │
|
|
38
|
+
│ │ │ Easing │ │ Mouse │ │ Noise │ │Position│ │ Anchor │ │ │
|
|
39
|
+
│ │ │ Motion │ │ Keys │ │Fractals│ │ZIndex │ │ │ │ │
|
|
40
|
+
│ │ │Tweenetik│ │ Touch │ │Patterns│ │ Tasks │ │ │ │ │
|
|
41
|
+
│ │ └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ │ │
|
|
42
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
43
|
+
│ │
|
|
44
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
45
|
+
│ │ Utility Modules │ │
|
|
46
|
+
│ │ ┌────────┐ │ │
|
|
47
|
+
│ │ │ logger │ │ │
|
|
48
|
+
│ │ │ Logger │ │ │
|
|
49
|
+
│ │ │Loggable│ │ │
|
|
50
|
+
│ │ │DebugTab│ │ │
|
|
51
|
+
│ │ └────────┘ │ │
|
|
52
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
53
|
+
│ │
|
|
54
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Module Responsibilities
|
|
58
|
+
|
|
59
|
+
### Core Modules
|
|
60
|
+
|
|
61
|
+
| Module | Responsibility | Key Classes |
|
|
62
|
+
|--------|---------------|-------------|
|
|
63
|
+
| **shapes** | Visual primitives and rendering | `Shape`, `Circle`, `Rectangle`, `Group`, `Transformable` |
|
|
64
|
+
| **game** | Game loop, lifecycle, object management | `Game`, `Pipeline`, `GameObject`, `Scene` |
|
|
65
|
+
| **painter** | Canvas context abstraction | `Painter`, `PainterShapes`, `PainterText` |
|
|
66
|
+
|
|
67
|
+
### Support Modules
|
|
68
|
+
|
|
69
|
+
| Module | Responsibility | Key Classes |
|
|
70
|
+
|--------|---------------|-------------|
|
|
71
|
+
| **motion** | Animation and tweening | `Motion`, `Tweenetik`, `Easing`, `Tween` |
|
|
72
|
+
| **io** | Input handling | `EventEmitter`, `Mouse`, `Keys`, `Touch`, `Input` |
|
|
73
|
+
| **math** | Mathematical utilities | `Random`, `Noise`, `Complex`, `Fractals`, `Patterns` |
|
|
74
|
+
| **util** | Layout and positioning | `Layout`, `Position`, `ZOrderedCollection` |
|
|
75
|
+
| **mixins** | Composable behaviors | `applyDraggable()`, `applyAnchor()` |
|
|
76
|
+
|
|
77
|
+
### Utility Modules
|
|
78
|
+
|
|
79
|
+
| Module | Responsibility | Key Classes |
|
|
80
|
+
|--------|---------------|-------------|
|
|
81
|
+
| **logger** | Debug and logging | `Logger`, `Loggable`, `DebugTab` |
|
|
82
|
+
|
|
83
|
+
## Data Flow
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
User Input Game Loop
|
|
87
|
+
│ │
|
|
88
|
+
▼ ▼
|
|
89
|
+
┌────────┐ events ┌────────┐
|
|
90
|
+
│ IO │ ──────────────────►│ Game │
|
|
91
|
+
└────────┘ └────────┘
|
|
92
|
+
│
|
|
93
|
+
┌──────────────┼──────────────┐
|
|
94
|
+
▼ ▼ ▼
|
|
95
|
+
┌──────────┐ ┌──────────┐ ┌──────────┐
|
|
96
|
+
│ Pipeline │ │ Tweenetik│ │ Painter │
|
|
97
|
+
│ update() │ │ update() │ │ clear() │
|
|
98
|
+
└──────────┘ └──────────┘ └──────────┘
|
|
99
|
+
│
|
|
100
|
+
▼
|
|
101
|
+
┌──────────┐
|
|
102
|
+
│GameObject│
|
|
103
|
+
│ update() │
|
|
104
|
+
│ render() │
|
|
105
|
+
└──────────┘
|
|
106
|
+
│
|
|
107
|
+
▼
|
|
108
|
+
┌──────────┐
|
|
109
|
+
│ Shape │
|
|
110
|
+
│ draw() │
|
|
111
|
+
└──────────┘
|
|
112
|
+
│
|
|
113
|
+
▼
|
|
114
|
+
┌──────────┐
|
|
115
|
+
│ Painter │
|
|
116
|
+
│ Canvas │
|
|
117
|
+
└──────────┘
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Design Patterns
|
|
121
|
+
|
|
122
|
+
### 1. Inheritance Chain (Shapes)
|
|
123
|
+
|
|
124
|
+
Shapes use a linear inheritance hierarchy where each class adds specific functionality:
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
Euclidian → Geometry2d → Traceable → Renderable → Transformable → Shape
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
See: [Rendering Pipeline](./rendering-pipeline.md)
|
|
131
|
+
|
|
132
|
+
### 2. Static Singleton (Painter)
|
|
133
|
+
|
|
134
|
+
`Painter` is a static utility class initialized once with the canvas context:
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
Painter.init(ctx);
|
|
138
|
+
Painter.shapes.circle(100, 100, 50);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 3. Component Pattern (GameObject + Shape)
|
|
142
|
+
|
|
143
|
+
GameObjects compose shapes rather than inheriting from them:
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
class Player extends GameObject {
|
|
147
|
+
constructor(game) {
|
|
148
|
+
super(game);
|
|
149
|
+
this.shape = new Circle(40, { color: 'blue' });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 4. Mixin Pattern (Behaviors)
|
|
155
|
+
|
|
156
|
+
Optional behaviors are applied via mixin functions:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
applyDraggable(gameObject, gameObject.shape);
|
|
160
|
+
applyAnchor(gameObject, { anchor: 'top-right' });
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 5. Observer Pattern (Events)
|
|
164
|
+
|
|
165
|
+
The `EventEmitter` provides pub-sub event handling:
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
game.events.on('click', (e) => { ... });
|
|
169
|
+
game.events.emit('custom-event', data);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Entry Point
|
|
173
|
+
|
|
174
|
+
The main entry point (`src/index.js`) re-exports all modules:
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
export * from "./util";
|
|
178
|
+
export * from "./math";
|
|
179
|
+
export * from "./logger";
|
|
180
|
+
export * from "./painter";
|
|
181
|
+
export * from "./shapes";
|
|
182
|
+
export * from "./io";
|
|
183
|
+
export * from "./game";
|
|
184
|
+
export * from "./motion";
|
|
185
|
+
export * from "./mixins";
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
This allows importing any component from the main package:
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
import { Game, Circle, Painter, Motion } from '@guinetik/gcanvas';
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Related
|
|
195
|
+
|
|
196
|
+
- [Two-Layer Architecture](./two-layer-architecture.md) - Shape vs Game layer
|
|
197
|
+
- [Rendering Pipeline](./rendering-pipeline.md) - Shape inheritance chain
|
|
198
|
+
- [Game Lifecycle](./lifecycle.md) - Update/render cycle
|
|
199
|
+
|
|
200
|
+
## See Also
|
|
201
|
+
|
|
202
|
+
- [Shapes Module](../modules/shapes/README.md)
|
|
203
|
+
- [Game Module](../modules/game/README.md)
|
|
204
|
+
- [Painter Module](../modules/painter/README.md)
|