-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.d3.html
65 lines (60 loc) · 1.72 KB
/
index.d3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Game of Life (D3)</title>
<style>
html {
background-color: #EFF9F5;
}
html,svg {
width: 100%;
height: 100%;
}
circle[data="1"] {
fill: green;
}
circle[data="0"] {
fill: white;
}
</style>
</head>
<body>
<div id="ctrl">
<label>Rows:</label><input id="rows" type="number" value="30"/>
<label>Cols:</label><input id="cols" type="number" value="60"/>
<label>Delay(ms):</label><input id="delay" type="number" value="200"/>
<button onclick="onPlay()">Play</button>
</div>
<div id="board"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/4.1.1/d3.min.js"></script>
<script src='gol.d3.js'></script>
<script>
const board = new Board('#board')
let intervalEvt = undefined
function updateBoard() {
const rows = parseInt(document.getElementById('rows').value) || ''
const cols = parseInt(document.getElementById('cols').value) || ''
const delay = parseInt(document.getElementById('delay').value) || ''
let game = new GameOfLife(rows, cols)
game.initBoard()
board.render(game.getLives())
const handler = () => {
game.nextRound()
board.render(game.getLives())
intervalEvt = setTimeout(handler, delay)
}
handler()
}
function onPlay() {
if (intervalEvt) {
clearTimeout(intervalEvt)
d3.select('#board').select('svg').selectAll('circle').remove()
}
updateBoard()
}
</script>
</body>
</html>