UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

129 lines (111 loc) 3.88 kB
<html> <head> <title>plot</title> <link rel="icon" type="image/x-icon" href="../favicon.ico" /> <!-- <style> #modelDiv { height: 400px; } </style> --> </head> <body> <div id="modelDiv"></div> <canvas id="plotCanvas"></canvas> <script type="module"> import * as util from '../src/utils.js' import Color from '../src/Color.js' import ThreeDraw from '../src/ThreeDraw.js' import Model from './Camera3DModel.js' import Chart from 'https://cdn.skypack.dev/chart.js@2.9.4' import dat from '../vendor/dat.gui.js' const toDeg = 180 / Math.PI const isPixel = t => t.breed.name === 'pixels' const uvToColor = t => { const { u, v } = t const r = Math.sqrt(u ** 2 + v ** 2) return Color.toTypedColor(`hsl(${(r * 240) % 240}, 100%, 50%)`) } const drawOptions = { turtlesMesh: 'Obj3DMesh', turtlesShape: t => (isPixel(t) ? 'Cube' : 'Dart'), turtlesColor: t => isPixel(t) ? uvToColor(t) : Color.toTypedColor('red'), turtlesSize: t => (isPixel(t) ? 0.35 : 3), linksColor: l => uvToColor(l.end0), } async function run() { const model = new Model() await model.startup() model.setup() const view = new ThreeDraw(model, { div: 'modelDiv', drawOptions, }) util.toWindow({ util, model, view }) const chart = initPlot() const gui = runGui(model, view, chart) plot(chart, model) await util.timeoutLoop( () => { model.step() view.draw() }, -1, //500, 33 ) } run() function runGui(model, view, chart) { const gui = new dat.GUI() // Helper for adding variables to gui. // "listen" to make reset values to appear in gui menu // "onChange" to have model update camera for each change. const guiAddVal = (obj, name, start, stop, step) => { gui.add(obj, name, start, stop, step) .listen() .onChange(val => { model.moveCamera() plot(chart, model) }) } guiAddVal(model, 'heading', -180, 180, 5) guiAddVal(model, 'tilt', -180, 180, 5) guiAddVal(model, 'roll', -180, 180, 5) guiAddVal(model, 'sphereRadius', 0, 16, 1) guiAddVal(model, 'fieldOfView', 1, 180, 1) gui.add(model, 'toggleLinks') gui.add(model, 'reset').onFinishChange(() => { plot(chart, model) }) return gui } function initPlot() { const canvas = document.getElementById('plotCanvas') const chart = new Chart(canvas, { type: 'scatter', data: { // labels: ticks, datasets: [ { data: [], label: 'PhiTheta', backgroundColor: 'red', borderColor: 'red', }, ], }, }) return chart } function plot(chart, model) { chart.clear() const points = model.pixels.map(t => ({ x: t.theta * toDeg, y: t.pitch * toDeg, })) chart.data.datasets[0].data = points chart.update() } </script> </body> </html>