-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.js
315 lines (315 loc) · 12.1 KB
/
core.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
(function (window, document, undefined) {
function extend(left, right) {for (var key in right) left[key] = right[key]; return left;}
function assertNumber(value, errors, message) {
if (value === undefined) {return false;}
if (value !== null && isNaN(parseFloat(value))) {errors.push(message); return false;}
return true;
}
function radians (degrees) {return degrees * Math.PI / 180;}
function degrees (radians) {return Math.round(radians * 180 / Math.PI);}
function Turtle(/*radians, [x, y]*/) {
var home = {direction: 0, x: 0, y: 0};
if (arguments.length == 1) {
extend(home, arguments[0]);
}
return extend(this, {_position: {}, _home: home}).home();
}
extend(Turtle.prototype, {
home: function home() {
var newhome = arguments[0];
if (typeof newhome === "object") {
this.position(newhome);
extend(this._home, this._position);
}
this.position(this._home);
return this;
},
position: function position () {
var newposition = arguments[0];
if (typeof newposition === "object") {
var errors = [];
if (assertNumber(newposition.direction, errors, "direction must be a number")) {
var newdirection = parseFloat(newposition.direction);
while (newdirection > 2*Math.PI)
newdirection -= 2*Math.PI;
while (newdirection < 0)
newdirection += 2*Math.PI
this._position.direction = newdirection;
}
if (assertNumber(newposition.x, errors, "x must be a number")) {
this._position.x = parseFloat(newposition.x);
}
if (assertNumber(newposition.y, errors, "y must be a number")) {
this._position.y = parseFloat(newposition.y);
}
if (errors.length) {
throw new TypeError(errors.join("\n"));
}
return this;
}
return extend({}, this._position);
},
turn: function turn (angle) {
this.position({direction:this._position.direction + parseFloat(angle)});
return this;
},
move: function move (pixels) {
var p = parseFloat(pixels);
this.position({
x: Math.cos(this._position.direction) * p + this._position.x,
y: Math.sin(this._position.direction) * p + this._position.y
});
return this;
}
});
function TurtlePenDecorator (turtle, context) {
context.lineCap = "round";
return extend(this, {"turtle":turtle, "context":context, "pen": "up"});
}
extend(TurtlePenDecorator.prototype, {
home: function home(/* newhome */) {
if (arguments.length === 0) {this.turtle.home();}
else if (arguments.length === 1) {this.turtle.home(arguments[0]);}
return this;
},
position: function position(/* newposition */) {
if (arguments.length === 0) {return this.turtle.position();}
else if (arguments.length === 1) {return this.turtle.position(arguments[0]);}
},
turn: function turn(angle) {
this.turtle.turn(angle);
return this;
},
move: function move(pixels) {
if (this.pen === "up") {
this.turtle.move(pixels);
this.context.moveTo(this.turtle.position().x, this.turtle.position().y);
}
else if (this.pen === "down") {
this.turtle.move(pixels);
this.context.lineTo(this.turtle.position().x, this.turtle.position().y);
this.context.stroke();
}
return this;
},
penup: function penup() {
this.pen = "up";
return this;
},
pendown: function pendown() {
this.pen = "down";
this.context.beginPath();
this.context.moveTo(this.turtle.position().x, this.turtle.position().y);
this.context.strokeStyle = this._pencolor;
return this;
},
pencolor: function pencolor(color) {
this._pencolor = color;
this.context.strokeStyle = color;
return this;
},
pensize: function pensize(size) {
this.context.lineWidth = size;
return this;
},
clear: function clear() {
this.context.clearRect(0,0,this.context.canvas.width, this.context.canvas.height);
this.turtle.home();
return this;
}
});
function TurtleLineDecorator (turtle, context) {
context.lineCap = "round";
return extend(this, {"turtle":turtle, "context":context, "pen": "up"});
}
TurtleLineDecorator.prototype = extend(TurtlePenDecorator.prototype, {
move: function move(pixels) {
if (this.pen === "up") {
this.turtle.move(pixels);
this.context.moveTo(this.turtle.position().x, this.turtle.position().y);
}
else if (this.pen === "down") {
this.context.beginPath();
this.context.moveTo(this.turtle.position().x, this.turtle.position().y);
this.turtle.move(pixels);
this.context.lineTo(this.turtle.position().x, this.turtle.position().y);
this.context.stroke();
}
return this;
},
pendown: function pendown() {
this.pen = "down";
this.context.strokeStyle = this._pencolor;
return this;
}
});
function TurtleShapeDecorator(turtlepen) {
var cs = document.createElement('canvas');
cs.width = 15;
cs.height = 10;
var ctx = cs.getContext('2d');
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(0,10);
ctx.lineTo(15,5);
ctx.lineTo(0,0);
ctx.lineWidth = 1;
ctx.strokeStyle = "#000000";
return extend(this, {turtle:turtlepen, shape:cs});
}
extend(TurtleShapeDecorator.prototype, {
home: function home(/* newhome */) {
this._restore_background();
if (arguments.length === 0) {this.turtle.home();}
else if (arguments.length === 1) {this.turtle.home(arguments[0]);}
this._play_shape_relative_to_current_position();
return this;
},
position: function position(/* newposition */) {
if (arguments.length == 1) {
this._restore_background();
this.turtle.position(arguments[0]);
this._play_shape_relative_to_current_position();
}
return this.turtle.position();
},
turn: function turn(angle) {
this._restore_background();
this.turtle.turn(angle);
this._play_shape_relative_to_current_position();
return this;
},
move: function move(pixels) {
this._restore_background();
this.turtle.move(pixels);
this._play_shape_relative_to_current_position();
return this;
},
penup: function penup() {
this._restore_background();
this.turtle.penup();
this._play_shape_relative_to_current_position();
return this;
},
pendown: function pendown() {
this._restore_background();
this.turtle.pendown();
this._play_shape_relative_to_current_position();
return this;
},
pencolor: function pencolor(color) {
this._restore_background();
this.turtle.pencolor(color);
this._play_shape_relative_to_current_position();
return this;
},
pensize: function pensize(size) {
this._restore_background();
this.turtle.pensize(size);
this._play_shape_relative_to_current_position();
return this;
},
clear: function clear() {
this.turtle.clear();
this._play_shape_relative_to_current_position();
return this;
},
_play_shape_relative_to_current_position: function _play_relative() {
var context = this.turtle.context;
var position = this.turtle.position();
var save_pen_state = this.turtle.pen;
var save_pen_color = context.strokeStyle;
this._save_background();
context.save();
var sx = this.shape.getContext('2d');
sx.fillStyle = save_pen_state == "up" ? "#ffffff" : save_pen_color;
sx.fill();
sx.stroke();
context.translate(position.x, position.y);
context.rotate(position.direction);
context.drawImage(this.shape, this.shape.width/-2, this.shape.height/-2);
context.restore();
this.turtle.position(position);
},
_save_background: function _save_background () {
var context = this.turtle.context;
this.savedBackground = context.getImageData(
0, 0, context.canvas.width, context.canvas.height);
},
_restore_background: function _restore_background() {
if (typeof(this.savedBackground) === "undefined")
return;
this.turtle.context.putImageData(this.savedBackground, 0, 0);
}
});
function TurtleCommandRecorder () {
return extend(this, {queue: []});
}
extend(TurtleCommandRecorder.prototype, {
home: function home () {
var newhome = arguments[0];
this.queue.push(function home(turtle) {turtle.home(newhome); return;});
return this;
},
position: function position() {
var newposition = arguments[0];
this.queue.push(function position(turtle) {turtle.position(newposition); return;});
return this;
},
turn: function turn(angle) {
this.queue.push(function turn(turtle) {turtle.turn(angle); return;});
return this;
},
move: function move(pixels) {
this.queue.push(function move(turtle) {turtle.move(pixels); return;});
return this;
},
pendown: function pendown() {
this.queue.push(function (turtle) {turtle.pendown(); return;});
return this;
},
penup: function penup() {
this.queue.push(function penup(turtle) {turtle.penup(); return;});
return this;
},
pencolor: function pencolor(color) {
this.queue.push(function pencolor(turtle) {turtle.pencolor(color); return;});
return this;
},
pensize: function pensize(size) {
this.queue.push(function pensize(turtle) {turtle.pensize(size); return;});
return this;
},
clear: function clear() {
this.queue.push(function clear(turtle) {turtle.clear(); return;});
return this;
},
play: function play (turtle, interval, commandsPerInterval) {
if (interval == undefined) {
this.queue.map(function(fn){fn.apply(undefined, [turtle]);});
}
else {
if (!commandsPerInterval)
commandsPerInterval = 1;
var queue = this.queue, i = 0, limit = this.queue.length;
(function animate() {
do {queue[i].apply(undefined, [turtle]); i++; limit--;}
while (limit > 0 && limit % commandsPerInterval);
if (limit > 0)
setTimeout(animate, interval);
})();
}
return this;
}
});
window.Turtle = Turtle;
extend(window.Turtle, {
degrees: degrees,
radians: radians,
extend: extend,
Recorder: TurtleCommandRecorder,
Pen: TurtlePenDecorator,
Line: TurtleLineDecorator,
Shape: TurtleShapeDecorator
});
})(this, this.document);