JSXGraph logo
JSXGraph
JSXGraph share

Share

Restrict points to limited area
QR code
<iframe 
    src="https://jsxgraph.uni-bayreuth.de/share/iframe/restrict-points-to-limited-area" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: Restrict points to limited area" 
    allowfullscreen
></iframe>
This code has to
<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 ShareAlike 4.0 International License.
    https://creativecommons.org/licenses/by-sa/4.0/
    
    Please note you have to mention 
    The Center of Mobile Learning with Digital Technology
    in the credits.
    */
    
    const BOARDID = 'board-0';

    var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 5, 5, -5], axis: true, keepaspectratio: true });
    var A = board.create('point', [-1, -1], { name: 'A' }),
        B = board.create('point', [-3, -3], { name: 'B' }),
        C = board.create('point', [-2, 0], { name: 'C' });
    
    board.on('move', function() {
        var list = [A, B, C],
            i;
    
        board.suspendUpdate();   // Prevent updates during correction of the points
        for (i = 0; i < list.length; ++i) {
            list[i].moveTo(
               [
                Math.min(0, list[i].X()),
                Math.min(0, list[i].Y())
               ]
            );
        }
        board.unsuspendUpdate();
    });
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution ShareAlike 4.0 International License.
https://creativecommons.org/licenses/by-sa/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!

var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 5, 5, -5], axis: true, keepaspectratio: true });
var A = board.create('point', [-1, -1], { name: 'A' }),
    B = board.create('point', [-3, -3], { name: 'B' }),
    C = board.create('point', [-2, 0], { name: 'C' });

board.on('move', function() {
    var list = [A, B, C],
        i;

    board.suspendUpdate();   // Prevent updates during correction of the points
    for (i = 0; i < list.length; ++i) {
        list[i].moveTo(
           [
            Math.min(0, list[i].X()),
            Math.min(0, list[i].Y())
           ]
        );
    }
    board.unsuspendUpdate();
});
<jsxgraph width="100%" aspect-ratio="1 / 1" title="Restrict points to limited area" description="This construction was copied from JSXGraph examples database: BTW HERE SHOULD BE A GENERATED LINKuseGlobalJS="false">
   /*
   This example is licensed under a 
   Creative Commons Attribution ShareAlike 4.0 International License.
   https://creativecommons.org/licenses/by-sa/4.0/
   
   Please note you have to mention 
   The Center of Mobile Learning with Digital Technology
   in the credits.
   */
   
   var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 5, 5, -5], axis: true, keepaspectratio: true });
   var A = board.create('point', [-1, -1], { name: 'A' }),
       B = board.create('point', [-3, -3], { name: 'B' }),
       C = board.create('point', [-2, 0], { name: 'C' });
   
   board.on('move', function() {
       var list = [A, B, C],
           i;
   
       board.suspendUpdate();   // Prevent updates during correction of the points
       for (i = 0; i < list.length; ++i) {
           list[i].moveTo(
              [
               Math.min(0, list[i].X()),
               Math.min(0, list[i].Y())
              ]
           );
       }
       board.unsuspendUpdate();
   });
</jsxgraph>

Restrict points to limited area

Points can be restricted to a certain area by correcting their position after a `move` events. In this example, the points *A*, *B*, and *C* are restricted to the lower left quadrant. We do the correction of all three points in a single event handler.
// Define the id of your board in BOARDID

var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 5, 5, -5], axis: true, keepaspectratio: true });
var A = board.create('point', [-1, -1], { name: 'A' }),
    B = board.create('point', [-3, -3], { name: 'B' }),
    C = board.create('point', [-2, 0], { name: 'C' });

board.on('move', function() {
    var list = [A, B, C],
        i;

    board.suspendUpdate();   // Prevent updates during correction of the points
    for (i = 0; i < list.length; ++i) {
        list[i].moveTo(
           [
            Math.min(0, list[i].X()),
            Math.min(0, list[i].Y())
           ]
        );
    }
    board.unsuspendUpdate();
});

license

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