Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P5 draw some items only once, but keep them rendered continuously

in P5 I have some drawings that remain static the entire time. It seems inefficient to put them in the draw method where they'll be drawn and redrawn again and again since they're not moving.

I tried placing the static drawings in setup but that doesn't work.

Is there a way to have some items drawn once and then kept statically rendered for more efficiency?

like image 512
risingBirdSong Avatar asked Nov 29 '25 08:11

risingBirdSong


2 Answers

Sounds like you might be looking for the createGraphics function, which allows you to create a buffer that you can draw to. You'd only need to draw to the buffer once, and then you can draw the buffer to the screen each frame.

See this search for more info.

You can also learn more in the p5.js reference.

like image 73
Kevin Workman Avatar answered Dec 01 '25 20:12

Kevin Workman


you could just put it in the draw function if it's not too complicated. I can render thousands of ellipses moving around without lagging. if you are using a for loop to draw some very complicated pattern then you should probably use the createGraphics function. the reason that you can't put it in the setup function is that if you have a background in the draw function it basically clears everything on the screen every frame

like image 37
Tony Zhang Avatar answered Dec 01 '25 20:12

Tony Zhang