Creating 2-D Animations on Canvas - interactive spring

In this tutorial, you will learn the following:

  • How to create a spring using Math.sin() function of JavaScript.
  • How to create a pendulum bob using radial gradient.
  • How to oscillate the ball on the spring vertically.
  • How to choose a speed from a choice of three.
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 Oscillating Spring

The spring is produced by manipulating Math.sin method of Math object.

The sphere is produced by context.arc method; the radial gradient produces the spherical appearance of the circle.

The speed is controlled by giving three different time intervals to window.setInterval() method.

Please choose the speed, and then click the Run button.

Slow



Medium



Fast

 

Please stop the spring before changing the speed.

 

The source code for the animation is given below:

<!--hidden element to store the values for animation--------------->
<input type="hidden" id="h" value="0" />
<canvas id="Canvas_Two" width="400" height="300" style="border:2px solid red;" class="imgforcontent">
</canvas>
<!----radio buttons for speed control---------------------------->
<form name="spring" action="">
<input id="R4" name="speed" value="Slow" type="radio" />Slow<br /><br />
<input id="R5" name="speed" value="Medium" type="radio" />Medium<br /><br />
<input id="R6" name="speed" value="Fast" type="radio" />Fast<br /><br />
</form>
<!---------------buttons to start and stop the animation------------------------>
<div style="width:400px;">
<input type="button" style="float:left;margin-left:10px;" value="Run" onclick="anim1()" id="Animation" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" style="float:right;margin-right:10px;" value="Stop" onclick="window.clearInterval(xx)" id="Stopping" >
</div>
<canvas id="Canvas_Two" width="400" height="300" style="border:5px solid red"></canvas> <script> var canvas = document.getElementById('Canvas_Two');
var context1 = canvas.getContext('2d');
//code for static spring in grey colour
context1.beginPath();
context1.moveTo(200,0);
for(var i=0;i<=220;i++)
{
context1.lineTo(200 +20*Math.sin(5*i*Math.PI/180),i*0.5);
context1.lineWidth=5;
context1.strokeStyle='grey';
context1.stroke();
}
// code for the ball in blue colour
context1.beginPath();
context1.arc(200,100,35,0,2*Math.PI,false);
var grd = context1.createRadialGradient(200, 100, 5, 200, 100, 30);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context1.fillStyle=grd;
context1.fill();
var a=1;
//code for the motion of the spring and the ball
function move(){
var x=eval(document.getElementById('h').value);
x=x+a
if (x==8){a=-1;}
if (x==4){a=1;}
context1.clearRect(0,0,400,300);
context1.beginPath();
context1.moveTo(200,0);
for(var i=0;i<=220;i++)
{
context1.lineTo(200 +20*Math.sin(5*i*Math.PI/180),i*x*0.1);
context1.lineWidth=5;
context1.strokeStyle='grey';
context1.stroke();
}
context1.beginPath();
context1.arc(200,x*20+40,35,0,2*Math.PI,false);
var grd = context1.createRadialGradient(200, x*20+40, 5, 200, x*20+40, 30);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context1.fillStyle=grd;
context1.fill();
document.getElementById('h').value=x;
}
// code for timing
function anim1() {
if
(document.getElementById('R4').checked)
{ sp = 400; }
else if (document.getElementById('R5').checked) { sp = 300; }
else if (document.getElementById('R6').checked) { sp = 200; }
else
{ sp = 500; }
xx = window.setInterval('move()', sp);
}
// code for stopping
function stop1() {
//empty the hidden element on exit
document.getElementById('h2').value = 4;
window.clearInterval(xx);
}
</script>

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Amazon Product Search

Sponsors