Skip to content

Instantly share code, notes, and snippets.

@ecridge
Last active October 27, 2016 17:54
Show Gist options
  • Save ecridge/3b9ef1d715285562bb9b0afcf976a3e9 to your computer and use it in GitHub Desktop.
Save ecridge/3b9ef1d715285562bb9b0afcf976a3e9 to your computer and use it in GitHub Desktop.
p5.js sketch that fills parent element
/**
* p5.js sketch that fills its parent element.
* On my phone on a train so I haven’t actually tested…
*/
var container;
function setup() {
var canvas;
// E.g. for a `<div id="container-id"></div>`.
container = document.getElementById('container-id');
canvas = createCanvas(container.offsetWidth, container.offsetHeight);
canvas.parent('container-id');
// Other setup…
}
/**
* Called automatically when the window is resized.
*/
function windowResized() {
resizeCanvas(container.offsetWidth, container.offsetHeight);
}
function draw() {
// Other code…
// May have to call `resizeCanvas()` periodically here if you have other
// things on your page which cause the container to change shape.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment