Repository navigation
Expand file tree
/
Copy pathgrid.canFit.js
More file actions
99 lines (77 loc) · 2.95 KB
/
Copy pathgrid.canFit.js
File metadata and controls
99 lines (77 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//// 0. Describe this demo
window.demoDescription = "Conway's Game of Life as a bathroom tile generator. Move the mouse to generate patterns.";
//// 1. Define Space and Form
var colors = {
a1: "#ff2d5d", a2: "#42dc8e", a3: "#2e43eb", a4: "#ffe359",
b1: "#96bfed", b2: "#f5ead6", b3: "#f1f3f7", b4: "#e2e6ef"
};
var space = new CanvasSpace("demo", colors.a1 ).display();
var form = new Form( space );
space.clear();
space.refresh(false);
//// 2. Create Elements
var grid = new Grid( space.size.$multiply( 0.1 ) ).to( space.size.$multiply(0.9) ).init(10, 10, "fix", "fix");
var buffer = [];
var debounceTimer = 0;
// Creates a "pentomino" in the middle
var halfC = Math.floor( grid.columns/2 );
var halfR = Math.floor( grid.rows/2 );
grid.occupy( halfC, halfR, 1,1 );
grid.occupy( halfC-1, halfR, 1,1 );
grid.occupy( halfC, halfR+1, 1,1 );
grid.occupy( halfC+1, halfR, 1,1 );
grid.occupy( halfC+1, halfR-1, 1,1 );
// Apply the buffer to the grid
grid.generate( function ( size, position, row, column, type, isOccupied ) {
if (buffer) grid.occupy( column, row, 1, 1, buffer[column][row] );
// Simple optimization, only redraw if the state is different.
if (buffer[column][row] != isOccupied) {
var fill = (isOccupied) ? colors["b"+(Math.floor(Math.random()*4) + 1)] : colors.a3;
form.stroke( "#fff" ).fill( fill ).rect( new Rectangle( position ).resizeTo( size ) );
}
}.bind( grid ) );
// Conway's game of life
function gameOfLife() {
for (var i=0; i<grid.columns; i++) {
buffer[i] = []; // reset row
for (var k=0; k<grid.rows; k++) {
var cnt = 0; // counter
for ( var m=i-1; m<i+2; m++) { // get the states of surrounding cells
var mm = (m<0) ? grid.columns-1 : ( (m>=grid.columns) ? m-grid.columns : m );
for ( var n=k-1; n<k+2; n++) {
var nn = (n<0) ? grid.rows-1 : ( (n>=grid.rows) ? n-grid.rows : n );
if (mm===i && nn===k) continue;
else if ( !grid.canFit(mm, nn, 1, 1) ) cnt++;
}
}
buffer[i][k] = (grid.canFit(i, k, 1, 1) && cnt==3) || (!grid.canFit(i, k, 1, 1) && cnt>=2 && cnt<=3);
}
}
}
//// 3. Visualize, Animate, Interact
space.add({
animate: function(time, fps, context) {
if (time-debounceTimer > 30) { // not too fast
debounceTimer = time;
gameOfLife();
grid.create(); // draw grid cells (via generate() callback)
}
},
onMouseAction: function(type, x, y, evt) {
if (type=="move") {
var p1 = grid.positionToCell( x, y);
grid.occupy( p1.x, p1.y, 1, 1, true );
var mouseCell = new Rectangle( p1.$multiply( grid.cell.size ).add( grid ) ).resizeTo( grid.cell.size );
form.stroke("#fff").fill(colors.a1).rect( mouseCell );
}
},
// callback to reset grid when space is resized
onSpaceResize: function(w, h) {
36F7
grid.set( space.size.$multiply( 0.1 ) ).to( space.size.$multiply( 0.9 ) ).init( 10, 10, "fix", "fix" );
space.clear();
buffer = [];
}
});
// 4. Start playing
space.bindMouse();
space.play();