Animated Heart with Polar Equations

This tutorial shows you how to produce a beautiful animation of a hear using polar equations.




The Code for the above animation is as follows:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var i = 0; j = 0.1, t = 0;
var col = new Array('green', 'blue', 'red', 'cyan', 'magenta', 'yellow
function timing() {
t = t + 1;
i = i + j;
if (t > 5) { t = 0; }
//var r=Math.pow(10000*Math.cos(2*i),0.5);
var x = 250 + 160*Math.sin(i)*Math.sin(i)*Math.sin(i); var y = -(-170+ 10*(13*Math.cos(i)- 5*Math.cos(2*i) - 2*Math.cos(3*i) - Math.cos(4*i)));
//context.font="40px Georgia";
context.moveTo(250, 200);
context.lineTo(x, y);
context.lineCap = 'round';
context.strokeStyle = 'rgba(0,0,255,0.6)';
context.moveTo(250, 200
context.arc(x, y, 8, 0, 2 * Math.PI);
context.fillStyle = col[t];
if (i > 6.5) { j = -0.1; context.clearRect(0, 0, 500, 400); }
if (i < -0.1) { j = 0.1; context.clearRect(0, 0, 500, 400);
window.setInterval('timing()', 300


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.