Skip to content

Instantly share code, notes, and snippets.

@brysonian
Last active May 8, 2019 03:40
Show Gist options
  • Select an option

  • Save brysonian/98bc61560e4f29ead093507ff4589fd5 to your computer and use it in GitHub Desktop.

Select an option

Save brysonian/98bc61560e4f29ead093507ff4589fd5 to your computer and use it in GitHub Desktop.
Thinking about an alternate API for p5.js's "instance mode"
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());
});
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