Getting Cursor X and Y Positions

143 views
Skip to first unread message

Minoush M

unread,
Apr 21, 2021, 8:42:30 AM4/21/21
to JSXGraph
Hello,

I would like the user to be able to create a point at the position of the mouse click. However, I do not know how to get the X,Y coordinates of the mouse cursor inside the JSX board when the mouse gets clicked. 

How would I get the X and Y as shown below?

const board = JXG.JSXGraph.initBoard('jxgbox', { 
            boundingbox: [0, 10, 10, 0], axis:true
        });
board.on('up', function () { 
        x = // get cursor's X
        y = // get cursor's Y
        var somePoint = board.create('point',[x,y], {name:'SomePoint',size:4});
        });

My apologies if the answer is straightforward. I searched the API and could not find it.

Thanks.

Alfred Wassermann

unread,
Apr 21, 2021, 9:26:52 AM4/21/21
to JSXGraph
You can get the coordinates with board.getUsrCoordsOfMouse(). Here is your example:

const board = JXG.JSXGraph.initBoard('jxgbox', { 
            boundingbox: [0, 10, 10, 0], axis:true
        });
board.on('up', function (evt) {
        var a = board.getUsrCoordsOfMouse(evt),
            x = a[0],
            y = a[1],
            somePoint = board.create('point', [x,y], {name:'SomePoint',size:4});
            // Shorter version:
            // somePoint = board.create('point', a, {name:'SomePoint',size:4});
        });
Best wishes,
Alfred



Minoush M

unread,
Apr 21, 2021, 9:37:56 AM4/21/21
to JSXGraph
Awesome! Works like a charm. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages