@automatajs/core 0.2.0
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/LICENSE +21 -0
- package/README.md +461 -0
- package/dist/engine.d.ts +103 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +255 -0
- package/dist/engine.js.map +1 -0
- package/dist/grid.d.ts +55 -0
- package/dist/grid.d.ts.map +1 -0
- package/dist/grid.js +186 -0
- package/dist/grid.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/interaction.d.ts +44 -0
- package/dist/interaction.d.ts.map +1 -0
- package/dist/interaction.js +145 -0
- package/dist/interaction.js.map +1 -0
- package/dist/presets.d.ts +62 -0
- package/dist/presets.d.ts.map +1 -0
- package/dist/presets.js +158 -0
- package/dist/presets.js.map +1 -0
- package/dist/renderer.d.ts +51 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +150 -0
- package/dist/renderer.js.map +1 -0
- package/dist/types.d.ts +113 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
# @automatajs/core
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@automatajs/core) [](LICENSE)
|
|
4
|
+
|
|
5
|
+
> A focused cellular automata engine for the browser. Inspired by p5.js. Minimal API, instant feedback, decoupled rules and style.
|
|
6
|
+
|
|
7
|
+
Zero dependencies. Works with any canvas-based environment.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @automatajs/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or with your preferred package manager:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pnpm add @automatajs/core
|
|
19
|
+
yarn add @automatajs/core
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Automata, gameOfLife } from "@automatajs/core";
|
|
26
|
+
|
|
27
|
+
const canvas = document.querySelector("canvas");
|
|
28
|
+
const ca = new Automata(canvas, {
|
|
29
|
+
cols: 80,
|
|
30
|
+
rows: 60,
|
|
31
|
+
fps: 10,
|
|
32
|
+
rule: gameOfLife,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
ca.randomize(0.3);
|
|
36
|
+
ca.play();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Writing Rules
|
|
40
|
+
|
|
41
|
+
The rule function runs once per cell per generation and decides the next state.
|
|
42
|
+
It receives a [`CellContext`](#cellcontext) and returns `0` (dead) or any positive integer (alive).
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { Automata } from "@automatajs/core";
|
|
46
|
+
|
|
47
|
+
// Game of Life rule
|
|
48
|
+
function rule(ctx) {
|
|
49
|
+
if (ctx.self === 1) {
|
|
50
|
+
return ctx.neighborCount === 2 || ctx.neighborCount === 3 ? 1 : 0;
|
|
51
|
+
}
|
|
52
|
+
return ctx.neighborCount === 3 ? 1 : 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ca = new Automata(canvas, { rule });
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### CellContext
|
|
59
|
+
|
|
60
|
+
Every rule (and style) function receives a `CellContext` object:
|
|
61
|
+
|
|
62
|
+
| Property | Type | Description |
|
|
63
|
+
|----------|------|-------------|
|
|
64
|
+
| `self` | `number` | Current cell state (`0` = dead, `1+` = alive) |
|
|
65
|
+
| `neighborCount` | `number` | Count of alive neighbors |
|
|
66
|
+
| `neighbors` | `number[]` | Individual neighbor states (Moore: 8, Von Neumann: 4) |
|
|
67
|
+
| `x` | `number` | Column coordinate |
|
|
68
|
+
| `y` | `number` | Row coordinate |
|
|
69
|
+
| `get(x, y)` | `number` | Read any cell on the grid |
|
|
70
|
+
| `cols` | `number` | Grid width |
|
|
71
|
+
| `rows` | `number` | Grid height |
|
|
72
|
+
|
|
73
|
+
### `createRule(birth, survive)`
|
|
74
|
+
|
|
75
|
+
Create a custom birth/survival rule from standard notation.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { createRule } from "@automatajs/core";
|
|
79
|
+
|
|
80
|
+
// B3/S23 — Game of Life
|
|
81
|
+
const gol = createRule([3], [2, 3]);
|
|
82
|
+
|
|
83
|
+
// B36/S23 — HighLife
|
|
84
|
+
const highLife = createRule([3, 6], [2, 3]);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Styling Cells
|
|
88
|
+
|
|
89
|
+
A `StyleFunction` controls the color of every individual cell. It can return:
|
|
90
|
+
|
|
91
|
+
- `[r, g, b, a]` — RGBA tuple (0–255 each)
|
|
92
|
+
- `"#rrggbb"` — CSS color string
|
|
93
|
+
- `true` — use the default alive color
|
|
94
|
+
- `false` — transparent / dead
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const ca = new Automata(canvas, {
|
|
98
|
+
rule: gameOfLife,
|
|
99
|
+
style: (state, ctx) => {
|
|
100
|
+
if (state === 0) return false;
|
|
101
|
+
// Neighbor heatmap: cool → warm
|
|
102
|
+
const t = ctx.neighborCount / 8;
|
|
103
|
+
return [Math.floor(255 * t), 80, 255 - Math.floor(255 * t), 255];
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Set dynamically
|
|
108
|
+
ca.setStyle((state) => (state === 0 ? false : "#f472b6"));
|
|
109
|
+
|
|
110
|
+
// Reset to default
|
|
111
|
+
ca.setStyle(null);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## API Reference
|
|
115
|
+
|
|
116
|
+
*Auto-generated from `src/engine.ts`.*
|
|
117
|
+
|
|
118
|
+
### Constructor
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
new Automata(canvas: HTMLCanvasElement, options?: AutomataOptions)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Creates a new `Automata` instance bound to a canvas element.
|
|
125
|
+
The canvas auto-resizes to fill its parent container.
|
|
126
|
+
Cell size is computed automatically from grid dimensions and available space.
|
|
127
|
+
|
|
128
|
+
See [`AutomataOptions`](#automataoptionss) for all configuration options.
|
|
129
|
+
|
|
130
|
+
### Grid Management
|
|
131
|
+
|
|
132
|
+
**`getGrid(): Grid`**
|
|
133
|
+
|
|
134
|
+
Get the underlying grid.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
**`createGrid(cols: number, rows: number): void`**
|
|
139
|
+
|
|
140
|
+
Create a new grid, replacing the current one.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
**`clearGrid(): void`**
|
|
145
|
+
|
|
146
|
+
Clear the grid.
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
**`randomize(density: number = 0.3): void`**
|
|
151
|
+
|
|
152
|
+
Randomize the grid.
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
**`getCell(x: number, y: number): number`**
|
|
157
|
+
|
|
158
|
+
Get cell state.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
**`setCell(x: number, y: number, state: number): void`**
|
|
163
|
+
|
|
164
|
+
Set cell state.
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
**`toggleCell(x: number, y: number): void`**
|
|
169
|
+
|
|
170
|
+
Toggle cell state.
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
**`stamp(pattern: number[][], offsetX?: number, offsetY?: number): void`**
|
|
175
|
+
|
|
176
|
+
Stamp a pattern onto the grid.
|
|
177
|
+
|
|
178
|
+
### Simulation
|
|
179
|
+
|
|
180
|
+
**`setRule(rule: RuleFunction): void`**
|
|
181
|
+
|
|
182
|
+
Set the rule function.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
**`setStyle(style: StyleFunction | null): void`**
|
|
187
|
+
|
|
188
|
+
Set the style function for per-cell coloring. Pass null to reset to default.
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
**`step(): void`**
|
|
193
|
+
|
|
194
|
+
Advance one generation.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
**`play(): void`**
|
|
199
|
+
|
|
200
|
+
Start the simulation loop.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
**`pause(): void`**
|
|
205
|
+
|
|
206
|
+
Pause the simulation.
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
**`reset(): void`**
|
|
211
|
+
|
|
212
|
+
Reset to initial state (from last randomize or manual setup).
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
**`saveState(): void`**
|
|
217
|
+
|
|
218
|
+
Save the current grid state as the initial state.
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
**`get playing(): boolean`**
|
|
223
|
+
|
|
224
|
+
Is the simulation running?
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
**`get generation(): number`**
|
|
229
|
+
|
|
230
|
+
Current generation count.
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
**`get fps(): number`**
|
|
235
|
+
|
|
236
|
+
Get the current FPS setting.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
**`setFrameRate(fps: number): void`**
|
|
241
|
+
|
|
242
|
+
Set the target frames per second.
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
**`get population(): number`**
|
|
247
|
+
|
|
248
|
+
Population count.
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
**`get cols(): number`**
|
|
253
|
+
|
|
254
|
+
Grid dimensions.
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
**`onStep(fn: (gen: number) => void): void`**
|
|
259
|
+
|
|
260
|
+
Register a callback after each step.
|
|
261
|
+
|
|
262
|
+
### Rendering
|
|
263
|
+
|
|
264
|
+
**`render(): void`**
|
|
265
|
+
|
|
266
|
+
Force a re-render.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
**`resize(): void`**
|
|
271
|
+
|
|
272
|
+
Resize the canvas to fit its container.
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
**`setAliveColor(color: string): void`**
|
|
277
|
+
|
|
278
|
+
Set alive cell color.
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
**`setDeadColor(color: string): void`**
|
|
283
|
+
|
|
284
|
+
Set dead cell color.
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
**`setBackground(color: string): void`**
|
|
289
|
+
|
|
290
|
+
Set background color.
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
**`toggleGridLines(show?: boolean): void`**
|
|
295
|
+
|
|
296
|
+
Toggle grid lines on/off.
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
**`toDataURL(format?: "png" | "jpeg"): string`**
|
|
301
|
+
|
|
302
|
+
Export canvas as data URL.
|
|
303
|
+
|
|
304
|
+
### Interaction
|
|
305
|
+
|
|
306
|
+
**`onCellClick(fn: CellCallback): void`**
|
|
307
|
+
|
|
308
|
+
Set custom cell click handler.
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
**`onCellDrag(fn: CellCallback): void`**
|
|
313
|
+
|
|
314
|
+
Set custom cell drag handler.
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
**`enableInteraction(): void`**
|
|
319
|
+
|
|
320
|
+
Enable mouse/touch interaction.
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
**`disableInteraction(): void`**
|
|
325
|
+
|
|
326
|
+
Disable mouse/touch interaction.
|
|
327
|
+
|
|
328
|
+
### Lifecycle
|
|
329
|
+
|
|
330
|
+
**`destroy(): void`**
|
|
331
|
+
|
|
332
|
+
Clean up all resources.
|
|
333
|
+
|
|
334
|
+
## Types
|
|
335
|
+
|
|
336
|
+
*Auto-generated from `src/types.ts`.*
|
|
337
|
+
|
|
338
|
+
### `GridOptions`
|
|
339
|
+
|
|
340
|
+
| Property | Type | Default | Description |
|
|
341
|
+
|----------|------|---------|-------------|
|
|
342
|
+
| `wrap?` | `boolean` | true | Enable toroidal wrapping |
|
|
343
|
+
| `neighborhood?` | `NeighborhoodType` | "moore" | Neighborhood type |
|
|
344
|
+
|
|
345
|
+
### `CellContext`
|
|
346
|
+
|
|
347
|
+
Context object passed to each cell during a rule evaluation. Provides easy access to the cell's own state, its neighbors, and the ability to read any cell on the grid.
|
|
348
|
+
|
|
349
|
+
| Property | Type | Default | Description |
|
|
350
|
+
|----------|------|---------|-------------|
|
|
351
|
+
| `self` | `number` | — | Current state of this cell (0 = dead, 1+ = alive / multi-state) |
|
|
352
|
+
| `x` | `number` | — | Column coordinate |
|
|
353
|
+
| `y` | `number` | — | Row coordinate |
|
|
354
|
+
| `neighborCount` | `number` | — | Number of alive neighbors (count of neighbors with state > 0) |
|
|
355
|
+
| `neighbors` | `number[]` | — | Array of individual neighbor state values (Moore: 8, Von Neumann: 4) |
|
|
356
|
+
| `cols` | `number` | — | Grid width |
|
|
357
|
+
| `rows` | `number` | — | Grid height |
|
|
358
|
+
|
|
359
|
+
### `AutomataOptions *(extends `GridOptions`)*`
|
|
360
|
+
|
|
361
|
+
| Property | Type | Default | Description |
|
|
362
|
+
|----------|------|---------|-------------|
|
|
363
|
+
| `cols?` | `number` | — | Number of columns |
|
|
364
|
+
| `rows?` | `number` | — | Number of rows |
|
|
365
|
+
| `fps?` | `number` | 10 | Frames per second |
|
|
366
|
+
| `aliveColor?` | `string` | "#22d3ee" | Color for alive cells |
|
|
367
|
+
| `deadColor?` | `string` | "#0f172a" | Color for dead cells |
|
|
368
|
+
| `backgroundColor?` | `string` | "#020617" | Background color |
|
|
369
|
+
| `showGridLines?` | `boolean` | true | Show grid lines |
|
|
370
|
+
| `gridLineColor?` | `string` | "#1e293b" | Grid line color |
|
|
371
|
+
| `cellSize?` | `number` | — | Cell size in pixels (auto-calculated if omitted) |
|
|
372
|
+
| `rule?` | `RuleFunction` | — | Rule function — determines next state for each cell |
|
|
373
|
+
| `style?` | `StyleFunction` | — | Style function — determines visual appearance per cell (optional) |
|
|
374
|
+
|
|
375
|
+
### `ExportOptions`
|
|
376
|
+
|
|
377
|
+
| Property | Type | Default | Description |
|
|
378
|
+
|----------|------|---------|-------------|
|
|
379
|
+
| `scale?` | `number` | — | |
|
|
380
|
+
| `format?` | `"png" | "jpeg"` | — | |
|
|
381
|
+
|
|
382
|
+
### `NeighborhoodType`
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
type NeighborhoodType = "moore" | "vonneumann"
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### `RGBA`
|
|
389
|
+
|
|
390
|
+
RGBA color tuple: [red, green, blue, alpha], each 0–255.
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
type RGBA = [number, number, number, number]
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### `StyleFunction`
|
|
397
|
+
|
|
398
|
+
Style function signature. Determines the visual appearance of a cell based on its state and context. Return an RGBA color tuple, a CSS color string, or a boolean (true = alive color).
|
|
399
|
+
|
|
400
|
+
Parameters:
|
|
401
|
+
- `state` — Current state of the cell
|
|
402
|
+
- `ctx` — Cell context with position and grid access
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
type StyleFunction = (
|
|
406
|
+
state: number,
|
|
407
|
+
ctx: CellContext
|
|
408
|
+
) => RGBA | string | boolean
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### `CellCallback`
|
|
412
|
+
|
|
413
|
+
```typescript
|
|
414
|
+
type CellCallback = (x: number, y: number, state: number) => void
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
## Presets
|
|
418
|
+
|
|
419
|
+
Built-in rules ready to use. Import directly or via the `presets` registry.
|
|
420
|
+
|
|
421
|
+
*Auto-generated from `src/presets.ts`.*
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
import { gameOfLife, highLife, seeds, presets } from "@automatajs/core";
|
|
425
|
+
|
|
426
|
+
ca.setRule(gameOfLife);
|
|
427
|
+
|
|
428
|
+
// Access by name via registry
|
|
429
|
+
ca.setRule(presets["dayAndNight"]);
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
| Export | Description |
|
|
433
|
+
|--------|-------------|
|
|
434
|
+
| `gameOfLife` | Conway's Game of Life (B3/S23) - A dead cell with exactly 3 neighbors becomes alive. - A live cell with 2 or 3 neighbors survives. - All other cells die or stay dead. |
|
|
435
|
+
| `highLife` | HighLife (B36/S23) Like Game of Life but also births at 6 neighbors. Famous for replicators. |
|
|
436
|
+
| `seeds` | Seeds (B2/S) Dead cells with exactly 2 neighbors come alive; all live cells die. Creates explosive growth patterns. |
|
|
437
|
+
| `dayAndNight` | Day & Night (B3678/S34678) Symmetric rule — patterns behave the same inverted. |
|
|
438
|
+
| `diamoeba` | Diamoeba (B35678/S5678) Creates large diamond-shaped regions. |
|
|
439
|
+
|
|
440
|
+
## Patterns
|
|
441
|
+
|
|
442
|
+
Famous patterns as 2D arrays for stamping onto the grid.
|
|
443
|
+
|
|
444
|
+
*Auto-generated from `src/presets.ts`.*
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
import { Automata, gameOfLife, patterns } from "@automatajs/core";
|
|
448
|
+
|
|
449
|
+
const ca = new Automata(canvas, { cols: 80, rows: 60, rule: gameOfLife });
|
|
450
|
+
|
|
451
|
+
ca.stamp(patterns.glider, 10, 10);
|
|
452
|
+
ca.stamp(patterns.gosperGliderGun, 2, 2);
|
|
453
|
+
ca.play();
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
| Pattern | Size | Description |
|
|
457
|
+
|---------|------|-------------|
|
|
458
|
+
|
|
459
|
+
## License
|
|
460
|
+
|
|
461
|
+
MIT
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Grid } from "./grid";
|
|
2
|
+
import type { AutomataOptions, RuleFunction, StyleFunction, CellCallback } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Main Automata engine class.
|
|
5
|
+
* Orchestrates grid, renderer, simulation loop, and interaction.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* ```ts
|
|
9
|
+
* const ca = new Automata(canvasElement, {
|
|
10
|
+
* cols: 80,
|
|
11
|
+
* rows: 60,
|
|
12
|
+
* fps: 10,
|
|
13
|
+
* rule: gameOfLife,
|
|
14
|
+
* });
|
|
15
|
+
* ca.randomize(0.3);
|
|
16
|
+
* ca.play();
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare class Automata {
|
|
20
|
+
private grid;
|
|
21
|
+
private initialGrid;
|
|
22
|
+
private renderer;
|
|
23
|
+
private interaction;
|
|
24
|
+
private rule;
|
|
25
|
+
private _fps;
|
|
26
|
+
private _generation;
|
|
27
|
+
private _playing;
|
|
28
|
+
private animFrameId;
|
|
29
|
+
private lastFrameTime;
|
|
30
|
+
private onStepCallbacks;
|
|
31
|
+
constructor(canvas: HTMLCanvasElement, options?: AutomataOptions);
|
|
32
|
+
/** Get the underlying grid. */
|
|
33
|
+
getGrid(): Grid;
|
|
34
|
+
/** Create a new grid, replacing the current one. */
|
|
35
|
+
createGrid(cols: number, rows: number): void;
|
|
36
|
+
/** Clear the grid. */
|
|
37
|
+
clearGrid(): void;
|
|
38
|
+
/** Randomize the grid. */
|
|
39
|
+
randomize(density?: number): void;
|
|
40
|
+
/** Get cell state. */
|
|
41
|
+
getCell(x: number, y: number): number;
|
|
42
|
+
/** Set cell state. */
|
|
43
|
+
setCell(x: number, y: number, state: number): void;
|
|
44
|
+
/** Toggle cell state. */
|
|
45
|
+
toggleCell(x: number, y: number): void;
|
|
46
|
+
/** Stamp a pattern onto the grid. */
|
|
47
|
+
stamp(pattern: number[][], offsetX?: number, offsetY?: number): void;
|
|
48
|
+
/** Set the rule function. */
|
|
49
|
+
setRule(rule: RuleFunction): void;
|
|
50
|
+
/** Set the style function for per-cell coloring. Pass null to reset to default. */
|
|
51
|
+
setStyle(style: StyleFunction | null): void;
|
|
52
|
+
/** Advance one generation. */
|
|
53
|
+
step(): void;
|
|
54
|
+
/** Start the simulation loop. */
|
|
55
|
+
play(): void;
|
|
56
|
+
/** Pause the simulation. */
|
|
57
|
+
pause(): void;
|
|
58
|
+
/** Reset to initial state (from last randomize or manual setup). */
|
|
59
|
+
reset(): void;
|
|
60
|
+
/** Save the current grid state as the initial state. */
|
|
61
|
+
saveState(): void;
|
|
62
|
+
/** Is the simulation running? */
|
|
63
|
+
get playing(): boolean;
|
|
64
|
+
/** Current generation count. */
|
|
65
|
+
get generation(): number;
|
|
66
|
+
/** Get the current FPS setting. */
|
|
67
|
+
get fps(): number;
|
|
68
|
+
/** Set the target frames per second. */
|
|
69
|
+
setFrameRate(fps: number): void;
|
|
70
|
+
/** Population count. */
|
|
71
|
+
get population(): number;
|
|
72
|
+
/** Grid dimensions. */
|
|
73
|
+
get cols(): number;
|
|
74
|
+
get rows(): number;
|
|
75
|
+
/** Register a callback after each step. */
|
|
76
|
+
onStep(fn: (gen: number) => void): void;
|
|
77
|
+
/** Force a re-render. */
|
|
78
|
+
render(): void;
|
|
79
|
+
/** Resize the canvas to fit its container. */
|
|
80
|
+
resize(): void;
|
|
81
|
+
/** Set alive cell color. */
|
|
82
|
+
setAliveColor(color: string): void;
|
|
83
|
+
/** Set dead cell color. */
|
|
84
|
+
setDeadColor(color: string): void;
|
|
85
|
+
/** Set background color. */
|
|
86
|
+
setBackground(color: string): void;
|
|
87
|
+
/** Toggle grid lines on/off. */
|
|
88
|
+
toggleGridLines(show?: boolean): void;
|
|
89
|
+
/** Export canvas as data URL. */
|
|
90
|
+
toDataURL(format?: "png" | "jpeg"): string;
|
|
91
|
+
/** Set custom cell click handler. */
|
|
92
|
+
onCellClick(fn: CellCallback): void;
|
|
93
|
+
/** Set custom cell drag handler. */
|
|
94
|
+
onCellDrag(fn: CellCallback): void;
|
|
95
|
+
/** Enable mouse/touch interaction. */
|
|
96
|
+
enableInteraction(): void;
|
|
97
|
+
/** Disable mouse/touch interaction. */
|
|
98
|
+
disableInteraction(): void;
|
|
99
|
+
/** Clean up all resources. */
|
|
100
|
+
destroy(): void;
|
|
101
|
+
private loop;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAI9B,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,aAAa,EACb,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,IAAI,CAAO;IACnB,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,IAAI,CAAe;IAC3B,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,aAAa,CAAa;IAGlC,OAAO,CAAC,eAAe,CAAoC;gBAE/C,MAAM,EAAE,iBAAiB,EAAE,OAAO,GAAE,eAAoB;IAuCpE,+BAA+B;IAC/B,OAAO,IAAI,IAAI;IAIf,oDAAoD;IACpD,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAW5C,sBAAsB;IACtB,SAAS,IAAI,IAAI;IAMjB,0BAA0B;IAC1B,SAAS,CAAC,OAAO,GAAE,MAAY,GAAG,IAAI;IAOtC,sBAAsB;IACtB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAIrC,sBAAsB;IACtB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAKlD,yBAAyB;IACzB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI;IAKtC,qCAAqC;IACrC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAOpE,6BAA6B;IAC7B,OAAO,CAAC,IAAI,EAAE,YAAY,GAAG,IAAI;IAIjC,mFAAmF;IACnF,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI;IAK3C,8BAA8B;IAC9B,IAAI,IAAI,IAAI;IASZ,iCAAiC;IACjC,IAAI,IAAI,IAAI;IAOZ,4BAA4B;IAC5B,KAAK,IAAI,IAAI;IAQb,oEAAoE;IACpE,KAAK,IAAI,IAAI;IAQb,wDAAwD;IACxD,SAAS,IAAI,IAAI;IAIjB,iCAAiC;IACjC,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,gCAAgC;IAChC,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,mCAAmC;IACnC,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED,wCAAwC;IACxC,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAI/B,wBAAwB;IACxB,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,uBAAuB;IACvB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,2CAA2C;IAC3C,MAAM,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAMvC,yBAAyB;IACzB,MAAM,IAAI,IAAI;IAId,8CAA8C;IAC9C,MAAM,IAAI,IAAI;IAKd,4BAA4B;IAC5B,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKlC,2BAA2B;IAC3B,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKjC,4BAA4B;IAC5B,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKlC,gCAAgC;IAChC,eAAe,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI;IAKrC,iCAAiC;IACjC,SAAS,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM;IAM1C,qCAAqC;IACrC,WAAW,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI;IAInC,oCAAoC;IACpC,UAAU,CAAC,EAAE,EAAE,YAAY,GAAG,IAAI;IAIlC,sCAAsC;IACtC,iBAAiB,IAAI,IAAI;IAIzB,uCAAuC;IACvC,kBAAkB,IAAI,IAAI;IAM1B,8BAA8B;IAC9B,OAAO,IAAI,IAAI;IAOf,OAAO,CAAC,IAAI;CAeb"}
|