-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
47 lines (42 loc) · 1.32 KB
/
main.js
File metadata and controls
47 lines (42 loc) · 1.32 KB
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
import { generateSeed } from "./seed.js"
import { rollout } from "./rule.js"
import { render } from "./render.js"
import { fromUri, toUri } from "./uris.js"
import { defaults } from "./defaults.js"
window.onload = window.onhashchange = _ => {
location.hash.length <= 1 &&
(console.log ('no pameters given, using defaults') ||
(location.hash = toUri (setDefaults())))
makeAll (setDefaults (fromUri (location.hash)))
}
const updateNav = config => {
[['prev', -1], ['next', 1]].map (([id, offset]) =>
document.getElementById(id).setAttribute('href',
toUri ({...config, ...{page: config.page + offset}})
)
)
}
const setDefaults = (config = {}) => ({ ...defaults, ...config })
const makeAll = config => {
removeAll(['canvas', 'svg'])
updateNav (config)
let {neighbors, radix, start, end, page, amount, seed, only} = config
start ??= only ?? (page ? page * amount : 0)
end ??= (only ? only + 1 :
(amount != -1) ? start + amount :
radix ** (radix ** neighbors)
)
let ruleNum = start;
const rolloutFun = rollout (config) (generateSeed (seed))
do document.body.appendChild (
render (
rolloutFun (ruleNum),
{ruleNum, __proto__: config}
))
while (++ ruleNum < end)
}
const removeAll = tags =>
tags.forEach(tag =>
Array.from(document.body.getElementsByTagName(tag))
.forEach (el => document.body.removeChild(el))
)