|
| 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