Skip to content

Commit

Permalink
improve performance of ParticlesNode (Canvas), #209
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Dec 5, 2022
1 parent 357c719 commit 90d2885
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 11 additions & 0 deletions js/concentration/model/SoluteParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export default class SoluteParticle extends PhetioObject {
public readonly positionProperty: Property<Vector2>;
public readonly orientation: number;

// Precompute these things, to improve the performance of ParticlesNode.
public readonly cos: number; // cosine of orientation
public readonly sin: number; // since of orientation
public readonly fillStyle: string; // fillStyle for rendering with Canvas
public readonly strokeStyle: string; // strokeStyle for rendering with Canvas

/**
* @param color
* @param size particles are square, this is the length of one side
Expand All @@ -57,6 +63,11 @@ export default class SoluteParticle extends PhetioObject {
this.size = size;
this.positionProperty = new Vector2Property( position );
this.orientation = orientation;

this.cos = Math.cos( orientation );
this.sin = Math.sin( orientation );
this.fillStyle = color.getCanvasStyle();
this.strokeStyle = color.darkerColor().getCanvasStyle();
}

public toStateObject(): SoluteParticleStateObject {
Expand Down
16 changes: 7 additions & 9 deletions js/concentration/view/ParticlesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class ParticlesNode extends CanvasNode {
*/
public override paintCanvas( context: CanvasRenderingContext2D ): void {

const particles = this.particleGroup.getArray();
const particles = this.particleGroup.getArray(); // reference - do not modify!
const numberOfParticles = particles.length;

// Set and compute static properties that should be shared by all the particles, and start the path.
Expand All @@ -51,21 +51,19 @@ export default class ParticlesNode extends CanvasNode {

const halfViewSize = this.modelViewTransform.modelToViewDeltaX( particles[ 0 ].size ) * Math.SQRT2 / 2;

context.fillStyle = particles[ 0 ].color.getCanvasStyle();
context.strokeStyle = particles[ 0 ].color.darkerColor().getCanvasStyle();
context.fillStyle = particles[ 0 ].fillStyle;
context.strokeStyle = particles[ 0 ].strokeStyle;
context.lineWidth = 1;

context.beginPath();

// draw into one big path
for ( let i = 0; i < numberOfParticles; i++ ) {
const particle = particles[ i ];

const position = this.modelViewTransform.modelToViewPosition( particle.positionProperty.value );
const x = position.x;
const y = position.y;
const cos = Math.cos( particle.orientation ) * halfViewSize;
const sin = Math.sin( particle.orientation ) * halfViewSize;
const x = this.modelViewTransform.modelToViewX( particle.positionProperty.value.x );
const y = this.modelViewTransform.modelToViewY( particle.positionProperty.value.y );
const cos = particle.cos * halfViewSize;
const sin = particle.sin * halfViewSize;
context.moveTo( x + cos, y + sin );
context.lineTo( x - sin, y + cos );
context.lineTo( x - cos, y - sin );
Expand Down

0 comments on commit 90d2885

Please sign in to comment.