Skip to content

Instance Mode

Quinton Ashley edited this page Oct 18, 2024 · 7 revisions

p5's instance mode enables multiple sketches to run on one page. To avoid needing to preface every p5 function with p. you can use a JS with statement. This works with Q5 as well.

let sketch = (p) => {
	with (p) {
		p.setup = () => {
			createCanvas(400, 400);
		};
		p.draw = () => {
			background(100);
		};
	}
};

let myp5 = new p5(sketch);

q5 introduces a new "instance" mode. You can call the instance variable whatever you like.

let q = new Q5('instance');

with (q) {
	q.setup = () => {
		createCanvas(400, 400);
	};
	q.draw = () => {
		background(100);
	};
}
Clone this wiki locally