I am currently attempting to program the Fibonacci Sequence animation on a HTML5 canvas using JavaScript.
I have calculated the Fibonacci numbers and am able to add the squares to a grid layout. The trouble I am having is being able to calculate the offset so they will automatically fit together side by side nicely. Does anyone have any pointers to how this can be achieved.
here is my JavaScript Code:
var canvas;
var context;
function init(){
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
drawgrid();
drawlines();
}
function drawgrid(){
context.strokeStyle="LightGrey";
for(var i = 0; i < 600; i+=20){
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, 600);
context.stroke();
context.beginPath();
context.moveTo(0, i);
context.lineTo(600, i);
context.stroke();
}
}
function drawlines(){
context.strokeStyle="blue";
var startLeft = (canvas.width / 2) - 20;
var startTop = (canvas.height / 2) - 20;
var first = 1;
var second = 1;
var next = 0;
var c = 0;
var count = 0;
for (var i = 0; i < 5; i++){
if ( c <= 1 ){
next = 1;
}else{
next = first + second;
first = second;
second = next;
}
c++;
next *= 20;
//This is a minor attempt at offsetting which does not work what so ever
switch(count) {
case 1:
startLeft += next;
break;
case 2:
startTop-=next;
break;
case 3:
startTop -= next - 20;
startLeft -= next;
break;
case 4:
startTop += next - 80;
startLeft += next - 160;
break;
}
context.beginPath();
context.rect(startLeft,startTop,next,next);
context.stroke();
count++;
if (count > 4){
count = 1;
}
startLeft = (canvas.width / 2) - 20;
startTop = (canvas.height / 2) - 20;
}
}
One way would be to keep track of all coordinates at all times and increment depending of the orientation of the following square to place. I think your whole structure is ok, but there are some info lacking, mainly keeping bottom and right coordinates.
Something like this seems to work (I've put increment of 10 to see if the logic was ok, but it should work with any):
See fiddle: http://jsfiddle.net/a0aLg6Ly/3/