
Cut And
Paste Code For Lesson 7
Here is a simple "for" loop that counts to 1000:
<!DOCTYPE html>
<html>
<body>
<script>
var
i;
for (i = 0; i < 1000; i++) {
document.write(i);
document.write("<br>")
}
</script>
</body>
</html>
Bouncing Circle Design Creator
The following
code uses a "for" loop to draw your circle patterns only the number of
times you specify.
It also draws your patterns nearly instantly!
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas"
width="500" height="300" style="border:1px solid #d3d3d3;">
Your
browser does not support the HTML5 canvas tag.</canvas>
<script>
var
xpos = 250;
var ypos = 0;
var ymove = 1;
var xmove = 1;
var x;
for
(repeat = 0; repeat < 90; repeat++) {
var c = document.getElementById("myCanvas");
var
ctx = c.getContext("2d");
ctx.strokeStyle = "blue";
ctx.beginPath();
ctx.arc(ypos,xpos,40,0,2*Math.PI);
ctx.stroke();
xpos=
xpos+ymove;
ypos = ypos+xmove;
if (xpos > 256)
{
ymove = -12
}
if
(xpos < 43) {
ymove
= 12
}
if (ypos > 455)
{
xmove = -12
}
if
(ypos < 43) {
xmove
= 12
}
}
</script>
</body>
</html>
Lesson 7 notes:
You can also bounce a square! Add this code instead of your circle code:
ctx.rect(ypos, xpos, 150, 100);