Coding

 

 

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.

 

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.