HTML5 Canvas Start and Stop an Animation Example
Instructions: Click on the canvas to start and stop the animation. Open in new window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | window.requestAnimFrame = (function(callback){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback){ window.setTimeout(callback, 1000 / 60); }; })(); function drawRect(myRectangle){ var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); context.beginPath(); context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height); context.fillStyle = "#8ED6FF"; context.fill(); context.lineWidth = myRectangle.borderWidth; context.strokeStyle = "black"; context.stroke(); } function animate(lastTime, myRectangle, animProp){ if (animProp.animate) { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); // update var date = new Date(); var time = date.getTime(); var timeDiff = time - lastTime; var linearSpeed = 100; // pixels / second var linearDistEachFrame = linearSpeed * timeDiff / 1000; var currentX = myRectangle.x; if (currentX < canvas.width - myRectangle.width - myRectangle.borderWidth / 2) { var newX = currentX + linearDistEachFrame; myRectangle.x = newX; } lastTime = time; // clear context.clearRect(0, 0, canvas.width, canvas.height); // draw drawRect(myRectangle); // request new frame requestAnimFrame(function(){ animate(lastTime, myRectangle, animProp); }); } } window.onload = function(){ var myRectangle = { x: 0, y: 50, width: 100, height: 50, borderWidth: 5 }; /* * make the animation properties an object * so that it can be modified by reference * from an event */ var animProp = { animate: false }; // add click listener to canvas document.getElementById("myCanvas").addEventListener("click", function(){ if (animProp.animate) { animProp.animate = false; } else { animProp.animate = true; var date = new Date(); var time = date.getTime(); animate(time, myRectangle, animProp); } }); drawRect(myRectangle); }; |
Hi, is it possible to change the square for an image? Or to place an image into the square and make the background tranparant?
Thank you for answering.
Cheers,
Dinant
@Dinant sure you can do that. Just animate the image along with the rectangle
Hi, first thing love your work been great help many thanks..
I’ve a question regarding a project of mine..
I’ve currently got three objects saved in an array moving around the screen however i would like the animation to freeze if the user hovers over the object and make something pop up until they move off..
can the the start and stop be achieved using the example start and stop with some tweaks?
Thanks
Michael
@Michael absolutely, should work just the same