-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
46 lines (39 loc) · 1.11 KB
/
view.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
window.ConwayView = (function(){
'use strict';
/**
* View
*
*/
function View(){
var id = 'game';
this.canvas = document.getElementById(id);
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
};
/**
* update
*
* @param {Array.<Array.<bool>>} gridData The new grid data
*/
View.prototype.update = function(gridData){
var hLength = gridData.length;
var vLength = gridData[0].length;
var ctx = this.canvas.getContext('2d');
var stepHorizontal = this.canvas.width / hLength;
var stepVertical = this.canvas.height / vLength;
var step = (stepHorizontal < stepVertical ? stepVertical : stepHorizontal) + 0.5;
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
ctx.strokeStyle = '#383C4D';
ctx.fillStyle = '#060608';
for(var i = 0; i < hLength; i++){
for(var j = 0; j < vLength; j++){
if(gridData[i][j]){
ctx.fillRect(i *step, j * step, step, step);
ctx.strokeRect(i *step, j * step, step, step);
}
}
}
}
return View;
})();