-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (116 loc) · 3.5 KB
/
index.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
document.addEventListener("DOMContentLoaded", () => {
const canvas = document.getElementById('canvas')
const canvas2d = canvas.getContext('2d')
const createGrid = (width, height) => {
const grid = new Array(height)
for(var y = 0; y < height; y++) {
grid[y] = []
for(var x = 0; x < width; x++) {
grid[y][x] = Math.floor(Math.random() * 2)
}
}
return grid
}
const createNudeGrid = (width, height) => {
const res = new Array(height)
for(var y = 0; y < height; y++) {
grid[y] = [];
for(var x = 0; x < width; x++) {
grid[y][x] = 0
}
}
return grid
}
// const drawGrid = (ctx, cellSize, grid) => {
// for(let y = 0; y < 0; y++)
// }
const drawCells = (ctx, cellSize, grid) => {
const height = grid.length
const width = grid[0].length
for(let y = 0; y < height; y++) {
ctx.strokeStyle = 'black'
ctx.beginPath()
ctx.moveTo(0, y * cellSize)
ctx.lineTo(width * cellSize, y * cellSize)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(y * cellSize, 0)
ctx.lineTo(y * cellSize, width * cellSize)
ctx.stroke()
for(let x = 0; x < width; x++) {
if (grid[y][x] === 1) {
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize)
}
}
}
}
const lineExtractor = (line, x) => {
return line ? [
...line[x - 1] !== undefined ? [line[x - 1]] : [],
line[x],
...line[x + 1] !== undefined ? [line[x + 1]] : []
] : []
}
const modifyGrid = (grid) => {
const height = grid.length
const width = grid[0].length
const newGrid = createNudeGrid(width, height)
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
const currentLine = []
if (grid[y][x - 1] !== undefined) {
currentLine.push(grid[y][x - 1])
}
// currentLine.push(grid[y][x])
if (grid[y][x + 1] !== undefined) {
currentLine.push(grid[y][x + 1])
}
const prevLine = lineExtractor(grid[y - 1], x)
const nextLine = lineExtractor(grid[y + 1], x)
const neighbors = prevLine.concat(currentLine, nextLine)
const aliveCells = neighbors.filter((n) => n === 1)
if (grid[y][x] === 0){
if ( aliveCells.length === 3) {
newGrid[y][x] = 1
}
} else {
if (aliveCells.length === 2 || aliveCells.length === 3) {
newGrid[y][x] = 1
}
}
}
}
return newGrid
}
const alterGrid = (ctx, grid, gridSize) => {
grid = modifyGrid(grid.slice(0))
ctx.clearRect(0, 0, gridSize, gridSize)
drawCells(canvas2d, cellSize, grid)
}
const cellSize = 15
const gridLength = 100
const gridSize = gridLength * cellSize
let grid = createGrid(gridLength, gridLength, cellSize)
// drawGrid(canvas2d, cellSize, grid)
drawCells(canvas2d, cellSize, grid)
canvas.addEventListener('click', (event) => {
const posX = event.offsetX
const posY = event.offsetY
for (let cell = 0; cell <= gridLength; cell++) {
const cellPosX = cell * cellSize
// if () {
// }
console.log('posX ===> ', posX)
console.log('cellPosX ===> ', cellPosX)
}
const heightX = posX * cellSize
console.log('X ===> ', posX)
console.log('Y ===> ', posY)
})
document.querySelector('.button').addEventListener('click', () => {
alterGrid(canvas2d, grid, gridSize)
})
setInterval(() => {
alterGrid(canvas2d, grid, gridSize)
}, 200)
})