From de5d35a03fa3f283585a940166bd10e1a9f90d10 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 18 Apr 2021 13:15:28 -0700 Subject: [PATCH] feat: incrementally add elements to sketch in draw --- lib/loadp5.js | 2 ++ polytree.coffee | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/loadp5.js b/lib/loadp5.js index edfa4aa..6f08cdc 100644 --- a/lib/loadp5.js +++ b/lib/loadp5.js @@ -1,4 +1,5 @@ export var p5; +export var p5vector; export var p5loaded = new Promise(async function(resolve, reject) { var success = false; try { @@ -13,5 +14,6 @@ export var p5loaded = new Promise(async function(resolve, reject) { catch {} console.log('Used local fallback for p5'); } p5 = window.p5; + p5vector = window.p5.Vector delete window.p5; if (success) { resolve(); } else { reject(); }}); diff --git a/polytree.coffee b/polytree.coffee index 8556dba..17049a9 100644 --- a/polytree.coffee +++ b/polytree.coffee @@ -1,13 +1,25 @@ # PolyTree, intended to be loaded as a module -import {p5, p5loaded} from './lib/loadp5.js' +import {p5, p5loaded, p5vector} from './lib/loadp5.js' sketch = (p) -> + pos = null + vel = null + abs = Math.abs # For convenience + p.setup = => p.createCanvas window.innerWidth, 575, p.WEBGL p.background 224 - p.ellipse 0, 0, 100, 50 - p.ellipse p.width/4, -p.height/4, 50, 100 + p.frameRate 5 + pos = p.createVector p.width, p.height + + p.draw = => + if abs(pos.x) > p.width/2 or abs(pos.y) > p.height/2 + pos = p.createVector 0 + vel = p5vector.random2D().mult 32 + else + pos.add vel + p.point pos show = () -> P5 = new p5 sketch