Mutually dependent line and gliders: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 27: Line 27:


</jsxgraph>
</jsxgraph>
===The JavaScript code===
<source lang="javascript">
var board = JXG.JSXGraph.initBoard('jxgbox', {
    boundingbox: [-5,5,5,-5],
    keepaspectratio: true, axis:false, grid:true
    });
board.create('axis',[[0,0],[1,0]], {ticks: {majorHeight:20}});
board.create('axis',[[0,0],[0,1]], {ticks: {majorHeight:20}});
var el0 = board.create('line',[[0,0], [0,1]], {strokeOpacity: .2, strokeColor: '#000000', fixed: true, name: 'x=0'});
var el1 = board.create('line',[[1,0],[1,1]], {strokeOpacity: .2, strokeColor: '#000000', fixed: true, name: 'x=1'});
var gl1 = board.create('glider', [0,0, el0], {name:'b'});
var gl2 = board.create('glider', [1,0, el1], {name:'a'});
var line = board.create('line', [gl1, gl2]);
gl1.draggable = function() {return true; };
gl2.draggable = function() {return true; };
var offset = gl2.Y() - gl1.Y();
gl1.on('drag', function() {
    gl2.moveTo([gl1.X(), gl1.Y() + offset]);
    });
gl2.on('drag', function() {
    offset = gl2.Y() - gl1.Y();
    });
</source>
[[Category:Examples]]

Revision as of 08:19, 1 June 2012

The JavaScript code

var board = JXG.JSXGraph.initBoard('jxgbox', {
    boundingbox: [-5,5,5,-5],
    keepaspectratio: true, axis:false, grid:true
    });
board.create('axis',[[0,0],[1,0]], {ticks: {majorHeight:20}});
board.create('axis',[[0,0],[0,1]], {ticks: {majorHeight:20}});

var el0 = board.create('line',[[0,0], [0,1]], {strokeOpacity: .2, strokeColor: '#000000', fixed: true, name: 'x=0'});
var el1 = board.create('line',[[1,0],[1,1]], {strokeOpacity: .2, strokeColor: '#000000', fixed: true, name: 'x=1'});

var gl1 = board.create('glider', [0,0, el0], {name:'b'});
var gl2 = board.create('glider', [1,0, el1], {name:'a'});
var line = board.create('line', [gl1, gl2]);
gl1.draggable = function() {return true; };
gl2.draggable = function() {return true; };

var offset = gl2.Y() - gl1.Y();

gl1.on('drag', function() {
    gl2.moveTo([gl1.X(), gl1.Y() + offset]);
    });
gl2.on('drag', function() {
    offset = gl2.Y() - gl1.Y();
    });