-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
295 lines (269 loc) · 8.49 KB
/
index.html
File metadata and controls
295 lines (269 loc) · 8.49 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
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
<!DOCTYPE html>
<html>
<head>
<!-- META -->
<title>p5.turtle.js - Example of turtle graphics for p5.js</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="description" content="turtle graphics for p5.js" />
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="css/kickstart.css" media="all" />
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<!-- Javascript -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="js/kickstart.js"></script>
</head>
<body>
<nav class="navbar">
<a class="hide-phone" id="logo" href="http://www.catch.jp/"><i class="fa fa-hand-o-right"></i> <span>catch.jp</span></a>
<ul>
<li><a href="https://github.com/ycatch"><span>ycatch - Github</span></a></li>
<li>[English]</li>
<li><a href="index.ja.html">日本語</a></li>
</ul>
</nav>
<div class="grid">
<div class="col_6" style="margin-top:100px;">
<h1><img src="images/turtle_2.png" width="54" hight="63"> p5.turtle.js</h1>
<h4 style="color:#999;">Example of turtle graphics for p5.js</h4>
<p>This is very simple and animated turtle graphics program. There are two step in the process. 1st step, it record turtle moving in setup(). 2nd step, it playback turtle moving for animation in draw(). This program needs p5.js and p5.play.js library.</p>
<p><a href='https://github.com/ycatch/p5.turtle.js/archive/master.zip' target='_blank'>
<button class="large red"><i class="fa fa-download"></i> Download ZIP</button>
</a></p>
<p><a href='https://github.com/ycatch/p5.turtle.js/blob/master/p5.turtle.js' target='_blank'>View Source</a></p>
</div>
<div class="col_4" style="margin-top:100px;">
<div id = "p5Canvas"></div>
</div>
<div class="col_12">
<pre id="code_0" hidden>
var turtles_path = []; // array of Turtle objects
var pathPointer = 0;
var turtle;
var turtleSprite;
var tPlane; // graphic plane for pen layer
setup = function() {
var canvas = createCanvas(480, 360);
canvas.parent("p5Canvas");
background(200);
turtleSprite = createSprite(0, 0, 56, 64);
turtleSprite.addAnimation("moving", "images/turtle_1.png", "images/turtle_4.png");
turtleSprite.changeAnimation("moving");
tPlane = createGraphics(width, height); // pen layer
// Start turtle code - recode turtle moving. -------------------------------------
</pre>
<p></p>
<h4>Edit</h4>
<textarea name="code_1" rows=30 cols=120>
turtle = new Turtle();
turtle.x = 200;
turtle.y = 180;
turtle.penColor = turtle.color.blue;
for(var i = 0; i < 18; i++){
for(var j = 0; j < 360; j++){
turtle.penDown = true;
turtle.forward(1);
turtle.right(1);
};
turtle.penDown = false;
turtle.forward(10);
turtle.left(20);
};
turtle.right(360);
turtle.right(360);
</textarea>
<p></p>
<input type="button" onclick="restart()" value="Re-Start">
<p><a href='https://github.com/ycatch/p5.turtle.js/blob/master/README.md' target='_blank'>Help</a></p>
<pre id="code_2" hidden>
// End of turtle code ------------------------------------------------------------
};
draw = function() {
// Playback turtle moving for animation.
background(200);
turtle.draw(pathPointer);
image(tPlane);
drawSprites();
pathPointer += 1;
if (pathPointer >= turtles_path.length) {
pathPointer = 0;
clear();
}
};
/** utility */
function clear() {
background(200);
tPlane.fill(200);
tPlane.noStroke();
tPlane.rect(0, 0, width, height);
}
/** Turtle Data */
function TBody() {
this.x = 200;
this.y = 60;
this.step = 3;
this.stepAngle = Math.PI / 36;
this.angleInRadians = 0;
this.penDown = false;
this.penColor = "#000000";
this.lineWidth = 2;
};
/** Turtle class */
function Turtle() {
var body = new TBody();
for (var prop in body) {
this[prop] = body[prop];
}
this.color = {
black : "#000000",
gray: "#808080",
lightgray: "#C0C0C0",
red: "#ff0000",
green: "#00ff00",
blue: "#0000ff",
yellow: "#ffff00",
magenta: "#ff00ff",
aqua: "#00ffff",
white: "#ffffff"
};
this.forward = function(length) {
var x0 = this.x;
var y0 = this.y;
var xx = sin(this.angleInRadians);
var yy = cos(this.angleInRadians);
var count = abs(int(length / this.step));
var dir = 1;
if(length < 0) {dir = -1};
for(var i=0; i < count - 1; i++) {
this.x += dir * this.step * xx;
this.y += dir * this.step * yy;
this.copy();
}
this.x = x0 + length * xx;
this.y = y0 + length * yy;
this.copy();
};
this.back = function(length) {
this.forward(-length);
};
this.left = function(angleInDegrees) {
var angle0 = this.angleInRadians;
var targetAngle = angleInDegrees * Math.PI / 180.0;
var count = abs(int(targetAngle / this.stepAngle));
var dir = 1;
if(targetAngle < 0) {dir = -1};
for(var i=0; i < count - 1; i++) {
this.angleInRadians += dir * this.stepAngle;
this.copy();
}
this.angleInRadians = angle0 + targetAngle;
this.copy();
};
this.right = function(angleInDegrees) {
this.left(-angleInDegrees);
};
// copy TBody object
this.copy = function() {
turtles_path.push(new TBody());
var target = turtles_path[turtles_path.length - 1];
for (var prop in this) {
target[prop] = this[prop];
}
};
// drawing turtle in loop
this.draw = function(pointer) {
var target = turtles_path[pointer];
// draw path by Pen
if (target.penDown) {
tPlane.strokeWeight(target.lineWidth);
tPlane.stroke(target.penColor);
var nextPointer = pointer + 1;
if(nextPointer >= turtles_path.length) {
nextPointer = 0;
}
tPlane.line(target.x, target.y, turtles_path[nextPointer].x, turtles_path[nextPointer].y);
}
// draw turtle image by sprite
turtleSprite.rotation = target.angleInRadians * -180 / Math.PI + 180;
turtleSprite.position.x = target.x;
turtleSprite.position.y = target.y;
};
};
</pre>
</div>
<div class="col_12">
<h2>Get Started</h2>
<h3>1.Download</h3>
<p>Download and Unzip this.</p>
<ul>
<li>p5.turtle.js - <a href='https://github.com/ycatch/p5.turtle.js/archive/master.zip' target='_blank'>download</a></li>
</ul>
<h3>2.HTML</h3>
<p>make html in the downloads and put this element in html</p>
<!-- Code HTML -->
<pre><div id = "p5Canvas"></div></pre>
<h3>3.Javascript</h3>
<p>put this library in html</p>
<!-- Code Javascript -->
<pre>
<script src="js/p5.min.js"></script>
<script src="js/p5.play_patch119.js"></script>
<script src="p5.turtle.js"></script></pre>
<h3>4.Edit turtle code</h3>
<p>Edit your own turtle code in p5.turtle.js</p>
<!-- Code Javascript -->
<pre>
// Start turtle code - recode turtle moving. -------------------------------------
turtle = new Turtle();
turtle.x = 140;
turtle.y = 80;
turtle.penDown = true;
turtle.penColor = turtle.color.blue;
for(var i = 0; i < 4; i++){
turtle.forward(200);
turtle.left(90);
};
turtle.right(360);
turtle.right(360);
// End of turtle code ------------------------------------------------------------</pre>
<h2>Example</h2>
<ul>
<li><a href='example/basic/index.html'>Basic</a></li>
<li><a href='example/instance_w_play/index.html'>Instance mode and draw with turtle</a></li>
<li><a href='example/instance_mode/index-is-animation.html'>Instance mode and draw with animation</a></li>
<li><a href='example/instance_mode/index-is-sandbox.html'>Instance mode sandbox with animation</a></li>
<li><a href='example/instance_mode/index.html'>Instance mode and draw without animation</a></li>
</ul>
</div>
<div class="col_12">
<h2>Links</h2>
<ul>
<li><a href='http://p5js.org/' target='_blank'>p5.js</a></li>
<li><a href='http://p5play.molleindustria.org/' target='_blank'>p5.play.js</a></li>
<li><a href='https://github.com/ycatch/p5.turtle.js' target='_blank'>Github - p5.turtle.js</a></li>
</ul>
</div>
<div class="col_12" id="footer">
Copyright 2015 Yutaka Kachi released under MIT License.
</div>
</div> <!-- End Grid -->
<script src="js/p5.js"></script>
<script src="js/p5.play_patch119.js"></script>
<!-- <script src="p5.turtle.js"></script> -->
<script>
var Pjs;
run()
function restart() {
location.reload();
}
function run(){
//console.log("Run");
var code_1 = document.getElementById('code_0').textContent;
var code_2 = document.getElementsByName('code_1')[0].value;
var code_3 = document.getElementById('code_2').textContent;
var s = new Function(code_1 + code_2 + code_3);
Pjs = new p5(s);
}
</script>
</body>
</html>