Resizing p5.Graphics

Hi all ,

I have a use case in which I draw few small dots ( graphics.ellipse(…) ) on ( for efficiency purpose ) . now on resizing the window I want to resize the graphics along with it’s content ( those small dots ) .

I tried to use resize funs on graphics but it doesn’t seems to b working .
Has any one ever faced such issue or any one can guide on what can be done to achieve this

thanks in advance

1 Like

Hi GotoLoop

I tried this but still no luck .

function setup() {
createCanvas(maxWindowWidth, maxWindowHeight);
graphics = createGraphics(width, height);


function windowResized() {

// canvas is getting resized as expected
resizeCanvas(windowWidth, windowHeight);

// tried but no luck


Shouldn’t you use width & height instead as you did for:
graphics = createGraphics(width, height);

graphics.size(width, height);

Tried that too , but still there is no effect on the graphics size .

Why don’t you post your whole code, including draw()?
Better yet, host it on:

1 Like

not sure if you need anyhow:

function windowResized() {
  resizeCanvas(windowWidth, windowHeight); // disable that for test
  print(" windowinfo width: " + width + " height: " + height + " windowWidth " + windowWidth + " windowHeight " + windowHeight);

ok, sorry you have already.

1 Like

It’s really confusing , I am getting two different type of results .

I tried online and it worked as expected , content was getting resized with the browser resize .

and I copied the same code to my local setup (with same version as on online editor ) and got completely different result .
1. there is no resize effect .
2. on resizing i am getting one extra blue dot with white background.

refer the snapshot of these two behaviors

am i missing anything ?

Found the issue finally ,

width and height variables contains the dimension of the canvas being created and they only update once you resize the canvas .

whereas windowWidth and windowHeight gets updated as soon as the size of the browser is changed .

therefore use windowWidth and windowHeight …

thanks GoToLoop for suggesting the size() method .

in p5.js


works also