-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshapes.html
80 lines (75 loc) · 2.96 KB
/
shapes.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
<html>
<head>
<title>Hello</title>
</head>
<body>
<script type="module">
// Add two images to the standard paths/shapes: redfish & twitter
// Run in Hello model.
import {
World,
Model,
util,
} from 'https://backspaces.github.io/agentscript/dist/agentscript.esm.js'
import TwoView from 'https://backspaces.github.io/agentscript/src/TwoView.js'
import Shapes from 'https://backspaces.github.io/agentscript/src/Shapes.js'
import ColorMap from 'https://backspaces.github.io/agentscript/src/ColorMap.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 shapes = new Shapes()
async function addImages() {
await shapes.imagePathPromise('tweet', './data/twitter.png')
await shapes.imagePathPromise('redfish', './data/redfish.png')
}
addImages().then(() => {
const colorMap = ColorMap.Bright16
const shapeNames = shapes.getPathNames() // will include images
const model = new HelloModel()
model.setup()
// Use the model's world for the view:
const view = new TwoView(model.world, { patchSize: 20 })
function draw() {
view.clear('black')
view.drawLinks(model.links, { color: 'white' })
view.drawTurtles(model.turtles, t => ({
shape: shapeNames[t.id % shapeNames.length],
color: colorMap.atIndex(t.id).css,
size: 2.5,
}))
}
util.timeoutLoop(
() => {
model.step()
draw()
},
500,
33
).then(() => console.log('done'))
})
</script>
</body>
</html>