-
Notifications
You must be signed in to change notification settings - Fork 18
/
pen.js
319 lines (214 loc) · 6.04 KB
/
pen.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
/**
Pen
===
Canvas implementation of turtle graphics found in Logo, at least in spirit, and
reimagined for a JavaScript world.
Conventions
-----------
- coordinate and distance units are expressed in pixels
- angles are expressed in degrees (mecause most humans don't grok radians)
Methods
-------
- `begin()`
Starts a new path in canvas, and looks more Logo-ish along the way.
- `go(distance)`
Moves forward in the current direction by `distance` pixels.
- `back(distance)`
The opposite of `go`.
- `turn(angle)`
Turns your current direction. To turn left (counter-clockwise), use negative
numbers. To turn right, well, do the opposite.
- `penup()` and `pendown()`
Sets the pen down (turns on drawing) or picks it up (turns off drawing).
- `up(distance)`, `down(distance)`, `left(distance)` and `right(distance)`
Makes a relative move using the coordinate system. These are convenience methods.
- `goto(x, y)`
Moves the pen to the specified coordinates, respecting pen state (up or down).
- `jump(x, y)`
Like `goto` except it will never draw a line to the specified point, and it
also quietly calls `begin`.
- `draw()`
Convenience method which calls `fill()` and `stroke()`, in that order, but only
calls each if there is a fill style and stroke style defined, respectively.
- `close()`
If you're making a closed shape and you want the corners to match up, put this
after you're done drawing your shape but before you call `draw()`.
- `stroke()` and `fill()`
Canvas wrappers which let you manually specify which of these operations you want
to perform on your path (everything since the last `begin` or `jump` call).
- `pensize(width)`
Sets the thickness of the line your pen draws with.
- `penstyle(string)`
A canvas passthru to set the line style of your pen. For example: `penstyle("#00f")`
will turn your pen blue.
- `fillstyle(string)`
A canvas wrapper, usually this is a solid color, e.g. `fillstyle("#ff0")` will
fill your shapes with eye-blinding yellow.
- `font(string)`
Pass in a typical CSS font spec, e.g. `font("bold 15px Helvetica")`. The color
of your font will depend on your last `fillstyle` (defaults to black).
- `text(string)`
Draws, well, text... at the current position. Currently does not support current
angle, but it should in the future.
- `origin()`
Sets current location as the origin point for `polar()` moves.
- `polar(distance, angle)`
Performs a `goto` using polar coordinates which are relative to the last origin set.
This is an interesting addition to typical turtle features, and can be a huge time
(math) saver.
- `set()` and `home()`
Stores current position, angle and origin. A call to `home()` performs a move
to this position. Limited use convenience; it doesn't quite work yet and may go away.
- `angle(direction)`
Resets the pen's current direction relative to the page (0 degrees is "up").
*/
Pen = function(tag) {
this.dir = -90;
this.x = 0;
this.y = 0;
this.tag = document.getElementById(tag) || tag;
this.canvas = this.tag.getContext("2d");
this.strokeStyle = this.canvas.strokeStyle = "#000";
this.lineWidth = this.canvas.lineWidth = 1;
this.fillStyle = this.canvas.fillStyle = "";
this.ox = 0;
this.oy = 0;
this.pen = true;
this.homes = [];
this.canvas.beginPath();
};
Pen.prototype = {
turn: function(deg) {
this.dir += deg;
this.dir = this.dir % 360;
return this;
},
fillstyle: function(style) {
this.fillStyle = this.canvas.fillStyle = style;
return this;
},
set: function() {
this.homes.push({ x: this.x, y: this.y, angle: this.dir });
return this;
},
angle: function(a) {
this.dir = a - 90;
return this;
},
home: function() {
var last = this.homes.pop();
this.dir = last.angle;
return this.goto(last.x, last.y);
},
go: function(r) {
var a = this.toRad(this.dir);
this.x += r * Math.cos(a);
this.y += r * Math.sin(a);
if (this.pen)
this.canvas.lineTo(this.x, this.y);
else
this.canvas.moveTo(this.x, this.y);
return this;
},
back: function(r) {
this.turn(-180);
this.go(r);
this.turn(180);
return this;
},
stroke: function() {
this.canvas.stroke();
return this;
},
fill: function() {
this.canvas.fill();
return this;
},
begin: function() {
this.canvas.beginPath();
return this;
},
close: function() {
this.canvas.closePath();
return this;
},
draw: function() {
if (this.fillStyle)
this.fill();
if (this.lineWidth)
this.stroke();
return this.begin();
},
penup: function() {
this.pen = false;
return this;
},
pendown: function() {
this.pen = true;
return this;
},
goto: function(x, y) {
this.x = x;
this.y = y;
if (!this.pen)
this.canvas.moveTo(x, y);
else
this.canvas.lineTo(x, y);
return this;
},
jump: function(x, y) {
this.canvas.beginPath();
var p = this.pen;
this.pen = true;
this.goto(x, y);
this.pen = p;
return this;
},
up: function(r) {
return this.goto(this.x, this.y - r);
},
down: function(r) {
return this.goto(this.x, this.y + r);
},
left: function(r) {
return this.goto(this.x - r, this.y);
},
right: function(r) {
return this.goto(this.x + r, this.y);
},
polar: function(r, angle) {
var a = this.toRad(angle + this.dir);
this.x = this.ox + r * Math.cos(a);
this.y = this.oy + r * Math.sin(a);
if (this.pen)
this.canvas.lineTo(this.x, this.y);
else
this.canvas.moveTo(this.x, this.y);
return this;
},
origin: function() {
this.ox = this.x;
this.oy = this.y;
return this;
},
rad: Math.PI / 180.0,
toRad: function(d) {
return d * this.rad;
},
pensize: function(size) {
this.lineWidth = this.canvas.lineWidth = size;
return this;
},
penstyle: function(str) {
this.canvas.strokeStyle = this.strokeStyle = str;
return this;
},
text: function(str) {
this.canvas.fillText(str, this.x, this.y);
return this;
},
font: function(str) {
this.canvas.font = str;
return this;
}
};