-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize ChartCanvasNode from CanvasLinePlot, see #16
- Loading branch information
Showing
5 changed files
with
130 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2020, University of Colorado Boulder | ||
|
||
import bamboo from './bamboo.js'; | ||
|
||
/** | ||
* Performs an operation on a canvas context. Typically this would render something, but some implementations | ||
* may just change the context state (such as transform or stroke). | ||
* | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
class CanvasPainter { | ||
|
||
// Modeled as a class for readability because JavaScript does not have interfaces | ||
constructor() { | ||
} | ||
|
||
// @public @abstract - override to paint or change the canvas context state | ||
paintCanvas( context ) { | ||
assert && assert( false, 'should be overridden in subclasses' ); | ||
} | ||
} | ||
|
||
bamboo.register( 'CanvasPainter', CanvasPainter ); | ||
export default CanvasPainter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2020, University of Colorado Boulder | ||
|
||
/** | ||
* Renders a dataset of Vector2[] on a canvas. Typically it is preferable to use LinePlot, but this alternative | ||
* is provided for cases where canvas must be used for performance. | ||
* | ||
* @author Sam Reid (PhET Interactive Simulations) | ||
*/ | ||
|
||
import merge from '../../phet-core/js/merge.js'; | ||
import bamboo from './bamboo.js'; | ||
import CanvasPainter from './CanvasPainter.js'; | ||
|
||
class ChartCanvasLinePlot extends CanvasPainter { | ||
|
||
/** | ||
* @param {ChartModel} chartModel | ||
* @param {Vector2[]} dataSet | ||
* @param {Object} [options] | ||
*/ | ||
constructor( chartModel, dataSet, options ) { | ||
|
||
options = merge( { | ||
stroke: 'black', | ||
lineWidth: 1 | ||
}, options ); | ||
|
||
super(); | ||
|
||
// @private | ||
this.chartModel = chartModel; | ||
|
||
// @public if you change this directly, you are responsible for calling update on the corresponding ChartCanvasNode | ||
this.dataSet = dataSet; | ||
this.stroke = options.stroke; | ||
this.lineWidth = options.lineWidth; | ||
} | ||
|
||
/** | ||
* Sets dataSet. You are responsible for calling update on the ChartCanvasNode | ||
* @param {Vector2[]} dataSet | ||
* @public | ||
*/ | ||
setDataSet( dataSet ) { | ||
this.dataSet = dataSet; | ||
} | ||
|
||
// @public | ||
paintCanvas( context ) { | ||
context.beginPath(); | ||
context.strokeStyle = this.stroke; | ||
context.lineWidth = this.lineWidth; | ||
|
||
for ( let i = 0; i < this.dataSet.length; i++ ) { | ||
const point = this.chartModel.modelToViewPosition( this.dataSet[ i ] ); | ||
i === 0 && context.moveTo( point.x, point.y ); | ||
i !== 0 && context.lineTo( point.x, point.y ); | ||
} | ||
context.stroke(); | ||
} | ||
|
||
/** | ||
* @public | ||
* @override | ||
*/ | ||
dispose() { | ||
this.disposeLinePlot(); | ||
super.dispose(); | ||
} | ||
} | ||
|
||
bamboo.register( 'ChartCanvasLinePlot', ChartCanvasLinePlot ); | ||
export default ChartCanvasLinePlot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters