-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (40 loc) · 1.61 KB
/
script.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
const field_size = 5, // размер ячейки, px
rows_number = 100, // кол-во строк
columns_number = 100 // кол-во столбцов
background_color = 'gray',
field_color = 'violet'; // цвет живой клетки
generation_time = 100;
const canvas = document.querySelector('canvas'); // передаем в canvas dom-элемент с тегом canvas
const context = canvas.getContext('2d');
const lifeGame = new LifeGame(rows_number, columns_number) // создаем новый экземпляр класса LifeGame
console.log(lifeGame)
start();
function start() {
canvas.width = field_size * columns_number; // задаем ширину канваса, берем 1 ячейку и * на кол-во колонок
canvas.height = field_size * rows_number;
console.log('started');
lifeGame.reviveRandomFields(rows_number * columns_number / 2); // оживляем половину поля
clearCanvas();
lifeGame.forFreeEach((x, y) => {drawField(x, y, field_color)})
requestAnimationFrame(tick)
}
function tick(timestamp) {
clearCanvas();
if (timestamp > lifeGame.generationNumber * generation_time) {
lifeGame.changeGeneration();
}
lifeGame.forFreeEach((x, y) => {drawField(x, y, field_color)});
requestAnimationFrame(tick);
}
function clearCanvas() {
context.fillStyle = background_color;
context.beginPath();
context.rect(0, 0, canvas.width, canvas.height);
context.fill();
}
function drawField (x, y, color) {
context.fillStyle = color;
context.beginPath();
context.rect(x*field_size, y*field_size, field_size, field_size);
context.fill();
}