-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpaperline.html
More file actions
254 lines (190 loc) · 5.1 KB
/
paperline.html
File metadata and controls
254 lines (190 loc) · 5.1 KB
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
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" resize></canvas>
</body>
<head>
<!-- Load the Paper.js library -->
<script type="text/javascript" src="http://www.andrewkind.com/paper.js"></script>
<!-- Define inlined PaperScript associate it with myCanvas -->
<script type="text/paperscript" canvas="myCanvas">
// Create a raster item using the image tag with id='mona'
var raster = new Raster('turtle.png');
// Move the raster to the center of the view
// home coordinates
var xhome = 150;
var yhome = 150;
// turtle coordinates
var xcoord = xhome;
var ycoord = yhome;
// image position
raster.position = (xcoord, xcoord);
// Scale the image by 50%
raster.scale(0.5);
// create path object for turtle
var turtlePath = new Path();
turtlePath.strokeColor = 'black';
turtlePath.add(new Point (xcoord, xcoord));
//Lets create a list of events...
var angle = 90; //this is to keep track of the turtles angle.
//sample commands: Pentagon
var event1 = [];
event1.command = "right";
event1.amount = 45;
var event2 = [];
event2.command = "forward";
event2.amount = 30;
var event3 = [];
event3.command = "right";
event3.amount = 45;
var event4 = [];
event4.command = "forward";
event4.amount = 30;
var event5 = [];
event5.command = "right";
event5.amount = 45;
var event6 = [];
event6.command = "forward";
event6.amount = 30;
var event7 = [];
event7.command = "right";
event7.amount = 45;
var event8 = [];
event8.command = "forward";
event8.amount = 30;
var event9 = [];
event9.command = "right";
event9.amount = 45;
var event10 = [];
event10.command = "forward";
event10.amount = 30;
var event11 = [];
event11.command = "right";
event11.amount = 45;
var event12 = [];
event12.command = "forward";
event12.amount = 30;
var event13 = [];
event13.command = "right";
event13.amount = 45;
var event14 = [];
event14.command = "forward";
event14.amount = 30;
var event15 = [];
event15.command = "right";
event15.amount = 45;
var event16 = [];
event16.command = "forward";
event16.amount = 30;
var event17 = [];
event17.command = "right";
event17.amount = 19;
var event18 = [];
event18.command = "forward";
event18.amount = 41;
var event19 = [];
event19.command = "right";
event19.amount = 110;
var event20 = [];
event20.command = "forward";
event20.amount = 35;
//list of events
var eventList = [];
//add samples into event list
eventList.push (event1);
eventList.push (event2);
eventList.push (event3);
eventList.push (event4);
eventList.push (event5);
eventList.push (event6);
eventList.push (event7);
eventList.push (event8);
eventList.push (event9);
eventList.push (event10)
eventList.push (event11)
eventList.push (event12)
eventList.push (event13)
eventList.push (event14);
eventList.push (event15);
eventList.push (event16);
eventList.push (event17);
eventList.push (event18);
eventList.push (event19);
eventList.push (event20);
//eventList.push (event5);
var x = 0;
function onFrame(event) {
//check if we have an event in list..
if (x < eventList.length) {
//we must check which command is issued, starting at x = 0
/* if (eventList[x].command == "forward") {
//check if we have amount value (our distance to travel)
if (eventList[x].amount > 0) {
//move turtle
raster.translate(0,-1);
//decrement distance
eventList[x].amount--;
ycoord--;
turtlePath.add(new Point (xcoord, ycoord)); // increase path
}
else {
//check if we have to increment to next event, or are we done
if (x < eventList.length-1) {
x++;
console.log("finished event " + x);
}
}
} */
if (eventList[x].command == "right") {
if (eventList[x].amount > 0) {
//rotate one degree
raster.rotate(1);
//decrement our event angle
eventList[x].amount--;
angle++; //increment our turtle angle
}
else {
//check if we have to increment to next event, or are we done
if (x < eventList.length-1) {
x++;
}
}
}
if (eventList[x].command == "forward") {
if (eventList[x].amount > 0) {
//we need to figure out x and y points to reach
var xDest;
var yDest;
angle = -angle;
//convert to radians
var angleRadians = (angle * (Math.PI / 180));
//calculate coordinate
yDest = 1 * Math.sin(angleRadians);
yDest = -yDest; //y coord increase in down direction
xDest = 1 * Math.cos(angleRadians);
// increase path
turtlePath.add(new Point (xcoord - xDest, ycoord - yDest));
//move turtle to new point
raster.position.x = xcoord - xDest;
raster.position.y = ycoord - yDest;
//update new coordinate for turtle position
xcoord = xcoord - xDest;
ycoord = ycoord - yDest;
//set angle back
angle = -angle;
eventList[x].amount--;
}
else {
x++;
}
}
}
} //END ON FRAME FUNCTION
// test function for onkeydown
/*function onKeyDown(event) {
raster.translate(-1,0);
raster.rotate(1);
} */
</script>
</head>
</html>