-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ca2d.js
176 lines (134 loc) · 4.67 KB
/
jquery.ca2d.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;
(function($) {
var methods = {
init: function(options) {
var settings = $.extend({}, $.fn.ca2d.defaults, options);
return this.filter('canvas').each(function() {
$this = $(this);
if(!$this[0].getContext) {
return true; // continue
}
$this.data('ca2d');
// If the plugin hasn't been initialized yet
if(!$this.data('ca2d')) {
var cW = $this.width();
var cH = $this.height();
var grid = new Grid(cW, cH, settings.cellSize);
var ctx = this.getContext('2d');
$(this).data('ca2d', {
grid: grid,
context: ctx,
settings: settings
});
}
var data = $this.data('ca2d');
});
function Grid(x, y, cellSize) {
this.x = x;
this.y = y;
this.cellSize = cellSize;
this.rows = Math.floor(y / cellSize);;
this.cols = Math.floor(x / cellSize);;
var cells = new Array(this.rows);
for(var i = 0; i < this.rows; i++) {
var row = new Array(this.cols);
for(var j = 0; j < this.cols; j++) {
var state = Math.round(Math.random()); // 0 or 1
row[j] = new Cell(i, j, state);
}
cells[i] = row;
}
this.cells = cells;
function Cell(x, y, state) {
this.x = x;
this.y = y;
this.state = state;
this.nextstate = state;
} // end of Cell
} // end of Grid
},
// end of init
step: function() {
return this.filter('canvas').each(function() {
$this = $(this);
if(!$this.data('ca2d')) {
console.log("need to initialize ca2d");
return;
}
var data = $this.data('ca2d');
_updState(data.grid, data.settings);
_print(data.context, data.grid, data.settings);
});
function _updState(grid, settings) {
// A B C
// D E F
// H I J
for(var i = 0; i < grid.rows; i++) {
var row = grid.cells[i];
for(var j = 0; j < grid.cols; j++) {
var cell = row[j]; // cell_E
var cell_A = grid.cells[i - 1 < 0 ? grid.rows - 1 : i - 1][j - 1 < 0 ? grid.cols - 1 : j - 1];
var cell_B = grid.cells[i - 1 < 0 ? grid.rows - 1 : i - 1][j];
var cell_C = grid.cells[i - 1 < 0 ? grid.rows - 1 : i - 1][j + 1 >= grid.cols ? 0 : j + 1];
var cell_D = grid.cells[i][j - 1 < 0 ? grid.cols - 1 : j - 1];
var cell_F = grid.cells[i][j + 1 >= grid.cols ? 0 : j + 1];
var cell_H = grid.cells[i + 1 >= grid.rows ? 0 : i + 1][j - 1 < 0 ? grid.cols - 1 : j - 1];
var cell_I = grid.cells[i + 1 >= grid.rows ? 0 : i + 1][j];
var cell_J = grid.cells[i + 1 >= grid.rows ? 0 : i + 1][j + 1 >= grid.cols ? 0 : j + 1];
cell.nextstate = settings.rule(cell.state, [cell_A.state, cell_B.state, cell_C.state, cell_D.state, cell_F.state, cell_H.state, cell_I.state, cell_J.state]);
} // end of for col
} // end of for row
} // end of _updState
function _print(ctx, grid, settings) {
for(var i = 0; i < grid.rows; i++) {
var row = grid.cells[i];
var y = i * grid.cellSize;
for(var j = 0; j < grid.cols; j++) {
var x = j * grid.cellSize;
var cell = row[j];
ctx.fillStyle = settings.color(cell.nextstate);
ctx.fillRect(x, y, grid.cellSize, grid.cellSize);
// step futher
cell.state = cell.nextstate;
}
}
} // end of _print
}, // end of step
update: function(content) {
console.log("update");
}
};
$.fn.ca2d = function(method) {
// Method calling logic
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if(typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.ca2d');
}
}; // end of $.fn.ca2d
$.fn.ca2d.defaults = {
cellSize: 10,
color: function(state) {
return state == 1 ? "#aaaaaa" : "#ffffff";
},
rule: function(selfstate, neighborStates) {
var otherTotal = 0;
jQuery.each(neighborStates, function(idx, value) {
otherTotal += value;
});
var nextState = selfstate;
if(selfstate == 1) {
if(otherTotal <= 1 || otherTotal >= 4) {
nextState = 0;
}
} else {
if(otherTotal == 3) {
nextState = 1;
}
}
return nextState;
},
} // end of $.fn.ca2d.defaults
})(jQuery);