JSXGraph logo
JSXGraph
JSXGraph share

Share

Add generic DOM events to JSXGraph elements
QR code
<iframe 
    src="https://www.jsxgraph.org/share/iframe/add-generic-dom-events-to-jsxgraph-elements" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: Add generic DOM events to JSXGraph elements" 
    allowfullscreen
></iframe>
This code has to
<p id="myOutput">&nbsp;</p>


<div id="board-0-wrapper" class="jxgbox-wrapper " style="width: 100%; ">
   <div id="board-0" class="jxgbox" style="aspect-ratio: 1 / 1; width: 100%;" data-ar="1 / 1"></div>
</div>

<script type = "text/javascript"> 
    /*
    This example is licensed under a 
    Creative Commons Attribution 4.0 International License.
    https://creativecommons.org/licenses/by/4.0/
    
    Please note you have to mention 
    The Center of Mobile Learning with Digital Technology
    in the credits.
    */
    
    const BOARDID = 'board-0';

    const board = JXG.JSXGraph.initBoard(BOARDID, {
        boundingbox: [-4, 4, 4, -4],
        keepaspectratio: true
    });
    var p = board.create('point', [1, 1], {
        size: 5
    });
    
    JXG.addEvent(p.rendNode, 'mouseover',
        function() {
            document.getElementById('myOutput').innerHTML = "Point " + this.name;
        },
        p);
    JXG.addEvent(p.rendNode, 'mouseout',
        function() {
            document.getElementById('myOutput').innerHTML = '&nbsp;';
        },
        p);
    
    // User can not move "A" anymore.
    p.hasPoint = function() {
        return false;
    };
    
    var p2 = board.create('point', [-1, 1], {
        size: 5
    });
    
    JXG.addEvent(p2.rendNode, 'mouseover',
        function() {
            document.getElementById('myOutput').innerHTML = "Point " + this.name;
        },
        p2);
    
    JXG.addEvent(p2.rendNode, 'mouseout',
        function() {
            document.getElementById('myOutput').innerHTML = '&nbsp;';
        },
        p2);
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution 4.0 International License.
https://creativecommons.org/licenses/by/4.0/

Please note you have to mention 
The Center of Mobile Learning with Digital Technology
in the credits.
*/

const BOARDID = 'your_div_id'; // Insert your id here!

const board = JXG.JSXGraph.initBoard(BOARDID, {
    boundingbox: [-4, 4, 4, -4],
    keepaspectratio: true
});
var p = board.create('point', [1, 1], {
    size: 5
});

JXG.addEvent(p.rendNode, 'mouseover',
    function() {
        document.getElementById('myOutput').innerHTML = "Point " + this.name;
    },
    p);
JXG.addEvent(p.rendNode, 'mouseout',
    function() {
        document.getElementById('myOutput').innerHTML = '&nbsp;';
    },
    p);

// User can not move "A" anymore.
p.hasPoint = function() {
    return false;
};

var p2 = board.create('point', [-1, 1], {
    size: 5
});

JXG.addEvent(p2.rendNode, 'mouseover',
    function() {
        document.getElementById('myOutput').innerHTML = "Point " + this.name;
    },
    p2);

JXG.addEvent(p2.rendNode, 'mouseout',
    function() {
        document.getElementById('myOutput').innerHTML = '&nbsp;';
    },
    p2);

Add generic DOM events to JSXGraph elements

*Please note:* Adding generic DOM events works only if SVG renderer is used. Nearly all JSXGraph elements have a subobject `rendNode` which is a pointer to its SVG element in the DOM. This element can be supplied with event listeners.

 

<p id="myOutput">&nbsp;</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
});

JXG.addEvent(p.rendNode, 'mouseover',
    function() {
        document.getElementById('myOutput').innerHTML = "Point " + this.name;
    },
    p);
JXG.addEvent(p.rendNode, 'mouseout',
    function() {
        document.getElementById('myOutput').innerHTML = ' ';
    },
    p);

// User can not move "A" anymore.
p.hasPoint = function() {
    return false;
};

var p2 = board.create('point', [-1, 1], {
    size: 5
});

JXG.addEvent(p2.rendNode, 'mouseover',
    function() {
        document.getElementById('myOutput').innerHTML = "Point " + this.name;
    },
    p2);

JXG.addEvent(p2.rendNode, 'mouseout',
    function() {
        document.getElementById('myOutput').innerHTML = ' ';
    },
    p2);

license

This example is licensed under a Creative Commons Attribution 4.0 International License.
Please note you have to mention The Center of Mobile Learning with Digital Technology in the credits.