Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sure the answers above are more elegant, but I've been working on this code all morning and I want to share ...</p> <p><a href="http://jsfiddle.net/KaliedaRik/hceDQ/" rel="nofollow" title="Demo of code here">Demo of code here</a></p> <pre><code>//requestAnimFrame shim via Paul Irish window.requestAnimFrame = (function(callback){ return (window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback){window.setTimeout(callback, 1000/60);}); })(); //main code window.onload = function(){ //setup canvas var canvas = document.getElementById('mycanvas'); var ctx = canvas.getContext('2d'); //variables var canvasWidth = 400, canvasHeight = 400; //as set up in the &lt;canvas&gt; tag var animationTime = 10000; //animation length in milliseconds //clear canvas function var clearCanvas = function(){ ctx.clearRect(0, 0, canvasWidth, canvasHeight); }; //define the line; build function for displaying it var _myPtArr = [ {x:297, y:30}, {x:299,y:47}, {x:350,y:56}, {x:305,y:176}, {x:278,y:169}, {x:303,y:108}, {x:269,y:79}, {x:182,y:90}, {x:137,y:81}, {x:173,y:33}, {x:231,y:38} ]; var lineColor = 'red'; var drawLine = function(){ ctx.beginPath(); ctx.moveTo(_myPtArr[0].x, _myPtArr[0].y); for(var i=1, z=_myPtArr.length; i&lt;z; i++){ ctx.lineTo(_myPtArr[i].x, _myPtArr[i].y); } ctx.strokeStyle = lineColor; ctx.stroke(); }; //define the ball; build function for displaying it var myBall = {x: 0, y: 0, radius: 5, fillStyle: 'blue', strokeStyle: 'black'}; var drawBall = function(){ ctx.beginPath(); ctx.arc(myBall.x, myBall.y, myBall.radius, 0, (Math.PI*2)); ctx.closePath(); ctx.fillStyle = myBall.fillStyle; ctx.strokeStyle = myBall.strokeStyle; ctx.fill(); ctx.stroke(); }; //build an array of objects for each line to help ball position calculations var myBallVectors = []; var fullDistance = 0, p, q, dx, dy, tempDistance, tempTime, fullTime = 0; for(var i=1, z=_myPtArr.length; i&lt;z; i++){ p = _myPtArr[i-1], q = _myPtArr[i]; dx = q.x - p.x; dy = q.y - p.y; tempDistance = Math.sqrt((dx*dx)+(dy*dy)); fullDistance += tempDistance; myBallVectors.push({x: p.x, y: p.y, dX: dx, dY: dy, magnitude: tempDistance}); } for(var i=0, z=myBallVectors.length; i&lt;z; i++){ myBallVectors[i]['start'] = fullTime * animationTime; tempTime = myBallVectors[i].magnitude/fullDistance; fullTime += tempTime myBallVectors[i]['duration'] = tempTime * animationTime; myBallVectors[i]['finish'] = fullTime * animationTime; } //function to work out where ball is in space and time var myBallPosition = function(){ if(Date.now() &gt; breakTime){ //move on to next line currentLine++; //check to see if animation needs to finish if(currentLine &gt;= myBallVectors.length){ doAnimation = false; } //otherwise, set new break point else{ breakTime = startTime + parseInt(myBallVectors[currentLine].finish); } } if(doAnimation){ //calculate ball's current position var timePassed = Date.now() - (startTime + myBallVectors[currentLine].start) var percentageLineDone = timePassed/myBallVectors[currentLine].duration; myBall.x = myBallVectors[currentLine].x + (myBallVectors[currentLine].dX * percentageLineDone); myBall.y = myBallVectors[currentLine].y + (myBallVectors[currentLine].dY * percentageLineDone); } }; //setup and display initial scene drawLine(); myBall.x = _myPtArr[0].x; myBall.y = _myPtArr[0].y; drawBall(); //animation loop function animate = function(){ clearCanvas(); drawLine(); myBallPosition(); drawBall(); if(doAnimation){ requestAnimFrame(function(){ animate(); }); } }; //setup control variables, and start animation var startTime = Date.now(), currentLine = 0; var breakTime = startTime + parseInt(myBallVectors[currentLine].finish); var doAnimation = true; animate(); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload