Share JSXGraph: example "Adding JSXGraph events"

JSXGraph
Share JSXGraph: example "Adding JSXGraph events"
This website is a beta version. The official release will be in **2024**.

Adding JSXGraph events

It is possible to supply user-defined defined functions for certain events. The user-defined functions are called additionally to the default JSXGraph events. For the events `down` and `up` the user-supplied event handlers are called before the default JSXGraph event handler. For the event `update` the user-supplied handler is called after the default JSXGraph event handler which is an update of the board. A user-supplied event handler can be defined by the method `on`: ~~~ var p = board.create('point',[1,1]); p.on('update', function(e) { /* do something ... */); ~~~ The possible events are listed in the API docs for [elements, see Event Summary](https://jsxgraph.org/docs/symbols/JXG.GeometryElement.html) and [board, see Event Summary](https://jsxgraph.org/docs/symbols/JXG.Board.html). As of version 1.5.0, it is only possible to access the element via *this* if the event listener is supplied as regular JavaScript function and not as *arrow function*.

 

<p id="myOutput"> </p>
// Define the id of your board in BOARDID

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-4, 4, 4, -4],
    keepaspectratio: true
});

var p = board.create('point', [1, 1], {
    size: 5,
    fixed: true
});
p.on('over', (e) => {
    document.getElementById('myOutput').innerHTML = "Point " + p.name;
});

p.on('out', (e) => {
    document.getElementById('myOutput').innerHTML = ' ';
});

var p2 = board.create('point', [-1, 1], {
    size: 5
});
p2.on('over', function(e) {
    document.getElementById('myOutput').innerHTML = "Point " + this.name;
});
p2.on('out', function(e) {
    document.getElementById('myOutput').innerHTML = ' ';
});