-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
134 lines (114 loc) · 2.51 KB
/
index.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
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
<style>
body {
margin: 0;
}
.row {
clear: both;
}
.cell {
background: #380036;
float: left;
height: 0.5vw;
width: 0.5vw;
}
.cell.active {
background: #EF6F6C;
}
</style>
<div class="container"></div>
<script>
const rulesets = {
45: [
[true, false, true],
[false, true, true],
[false, true, false],
[false, false, false],
],
57: [
[true, false, true],
[true, false, false],
[false, true, true],
[false, false, false],
],
105: [
[true, true, false],
[true, false, true],
[false, true, true],
[false, false, false],
],
225: [
[true, true, true],
[true, true, false],
[true, false, true],
[false, false, false],
]
}
const config = {
perRow: 200,
ruleset: rulesets[45],
random: false,
timeout: 20,
}
const container = document.querySelector('.container')
const getLastRow = () => {
const allRows = document.querySelectorAll('.row')
return allRows[allRows.length - 1]
}
const createRow = () => {
const row = document.createElement('div')
row.classList.add('row')
createCells(row)
container.appendChild(row)
}
const isActive = cell => cell.classList.contains('active')
const cellMatchesRule = (left, prev, right, rule) => {
return (
isActive(left) === rule[0] &&
isActive(prev) === rule[1] &&
isActive(right) === rule[2]
);
}
const matchesRuleSet = compare => {
return config.ruleset.reduce((matches, rule) => {
if (matches || compare(rule)) {
return true;
}
return matches
}, false)
}
const setState = (cell, index, lastRow) => {
if (lastRow) {
const prev = lastRow.children[index];
const left = prev.previousSibling || lastRow.children[lastRow.children.length - 1];
const right = prev.nextSibling || lastRow.children[0];
const matchesRule = rule => cellMatchesRule(left, prev, right, rule)
if (matchesRuleSet(matchesRule)) {
cell.classList.add('active')
}
} else {
if (config.random) {
const randomBinary = Math.floor((Math.random() * 2));
if (randomBinary === 1) {
cell.classList.add('active')
}
} else {
if (index === config.perRow / 2) {
cell.classList.add('active')
}
}
}
}
const createCells = row => {
const lastRow = getLastRow()
for (let i = 0; i < config.perRow; i++) {
const cell = document.createElement('div')
cell.classList.add('cell')
setState(cell, i, lastRow)
row.appendChild(cell)
}
}
setInterval(() => {
createRow()
window.scrollTo(0, document.body.scrollHeight)
}, config.timeout)
</script>