-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
138 lines (111 loc) · 3.35 KB
/
main.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
import rules from "./rules.js"
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
const mainCanvas = document.getElementById('main-canvas')
const ctx = mainCanvas.getContext('2d')
const grid = 100
const boardSize = {x: mainCanvas.scrollWidth, y: mainCanvas.scrollHeight}
const cellSize = {x: boardSize.x / grid, y: boardSize.y / grid}
mainCanvas.width = boardSize.x;
mainCanvas.height = boardSize.y;
let board = []
let loop
// Init board
function initBoard(){
board = []
clearInterval(loop)
for (var x = 0; x < grid; x++) {
board.push([])
for (var y = 0; y < grid; y++) {
board[x].push(0)
}
}
drawBoard(board)
}
// Process all pixels
function forEachPixel(operation, board) {
for (var x = 0; x < board.length; x++) {
for (var y = 0; y < board[x].length; y++) {
operation({x: x, y: y})
}
}
}
// Draw one pixel at x, y
function drawPixel(x, y){
ctx.fillStyle = board[x][y] ? "black" : "white"
ctx.fillRect(x*cellSize.x, y*cellSize.y, cellSize.x, cellSize.y)
}
// Draw whole board
function drawBoard(board){
forEachPixel((pix) => {
drawPixel(pix.x, pix.y)
}, board)
}
function processRules(board){
// Securely get one pixel
function getValue(x, y){
return (x in board && y in board[x]) ? board[x][y] : 0
}
let boardBuffer = JSON.parse(JSON.stringify(board))
forEachPixel((px) => {
var currentValue = getValue(px.x, px.y)
const sum = getValue(px.x + 1, px.y) +
getValue(px.x - 1, px.y) +
getValue(px.x, px.y + 1) +
getValue(px.x, px.y - 1) +
getValue(px.x + 1, px.y - 1) +
getValue(px.x - 1, px.y + 1) +
getValue(px.x - 1, px.y - 1) +
getValue(px.x + 1, px.y + 1)
boardBuffer[px.x][px.y] = (
rules.one(currentValue, sum) ||
rules.two(currentValue, sum) ||
rules.three(currentValue, sum) ||
rules.four(currentValue, sum)
)
}, boardBuffer)
return JSON.parse(JSON.stringify(boardBuffer))
}
function iterate() {
// TODO: Should probably not mutate board here
board = processRules(board)
drawBoard(board) // TODO: Should receive a board
}
function startSimulation(){
loop = setInterval(()=>{
iterate()
}, 20)
}
// Event handlers ---------------------------------------------------
// Set up button clicks
const buttonIterate = document.getElementById('btn-iterate')
buttonIterate.onclick = startSimulation
const buttonReset = document.getElementById('btn-reset')
buttonReset.onclick = initBoard
// Set up canvas interaction
mainCanvas.addEventListener('click', (e)=>{
clickCanvas(e.offsetX, e.offsetY)
})
function clickCanvas(x, y){
const convertedX = Math.floor(x / cellSize.x)
const convertedY = Math.floor(y / cellSize.y)
if (board[convertedX][convertedY] == 0) {
board[convertedX][convertedY] = 1
} else {
board[convertedX][convertedY] = 0
}
drawBoard(board)
}
mainCanvas.addEventListener('mousemove', (e)=>{
highlightPixel(e.offsetX, e.offsetY)
})
function highlightPixel(x, y){
drawBoard(board)
const convertedX = Math.floor(x / cellSize.x)
const convertedY = Math.floor(y / cellSize.y)
ctx.fillStyle = "pink"
ctx.fillRect(convertedX*cellSize.x, convertedY*cellSize.y, cellSize.x, cellSize.y)
}
initBoard()
}
}