Skip to content

Commit

Permalink
Constrain dragged point to chart bounds, see #27
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jun 11, 2021
1 parent ef93365 commit 79084b0
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions js/demo/DemoMultiplePlots.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import Property from '../../../axon/js/Property.js';
import Bounds2 from '../../../dot/js/Bounds2.js';
import dotRandom from '../../../dot/js/dotRandom.js';
import Range from '../../../dot/js/Range.js';
import Utils from '../../../dot/js/Utils.js';
Expand Down Expand Up @@ -44,7 +45,7 @@ class DemoMultiplePlots extends VBox {
viewWidth: 600,
viewHeight: 400,
modelXRange: new Range( 2, 10 ),
modelYRange: new Range( Math.exp( 2 ), Math.exp( 10 ) ),
modelYRange: new Range( 1, 22000 ),
yScale: new Transform1( Math.log, Math.exp, {
domain: new Range( 0, Number.POSITIVE_INFINITY ),
range: new Range( 0, Number.POSITIVE_INFINITY )
Expand Down Expand Up @@ -130,7 +131,8 @@ class DemoMultiplePlots extends VBox {

const point = event.pointer.point;
const parentPoint = chartRectangle.globalToParentPoint( point );
const modelPt = chartTransform.viewToModelPoint( parentPoint );
const constrainedParentPoint = new Bounds2( 0, 0, chartTransform.viewWidth, chartTransform.viewHeight ).closestPointTo( parentPoint );
const modelPt = chartTransform.viewToModelPoint( constrainedParentPoint );

readout.text = `x: ${Utils.toFixed( modelPt.x, 1 )}, y: ${Utils.toFixed( modelPt.y, 1 )}`;
};
Expand Down

0 comments on commit 79084b0

Please sign in to comment.