Coding

 

 

Creating 2-D Animations on Canvas - mathematical spirals

  • How to draw six famous spirals, including Fibonacci and Archimedean.
  • How to change the colours and animate the spirals.

The polar equations of the curves are as follows:

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:

The World of Spirals

There are six famous spirals in the animation. They are:

  1. Archimedes Spiral
  2. Fermat's Spiral
  3. Hyperbolic Spiral
  4. Logarithmic Spiral
  5. Lemniscates
  6. Lituus

 

 Archimedes

 Fermat

 Hyperbolic

 Logarithmic

 Lemniscate

 Lituus

 Clear

 

Please press clear button, before choosing a new spiral, so that you can see each smoothly.

The Code for the animation is as follows:

<script>
{
var canvas = document.getElementById('Canvas_One');
var context = canvas.getContext('2d');
var i=0;j=0.1;var ang;var col1;var col2;var p;
function clear(){
window.clearInterval(p);
context.clearRect(0,0,400,300);
context.strokeStyle="black";
context.fillStyle="black";
}
function timing(col1,col2) {
i=i+j;
//for Archimedes Spiral
if(document.getElementById('R1').checked){r=15*i;ang=12;col1='rgb(255,255,0)';col2='rgba(0,255,0,0.6)';}
//for Fermat's Spiral
if(document.getElementById('R2').checked){r=20*Math.pow(i,0.5);var ang=36;col1='orange';col2='rgba(255,0,0,0.6)';}
// for Hyperbolic Spiral
if(document.getElementById('R3').checked){r=200/i;ang=24;col1='purple';col2='rgba(0,0,255,0.6)';}
// for Logarithmic
if(document.getElementById('R4').checked){r=Math.pow(1.2,i);ang=30;col1='rgb(255,255,0)';col2='rgba(0,255,0,0.6)';}
// for Lemniscate
if(document.getElementById('R5').checked){r=Math.pow(10000*Math.cos(2*i),0.5);ang=32;col1='orange';col2='rgba(255,0,0,0.6)';}
// for Lituus
if(document.getElementById('R6').checked){r=Math.pow(10000/i,0.5);ang=32;col1='purple';col2='rgba(0,0,255,0.6)';}
// To clear
if(document.getElementById('R7').checked){clear();}
context.beginPath();
context.moveTo(200,150);
var x=200+r*Math.sin(i); var y=150+r*Math.cos(i);
context.font="40px Georgia";
context.textAlign='center';
context.fillText('.',x,y);
context.fillStyle=col1;
context.lineTo(x,y);
context.strokeStyle=col2;
context.stroke();
if(i>ang){i=0;context.clearRect(0,0,400,300);}
}
function motion(){
p=window.setInterval('timing()',100);
}
</script>

 

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.