forked from bjpop/js-turtle
-
Notifications
You must be signed in to change notification settings - Fork 7
/
animation.html
1554 lines (1422 loc) · 40.3 KB
/
animation.html
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<title>Polygon Animation</title>
</head>
<link type="text/css" rel="stylesheet" href="./turtleweb.css"/>
<link type="text/css" rel="stylesheet" href="./tutorial.css"/>
<link href="https://fonts.googleapis.com/css?family=Poppins|Roboto" rel="stylesheet">
<script src="tutorial.js"></script>
<body>
<header>
<h1>JavaScript Turtle Graphics</h1>
<div class="navigation">
<ul>
<li><a href="overview.html" title="Overview of Turtle Graphics">Intro</a></li>
<li><a href="guide.html" title="User Guide for Turtle Graphics Integrated Development Environment">Guide</a></li>
<li><a href="turtle.html" title="Integrated Development Environment for Turtle Graphics">IDE</a></li>
<li><a href="javascript.html" title="Tutorial for Simple JavaScript">JavaScript</a></li>
<li><a href="tutorial.html" title="Tutorial for Basic Turtle Graphics">Tutorial</a></li>
<li><a class="active" href="animation.html" title="Tutorial for Animation with Turtle Graphics">Animation</a></li>
<li><a href="examples.html" title="Examples of Things Created with Turtle Graphics">Examples</a></li>
<li><a href="reference.html" title="Language Reference">Reference</a></li>
<li><a href="nerd.html" title="Nerd Links">Nerd</a></li>
<li><a href="about.html" title="About Turtle Graphics">About</a></li>
</div>
</header>
<article>
<!-- original source file is at ~/dev/js-tur/backups/examplesHold/polygonStory.js-->
<h1>Polygon Animation</h1>
<h2>Introduction</h2>
<p>
This tutorial will take you through the basics of drawing with turtle
graphics through the creation of the motion of stars as seen from a
space craft exceeding the speed of light. This is a journey, but like all
journeys, it has a series of small steps.
</p>
<p>
In preparation for the journey, it is assumed that you already have some knowledge
of JavaScript and Turtle Graphics. If not, you may want to read or review the
following pages before starting here:
<ul>
<li>
<a href="javascript.html">JavaScript Basics</a>
</li>
<li>
<a href="tutorial.html">Turtle Graphics Basics</a>
</li>
<li>
<a href="guide.html">Guide to the IDT Turtle Graphics</a>
</li>
</ul>
<p>
On this journey we will visit various turtle graphics functions,
creating JavaScript functions to do new things,
repeating mechanisms to do the same thing over and over,
using coordinate systems to plot things on the screen,
using JavaScript objects to simplify parameter passing and processing,
and finally animating some polygons.
</p>
<p>
Another way is to take advantage of the loop control mechanisms that
are part of JavaScript. Since this is "real" JavaScript programming, it
is the preferred way to go.
JavaScript has many looping mechanisms from which to choose including:
a <code>while</code> loop,
a <code>do...while</code> loop and a <code>for</code> loop.
All of these require the following be done:
</p>
<h2> Coloring the Polygon or Spikey </h2>
<p>
In the tutorial you learned how to change the color and width of the stroke.
We will review that and add a fill function to fill a shape with a color.
To change the color of the turtle's pen use use the <code>color()</code>
function with a color as in the following example:
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo() {
reset() // just clear the screen and home the turtle
color("red")
spikey(5, 2, 50)
}
</code>
<p>
To change the width of the pen stroke, we use the <code>width</code> function
with the width expressed as a number of pixels. A <code>pixel</code> is the
size of smallest piece of the display, a dot or maybe a period.
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo () {
reset()
color("blue")
width(5)
spikey(5, 2, 50)
}
</code>
<p>
We can even fill in the interior of the polygons or spikeys
by using the fill directives of turtle graphics. This directive
is a little different than others we have used in that it has
to bracket a series of moves with a <code>beginShape()</code> and
a <code>fillShape()</code> functions. This lets the program know the boundaries
of the shape to fill.
Of course the movements between the <code>beginShape()</code> and
<code>fillShape()</code> functions should start and end at the same point on
the canvas.
</p>
<p>
So let's give it a try:
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo () {
reset()
color("black")
beginShape()
spikey(5, 2, 50)
fillShape("red")
}
</code>
<p>
The stroke color, the color left by the turtle's pen, and the fill
color can be different.
</p>
<p>
You may have noticed that the fill color goes over the top of
pen stroke and it overlaps about half
of the stroke line. To make the width of the stroke as you intended
it, you need to re-stroke the graphic to get it on top of the fill.
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo () {
reset()
width(1)
color("white")
beginShape()
spikey(5, 2, 50)
fillShape("blue")
width(2)
color("white")
spikey(5, 2, 50)
}
</code>
<h2>Positioning the Polygon or Spikey </h2>
<p>
<img src="./images/positioned_spikey.png">
We want to start with the <code>spikey()</code> function that was discussed
in the Turtle Graphics Tutorial.
We don't have to always start at the center of the canvas. We can start
anywhere on the canvas. We can use the basic turtle moves like
the following code to move the figure:
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo() {
reset()
forward (100)
right(90)
forward( 100)
left(90)
spikey( 5, 2, 40)
}
</code>
<img src="./images/transparently_positioned_spikey.png">
<p>
That leaves a line of where the turtle traveled. We can prevent the
turtle from marking by having it lift the pen as in the following code.
</p>
<code class="tryme">function spikey (n, revs, size) {
var i = 0
while (i < n) {
write (i)
forward (size)
right (revs*360/n)
i = i + 1
}
}
function demo() {
reset()
penup()
forward (100)
right(90)
forward( 100)
left(90)
pendown()
spikey( 5, 2, 40)
}
</code>
<p>
But it still isn't centered about a specific point. It would be nice if
it were centered because
spikies of different sizes could be place concentrically and the points of
the spike can be reoriented without appearing to move the spikey, sort of
like the spokes of a bicycle wheel.
To center the two spikies takes a little geometry and trigonometry.
So if you don't understand the following
calculations, don't worry, this will be the only time that the math will be
this difficult in this tutorial. Consulting the figure on the right we find that:
</p>
<ul>
<img src="./images/spikey_radius.png" width=400>
<li>
Turn Angle = 360° * revolutions / number of points
</li>
<li>
Outside Angle = 180° + Turn Angle
</li>
<li>
Inside Angle = 360° - Outside Angle
</li>
<li>
Angle a = (360° - Outside Angle) / 2
</li>
<li>
Angle a = (180° - Turn Angle) / 2
</li>
<li>
Angle a = (180° - (360 * revolutions / number of points)) / 2
</li>
<li>
adjacent side = stroke length /2 = radius * cosine (Angle a)
</li>
<li>
multiplying both sides by 2 gives:
<br>
stroke length = 2 * radius * cosine (Angle a)
</li>
<li>
or replacing Angle a:
<br>
stroke length = 2 * radius * cosine((180° - Turn Angle) / 2)
</li>
</ul>
<p>
These calculations can be used to center a spikey. The size of the spikey
can now be the radius of a circle that would go around the spikey.
The code for a spikey that uses a central point and a radius
instead of a side to determine size in the following code.
<code>Math.cos()</code> is the cosine function from the JavaScript Math
library. The beauty of the cosine function is that it expresses the ratio of
the side adjacent to the angle to the hypotenuse (the long side of the
right triagle as a function of the angle. Since the JavaScript implementation
of the cosine function uses radians instead of degrees, the function
<code>degToRad()</code> is used to make the proper conversion. There
are 2 pi radians in a circle and there are 360 in the same circle. So this
conversion finds the fraction of a circle (number of degrees divided by
the number of degrees in a cirles), and multiples that fraction by the
number of radian in a circle (2 pi).
</p>
<p>
This also uses the
<code>goto()</code> function to move the turtle to an x, y position on the
canvas Cartesian coordinate system. It also uses the
<code>setheading()</code> function to orient the spikey. Although the
spikey will be drawn with a point straight up, it could be set to any angle.
</p>
<code class="tryme">function spikey ( points, revs, radius, x, y) {
penup()
goto(x, y)
setheading(0)
forward(radius)
var turnAngle = 360 * revs/points
var angleA = ( 180 - turnAngle)/2
var stroke = 2 * radius * Math.cos( degToRad( angleA))
right( 180 - angleA)
pendown()
for( var i = 0; i < points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
}
function demo () {
reset()
hideTurtle()
for (rad = 40; rad <=100; rad = rad + 10) {
spikey( 5, 2, rad, -10, 10)
}
}
</code>
<p>
Since there are a lot of changes to the <code>spikey()</code> function all at
once, let's stop to explain what is going on.
First up, the spikey function has different parameters.
The <span class="var">points</span> and
<span class="var">revs</span> parameters have the same purpose as before,
that is to limit the scope of the variables to within the <code>spikey</code>
function.
The <span class="var">side</span> parameter has been replaced with a
<span class="var">radius</span> parameter. Both specify the size of the spikey,
but in different ways. <span class="var">radius</span> specifies the radius
of a circle that would go around all of the points of the spikey. So spikeys
with different
number of <span class="var">points</span> or different number of
<span class="var">revs</span> would still be the same size as each other.
The new parameters <span class="var">x</span> and <span class="var">y</span>
specify the center point of the spikey.
</p>
<p>
As you can see when the code runs, there are seven perfectly centered and aligned
five-pointed stars.
</p>
<h2>Using Named Parameters and Arguments</h2>
<p>
The <code>spikey()</code> function has three parameters: number of points,
number of revolutions and length of side. But even with just three parameters,
it starts to get confusing as to what the order of the parameters are.
JavaScript has a mechanism to help with that, although it is not direct.
To review, JavaScript has objects. For instance a car object may be
</p>
<code>car = { make: "Volkswagen", model: "bug", year: 1967, color: "red" }
</code>
<p>
The make, model, year and color are attributes of the car. You can have
other cars with other attributes. To access an attribute you use the dot
notation with the object.attribute as in:
</p>
<code>car1 = { make: "Volkswagen", model: "bug", year: 1967, color: "red" }
car2 = { make: "Ford", model: "Mustang", year: 2008, color: "blue" }
paint = car1.color // paint is red
paint = car2.color // paint is blue
</code>
<p>
A function can use an object as its only parameter and then break out
the individual attributes. When the function is invoked, one must use
the object notation in the argument. So taking the familiar, by now,
<code>spikey()</code> function and changing it for object notation:
</p>
<code class="tryme">function spikey ( args ) {
for( var i=0; i < args.points; i = i+1) { //>
forward (args.side)
right( 360*args.revs/args.points)
}
}
function demo() {
reset();
spikey({points:5, revs:2, side:50})
left(180)
spikey({side:50, revs:2, points:5}) // order doesn't matter.
}
</code>
<p>
This was for the old simple spikey function.
Let's say you want a spikey function that does what a
spikey does, but now you want to add a starting point, a starting angle,
a stroke width and color and a fill color. That's a lot to remember just in
itself, but then you also have to remember the parameter order.
Naming the parameters reduces that complexity.
</p>
<p>
This function also know whether to re-stroke the spikey or not. This is a
case where a simple conditional provides some functionality and saves
the user of the function having to call it twice.
</p>
<code class="tryme">function spikey ( args ) {
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function demo () {
reset()
hideTurtle()
spikey({ points:5,
revs:2,
radius:15,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white"
})
spikey({ x:0,
y:30,
radius: 10,
heading:180,
side:50,
points:5,
revs:2,
color:"blue",
fill:"yellow"
})
spikey({ x:30,
y:10,
radius: 10,
heading:180-72,
side:50,
points:5,
revs:2,
color:"blue",
fill:"yellow"
})
spikey({ x:20,
y:-28,
radius: 10,
heading:-36,
side:50,
points:5,
revs:2,
stroke:"blue",
fill:"yellow"
})
spikey({ x:-20,
y:-28,
radius: 10,
heading:36,
side:50,
points:5,
revs:2,
stroke:"blue",
fill:"yellow"
})
spikey({ x:-30,
y:10,
radius: 10,
heading:180-72,
side:50,
points:5,
revs:2,
color:"blue",
fill:"yellow"
})
}
</code>
<h2>
Default Parameters
</h2>
<p>
So now we don't need to worry about the order of the parameters, but what happens
if we leave one out. As it stands, if an attribute is not supplied, JavaScript
will stop with an error because that attribute is undefined. 'undefined' is a
special value any variable can have. The easiest thing to do is to set all
undefined attributes with a default value as shown below. Note that even
arguments which probably should have been supplied are furnished with a default
here. It may be a better idea to let an argument like side remain undefined,
so that the function is called properly.
The following is a code segment to handle default values
</p>
<code>// fill out default values for undefined attributes
if (args.x === undefined) args.x = 0
if (args.y === undefined) args.y = 0
if (args.radius === undefined) args.radius = 10 // shouldn't default
if (args.heading === undefined) args.heading = 0
if (args.color === undefined) args.color = "black"
if (args.fill === undefined) args.fill = "white"
if (args.width === undefined) args.width = 1
if (args.revs === undefined) args.revs = 1 // garden variety polygon
if (args.points === undefined) args.points = 5 // shouldn't default
</code>
<h2> Polygon Animation </h2>
<p>
Now we can play around a bit with animation.
Animation is done by drawing a series of pictures or frames.
Each frame is slightly different than the preceding frame.
If the frames are played at the right speed the
human eye blurs the frames together to form motion.
Let's start with simply twisting a simple star.
</p>
<code class="tryme">function spikey ( args ) {
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
// GLOBALS
var twistAngle
// FUNCTIONS
function twistStar() {
reset()
hideTurtle()
spikey({
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:twistAngle,
color:"red",
width:3,
fill:"white"
})
twistAngle = twistAngle + 2
delay( twistStar, 30)
}
function demo () {
reset()
hideTurtle()
twistAngle = 0
twistStar()
}
</code>
<p>
An improvement can be made to this code. The object defining
the star can be made a stand alone definition. This allows
the program to be maintained in a more organized fashion: the
definitions allow the characteristics of the star to be defined
separately from the code that does the action.
This code is shown below. Note the two sections of the code: GLOBALS
and FUNCTIONS. The variables defined as they are in the GLOBALS section
are global and may be accessed by any function. ("// GLOBALS" is a comment
that is just a signal to the programmer that this is the beginning
of a section.) The name "deltaRotation" is the way that an engineer or
physisist would talk about change in rotation.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white"
}
var deltaRotation = 2 // number of degrees of twist between frames
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function twistStar() {
clear()
spikey(star1)
star1.heading = star1.heading + deltaRotation
delay( twistStar, frameDelay)
}
function demo () {
reset()
hideTurtle()
twistStar()
}
</code>
<p>
Now let's move the star about. So instead of changing the rotation,
we will change its x and y position. These are independent so
you can move the star in any direction including backward when
using a negative change or delta.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white"
}
var deltaX = 2 // pixels per frame in x direction
var deltaY = 2 // pixels per frame in y direction
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function moveStar() {
clear()
spikey(star1)
star1.x = star1.x + deltaX
star1.y = star1.y + deltaY
delay( moveStar, frameDelay)
}
function demo () {
reset()
hideTurtle()
wrap(false) // fix edge condition
moveStar()
}
</code>
<p>
Now let's grow the star over time. We do that by adding a delta to the radius for each frame.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white"
}
var deltaRadius = 2 // pixels per frame in x direction
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function growStar() {
clear()
spikey(star1)
star1.radius = star1.radius + deltaRadius
delay( growStar, frameDelay)
}
function demo () {
reset()
hideTurtle()
wrap(false) // fix edge condition
growStar()
}
</code>
<p>
Now let's combine all three and move, grow, and twist it at the same time.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white"
}
var deltaX = 2 // change in pixels per frame in x direction
var deltaY = 2 // change in pixels per frame in y direction
var deltaHeading = 2 // change in heading per frame
var deltaRadius = 2 // change in radius per frame
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function changeStar() {
clear()
spikey(star1)
star1.x = star1.x + deltaX
star1.y = star1.y + deltaY
star1.heading = star1.heading + deltaHeading
star1.radius = star1.radius + deltaRadius
delay( changeStar, frameDelay)
}
function demo () {
reset()
hideTurtle()
wrap(false) // fix edge condition
changeStar()
}
</code>
<p>
This code could be improved a small bit. The deltas can be
part of the other attributes of the star. This way if there
were more than one star, they would have separate speeds.
While we are at it, let's also shorten "delta" to just plain "d."
To make it interesting, we will also add a second star.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white",
dx: 2,
dy: 2,
dhead: 2,
drad: 2
}
var star2 = {
points:7,
revs:3,
radius:40,
x:-100,
y:0,
heading:0,
color:"black",
width:3,
fill:"blue",
dx: 4,
dy: 2,
dhead: 6,
drad: 3
}
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function changeStars() {
clear()
spikey(star1)
star1.x = star1.x + star1.dx
star1.y = star1.y + star1.dy
star1.heading = star1.heading + star1.dhead
star1.radius = star1.radius + star1.drad
spikey(star2)
star2.x = star2.x + star2.dx
star2.y = star2.y + star2.dy
star2.heading = star2.heading + star2.dhead
star2.radius = star2.radius + star2.drad
delay( changeStars, frameDelay)
}
function demo () {
reset()
hideTurtle()
wrap(false) // fix edge condition
changeStars()
}
</code>
<p>
Now let's add acceleration to the mix.
Acceleration just changes the speed parameters a bit over time.
Acceleration is the feeling you get when speeding up. Negative
acceleration or deceleration is the feeling you get when slowing
down. Since acceleration is just a change in speed we will
abbreviate acceleration as "dd..."
We can add acceleration to any speed attribute, so
we will add variables <span class="var">ddx</span>,
<span class="var">ddx</span>, <span class="var">ddy</span>,
<span class="var">ddhead</span>, and <span class="var">dddrad</span> to add
acceleration attributes for x, y, heading and radius respectively.
</p>
<code class="tryme">// GLOBALS
var star1 = {
points:5,
revs:2,
radius:40,
x:0,
y:0,
heading:0,
color:"red",
width:3,
fill:"white",
dx: 2,
dy: 2,
dhead: 2,
drad: 2,
ddx: .2,
ddy: .2,
ddhead: .2,
ddrad: .2
}
var star2 = {
points:7,
revs:3,
radius:40,
x:-100,
y:0,
heading:0,
color:"black",
width:3,
fill:"blue",
dx: 4,
dy: 2,
dhead: 6,
drad: 3,
ddx: -.4,
ddy: .1,
ddhead: -.3,
ddrad: -.2
}
var frameDelay = 30 // milliseconds between frames
// FUNCTIONS
function spikey ( args ) { // the simpler version for brevity
goto( args.x, args.y)
setheading( args.heading)
forward( args.radius)
var turnAngle = 360 * args.revs/args.points
var angleA = (180 - turnAngle)/2
var stroke = 2 * args.radius * Math.cos(degToRad(angleA))
right(180-angleA)
pendown()
color( args.color)
width( args.width)
beginShape()
for( var i = 0; i < args.points; i = i + 1) { //>
forward( stroke)
right( turnAngle)
}
fillShape( args.fill)
}
function changeStar() {
clear()
spikey(star1)
star1.x = star1.x + star1.dx
star1.y = star1.y + star1.dy
star1.heading = star1.heading + star1.dhead
star1.radius = star1.radius + star1.drad
star1.dx = star1.dx + star1.ddx
star1.dy = star1.dy + star1.ddy
star1.dhead = star1.dhead + star1.ddhead
star1.drad = star1.drad + star1.ddrad