-
Notifications
You must be signed in to change notification settings - Fork 0
/
logo2.html
215 lines (187 loc) · 4.4 KB
/
logo2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!doctype html>
<html><head><title>HTML 5 Logo test</title></head>
<script>
<!--
function error()
{
alert("But the future refused to change...");
return true;
}
// window.onerror=function(){error()};
function process()
{
var cmd = document.forms["commands"].elements["cmd"].value;
// Need to see if we need to scrub this
histadd(cmd);
drawit(cmd);
return;
try
{ // Hoping the try block will limit scope of a failure
// drawrandradial(cmd);
drawit(cmd);
}
catch(error)
{
}
}
function histadd(histline)
{
var histlist = document.getElementById("history");
var newhistelement = document.createElement("LI");
newhistelement.innerHTML = histline;
histlist.appendChild(newhistelement);
}
function histaddcolour(histline, colour)
{
histadd("<span style=\"color:".concat(colour).concat(";\">").concat(histline).concat("</span>"));
}
function histerror(histline)
{
histaddcolour(histline, "red");
}
function histstatus(histline)
{
histaddcolour(histline, "green");
}
function clean()
{ // We back out of a form submit partway through, and so need some cleanup.
document.forms["commands"].elements["cmd"].value = "";
}
// -------------------------------------------------------
// Drawing code
var posx = 200; // Yes, probably should not hardcode this.
var posy = 200; // ok for a proof of concept though.
var pen_on = true;
var angledegree = 0;
// commands:
// pen_off
// pen_on
// move [distance]
// rotate [degrees]
function drawit(cmd)
{ // Given a command to draw, actually manipulate the canvas accordingly
// mainly a command parser
// histadd("entered drawit()");
var cmdparts = cmd.split(" ");
if(cmdparts.length == 0)
{
histstatus("EMPTY COMMAND");
}
else if((cmdparts.length == 1) && (cmdparts[0] == "pen_off"))
{
pen_on = false;
histstatus("OK");
}
else if((cmdparts.length == 1) && (cmdparts[0] == "pen_on"))
{
pen_on = true;
histstatus("OK");
}
else if((cmdparts.length == 2) && (cmdparts[0] == "move"))
{ // This is where the real work happens
var power = parseInt(cmdparts[1]);
if(isNaN(power))
{
histerror("BADNUM");
return;
}
if(isNaN(angledegree))
{histerror("INTERNAL ERROR adeg NaN WTF");return;}
var ymot=Math.sin(angledegree*Math.PI/180);
var xmot=Math.cos(angledegree*Math.PI/180);
if(isNaN(xmot))
{histerror("INTERNAL ERROR xmot NaN");return;}
if(isNaN(ymot))
{histerror("INTERNAL ERROR ymot NaN");return;}
var newx = posx + (xmot*power);
var newy = posy + (ymot*power);
// For this we assume that if it hits the edge it slides along it
if(newx > 399) {newx = 399;}
if(newy > 399) {newy = 399;}
if(newx < 0) {newx = 0;}
if(newy < 0) {newy = 0;}
// We're ready to draw!
if(pen_on)
{
var canvas = document.getElementById("drawhere");
var cc = canvas.getContext("2d");
cc.beginPath();
cc.moveTo(posx,posy);
cc.lineTo(newx, newy);
cc.stroke();
}
// And now let's record our new position
posx = newx;
posy = newy;
histstatus("OK"); // .concat(newx).concat(",").concat(newy));
}
else if((cmdparts.length == 2) && (cmdparts[0] == "rotate"))
{
var angle = parseInt(cmdparts[1]);
if(isNaN(angle))
{
histerror("BADNUM");
return;
}
angledegree += angle;
angledegree %= 360; // Hoping JS defines modulus of
// negative numbers more consistently
// than C does
histstatus("OK");
}
else
{
histerror("SIGNOPARSE");
}
}
// -------------------------------------------------------
// Simple drawing test code below
function randint(low,high)
{ // Quick, dirty, and possibly wrong.
var scalef = high - low;
var scaled = Math.random()*scalef;
return 1 + Math.floor(scaled+low);
}
function drawrandradial(cmd)
{ // Sample function to test drawing
var canvas = document.getElementById("drawhere");
var cc = canvas.getContext("2d");
cc.beginPath();
cc.moveTo(200,200);
cc.lineTo(randint(1,400), randint(1,400));
cc.stroke();
histadd("Drawn");
}
-->
</script>
<style type="text/css">
@import url("logo1.css");
</style>
<body>
<div id="canvasandhistory">
<div id="canvaspart">
The canvas:<br />
<canvas id="drawhere" width="400" height="400">
Are canvases not enabled in your browser?
</canvas>
</div>
<div id="comhistpart">
History goes here:
<ul id="history">
</ul>
</div>
</div>
<div id="commandpart">
Commands:
<ul>
<li>pen_on</li>
<li>pen_off</li>
<li>move PIXELS</li>
<li>rotate DEGREES</li>
</ul>
<form name="commands" onsubmit="process(); clean(); return false;">
Your bidding: <input type="text" name="cmd" />
</form>
</div>
</body>
</html>