-
Notifications
You must be signed in to change notification settings - Fork 2
/
hellodat.html
77 lines (69 loc) · 2.62 KB
/
hellodat.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
<html>
<head>
<title>Dat.gui</title>
</head>
<body>
<script type="module">
import World from 'https://agentscript.org/src/World.js'
import Model from 'https://agentscript.org/src/Model.js'
import util from 'https://agentscript.org/src/util.js'
import TwoDraw from 'https://agentscript.org/src/TwoDraw.js'
import dat from 'https://unpkg.com/dat.gui/build/dat.gui.module.js'
class HelloModel extends Model {
constructor() {
super(World.defaultOptions()) // Default "NL" world
Object.assign(this, {
population: 100,
speed: 0.1,
wiggle: 0.1,
})
}
setup() {
this.turtles.setDefault('atEdge', 'bounce')
this.turtles.create(this.population, t => {
const patch = this.patches.oneOf()
t.setxy(patch.x, patch.y)
})
this.turtles.ask(t => {
this.links.create(t, this.turtles.otherOneOf(t))
})
}
step() {
this.turtles.ask(t => {
t.direction += util.randomCentered(this.wiggle)
t.forward(this.speed)
})
}
}
const model = new HelloModel()
model.setup()
const view = new TwoDraw(
model,
{ patchSize: 20 },
{
patchesColor: 'black',
linksColor: 'white',
turtlesColor: 'red',
}
)
// https://github.com/dataarts/dat.gui
const gui = new dat.GUI()
gui.add(model, 'speed', 0, 2)
gui.add(model, 'wiggle', 0, 1)
// patchSize requires resetting the view, so uses a command
gui.add(view, 'patchSize', 2, 25).onChange(val => view.reset(val))
window.pause = false // modules do not have a "this", use window. sigh!
gui.add(window, 'pause')
util.toWindow({ model, view, gui })
util.timeoutLoop(
() => {
if (pause) return
model.step()
view.draw()
},
-1, // -1 means go forever, use "pause" to start/stop
33
).then(() => console.log('done'))
</script>
</body>
</html>