creating canvas lines with random/array generated co-ordinates. Not working, why?

Zoolu

I can't figure out why the co-ordinates for the canvas lines aren't being digested using the arrays: any suggestions?

I'm trying to create an algorithm for randomly generated sets of lines which eventually link together from where the last one ended. like a snake that gets longer if you like.

     var c = document.getElementById("playground");
     var ctx = c.getContext("2d"); 

  //global scope
  var i; 
var c1 = []; //c is short for collect
var c2 = [];
var c3 = [];
var c4 = [];

var initiate = function(){ //the buttom that triggers the program


var clock = function(){

 /* if(i){  
 alert(i); 
}*/

  i+=1; //increment each time the..
  //function gets called. 

 var a = Math.round(Math.random()*200); 
 var b = Math.round(Math.random()*200); 
 var c = Math.round(Math.random()*200); 
 var d = Math.round(Math.random()*200); 
 c1.push(a); 
 c2.push(b);
 c3.push(c);
 c4.push(d);


   ctx.beginPath(); 
   ctx.moveTo(c1[i], c2[i]);  //Here is where the issue seems to be? they don't run.
   ctx.lineTo(c3[i], c4[i]); 
   ctx.stroke(); 

    //if(c1.length===10){
    //alert(c1);
    //}

   }; //end of clock

    setInterval(clock,80);



    }; //end of parent function
Atutouato

You are initializing i as var i;. When you increment it, it results in NaN, so just initialize it as var i = 0; or the like instead.

EDIT: Also why your problem is at that line is because you are trying to access the NaNth element of an array, which JavaScript doesn't like.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

creating canvas lines with random/array generated co-ordinates. Not working, why?

From Dev

Capturing mouse click co-ordinates on canvas using Angularjs

From Dev

How to get Co-ordinates of image element in canvas

From Dev

how to calculate the x and y co-ordinates in a canvas

From Dev

Distance between two canvas co-ordinates in javascript

From Dev

CreateJs Canvas's shape losses its co-ordinates on Windows Phone

From Dev

CreateJs Canvas's shape losses its co-ordinates on Windows Phone

From Dev

How can I find the centre co-ordinates of an array of connected lines?

From Dev

Flash / Actionscript line thickness will draw lines outside co-ordinates given in lineTo

From Dev

Screen co-ordinates to Normalised device co-ordinates

From Dev

X Y Co-ordinates of Stacked Bar

From Dev

Robocode how to get the enemies co-ordinates

From Dev

Google map with co-ordinates not showing

From Dev

Parallel co-ordinates plot in R (ggparcoord)

From Dev

paperjs selection rectangle co-ordinates

From Dev

Python - Write Co-ordinates into array

From Dev

Maven co-ordinates for commons dbcp 2.0

From Dev

Displaying co-ordinates of points Matlab

From Dev

OverPass API - SpeedLimit around a co-ordinates

From Dev

NetLogo Initializing Turtles at Specific Co-Ordinates

From Dev

Using ASCII for board co-ordinates in Java

From Dev

Draw Co-ordinates using Turtle

From Dev

Mapping the latitude longitude co-ordinates on Map

From Dev

Co-ordinates on wp8 emulator

From Dev

Converting co-ordinates in VB2008

From Dev

Parallel Co-ordinates Plot in R

From Dev

Check co-ordinates validity - UITextField - iOS

From Dev

Array for visited co-ordinates in c++

From Dev

How to get Co-ordinates on onmouseup?

Related Related

HotTag

Archive