Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paintCanvas is called continuously #62

Closed
pixelzoom opened this issue Aug 30, 2016 · 11 comments
Closed

paintCanvas is called continuously #62

pixelzoom opened this issue Aug 30, 2016 · 11 comments

Comments

@pixelzoom
Copy link
Contributor

As soon as a screen is selected on the Home screen, BallsLayerNode.paintCanvas and GaltonBoardNode.paintCanvas are called continuously. This occurs without having otherwise interacted with the sim, and there's nothing visibly changing on the screen.

@pixelzoom
Copy link
Contributor Author

I reverted to @ fa37ddb, before I started working on this sim. I see BallsLayerNode.paintCanvas called continuously, but not GaltonBoardNode.paintCanvas.

GaltonBoardNode.paintCanvas started being called continuously with eca6eba, the changes made for #58.

@pixelzoom
Copy link
Contributor Author

And here's the offending code in PlinkoProbabilityCommonView:

      // Checks if the galtonBoard has been initially painted and if not then paint it.
      if ( !this.galtonBoardNode.isInitiallyPainted ) {
        this.galtonBoardNode.invalidatePaint();
      }
      // update view on model step
      this.ballsNode.invalidatePaint();

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Aug 30, 2016

GaltonBoardNode.paintCanvas was being called because the isInitiallyPainted flag was removed in the work that I did for #58. So I deleted the call to galtonBoardNode.invalidatePaint because it's no longer needed. That solves half of the problem.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Aug 30, 2016

2 fundamental problems with this sim:

(1) It doesn't adhere to the MVC pattern. For the balls, the view isn't notified when the model has changed. Instead, the view redraws itself on every frame (by calling BallsNode.invalidatePaint), whether the model has changed on not. So the sim is doing a lot of work when nothing is happening.

(2) The sim is relying on the call to BallsNode.invalidatePaint in PlinkoProbabilityCommonView.step as the means to clear the balls from the screen when the hopper mode is set (via radio buttons) to "Path" or "None". Instead, it seems like the sim should simply set the BallsNode instance to invisible.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Sep 2, 2016

Fixed fundamental problem (2) above, so that the BallsNode instance is made invisible when "Ball" is not selected for the hopper mode. This also prevents BallsNode.paintCanvas from being called when "Path" or "None" is selected (which was in fact happening previously.)

pixelzoom added a commit that referenced this issue Sep 2, 2016
…bins were being drawn when histogram mode === 'fraction', '#62
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Sep 9, 2016

Fundamental problem (1) above was not possible to fix. Doing so would have involved fundamental changes to the model, would have surely destabilized the sim, and was ultimately not worth the effort.

Instead, I added a workaround, summarized as follows:

• The model step function is called on every animation frame, and it calls step for each ball.
• If a ball is already at rest in a bin, that ball's step function is a no-op.
• If any ball actually moves, the model sets flag someBallMoved = true.
• In the view step function, the balls are redrawn only if model.someBallMoved == true.
• When the histogram mode is switched to display bins, the balls are redrawn.

So the sim still isn't following the MVC pattern - the model isn't notifying the view when it has changed. But at least the view can consult the model on each time step, and ask it whether the balls need to be redrawn.

pixelzoom added a commit that referenced this issue Sep 9, 2016
@pixelzoom
Copy link
Contributor Author

The above fixes are demonstrated in this dev version:
http://www.colorado.edu/physics/phet/dev/html/plinko-probability/1.0.0-dev.12/plinko-probability_en.html

@pixelzoom
Copy link
Contributor Author

Tested on iPad2 (mobile Safari) and Mac OS X (Chrome, Safari, Firefox). With the exception of the Play button (#26) everything feels smooth and responsive. Closing this issue.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Sep 9, 2016

Reopening because I had another thought about a different way to address this that is more MVC-like. Instead of setting a flag in the model, have an emitter in the model that fires whenever at least 1 ball has moved. BallNode would observe this emitter and call invalidatePaint when it fires. So the model would be informing the view, rather than the view polling the model.

@pixelzoom pixelzoom reopened this Sep 9, 2016
@pixelzoom
Copy link
Contributor Author

There's also a bug in the previous approach. The erase button is not erasing the ball, it needs to call BallsNode.invalidatePaint.

@pixelzoom
Copy link
Contributor Author

New approach implemented in above commit. The model now uses an Emitter to notify the view when the balls have moved, and thus need to be redrawn. This approach conforms to the MVC pattern. (Also fixed the issue with the erase button.)

Demonstrated in this dev version:
http://www.colorado.edu/physics/phet/dev/html/plinko-probability/1.0.0-dev.14/plinko-probability_en.html

Closing.

pixelzoom added a commit that referenced this issue Sep 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant