2

I try to draw canvas with p5.js but the canvas stop draws when the tab is not active. What should I do? , here is example code

 function setup() {
  frameRate(30);
  textSize(20);
  textSize(30);
  textAlign(CENTER);
}

function draw() {
  background(200);
  text(frameCount, width / 2, height / 2);
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Possible duplicate of [Run canvas on background tab](https://stackoverflow.com/questions/11122949/run-canvas-on-background-tab) – Kevin Workman Apr 20 '18 at 22:59

1 Answers1

1

This is an intentional behavior of JavaScript.

One way around this is to check the time (using the millis() function) and figure out the state of your scene based on that. For example if 10 seconds have elapsed since the last frame, then move your text as if 600 frames have elapsed.

For most sketches this is overkill. Just live with the default behavior.

More info here: Run canvas on background tab

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 2
    I found the solution. Need to set 'backgroundThrottling' to false in browserWindows, but I use electron framework in this project. Thank you anyway. – Kunanan Tassuwan Apr 21 '18 at 07:04