Skip to content

Commit f7815b9

Browse files
committed
hellodat.html: add patchSize control
new model: prefAttach.html
1 parent 6882652 commit f7815b9

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

hellodat.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
const gui = new dat.GUI()
5858
gui.add(model, 'speed', 0, 2)
5959
gui.add(model, 'wiggle', 0, 1)
60-
60+
// patchSize requires resetting the view, so uses a command
61+
gui.add(view, 'patchSize', 2, 25).onChange((val) => view.reset(val))
6162
window.pause = false // modules do not have a "this", use window. sigh!
6263
gui.add(window, 'pause')
6364

prefAttach.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<html>
2+
<head>
3+
<title>PrefAttach</title>
4+
</head>
5+
<body>
6+
<script type="module">
7+
import {
8+
World,
9+
Model,
10+
util,
11+
} from 'https://backspaces.github.io/agentscript/dist/agentscript.esm.js'
12+
import TwoView from 'https://backspaces.github.io/agentscript/src/TwoView.js'
13+
import ColorMap from 'https://backspaces.github.io/agentscript/src/ColorMap.js'
14+
15+
class PrefAttachModel extends Model {
16+
constructor() {
17+
super(World.defaultOptions(45))
18+
}
19+
setup() {
20+
let [t1, t2] = this.turtles.create(2)
21+
this.links.create(t1, t2)
22+
}
23+
step() {
24+
const partner = this.links.oneOf().bothEnds().oneOf()
25+
this.turtles.create(1, (t) => {
26+
this.links.create(t, partner)
27+
})
28+
this.turtles.layoutCircle(model.world.maxX * 0.95)
29+
}
30+
}
31+
32+
const model = new PrefAttachModel()
33+
model.setup()
34+
// Use the model's world for the view:
35+
const view = new TwoView(model.world, { patchSize: 8 })
36+
37+
util.toWindow({ model, view, util })
38+
39+
const colorMap = ColorMap.Basic16
40+
function draw() {
41+
view.clear('black')
42+
view.drawLinks(model.links, { color: 'white' })
43+
view.drawTurtles(model.turtles, (t) => ({
44+
shape: 'circle',
45+
// +2 avoid white on top
46+
// .css: colorMap use Color.typedColor's, thus need css type
47+
color: colorMap.atIndex(t.id + 2).css,
48+
size: Math.sqrt(t.linkNeighbors().length),
49+
}))
50+
}
51+
52+
util.timeoutLoop(
53+
() => {
54+
model.step()
55+
draw()
56+
},
57+
500,
58+
33
59+
).then(() => console.log('done'))
60+
</script>
61+
</body>
62+
</html>

0 commit comments

Comments
 (0)