Rotating Circle Animation in HTML5

This article describes 2-D transformations, especially translation and rotation.
Step 1
The first step is to define the parameters of the circle. Keep in mind that the preceding circle is not one single arc drawn as a circle, but 10 sectors drawn around the center to make a circle. So each sector will subtend an angle of 36 degrees.
  1. var centreX = 100; var centreY = 100;
  2. var radius = 75;
  3. var rotateAngle = 36 * Math.PI / 180;
  4. var startAngle = 0 * Math.PI / 180;
  5. var endAngle = 36 * Math.PI / 180;
Step 2
We create an array of colors to fill each sector of a circle.
var colors = ["teal", "red", "green", "blue", "yellow", "violet", "orange", "grey", "navy blue", "purple"];
Step 3
Since 10 sectors make up the circle, we will draw them using a "for loop". Before drawing each sector, we will:
  1. Rotate the canvas about the center of the circle.
  2. Assign a color for the sector from the array "colors".
  1. function drawWheel() {
  2. var canvas = document.getElementById("canvas");
  3. if (canvas.getContext) {
  4. var ctx = canvas.getContext("2d");
  5. for (i = 0; i < 10; i++) {
  6. ctx.fillStyle = colours[i];
  7. ctx.translate(centreX, centreY);
  8. ctx.rotate(rotateAngle);
  9. ctx.translate(-centreX, -centreY);
  10. ctx.beginPath();
  11. ctx.moveTo(centreX, centreY);
  12. ctx.lineTo(centreX + radius, centreY);
  13. ctx.arc(centreX, centreY, radius, startAngle, endAngle, false);
  14. ctx.closePath();
  15. ctx.fill();
  16. }
  17. }
  18. }
Step 4
To rotate the circle we first define a rotating angle, using the following:
var rotateAngle = 36 * Math.PI / 180;
Step 5
Rotate the canvas about the centre of the circle.
  1. ctx.translate(centreX,centreY);
  2. ctx.rotate(rotateAngle);
  3. ctx.translate(-centreX,-centreY);
Step 6
Draw the circle again, with the rotated canvas.
drawWheel();
Step 7
A counter keeps track of the number of times the canvas is rotated. When the number of rotations desired is done, stop the animation using the clearInterval() method.
  1. counter++;
  2. if (counter > rnd) {
  3. counter = 0;
  4. clearInterval(animFlag);
  5. }
Step 8
Draw the "Rotate" button. Use the addEventListener() method to detect a mouse click. If the mouse is clicked inside the "Rotate" button, then call the rotateWheel() function.
  1. function mouseClick(event) {
  2. var x = event.clientX;
  3. var y = event.clientY;
  4. var rnd = Math.ceil(Math.random() * 100);
  5. if ((x > 200) && (x < 275) && (y > 100) && (y < 120))
  6. animFlag = setInterval(function () { rotateWheel(rnd) }, 25);
  7. }
  8. window.addEventListener("click", mouseClick, false);
Example
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta content="text/html; charset=ISO-8859-1"
  5. http-equiv="content-type">
  6. <script type="application/javascript">
  7. var centreX = 100; var centreY = 100;
  8. var radius = 75;
  9. var rotateAngle = 36 * Math.PI / 180;
  10. var startAngle = 0 * Math.PI / 180;
  11. var endAngle = 36 * Math.PI / 180;
  12. var counter = 0;
  13. var animFlag;
  14. var colours = ["teal", "red", "green", "blue", "yellow", "violet", "orange", "grey", "navy blue", "purple"];
  15. function init() {
  16. var canvas = document.getElementById("canvas");
  17. if (canvas.getContext) {
  18. var ctx = canvas.getContext("2d");
  19. ctx.lineWidth = 3.0;
  20. ctx.fillStyle = "orange";
  21. ctx.fillRect(200, 100, 75, 20);
  22. ctx.fillStyle = "black";
  23. ctx.font = "15px verdana";
  24. ctx.fillText("Rotate", 215, 114);
  25. drawWheel();
  26. }
  27. }
  28. function drawWheel() {
  29. var canvas = document.getElementById("canvas");
  30. if (canvas.getContext) {
  31. var ctx = canvas.getContext("2d");
  32. for (i = 0; i < 10; i++) {
  33. ctx.fillStyle = colours[i];
  34. ctx.translate(centreX, centreY);
  35. ctx.rotate(rotateAngle);
  36. ctx.translate(-centreX, -centreY);
  37. ctx.beginPath();
  38. ctx.moveTo(centreX, centreY);
  39. ctx.lineTo(centreX + radius, centreY);
  40. ctx.arc(centreX, centreY, radius, startAngle, endAngle, false);
  41. ctx.closePath();
  42. ctx.fill();
  43. }
  44. }
  45. }
  46. function rotateWheel(rnd) {
  47. var canvas = document.getElementById("canvas");
  48. if (canvas.getContext) {
  49. var ctx = canvas.getContext("2d");
  50. ctx.translate(centreX, centreY);
  51. ctx.rotate(rotateAngle);
  52. ctx.translate(-centreX, -centreY);
  53. drawWheel();
  54. counter++;
  55. if (counter > rnd) {
  56. counter = 0;
  57. clearInterval(animFlag);
  58. }
  59. }
  60. }
  61. function mouseClick(event) {
  62. var x = event.clientX;
  63. var y = event.clientY;
  64. var rnd = Math.ceil(Math.random() * 100);
  65. if ((x > 200) && (x < 275) && (y > 100) && (y < 120))
  66. animFlag = setInterval(function () { rotateWheel(rnd) }, 25);
  67. }
  68. window.addEventListener("click", mouseClick, false);
  69. </script>
  70. <title>Animation - Moving Banner</title>
  71. </head>
  72. <body onload="init();">
  73. <canvas id="canvas" width="600" height="500"></canvas>
  74. <br>
  75. </body>
  76. </html>
Output
circle.jpg