Looping with canvas: Difference between revisions
No edit summary |
|||
Line 8: | Line 8: | ||
== Examples == | == Examples == | ||
<canvas id="c1"> | <canvas id="c1"> | ||
Line 53: | Line 25: | ||
It redefines the "x" value to the begin of the line and adds 40px to "y". | It redefines the "x" value to the begin of the line and adds 40px to "y". | ||
<canvas id="c2" width="300" height="300" showsrc | <canvas id="c2" width="300" height="300" showsrc> | ||
function draw() { | function draw() { | ||
c = document.getElementById("c2"); | c = document.getElementById("c2"); |
Revision as of 15:57, 25 September 2012
while CONDITION { LOOP LOOP LOOP... }
Examples
<canvas id="c1"> function draw(){
c = document.getElementById("c1") //console.log("c is",c) p = c.getContext("2d") x = 10 while (x<250) { p.strokeRect(x,100,20,40) x+=60 }
} </canvas>
Variation: When the loop reaches the end of page it breaks the line. It redefines the "x" value to the begin of the line and adds 40px to "y".
<canvas id="c2" width="300" height="300" showsrc> function draw() {
c = document.getElementById("c2"); p = c.getContext("2d"); hands = 1; x = 20; y = 20; while (hands < 100) { //console.log(hands); p.strokeRect(x,y,20,20); x = x + 50; hands++; if (x > 600) { x = 20; y = y + 40; } }
} </canvas>