-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.js
320 lines (288 loc) · 8.21 KB
/
turtle.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// get a handle for the canvases in the document
var imageCanvas = $('#imagecanvas')[0];
var imageContext = imageCanvas.getContext('2d');
imageContext.textAlign = "center";
imageContext.textBaseline = "middle";
var turtleCanvas = $('#turtlecanvas')[0];
var turtleContext = turtleCanvas.getContext('2d');
// the turtle takes precedence when compositing
turtleContext.globalCompositeOperation = 'destination-over';
// initialise the state of the turtle
var turtle = undefined;
function initialise() {
turtle = { pos: {
x: 0,
y: 0
},
angle: 0,
penDown: true,
width: 1,
visible: true,
redraw: true, // does this belong here?
wrap: true,
colour: {r: 0, g: 0, b: 0, a: 1},
sleep: 1000
};
imageContext.lineWidth = turtle.width;
imageContext.strokeStyle = "black";
imageContext.globalAlpha = 1;
}
function sleep(milliseconds) {
var start = new Date().getTime();
while((new Date().getTime() - start) < milliseconds){
//sleep
console.log((new Date().getTime() - start));
}
}
// draw the turtle and the current image if redraw is true
// for complicated drawings it is much faster to turn redraw off
function drawIf() {
if (turtle.redraw) setTimeout(draw,turtle.sleep);
}
// use canvas centered coordinates facing upwards
function centerCoords (context) {
var width = context.canvas.width;
var height = context.canvas.height;
context.translate(width/2, height/2);
context.transform(1, 0, 0, -1, 0, 0);
}
// draw the turtle and the current image
function draw() {
clearContext(turtleContext);
if (turtle.visible) {
var x = turtle.pos.x;
var y = turtle.pos.y;
var w = 10;
var h = 15;
turtleContext.save();
// use canvas centered coordinates facing upwards
centerCoords(turtleContext);
// move the origin to the turtle center
turtleContext.translate(x, y);
// rotate about the center of the turtle
turtleContext.rotate(-turtle.angle);
// move the turtle back to its position
turtleContext.translate(-x, -y);
// draw the turtle icon
turtleContext.beginPath();
turtleContext.moveTo(x - w/2, y);
turtleContext.lineTo(x + w/2, y);
turtleContext.lineTo(x, y + h);
turtleContext.closePath();
turtleContext.fillStyle = "green";
turtleContext.fill();
turtleContext.restore();
}
turtleContext.drawImage(imageCanvas, 0, 0, 300, 300, 0, 0, 300, 300);
}
// clear the display, don't move the turtle
function clear() {
clearContext(imageContext);
drawIf();
}
function clearContext(context) {
context.save();
context.setTransform(1,0,0,1,0,0);
context.clearRect(0,0,context.canvas.width,context.canvas.height);
context.restore();
}
// reset the whole system, clear the display and move turtle back to
// origin, facing the Y axis.
function reset() {
initialise();
clear();
draw();
}
// Trace the forward motion of the turtle, allowing for possible
// wrap-around at the boundaries of the canvas.
function forward(distance) {
imageContext.save();
centerCoords(imageContext);
imageContext.beginPath();
// get the boundaries of the canvas
var maxX = imageContext.canvas.width / 2;
var minX = -imageContext.canvas.width / 2;
var maxY = imageContext.canvas.height / 2;
var minY = -imageContext.canvas.height / 2;
var x = turtle.pos.x;
var y = turtle.pos.y;
// trace out the forward steps
while (distance > 0) {
// move the to current location of the turtle
imageContext.moveTo(x, y);
// calculate the new location of the turtle after doing the forward movement
var cosAngle = Math.cos(turtle.angle);
var sinAngle = Math.sin(turtle.angle)
var newX = x + sinAngle * distance;
var newY = y + cosAngle * distance;
// wrap on the X boundary
function xWrap(cutBound, otherBound) {
var distanceToEdge = Math.abs((cutBound - x) / sinAngle);
var edgeY = cosAngle * distanceToEdge + y;
imageContext.lineTo(cutBound, edgeY);
distance -= distanceToEdge;
x = otherBound;
y = edgeY;
}
// wrap on the Y boundary
function yWrap(cutBound, otherBound) {
var distanceToEdge = Math.abs((cutBound - y) / cosAngle);
var edgeX = sinAngle * distanceToEdge + x;
imageContext.lineTo(edgeX, cutBound);
distance -= distanceToEdge;
x = edgeX;
y = otherBound;
}
// don't wrap the turtle on any boundary
function noWrap()
{
imageContext.lineTo(newX, newY);
turtle.pos.x = newX;
turtle.pos.y = newY;
distance = 0;
}
// if wrap is on, trace a part segment of the path and wrap on boundary if necessary
if (turtle.wrap) {
if (newX > maxX)
xWrap(maxX, minX);
else if (newX < minX)
xWrap(minX, maxX);
else if (newY > maxY)
yWrap(maxY, minY);
else if (newY < minY)
yWrap(minY, maxY);
else
noWrap();
}
// wrap is not on.
else {
noWrap();
}
}
// only draw if the pen is currently down.
if (turtle.penDown)
imageContext.stroke();
imageContext.restore();
drawIf();
}
/*
// move the turtle forward by some distance from its current position
function forward(distance) {
imageContext.save();
centerCoords(imageContext);
imageContext.beginPath();
imageContext.moveTo(turtle.pos.x, turtle.pos.y);
turtle.pos.x += Math.sin(turtle.angle) * distance;
turtle.pos.y += Math.cos(turtle.angle) * distance;
imageContext.lineTo(turtle.pos.x, turtle.pos.y);
// only draw if the pen is currently down.
if (turtle.penDown)
imageContext.stroke();
imageContext.restore();
drawIf();
}
*/
// turn edge wrapping on/off
function wrap(bool) {
turtle.wrap = bool;
}
// show/hide the turtle
function hideTurtle() {
turtle.visible = false;
drawIf();
}
// show/hide the turtle
function showTurtle() {
turtle.visible = true;
drawIf();
}
// turn on/off redrawing
function redrawOnMove(bool) {
turtle.redraw = bool;
}
// lift up the pen (don't draw)
function penup() { turtle.penDown = false; }
// put the pen down (do draw)
function pendown() { turtle.penDown = true; }
// turn right by an angle in degrees
function right(angle) {
turtle.angle += degToRad(angle);
drawIf();
}
// turn left by an angle in degrees
function left(angle) {
turtle.angle -= degToRad(angle);
drawIf();
}
// move the turtle to a particular coordinate (don't draw on the way there)
function goto(x,y) {
turtle.pos.x = x;
turtle.pos.y = y;
drawIf();
}
// set the angle of the turtle in degrees
function angle(angle) {
turtle.angle = degToRad(angle);
}
// convert degrees to radians
function degToRad(deg) {
return deg / 180 * Math.PI;
}
// convert radians to degrees
function radToDeg(deg) {
return deg * 180 / Math.PI;
}
// set the width of the line
function width(w) {
turtle.width = w;
imageContext.lineWidth = w;
}
// write some text at the turtle position.
// ideally we'd like this to rotate the text based on
// the turtle orientation, but this will require some clever
// canvas transformations which aren't implemented yet.
function write(msg) {
imageContext.save();
centerCoords(imageContext);
imageContext.translate(turtle.pos.x, turtle.pos.y);
imageContext.transform(1, 0, 0, -1, 0, 0);
imageContext.translate(-turtle.pos.x, -turtle.pos.y);
imageContext.fillText(msg, turtle.pos.x, turtle.pos.y);
imageContext.restore();
drawIf();
}
// set the colour of the line using RGB values in the range 0 - 255.
function colour (r,g,b,a) {
imageContext.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + a + ")";
turtle.colour.r = r;
turtle.colour.g = g;
turtle.colour.b = b;
turtle.colour.a = a;
}
// Generate a random integer between low and hi
function random(low, hi) {
return Math.floor(Math.random() * (hi - low + 1) + low);
}
function repeat(n, action) {
for (var count = 1; count <= n; count++)
action();
}
function animate(f,ms) {
return setInterval(f, ms);
}
function setFont(font) {
imageContext.font = font;
}
// Execute the program when the command box is changed
// (when the user presses enter)
$('#command').change(function () {
var commandText = $(this).val();
var definitionsText = $('#definitions').val();
// execute any code in the definitions box
eval(definitionsText);
// execute the code in the command box
eval(commandText);
// clear the command box
$(this).val('');
});
reset();