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

Implement particles using Sprites #276

Closed
pixelzoom opened this issue Mar 11, 2021 · 4 comments
Closed

Implement particles using Sprites #276

pixelzoom opened this issue Mar 11, 2021 · 4 comments
Assignees

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Mar 11, 2021

To address the performance issues reported in #209, investigate using Sprites to draw shaker particles and precipitate particles. They are currently implemented as a subclass of CanvasNode. See base class ParticlesNode.ts.

ShakerParticlesNode.ts:

screenshot_2054

PrecipitateNode.ts:

screenshot_2051

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Dec 6, 2022

I took a look at this. It's going to be a significant chunk of work. And the implementation will be much more complicated -- the currently implementation (ParticlesNode extends CanvasNode) is very simple. So over in #209 I'm going to try to improve the performance of ParticlesNode by reducing the number of things that are computed/allocated in paintCanvas.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Dec 12, 2022

Questions for @jonathanolson via Slack DM:

I’m investigating re-implementing the particle systems in Beer’s Law Lab using Sprites (they are currently CanvasNode). Each particle is a square, with random rotation. What’s the best way to create an ImageableImage for use with SpriteImage? I’ve considered new Rectangle().rasterize(), but it's return type is Node. It sure would be convenient if any Node could be used with SpriteImage, or if there was a utility for converting any Node to what’s needed by SpriteImage.

Here’s what I came up with. It seems to work if I add it to demoSprites.js. But in TypeScript, it involves a cast (from Node to Image) that feels unsafe. Is there a better way?

const particleRectangle = new Rectangle( 0, 0, 50, 50, {
  fill: 'red',
  stroke: 'black'
} );
const particleCanvasElement = ( particleRectangle.rasterized( { wrap: false } ) as Image ).image;
const particleSpriteImage = new SpriteImage( particleCanvasElement, particleRectangle.center );

@jonathanolson
Copy link
Contributor

Node has toCanvas() which is a bit older and clunky, but can rasterize the Node to a Canvas for use in images. The main thing rasterized() provides is the resolution scaling. If you don't need that, you could just directly use toCanvas() (it is synchronous but has an asynchronous api currently).

pixelzoom added a commit to phetsims/scenery-phet that referenced this issue Dec 13, 2022
@pixelzoom
Copy link
Contributor Author

As reported in #209, there is no longer any evidence of a performance issue with the current CanvasNode implementation. So I see no reason to convert to Sprites, and I'm closing this issue.

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

2 participants