Animated Percentage Circles with JavaScript

This tutorial shows you that percentage circles or progress circles can be created without the aid of jQuery. With the use of JavaScript on HTML5 canvas, the same effect can be produced easily, as shown below in the animation.

 

 

 

The Code for the above percentage circle is as follows; you can modify and use it without worrying about JQuery.

<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
//Declare variables for the angle, increment and percentage
var a = 0, b=5,d;
function draw() {
a = a + b;
if(a==360){a=0;}
c = Math.PI * a / 180;
d = Math.round(100 * a / 360);
context.clearRect(0, 0, 500, 500);
context.beginPath();
context.lineWidth = 25;
context.strokeStyle='#343434';
//Draw a dark static circle
context.arc(200,200,100,0,2*Math.PI,false);
context.stroke();
//Display the percentage
context.beginPath();
context.font = 'bold 30pt Calibri';
context.fillText(d + '%', 180, 210);
context.fillStyle = "blue";
context.fill();
//Draw the dynamic circle
context.beginPath();
context.arc(200,200,100,0,c,false);
//Gradient for the dynamic circle
var grd = context.createRadialGradient(200, 200, 102, 200, 200, 104);
grd.addColorStop(0, 'lightblue');
grd.addColorStop(1, 'blue');
context.strokeStyle = grd;
context.stroke();
}
//Call the function every in 150 milliseconds
window.setInterval(draw, 150);
</script>

 

Of course, the code can be easily amended to control the animation: for instance, this animation runs for ever; with setTimeout() function, however, the number of times that the animation runs can be easily controlled.

 

 

 

 

 

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.

Email: 

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.

Advertisements

Advertisement