With visible drawing progress:
<!DOCTYPE html>
<html>
<head>
<title>Longton's Ant</title>
<meta charset="utf-8">
<style type="text/css">
body {background-color: black}
div {text-align: center}
</style>
<script>
var x = 200;
var y = 200;
var kier = 1;
var step = 12000;
function aktywuj(){
plotno = document.getElementById("rysunek");
zawartosc = plotno.getContext("2d");
zawartosc.fillStyle = "#ffffff";
zawartosc.fillRect(0, 0, 399, 399);
t = setTimeout("rysuj()", 1);
}
function rysuj() {
plotno = document.getElementById("rysunek");
zawartosc = plotno.getContext("2d");
punkt = zawartosc.getImageData(x, y, 1, 1);
if (punkt.data[0] == 255) {
zawartosc.fillStyle = "#000000";
zawartosc.fillRect(x, y, 1, 1);
switch (kier) {
case 1:
kier = 4;
x++;
break;
case 2:
kier = 1;
y++;
break;
case 3:
kier = 2;
x--;
break;
case 4:
kier = 3;
y--;
break;
}
}
else {
zawartosc.fillStyle = "#ffffff";
zawartosc.fillRect(x, y, 1, 1);
switch(kier) {
case 1:
kier = 2;
x--;
break;
case 2:
kier = 3;
y--;
break;
case 3:
kier = 4;
x++;
break;
case 4:
kier = 1;
y++;
break;
}
}
step--;
if (step) {
setTimeout("rysuj()", 1);
}
}
</script>
</head>
<body onload = "aktywuj()">
<div>
<canvas id="rysunek" width="400" height="400"></canvas>
</div>
</body>
</html>
Without visible drawing progress (much faster):
<!DOCTYPE html>
<html>
<head>
<title>Longton's Ant</title>
<meta charset="utf-8">
<style type="text/css">
body {background-color: black}
div {text-align: center}
</style>
<script>
var x = 200;
var y = 200;
var kier = 1;
function aktywuj(){
plotno = document.getElementById("rysunek");
zawartosc = plotno.getContext("2d");
zawartosc.fillStyle = "#ffffff";
zawartosc.fillRect(0, 0, 399, 399);
t = setTimeout("rysuj()", 1);
}
function rysuj() {
step = 12000;
while (step) {
plotno = document.getElementById("rysunek");
zawartosc = plotno.getContext("2d");
punkt = zawartosc.getImageData(x, y, 1, 1);
if (punkt.data[0] == 255) {
zawartosc.fillStyle = "#000000";
zawartosc.fillRect(x, y, 1, 1);
switch (kier) {
case 1:
kier = 4;
x++;
break;
case 2:
kier = 1;
y++;
break;
case 3:
kier = 2;
x--;
break;
case 4:
kier = 3;
y--;
break;
}
}
else {
zawartosc.fillStyle = "#ffffff";
zawartosc.fillRect(x, y, 1, 1);
switch(kier) {
case 1:
kier = 2;
x--;
break;
case 2:
kier = 3;
y--;
break;
case 3:
kier = 4;
x++;
break;
case 4:
kier = 1;
y++;
break;
}
}
step--;
}
}
</script>
</head>
<body onload = "aktywuj()">
<div>
<canvas id="rysunek" width="400" height="400"></canvas>
</div>
</body>
</html>
Sources are attached.