Last active
May 8, 2019 03:40
-
-
Save brysonian/98bc61560e4f29ead093507ff4589fd5 to your computer and use it in GitHub Desktop.
Thinking about an alternate API for p5.js's "instance mode"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as p5 from './p5'; | |
| import Thing from './Thing'; | |
| const { | |
| setup, | |
| draw, | |
| background, | |
| } = p5; | |
| const things = []; | |
| setup(() => { | |
| createCanvas(1024, 768); | |
| for (let i = 0; i < 100; i += 1) { | |
| things.push(new Thing(0, p5.random(0, 768), p5.random(1, 5))); | |
| } | |
| }); | |
| draw(() => { | |
| background(255, 255, 255); | |
| things.forEach(t => t.update()); | |
| things.forEach(t => t.render()); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| rect, | |
| fill, | |
| } from './p5'; | |
| export default class Thing { | |
| x = 0; | |
| y = 0; | |
| speed = 0; | |
| constructor(x, y, speed = 1) { | |
| this.x = x; | |
| this.y = y; | |
| this.speed = speed; | |
| } | |
| update() { | |
| this.x += this.speed; | |
| } | |
| render() { | |
| fill(0, 0, 0); | |
| rect(this.x - 5, this.y - 5, 10, 10); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment