-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemos.html
866 lines (684 loc) · 27.6 KB
/
demos.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
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" >
<title>Bernie's Better Animation Class</title>
<script type="text/javascript" src="animator.js"></script>
<link href="../include/style.css" rel="stylesheet" type="text/css" media="all">
<script type="text/javascript">
window.ANIMATOR_DEBUG = true;
</script>
<style>
.foo {
background-color: #CCAA88;
width: 200px;
}
.exampleSubjects {
background-color: #395;
margin-left: 10px;
}
pre {
border: solid 1px #3C6AD2;
background-color: #DDDDDD;
padding: 10px;
}
button {
border: solid 1px black;
background-color: #FFFFFF;
vertical-align: middle;
text-align: center;
padding: 5px;
}
.small {
width: 250px;
}
.big {
width: 500px;
}
.white {
background-color: #FFFFFF;
color: #000000;
font-style: normal;
}
.blue {
background-color: #3366CC;
color: #FFFFFF;
font-style: italic;
}
.bendy {
font-style: italic;
}
.greenish {
background-color: #3F9;
}
.blobStart {
width: 20px;
height: 20px;
background-color: #FFFFFF;
border: solid 1px #000000;
margin-top: 10px;
}
.blobEnd {
width: 20px;
height: 20px;
background-color: #FF0000;
border: solid 1px #000000;
margin-top: 10px;
}
</style>
</head>
<body>
<p>Animator.js was a library I wrote back in 1996 to handle animation on web pages. For a time it was quite ahead of the curve: I believe it was the first library to feature CSS morphing - the ability to smoothly transition between two styles defined as CSS classes.</p>
<p>The world of JavaScript has moved on a lot since then. The ideas presented in it have now been incorporated into most of the major JavaScript frameworks, either by directly incorporating the code of Animator.js (it's lisenced under a BSD lisence that allows people to do this) or by copying the techniques. Animator.js has served its purpose, and most people will be better served by a general purpose JavaScript framework: I recommend jQuery.</p>
<p>I'm keeping this page up here for historical interest, and because Animator.js may still be of interest to people who want a small, fast library that does animation and nothing else.</p>
<hr>
<p>Thanks to Tim Stone, Kaspar Fischer, Clint Priest and other developes who contributed feedback and features.</p>
<p>In this article I describe <code>Animator</code>, a class for creating animated effects à la <a href="http://moofx.mad4milk.net/">moofx</a> or <a href="http://script.aculo.us/">scriptaculous</a>.</p>
<p>Right-click here to download <a href="../include/animator.js">Animator.js version 1.1.9</a>, released under the BSD license (i.e. use it however you like).</p>
<h2>Background and motivation</h2>
<p><em>I've removed this section because it contained criticisms of other JavaScript libraries that were true in 2006, but not today. I also took the opportunity to rant on the topic of object-oriented inheritance, which was a topic I explored in greater length in the article "Inheritance is Evil and Must Be Destroyed" <link></em> - Bernie Sumption, 2010</p>
<h2>A better way of animating</h2>
<p>An Animator is an object that generates a series of nunbers between zero and one. Zero represents the start of the animation, one the end.</p>
<p>Check this out:</p>
<!-- EXAMPLE EX1 -->
<pre lang="javascript">
// This object controls the progress of the animation
ex1 = new Animator()
// the Animator's subjects define its behaviour
ex1.addSubject(updateButton);
function updateButton(value) {
var button = document.getElementById("ex1Button");
button.innerHTML = "Progress: " + Math.round(value * 100) + "%";
}
// now click below: each click to the button calls ex1.toggle()
</pre>
<p><button onclick="ex1.toggle();" id="ex1Button">Try Me! (this calls ex1.toggle())</button></p>
<script type="text/javascript">
ex1 = new Animator()
ex1.addSubject(updateButton);
function updateButton(value) {
var button = document.getElementById("ex1Button");
button.innerHTML = "Progress: " + Math.round(value * 100) + "%";
}
</script>
<p>The <code>Animator</code> above was left with the default options, here's an example that uses more configuration:</p>
<!-- EXAMPLE EX2 -->
<pre lang="javascript">
ex2 = new Animator({
duration: 1200,
interval: 400,
onComplete: function() {
document.getElementById("ex2Target").innerHTML += "Bing! ";
}
})
ex2.addSubject(updateButton);
function updateButton(value) {
document.getElementById("ex2Target").innerHTML += " Badda ";
}
</pre>
<table>
<tr>
<td><button onclick="ex2.toggle();" id="Button1">Try Me too! (this calls ex2.toggle())</button></td>
<td id="ex2Target"></td>
</tr>
</table>
<script type="text/javascript">
ex2 = new Animator({
duration: 1200,
interval: 400,
onComplete: function() {
document.getElementById("ex2Target").innerHTML += "Bing! ";
}
})
ex2.addSubject(updateButton);
function updateButton(value) {
document.getElementById("ex2Target").innerHTML += " Badda ";
}
</script>
<h2>Animating element styles</h2>
<p>Most of the time you want to animate one or more a style properties of an element. There are essentially only three kinds of CSS value - ones that scale numerically (like <code>10px</code>), ones that scale with a RGB colour value(like <code>#RRGGBB</code>), and ones that don't scale (like <code>bold / italic</code>). <code>Animator</code> provides three utility classes for each of these kinds of properties, and between them they can animate any CSS style.</p>
<!-- EXAMPLE EX3 -->
<pre lang="javascript">
// animate margin-left from 0 to 100 px
ex3 = new Animator().addSubject(
new NumericalStyleSubject(
"ex3Button",
"margin-left",
0,
100));
</pre>
<p><button onclick="ex3.toggle();" id="ex3Button">Try me three! (this calls ex3.toggle()... you're getting the idea now right?)</button></p>
<script type="text/javascript">
ex3 = new Animator().addSubject(
new NumericalStyleSubject(
"ex3Button",
"margin-left",
0,
100));
</script>
<!-- EXAMPLE EX4 -->
<pre lang="javascript">
// animate background-color from white to black
ex4 = new Animator().addSubject(
new ColorStyleSubject(
"ex4Button",
"background-color",
"#FFFFFF",
"#F4C"));
</pre>
<p><button onclick="ex4.toggle();" id="ex4Button">And Me!</button></p>
<script
type="text/javascript">
ex4 = new Animator().addSubject(
new ColorStyleSubject(
"ex4Button",
"background-color",
"#FFFFFF",
"#F4C"));
</script>
<!-- EXAMPLE EX5 -->
<pre lang="javascript">
// animating both - note how calls to addSubject() can be chained together:
ex5 = new Animator()
.addSubject(
new NumericalStyleSubject(
"ex5Button",
"margin-left",
0,
100))
.addSubject(
new ColorStyleSubject(
"ex5Button",
"background-color",
"#FFFFFF",
"#F4C"))
.addSubject(
new DiscreteStyleSubject(
"ex5Button", "font-weight",
"normal",
"bold",
0.5));
// also, check out the last line, which causes font-weight to switch from
// normal to bold half way through the animation
</pre>
<p><button onclick="ex5.toggle();" id="ex5Button">Me Too!</button></p>
<script type="text/javascript">
ex5 = new Animator()
.addSubject(
new NumericalStyleSubject(
"ex5Button",
"margin-left",
0,
100))
.addSubject(
new ColorStyleSubject(
"ex5Button",
"background-color",
"#FFFFFF",
"#F4C"))
.addSubject(
new DiscreteStyleSubject(
"ex5Button",
"font-weight",
"normal",
"bold",
0.5));
</script>
<p>If you've ever used moofx or scriptaculous then you are probably thinking that this is quite verbose, and you'd be right. <code>Animator</code> has a killer feature that removes the verbosity, but before we get to that, here are a couple more things you can do:</p>
<h2>More complex effects</h2>
<p>What if you have a number of elements that you want to animate in the same way? In that case, pass an array of elements into the <code>Subject</code>'s constructor. There is no way to add and remove elements from a <code>Subject</code> after it has been constructed - if you want to do that, use one <code>Subject</code> for each element and use <code>addSubject()</code> and <code>removeSubject()</code> on the <code>Animator</code>.</p>
<!-- EXAMPLE EX6 -->
<pre lang="javascript">
// Applying the same effect to different elements is easy
ex6 = new Animator().addSubject(
new NumericalStyleSubject(
["dot1", "dot2", "dot3"],
"margin-left",
10,
50));
</pre>
<table>
<tr>
<td><button onclick="ex6.toggle();" ID="Button2">Stretch those dots...</button></td>
<td align=left"><span class="exampleSubjects" id="dot1"> </span> <span class="exampleSubjects" id="dot2"> </span> <span class="exampleSubjects" id="dot3"> </span></td>
</tr>
</table>
<script type="text/javascript">
ex6 = new Animator().addSubject(
new NumericalStyleSubject(
["dot1", "dot2", "dot3"],
"margin-left",
10,
50));
</script>
<p>In previous examples, each subject has referred to the same element. This does not have to be the case:</p>
<!-- EXAMPLE EX7 -->
<pre lang="javascript">
// applying different effects to different elements is possible
ex7 = new Animator()
.addSubject(
new ColorStyleSubject(
"ex5ButtonA",
"background-color",
"#FF9",
"#9F9"))
.addSubject(
new NumericalStyleSubject(
"ex5ButtonB",
"padding",
"5px",
"15px"));
</pre>
<p><button onclick="ex7.toggle();" id="ex5ButtonA" style="background-color: #FF9">Click either...</button> <button onclick="ex7.toggle();" id="ex5ButtonB" style="background-color: #FF9">...of these</button></p>
<script type="text/javascript">
ex7 = new Animator()
.addSubject(
new ColorStyleSubject(
"ex5ButtonA",
"background-color",
"#FF9",
"#9F9"))
.addSubject(
new NumericalStyleSubject(
"ex5ButtonB",
"padding",
"5px",
"15px"));
</script>
<p>Opacity gets special treatment. Since IE does not support the standard 'opacity' CSS style, NumericalStyleSubject
will convert it into an appropriate filter.</p>
<!-- EXAMPLE EX8 -->
<pre lang="javascript">
ex8 = new Animator().addSubject(
new NumericalStyleSubject(
"ex8Button",
"opacity",
1,
0.25));
</pre>
<p><button onclick="ex8.toggle();" id="ex8Button">Fade</button></p>
<script type="text/javascript">
// On IE, "opacity: X" will be tuened into "filter: alpha(opacity=100*X)"
ex8 = new Animator().addSubject(
new NumericalStyleSubject(
"ex8Button",
"opacity",
1,
0.25));
</script>
<h2>Controlling the animation</h2>
<!-- EXAMPLE EX9 -->
<p>When you click on a sample button in this article, you are calling toggle() on an animator object. There are a few more control functions:</p>
<ul>
<li><code>play()</code> plays the animation from the start to the end
<li><code>stop()</code> stops the animation at its currrent position
<li><code>reverse()</code> plays the animation in reverse
<li><code>seekTo(pos)</code> plays the animation from it's current position
<li><code>seekFromTo(from, to)</code>
</ul>
<table>
<tr>
<td width="200"><a href="javascript:window.controlAnim.play();"><code>play()</code></a></td>
<td rowspan="4">
<div id="controlExample" style="width: 30px; border: solid 1px #6688CC; height: 20px;">0%</div>
</td>
</tr>
<tr>
<td><a href="javascript:window.controlAnim.stop();"><code>stop()</code></a></td>
</tr>
<tr>
<td><a href="javascript:window.controlAnim.reverse();"><code>reverse()</code></a></td>
</tr>
<tr>
<td><a href="javascript:window.controlAnim.seekTo(0.5);"><code>seekTo(0.5)</code></a></td>
</tr>
<tr>
<td><a href="javascript:controlAnim.seekFromTo(0.25, 0.75);"><code>seekFromTo(0.25, 0.75)</code></a></td>
</tr>
</table>
<script type="text/javascript">
window.controlAnim = new Animator({
duration: 2000,
onStep: function() {document.getElementById("controlExample").innerHTML = Math.round(this.state * 100) + "%"}
}).addSubject(
new NumericalStyleSubject(document.getElementById("controlExample"), 'width', 30, 230)
);
</script>
<p>The benefit of using seekTo() is that it wil avoid sudden jumps in state when called half way through an animation:</p>
<div id="controlExample2" style="background-color: #AAA; width: 200px; margin-bottom: 10px; padding: 10px;">this div uses play() and reverse on mouseover and mouseout</div>
<div id="controlExample3" style="background-color: #AAA; width: 200px; padding: 10px;">this div uses seekTo(1) and seekTo(0) on mouseover and mouseout</div>
<script type="text/javascript">
var anim2 = Animator.apply(document.getElementById("controlExample2"), "background-color: #FF00FF", {duration: 1000});
document.getElementById("controlExample2").onmouseover = function(){anim2.play()};
document.getElementById("controlExample2").onmouseout = function(){anim2.reverse()};
var anim3 = Animator.apply(document.getElementById("controlExample3"), "background-color: #FF00FF", {duration: 1000});
document.getElementById("controlExample3").onmouseover = function(){anim3.seekTo(1)};
document.getElementById("controlExample3").onmouseout = function(){anim3.seekTo(0)};
</script>
<h2>Transitions</h2>
<p>A transition is a function that takes a state (a number between 0 and 1) and returns another number
between 0 and 1. This can be used to simulate acceleration, deceleration and more complex effects. You can pass in a transition to an <code>Animator</code> object's constructor using the
cleverly named <code>transition</code> property.</p>The Animator.tx object provides a few ready made transitions:
<ul>
<li><code>Animator.tx.easeInOut</code> is the default transition, and creates a smooth effect
<li><code>Animator.tx.linear</code> maintains a constant rate of animation
<li><code>Animator.tx.easeIn</code> starts slow, gets faster
<li><code>Animator.tx.strongEaseIn</code> exaggerated version of the above
<li><code>Animator.tx.easeOut</code> starts fast, gets slower
<li><code>Animator.tx.strongEaseOut</code> exaggerated version of the above
<li><code>Animator.tx.elastic</code> go slightly past the target point, then be drawn back
<li><code>Animator.tx.veryElastic</code> as above, but with an extra pass<li><code>Animator.tx.bouncy</code> Hit the target point then bounce back
<li><code>Animator.tx.veryBouncy</code> as above, but with an extra 2 bounces
</ul>
<p>Sometimes you'll want to fine tune the above transitions. If you look at the source code where the Animator.tx object is created, you'll see that the above functions are all made by four factory functions:</p>
<h3>Animator.makeEaseIn()</h3>
<!-- EXAMPLE EX9 -->
<pre lang="javascript">
// make a transition that gradually accelerates. pass in 1 for smooth
// gravitational acceleration, higher values for an exaggerated effect
ex9 = new Animator({
transition: Animator.makeEaseIn(3),
duration: 1000
});
ex9.addSubject(
new NumericalStyleSubject(
"ex9Button",
"margin-left",
0,
200));
</pre>
<p><button onclick="ex9.toggle();" id="ex9Button">Ease me Ease me bay-beehee <span class="disclaimer">(sorry)</span></button></p>
<script type="text/javascript">
ex9 = new Animator({
transition: Animator.makeEaseIn(3),
duration: 1000
});
ex9.addSubject(
new NumericalStyleSubject(
"ex9Button",
"margin-left",
0,
200));
</script>
<h3>Animator.makeElastic()</h3>
<!-- EXAMPLE EX10 -->
<pre lang="javascript">
// make a transition that, like an object with momentum being attracted
// to a target point, goes past the target then returns
ex10 = new Animator({
transition: Animator.makeElastic(3),
duration: 2000
});
ex10.addSubject(
new NumericalStyleSubject(
"ex10Button",
"margin-left",
0,
200));
</pre>
<p><button onclick="ex10.toggle();" id="ex10Button">And sliiiide....</button></p>
<script type="text/javascript">
ex10 = new Animator({
transition: Animator.makeElastic(3),
duration: 2000
});
ex10.addSubject(
new NumericalStyleSubject(
"ex10Button",
"margin-left",
0,
200));
</script>
<h3>Animator.makeBounce()</h3>
<!-- EXAMPLE EX11 -->
<pre lang="javascript">
// make a transition that, like a ball falling to floor, reaches
// the target and bounces back again
ex11 = new Animator({
transition: Animator.makeBounce(3),
duration: 2000
});
ex11.addSubject(
new NumericalStyleSubject(
"ex11Button",
"margin-left",
0,
200));
</pre>
<p><button onclick="ex11.toggle();" id="ex11Button">Boi-oi-oiiiing!</button></p>
<script type="text/javascript">
ex11 = new Animator({
transition: Animator.makeBounce(3),
duration: 2000
});
ex11.addSubject(
new NumericalStyleSubject(
"ex11Button",
"margin-left",
0,
200));
</script>
<h3>Animator.makeADSR()</h3>
<p><a href="http://en.wikipedia.org/wiki/ADSR_envelope">An Attack Decay Sustain Release envelope</a> is a technique I lifted from music production. It is very useful for animations that start and end at the same value.</p>
<!-- EXAMPLE EX12 -->
<pre lang="javascript">
// This example shows you what an ADSR envelope looks like, but is otherwise useless
// the first three arguments are the boundary points of the 4 phases. The last is the
// sustain level. All should be between 0 and 1.
ex12 = new Animator({
transition: Animator.makeADSR(0.25, 0.5, 0.75, 0.5),
duration: 2000
});
ex12.addSubject(
new NumericalStyleSubject(
"ex12Button",
'margin-left',
0,
400));
</pre>
<p><button onclick="ex12.play();" id="ex12Button">I can't think of a good<br>caption for this one...</button></p>
<script type="text/javascript">
ex12 = new Animator({
transition: Animator.makeADSR(0.25, 0.5, 0.75, 0.5),
duration: 2000
});
ex12.addSubject(
new NumericalStyleSubject(
"ex12Button",
"margin-left",
0,
400));
</script>
<p>A practical use of ADSR is to hold an animation in a certain state for a while, as in this yellow fade example</p>
<!-- EXAMPLE EX13 -->
<pre lang="javascript" id="ex13" highlight="javascript">
// This yellow fade is emphasised by holding it at full yellow
// for the first half of the animation
ex13= new Animator({
transition: Animator.makeADSR(0, 0, 0.5, 1),
duration: 1500
});
ex13.addSubject(
new ColorStyleSubject(
"ex13Button",
"background-color",
"#FFFFFF",
"#FFFF00"));
</pre>
<p><button onclick="ex13.play();" id="ex13Button">Or this one neither</button></p>
<script type="text/javascript">
ex13= new Animator({
transition: Animator.makeADSR(0, 0, 0.5, 1),
duration: 1500
});
ex13.addSubject(
new ColorStyleSubject(
"ex13Button",
"background-color",
"#FFFFFF",
"#FFFF00"));
</script>
<h3>Custom functions</h3>
<p>Of course, you can write your own functions that do any kind of transition:</p>
<!-- EXAMPLE EX14 -->
<pre lang="javascript">
function setupEx14() {
var wobbles = parseInt(prompt("Enter a number of wobbles (try between 1 and 5)", ""));
if (!wobbles) {
alert("Sorry, I didn't understand that, have 2 wobbles.");
wobbles = 2;
}
// do some kind of crazy trigonometric stuff. I don't even know what it
// all means, I just went crazy with the Math functions
ex14Tx = function(pos) {
return ((-Math.cos(pos*Math.PI*((1+(2*wobbles))*pos))/2) + 0.5);
}
ex14 = new Animator({
transition: ex14Tx,
duration: 2000
});
ex14.addSubject(
new NumericalStyleSubject(
"ex14Button",
"margin-left",
0,
100));
ex14.play();
};
</pre>
<p><button onclick="setupEx14()" id="ex14Reset">Call setupEx14()</button> <button onclick="if (!window.ex14Tx) { alert('Call setupEx14() first!') } else { ex14.toggle(); }" id="ex14Button">call ex14.toggle()</button></p>
<script type="text/javascript">
function setupEx14() {
var wobbles = parseInt(prompt("Enter a number of wobbles (try between 1 and 5)", ""));
if (!wobbles) {
alert("Sorry, I didn't understand that, have 2 wobbles.");
wobbles = 2;
}
// do some kind of crazy trigonometric stuff. I don't even know what it
// all means, I just went crazy with the Math functions
ex14Tx = function(pos) {
return ((-Math.cos(pos*Math.PI*((1+(2*wobbles))*pos))/2) + 0.5);
}
ex14 = new Animator({
transition: ex14Tx,
duration: 2000
});
ex14.addSubject(
new NumericalStyleSubject(
"ex14Button",
"margin-left",
0,
100));
ex14.play();
};
</script>
<h2>The killer feature</h2>
<p>I wanted to explain how Animator works under the hood before I revealed this feature.</p>
<p>Like I said before, it's all a bit verbose at the moment, and most of the code in the above examples is
just boilerplate. What we need is some kind of language that lets us define the style that we want to
animate an object towards. Oh wait a second, we've already got one: CSS. A CSS style contains all the
information we need to define an animation state:</p>
<!-- EXAMPLE EX15 -->
<pre lang="javascript">
ex15 = new Animator().addSubject(
new CSSStyleSubject(
"ex15Button",
"width: 10em; background-color: rgb(256, 256, 256); font-style: normal",
"width: 40em; background-color: #F39; font-style: italic"));
// note how you can use any unit, not just 'px'.
</pre>
<p><button onclick="ex15.toggle();" id="ex15Button" style="width: 10em; height: auto">Even a designer<br>could use this ;o)</button></p>
<script type="text/javascript">
ex15 = new Animator().addSubject(
new CSSStyleSubject(
"ex15Button",
"width: 10em; background-color: rgb(256, 256, 256); font-style: normal",
"width: 40em; background-color: #F39; font-style: italic"));
// note how you can use any unit, not just 'px'.
</script>
<p><code>CSSStyleSubject</code> is a wrapper around the other three <code>Subject</code>s. It parses two CSS rule sets and for each property declaration, creates a <code>NumericalStyleSubject</code> if it looks like a number, or a <code>ColorStyleSubject</code> if it looks like a colour, or a <code>DiscreteStyleSubject</code> otherwise. <code>DiscreteStyleSubject</code>s are created with a threshold of 0.5, in other words the style changes from normal to italic half way through the animation.</p>
<p>Conveniently, you can also pass in CSS class names instead of rule sets:</p>
<!-- EXAMPLE EX16 -->
<pre lang="javascript">
ex16 = new Animator().addSubject(new CSSStyleSubject(
"ex16Button",
"small white",
"big blue bendy"));
// the classes small, big, white, blue and bendy are defined in this page's source.
</pre>
<button onclick="ex16.toggle();" id="ex16Button" class="small white">
With this technique, you can<br>
keep all style information in<br>
.css files, where it should be.
</button>
<script type="text/javascript">
ex16 = new Animator().addSubject(
new CSSStyleSubject(
"ex16Button",
"small white",
"big blue bendy"));
// the classes small, big, white, blue and bendy are defined in this page's source.
</script>
<p>When you're creating animations from CSS classes, it's easy to lose track of what your animator object is doing. The <code>Animator.inspect()</code> method returns a string that describes the animator:</p>
<p><button onclick="alert(ex16.inspect());">alert(ex16.inspect());</button></p>
<p>This is pretty good, but we can still remove some more cruft. Most of the time, the element you want to animate
will already be in its initial style. If this is the case, you can omit one of the rule sets and the
initial state will be inferred from the elements current style. This uses
<a href="http://developer.mozilla.org/en/docs/DOM:window.getComputedStyle">getComputedStyle</a> (or <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/currentstyle.asp">Element.currentStyle</a> in IE) so reflects the element's actual style after applying CSS rules in style sheets.</p>
<!-- EXAMPLE EX17 -->
<pre lang="javascript">
ex17 = new Animator().addSubject(
new CSSStyleSubject(
"ex17Button",
"width: 300px; background-color: #F39"));
</pre>
<p><button class="foo" onclick="ex17.toggle();" id="ex17Button">Die cruft, die!</button></p>
<script type="text/javascript">
ex17 = new Animator().addSubject(
new CSSStyleSubject(
"ex17Button",
"width: 300px; background-color: #F39"));
</script>
<p>Finally, there is one last bit of syntactic sugar to make it easy to apply effects. The
<code>Animator.apply(element, style, options)</code> function is a wrapper around creating a
single <code>CSSStyleSubject</code>. The second argument is the style to fade to and the third is an optional set of constructor parameters for the <code>Animator</code> object. If you want to specify the full from and to styles, pass in a two item array as the second parameter.</p>
<!-- EXAMPLE EX18 -->
<pre lang="javascript">
ex18 = Animator.apply("ex18Button", "greenish"); // ta da!
</pre>
<p><button onclick="ex18.toggle();" id="ex18Button">How's this for one line of code?</button></p>
<script type="text/javascript">
ex18 = Animator.apply("ex18Button", "greenish"); // ta da!
</script>
<h3>Oh go on, one more feature...</h3>
<p>By popular demand... Several people asked for an easy way to chain several animations together. <code>AnimatorChain</code> is an object that behaves a bit like an <code>Animator</code>, but wraps several other <code>Animator</code> objects and causes them to play in sequence.</p>
<!-- EXAMPLE EX19 -->
<pre lang="javascript">
var animators = [];
for (var i=0; i<3; i++) {
animators[i] = Animator.apply("ex18blob-" + i), "blobEnd";
}
ex19 = new AnimatorChain(animators);
// the AnimatorChain object has toggle(), play(), reverse() and seekTo(state) functions
// just like Animator objects, so you can often use them where code expects an Animator
</pre>
<p>
<button onclick="ex19.toggle();">ex19.toggle()</button>
<button onclick="ex19.play()">ex19.play()</button>
<button onclick="ex19.reverse();">ex19.reverse()</button>
<button onclick="ex19.seekTo(0)">ex19.seekTo(0)</button>
<button onclick="ex19.seekTo(1)">ex19.seekTo(1)</button>
</p>
<div id="ex18blob-0" class="blobStart"></div>
<div id="ex18blob-1" class="blobStart"></div>
<div id="ex18blob-2" class="blobStart"></div>
<script type="text/javascript">
var animators = [];
for (var i=0; i<3; i++) {
animators[i] = Animator.apply("ex18blob-" + i, "blobEnd");
}
ex19 = new AnimatorChain(animators);
// the AnimatorChain object has toggle(), play(), reverse() and seekTo(state) functions
// just like Animator objects, so you can often use them where code expects an Animator
</script>
</body>
</html>