Creating 2-D Animations on Canvas - simple pendulum

In this tutorial, you will learn:

  • How to draw a simple pendulum on HTML5 Canvas.
  • How to animate it using Window.setInterval function.
  • How to change the speed of oscillation.
Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

Interactive Simple Pendulum

The string of the pendulum is drawn as a straight line. Its bottom is changing while using a sine values of an angle so that it gets the oscillating effect.

The pendulum bob is created as a circle, filled with a gradient to give it the appearance of a sphere.

The speed is controlled by window.setInterval() method while passing on three different intervals as arguments.

The canvas is cleared up after each interval by context.clearRect() method to give the animated effect.

Choose Speed




Make sure you stop the animation before choosing the speed to see a smooth effect.

The Code for Interactive Pendulum:

<!--hidden element to store the values for animation--------------->
<input type="hidden" id="h" value="0" />
<canvas id="Canvas_One" width="400" height="300" style="border:2px solid red;" class="imgforcontent">
<!----radio buttons for speed control---------------------------->
<form name="pendulum" action="">
<input id="R1" name="speed" value="Slow" type="radio" />Slow<br /><br />
<input id="R2" name="speed" value="Medium" type="radio" />Medium<br /><br />
<input id="R3" name="speed" value="Fast" type="radio" />Fast<br /><br />
<!---------------buttons to start and stop the animation------------------------>
<div style="width:400px;">
<input type="button" style="float:left;margin-left:10px;" value="Run" onclick="anim()" id="Animation" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" style="float:right;margin-right:10px;" value="Stop" onclick="window.clearInterval(x)" id="Stopping" >
<script type="text/javascript">
var canvas = document.getElementById('Canvas_One');
var context = canvas.getContext('2d');
var i;
var x;
var sp;
//code for static string - grey colour
context.moveTo(200, 0);
context.lineTo(200, 135);
context.lineWidth = 5;
context.strokeStyle = "grey";
// code for the static ball - gradient blue
context.arc(200, 150, 20, 0, 2 * Math.PI, false);
var grd = context.createRadialGradient(200, 150, 2, 200, 150, 18);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#0000ff');
context.fillStyle = grd;
// function for the animation
function circ() {
context.clearRect(0, 0, canvas.width, canvas.height);
i = eval(document.getElementById('h').value) + 10;
context.moveTo(200, 0); context.lineTo(200 + 40 * Math.sin(i * Math.PI / 180), 135);
context.lineWidth = 5;
context.strokeStyle = "grey";
context.arc(200 + 40 * Math.sin(i * Math.PI / 180), 150, 20, 0, 2 * Math.PI, false);
if (i == 360 || i > 360) { i = 0; }
document.getElementById('h').value = i;
//create radial gradient
var grd = context.createRadialGradient(200 + 40 * Math.sin(i * Math.PI / 180), 150, 2, 200 + 40 * Math.sin(i * Math.PI / 180), 150, 18);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#0000ff');
context.fillStyle = grd;
// function for timing
function anim() {
{ sp = 200; }
else if (document.getElementById('R2').checked) { sp = 100; }
else if (document.getElementById('R3').checked) { sp = 80; }
{ sp = 200; }
x = window.setInterval('circ()', sp);


All Canvas Animations




Recommended Reading

Amazon Best Seller


Everything is evolving; so is the layout of a text book. By uniquely presenting the rich contents of the book, the author has elevated positive user experience of reading a text book to a new level: the examples are easy to follow and rich in standard. Highly recommended for those who want to master JavaScript and JQuery.

Progressive Web Apps(PWA)


The significance of app stores is over; progressive web apps is the next big thing. They are just websites that makes the need of going through app stores and need of storing redundant. They work offline too. If you have a reasonable understanding of HTML, CSS and JavaScript, this is the book for you to learn in no time.

HTML5 Canvas Animations


David Geary, in this book, shows how to combine JavaScript and HTML5 Canvas to produce amazing animations; he has set aside a whole chapter to teach you the role of physics in animations. If you want an in-depth understanding about HTML5 Canvas animations, this is a must read.