APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

How do I get the x and y coordinate in javascript in PlotPanel on mousedown post and replies

Return to the How do I get the x and y coordinate in javascript in PlotPanel on mousedown thread
Login to post to this thread

How do I get the x and y coordinate in javascript in PlotPanel
John Val
13 Posts

Dear all,

I would like to start a simulation from a point under a mouseclick in the plotpanel in the javascript version.
In the plotting panel of the FunctionPlotter example I enabled interaction and put
%mouseClick% at the Onpress event.

In this functioen I would like to read the x and y postion under the cursor.
I know I must be able to get them somewhere since in the lower left corner a box with these values appears.

function mouseClick() {
    var x= _view.plottingPanel.pointer.x; //  this does not work. But what does?
    alert(x);
}


Replies to How do I get the x and y coordinate in javascript in PlotPanel

Re: How do I get the x and y coordinate in javascript in PlotPanel -
Wolfgang
192 Posts

You can use the follow JavaScript function:

var position = _view.plottingPanel.getInteraction().getInteractionPoint();

Look at the Data Fitting example to see how it is used.

http://www.compadre.org/osp/items/detail.cfm?ID=13350



Re: Re: How do I get the x and y coordinate in javascript in PlotPanel -
John Val
13 Posts

Dear Wolfgang,

I tried to run the example in EJS 5.2 but got the errors:

ReferenceError: MAX is not defined
TypeError: _model is undefined

I have seen this error also in other examples.

Any idea how to solve this problem?

Yours,
John



Re: How do I get the x and y coordinate in javascript in PlotPanel -
Francisco Esquembre
237 Posts

Hi John,

1.- Add a parameter to the mouseClick function. As "point" in my example below. EjS will provide a double[2] array with the coordinates of the clicked point.

function mouseClick (point) {
  console.log ("Clicked at "+point);
}

2.- specify %mouseClick% at the OnPress event as you did.

3.- Make sure the panel is Enabled.

So, the trick was specifying (and using) a parameter in the mouseClick function.

Paco



Re: How do I get the x and y coordinate in javascript in PlotPanel -
John Val
13 Posts

Dear Wolfgang and Fransisco,

Your sugestions work both fine. Thanks a lot.

Do you know of any document where I can find a reference to all the programming capabilities in EJS?

Yours sincerely,
John Val



OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book