User:Cleonis/sandbox/requestAnimationFrame

From JSXGraph Wiki
Revision as of 18:47, 26 August 2014 by Cleonis (talk | contribs) (Created page with "<jsxgraph width="500" height="500"> var board = JXG.JSXGraph.initBoard( 'jxgbox', {boundingbox:[-1.5,1.5,1.5,-1.5], keepaspectratio:true, axis:true} ); var isPlaying = fals...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


    

The JavaScript code

var board = JXG.JSXGraph.initBoard(
  'jxgbox',
  {boundingbox:[-1.5,1.5,1.5,-1.5], keepaspectratio:true, axis:true}
);

var isPlaying = false;
var starttime = 0;
var timeElapsed = 0;
var requestId = 0;

var p = board.create(
  'point',
  [function() {return Math.cos(timeElapsed)}, function() {return Math.sin(timeElapsed)}]
);

function playing() {
  timeElapsed = (Date.now() - starttime)/1000;
  board.update();
  requestId = window.requestAnimationFrame(playing);
}

function play() {
  if (!isPlaying) {
    starttime = Date.now();
    isPlaying = true;
    window.requestAnimationFrame(playing);
  }
}

function resetAnimation() {
  if (requestId) {
    window.cancelAnimationFrame(requestId);
  }
  timeElapsed = 0;
  isPlaying = false;
  board.update();
}

The HTML

&nbsp;&nbsp;<button id="playbutton" onclick="play()">play</button>&nbsp;&nbsp;<button id="resetanimation" onclick="resetAnimation()">reset</button>