Skip to content

Commit

Permalink
Using canvas nodes to render natures mix of isotopes for performance …
Browse files Browse the repository at this point in the history
…enhancement, see #26
  • Loading branch information
aadish committed Jan 15, 2016
1 parent 537f906 commit cc5c216
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions js/mix-isotopes/model/MixIsotopesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ define( function( require ) {
// List of the isotope buckets.
this.bucketList = new ObservableArray();
this.isotopesList = new ObservableArray();
this.naturesIsotopesList = [];

// List of the numerical controls that, when present, can be used to add
// or remove isotopes to/from the test chamber.
Expand Down Expand Up @@ -586,6 +587,7 @@ define( function( require ) {
// Clear out anything that is in the test chamber. If anything
// needed to be stored, it should have been done by now.
this.removeAllIsotopesFromTestChamberAndModel();
self.naturesIsotopesList = [ ];

// Get the list of possible isotopes and then sort it by abundance
// so that the least abundant are added last, thus assuring that
Expand All @@ -605,7 +607,6 @@ define( function( require ) {
// one. This behavior was requested by the design team.
numToCreate = 1;
}
// { MovableAtom[] }
var isotopesToAdd = [];
for ( var i = 0; i < numToCreate; i++ ) {
var newIsotope = new MovableAtom( isotopeConfig.protonCount, isotopeConfig.neutronCount, self.testChamber.generateRandomLocation() );
Expand All @@ -615,11 +616,11 @@ define( function( require ) {
newIsotope.radius = SMALL_ISOTOPE_RADIUS;
newIsotope.showLabel = false;
isotopesToAdd.push( newIsotope );
self.isotopesList.add( newIsotope );
// notifyIsotopeInstanceAdded( newIsotope );
self.naturesIsotopesList.push( newIsotope );
}
self.testChamber.bulkAddIsotopesToChamber( isotopesToAdd );
});
this.trigger( 'naturesIsotopeUpdated' );

// Add the isotope controllers (i.e. the buckets).
this.addIsotopeControllers();
Expand All @@ -635,9 +636,11 @@ define( function( require ) {
// TODO Check back on this
removeAllIsotopesFromTestChamberAndModel: function() {
var self = this;
this.testChamber.containedIsotopes.forEach( function( isotope ) {
self.isotopesList.remove( isotope );
} );
if ( this.isotopesList.length > 0 ) {
this.testChamber.containedIsotopes.forEach( function( isotope ) {
self.isotopesList.remove( isotope );
} );
}
this.testChamber.removeAllIsotopes( true );
},

Expand Down
12 changes: 12 additions & 0 deletions js/mix-isotopes/view/MixIsotopesScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define( function( require ) {
var Dimension2 = require( 'DOT/Dimension2' );
var EraserButton = require( 'SCENERY_PHET/buttons/EraserButton' );
var ExpandedPeriodicTableNode = require( 'SHRED/view/ExpandedPeriodicTableNode' );
var IsotopeCanvasNode = require( 'SHRED/view/IsotopeCanvasNode' );
var HSlider = require( 'SUN/HSlider' );
var inherit = require( 'PHET_CORE/inherit' );
var isotopesAndAtomicMass = require( 'ISOTOPES_AND_ATOMIC_MASS/isotopesAndAtomicMass' );
Expand Down Expand Up @@ -232,6 +233,15 @@ define( function( require ) {
lineWidth: 1
} );
chamberLayer.addChild( testChamberNode );
this.isotopesLayer = new IsotopeCanvasNode( this.model.naturesIsotopesList, this.mvt, {
canvasBounds: this.mvt.modelToViewBounds( this.model.testChamber.getTestChamberRect() )
} );
this.addChild( this.isotopesLayer );
this.isotopesLayer.visible = false;
this.model.on( 'naturesIsotopeUpdated', function() {
self.isotopesLayer.setIsotopes( self.model.naturesIsotopesList );

});

var isotopeProprotionsPieChart = new IsotopeProprotionsPieChart( this.model );
isotopeProprotionsPieChart.scale( 0.6 );
Expand Down Expand Up @@ -299,10 +309,12 @@ define( function( require ) {
if ( mixIsotopesModel.showingNaturesMixProperty.get() === true ){
interactivityModeSelectionNode.visible = false;
clearBoxButton.visible = false;
self.isotopesLayer.visible = true;
}
else{
interactivityModeSelectionNode.visible = true;
clearBoxButton.visible = true;
self.isotopesLayer.visible = false;
}
} );

Expand Down

0 comments on commit cc5c216

Please sign in to comment.