forked from FakeSmile/FakeSmile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smil.user.js
executable file
·1579 lines (1487 loc) · 46.1 KB
/
smil.user.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
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
/*
@id {7eeff186-cfb4-f7c3-21f2-a15f210dca49}
@name FakeSmile
@version 0.3.0
@description SMIL implementation in ECMAScript
@creator David Leunen ([email protected])
@homepageURL http://leunen.me/fakesmile/
@ff_min_version 2.0
@ff_max_version 3.*
*/
// ==UserScript==
// @name smil
// @namespace svg.smil
// ==/UserScript==
/* MIT or GPLv3 Licenses */
/*
Copyright 2008 David Leunen
Copyright 2012 Helder Magalhaes
*/
/**
* Milliseconds Per Frame - relation between animation smoothness and resources usage:
* 83 for ~12fps (standard quality web animation; low CPU usage; slightly jumpy; recommended for discrete or slow-motion animations);
* 67 for ~15fps (high quality web animation; reasonable resources usage; recommended for most use-cases);
* 40 for 25fps ("cine"-look; recommended for good quality animations on television systems);
* 33 for ~30fps (half LCD refresh rate; recommended for high quality animations on desktop systems);
* 25 for 40fps (very smooth animation; recommended for high quality animations on dedicated desktop systems);
* 17 for ~60fps (LCD refresh rate; high CPU and system overhead; only recommended for very high quality animations running on high-end systems).
* Lower values are *not* recommended - may cause an overall negative impact on the Operating System and a relevant energy consumption increase!
* References:
* http://animation.about.com/od/faqs/f/faq_fpsnumber.htm
* http://en.wikipedia.org/wiki/Frame_rate#Frame_rates_in_film_and_television
* https://www.nczonline.net/blog/2011/12/14/timer-resolution-in-browsers/
*/
var mpf = 67;
var splinePrecision = 25;
var svgns="http://www.w3.org/2000/svg";
var smilanimns="http://www.w3.org/2001/smil-animation";
var smil2ns="http://www.w3.org/2001/SMIL20";
var smil21ns="http://www.w3.org/2005/SMIL21";
var smil3ns="http://www.w3.org/ns/SMIL30";
var timesheetns="http://www.w3.org/2007/07/SMIL30/Timesheets";
var xlinkns="http://www.w3.org/1999/xlink";
var animators = new Array(); // all animators
var id2anim = new Object(); // id -> animation elements (workaround a Gecko bug)
var animations = new Array(); // running animators
var timeZero; // timeline start-up timestamp
var prevTime; // previous render timestamp
var animTimer; // render loop timer id, when active
/**
* If declarative animations are not supported,
* the document animations are fetched and registered.
*/
function initSMIL() {
if (document.documentElement.getAttribute("smiling")=="fake")
return;
document.documentElement.setAttribute("smiling", "fake");
smile(document);
timeZero = new Date();
prevTime = new Date(0); // not yet rendered
// I schedule them (after having instantiating them, for sync-based events)
// (it doesn't work either: first 0s animation don't trigger begin event to the following -> make it asynchronous)
for (var i=0, j=animators.length; i<j; ++i)
animators[i].register();
}
function getURLCallback(data) {
if (data.success)
smile(parseXML(data.content, document));
}
function xhrCallback() {
if (this.readyState==4 && this.status==200 && this.responseXML!=null)
smile(this.responseXML);
}
function smile(animating) {
var request = null;
var src = null;
var impl = document.implementation;
// namespace-to-process cache
// ("process" in the sense of "feature check states that support by script is needed")
// (map is initialized this way to avoid variables names being picked up as key instead of their value)
var ns2proc = {};
// NOTE: feature strings are broken in ASV - apparently only declarative switch declarations work
// (we have already filter this implementation, though, during the loading phase)
// http://tech.groups.yahoo.com/group/svg-developers/message/61236
ns2proc[svgns] = !impl.hasFeature("http://www.w3.org/TR/SVG11/feature#Animation", "1.1"); //&& !impl.hasFeature("org.w3c.svg.animation", "1.0");
ns2proc[smilanimns] = !impl.hasFeature(smilanimns, "1.1");
ns2proc[smil2ns] = !impl.hasFeature(smil2ns, "2.0");
ns2proc[smil21ns] = !impl.hasFeature(smil21ns, "2.1");
ns2proc[smil3ns] = !impl.hasFeature(smil3ns, "3.0");
ns2proc[timesheetns] = !impl.hasFeature(timesheetns, "1.0");
var animates = animating.getElementsByTagName("*");
for (var i=0, j=animates.length; i<j; ++i) {
var anim = animates.item(i);
var nodeName = anim.localName;
var namespaceURI = anim.namespaceURI;
switch (nodeName.length) {
case 4: // "link".length
if ((nodeName=="link" || nodeName=="LINK") && anim.getAttribute("rel")=="timesheet" && anim.getAttribute("type")=="application/smil+xml") {
src = anim.getAttribute("src");
if (src)
break;
}
continue;
case 9: // "timesheet".length
if (nodeName=="timesheet" && ns2proc[anim.namespaceURI]) {
src = anim.getAttribute("href");
if (src)
break;
}
continue;
case 3: // "set".length
if (nodeName=="set") {
break;
}
continue;
case 7: // "animate".length
if (nodeName=="animate") {
break;
}
continue;
case 12: // "animateColor".length
if (nodeName=="animateColor") {
break;
}
continue;
case 13: // "animateMotion".length
if (nodeName=="animateMotion") {
break;
}
continue;
case 16: // "animateTransform".length
if (nodeName=="animateTransform") {
break;
}
continue;
default:
continue;
}
// deal with external timesheets
if (src && src.length > 0) {
if (!request){
// lazy initialization of XHR
request = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP.3.0") : null;
if (request) {
if (request.overrideMimeType)
request.overrideMimeType('text/xml');
request.onreadystatechange = xhrCallback;
}
}
if (request) {
request.open("GET", src, false);
request.send(null);
} else if (window.getURL && window.parseXML) {
getURL(src, getURLCallback);
}
// reset variable
src = null;
continue;
}
// deal with animations
if (ns2proc[anim.namespaceURI]) {
var targets = getTargets(anim);
var elAnimators = new Array();
for (var k=0; k<targets.length; ++k) {
var target = targets[k];
var animator = new Animator(anim, target, k);
animators.push(animator);
elAnimators[k] = animator;
}
anim.animators = elAnimators;
var id = anim.getAttribute("id");
if (id)
id2anim[id] = anim;
}
}
}
function getTargets(anim) {
if (anim.hasAttribute("select"))
return select(anim);
var href = anim.getAttributeNS(xlinkns, "href");
if (href!=null && href!="")
return [document.getElementById(href.substring(1))];
else {
var target = anim.parentNode;
if (target.localName=="item" && (target.namespaceURI==timesheetns || target.namespaceURI==smil3ns))
return select(target);
return [target];
}
}
function select(element) {
var selector = element.getAttribute("select");
var parent = element.parentNode;
while(parent && parent.nodeType==1) {
if (parent.localName=="item" && (parent.namespaceURI==timesheetns || parent.namespaceURI==smil3ns))
selector = parent.getAttribute("select")+" "+selector;
parent = parent.parentNode;
}
return document.querySelectorAll(selector);
}
function getEventTargetsById(id, ref) {
var element = null;
if (id=="prev") {
element = ref.previousSibling;
while(element && element.nodeType!=1)
element = element.previousSibling;
}
if (element==null)
element = document.getElementById(id);
if (element==null)
element = id2anim[id]; // because getElementById doesn't returns SMIL elements in Gecko
if (element==null)
return null;
if (element.animators)
return element.animators;
return [element];
}
/**
* Corresponds to one <animate>, <set>, <animateTransform>, ...
* (there can be more than one Animator for each element)
*/
Animator.prototype = {
/**
* Registers the animation.
* It schedules the beginnings and endings.
*/
register : function() {
var begin = this.anim.getAttribute("begin");
if (begin)
this.schedule(begin, this.begin);
else
this.begin(0);
var end = this.anim.getAttribute("end");
if (end)
this.schedule(end, this.finish);
},
/**
* Schedules the starts or ends of the animation.
*/
schedule : function(timeValueList, func) {
var me = this; // I do that because if I use "this", the addEventListener understands the event source
var timeValues = timeValueList.split(";");
for (var i=0; i<timeValues.length; ++i) {
var time = timeValues[i].trim();
if (time.length>11 && time.substring(0,10)=="wallclock(") {
var wallclock = new Date();
wallclock.setISO8601(time.substring(10,time.length-1));
if (!isNaN(wallclock.getTime())) {
var now = new Date();
var diff = wallclock-now;
func.call(me, diff);
}
} else if (isNaN(parseInt(time))) {
var offset = 0;
var io = time.indexOf("+");
if (io==-1)
io = time.indexOf("-");
if (io!=-1) {
offset = toMillis(time.substring(io).replace(/ /g, ""));
time = time.substring(0, io).trim();
}
io = time.indexOf(".");
var elements;
if (io==-1) {
elements = [this.target];
} else {
var id = time.substring(0, io);
if (id.indexOf("index(")==0)
id = id.substring(6,id.length-1)+this.index;
elements = getEventTargetsById(id, this.anim);
}
var event = time.substring(io+1);
var call = funk(func, me, offset);
for (var j=0; j<elements.length; ++j) {
var element = elements[j];
if (element==null)
continue;
element.addEventListener(event, call, false);
}
} else {
time = toMillis(time);
func.call(me, time);
}
}
},
/**
* Remembers the initial value of the animated attribute.
* This function is overridden.
*/
getCurVal : function() {
if (this.attributeType=="CSS") {
// should use this.target.getPresentationAttribute instead
return this.target.style.getPropertyValue(this.attributeName);
} else {
//var animAtt = this.target[this.attributeName];
//if (animAtt && animAtt.animVal)
// return animAtt.animVal.value;
//else
return this.target.getAttributeNS(this.namespace, this.attributeName);
}
},
/**
* Starts the animation.
* I mean the very beginning of it.
* Not called when repeating.
*/
begin : function(offset) {
if (this.restart=="never" || (this.running && this.restart=="whenNotActive"))
return;
if (this.running)
this.finish();
if (offset && offset>0) {
var me = this;
var myself = this.begin;
var call = function() {myself.call(me)};
window.setTimeout(call, offset);
return;
}
this.startTime = new Date();
if (offset && offset<0) {
this.startTime.setTime(this.startTime.getTime()+offset);
if (this.startTime<timeZero)
return;
}
this.stop();
this.running = true;
var initVal = this.getCurVal();
this.realInitVal = initVal;
// TODO
// I should get the inherited value here (getPresentationAttribute is not supported)
if (!initVal && propDefaults[this.attributeName] )
initVal = propDefaults[this.attributeName];
if (this.anim.nodeName=="set")
this.step(this.to);
this.iteration = 0;
if (this.values) {
this.animVals = this.values.split(";");
for (var i=0; i<this.animVals.length; ++i)
this.animVals[i] = this.animVals[i].trim();
} else {
this.animVals = new Array();
if (this.from)
this.animVals[0] = this.from;
else
this.animVals[0] = initVal;
if (this.by && this.animVals[0])
this.animVals[1] = this.add(this.normalize(this.animVals[0]), this.normalize(this.by));
else
this.animVals[1] = this.to;
}
if (this.animVals[this.animVals.length-1]) {
this.freezed = this.animVals[this.animVals.length-1];
if (this.animVals[0]) {
if ( (this.animVals[0][0]=="#" || colors[this.animVals[0]] || (this.animVals[0].length>5 && this.animVals[0].trim().substring(0,4)=="rgb(")) &&
(this.freezed[0]=="#" || colors[this.freezed] || (this.freezed.length>5 && this.freezed.trim().substring(0,4)=="rgb(")) )
this.color();
else {
var cp = new Array();
var oneVal = this.animVals[0];
var qualified = getUnit(oneVal);
cp[0] = qualified[0];
this.unit = qualified[1];
for (var i=1; i<this.animVals.length; ++i) {
var oneVal = this.animVals[i];
var qualified = getUnit(oneVal);
if (qualified[1]==this.unit)
cp[i] = qualified[0];
else {
cp = this.animVals;
break;
}
}
this.animVals = cp;
}
}
}
this.iterBegin = this.startTime;
animations.push(this);
// if this is the first running animator, start the rendering loop
if (!animTimer) {
// asynchronous to render all animators, listeners, etc. starting at this frame
window.setTimeout(animate, 0);
// schedules the rendering loop
animTimer = window.setInterval(animate, mpf);
}
for (var i=0; i<this.beginListeners.length; ++i)
this.beginListeners[i].call();
var onbegin = this.anim.getAttribute("onbegin");
if (onbegin)
eval(onbegin);
},
/**
* This function is overridden for multiple values attributes (scale, rotate, translate).
*/
normalize : function(value) {
return value;
},
/**
* Sums up two normalized values.
*/
add : function(a, b) {
return ""+(parseFloat(a)+parseFloat(b));
},
/**
* Computes and applies the animated value for a given time.
* Returns false if this animation has been stopped (removed from the running array).
*/
f : function(curTime) {
var dur = this.computedDur;
if (isNaN(dur))
return true;
var beginTime = this.iterBegin;
var diff = curTime-beginTime;
var percent = diff/dur;
if (percent>=1)
return this.end(curTime);
var iteration = this.iteration;
if (this.repeatCount && this.repeatCount!="indefinite" && (iteration+percent)>=this.repeatCount) {
if (this.fill=="freeze")
this.freezed = this.valueAt(this.repeatCount-iteration);
return this.end(curTime);
}
if (this.repeatDur && this.repeatDur!="indefinite" && (curTime-this.startTime)>=toMillis(this.repeatDur)) {
if (this.fill=="freeze") {
var div = toMillis(this.repeatDur)/dur;
this.freezed = this.valueAt(div-Math.floor(div));
}
return this.end(curTime);
}
var anim = this.anim;
if (anim.localName=="set")
return true;
var curVal = this.valueAt(percent);
this.step(curVal);
return true;
},
isInterpolable : function(from, to) {
var areN = (!isNaN(from) && !isNaN(to));
if (!areN && from.trim().indexOf(" ")!=-1 && to.trim().indexOf(" ")!=-1) {
var tfrom = from.trim().split(" ");
var tto = to.trim().split(" ");
areN = true;
if (tfrom.length==tto.length)
for (var i=0; i<tto.length; ++i)
if (!this.isInterpolable(tfrom[i], tto[i]))
return false;
}
return areN;
},
valueAt : function(percent) {
var tValues = this.animVals;
if (percent==1)
return tValues[tValues.length-1];
if (this.calcMode=="discrete" || !this.isInterpolable(tValues[0],tValues[1])) {
if (this.keyTimes) {
for (var i=1; i<this.keyTimes.length; ++i)
if (this.keyTimes[i]>percent)
return tValues[i-1];
return tValues[tValues.length-1];
}
var parts = tValues.length;
var div = Math.floor(percent*parts);
return tValues[div];
} else {
var index;
if (this.keyTimes) {
for (var i=1; i<this.keyTimes.length; ++i)
if (this.keyTimes[i]>percent) {
index = i-1;
var t1 = this.keyTimes[index];
percent = (percent-t1)/(this.keyTimes[i]-t1);
break;
}
if (i>=this.keyTimes.length)
index = i-2;
} else {
var parts = tValues.length-1;
index = Math.floor(percent*parts);
percent = (percent%(1/parts))*parts;
}
if (this.calcMode=="spline")
percent = this.spline(percent, index);
return this.interpolate(this.normalize(tValues[index]), this.normalize(tValues[index+1]), percent);
}
},
spline : function(percent, index) {
var path = this.keySplines[index];
var tot = path.getTotalLength();
var step = tot/splinePrecision;
for (var i=0; i<=tot; i+=step) {
var pt = path.getPointAtLength(i);
if (pt.x>percent) {
var pt1 = path.getPointAtLength(i-step);
percent -= pt1.x;
percent /= pt.x-pt1.x;
return pt1.y+((pt.y-pt1.y)*percent);
}
}
var pt = path.getPointAtLength(tot);
var pt1 = path.getPointAtLength(tot-step);
percent -= pt1.x;
percent /= pt.x-pt1.x;
return pt1.y+((pt.y-pt1.y)*percent);
},
/**
* Performs the interpolation.
* This function is overridden.
*/
interpolate : function(from, to, percent) {
if (!this.isInterpolable(from, to)) {
if (percent<.5)
return from;
else
return to;
}
if (from.trim().indexOf(" ")!=-1) {
var tfrom = from.split(" ");
var tto = to.split(" ");
var ret = new Array();
for (var i=0; i<tto.length; ++i)
ret[i] = parseFloat(tfrom[i])+((tto[i]-tfrom[i])*percent);
return ret.join(" ");
}
return parseFloat(from)+((to-from)*percent);
},
/**
* Apply a value to the attribute the animator is linked to.
* This function is overridden.
*/
step : function(value) {
var attributeName = this.attributeName;
var attributeType = this.attributeType;
if (attributeType=="CSS") {
// workaround a Gecko and WebKit bug
if (attributeName=="font-size" && !isNaN(value))
value += "px";
else if (this.unit)
value += this.unit;
this.target.style.setProperty(attributeName, value, "");
} else {
if (this.unit)
value += this.unit;
//var animAtt = this.target[attributeName];
//if (animAtt && animAtt.animVal)
// animAtt.animVal.value = value;
//else
this.target.setAttributeNS(this.namespace, attributeName, value);
}
},
/**
* Normal end of the animation:
* it restarts if repeatCount.
*/
end : function(now) {
if (!this.repeatCount && !this.repeatDur)
return this.finish();
else {
++this.iteration;
if (this.repeatCount && this.repeatCount!="indefinite" && this.iteration>=this.repeatCount)
return this.finish();
else if (this.repeatDur && this.repeatDur!="indefinite" && (now-this.startTime)>=toMillis(this.repeatDur))
return this.finish();
else {
if (this.accumulate=="sum") {
var curVal = this.getCurVal();
if (!curVal && propDefaults[this.attributeName] )
curVal = propDefaults[this.attributeName];
if (this.by && !this.from) {
this.animVals[0] = curVal;
this.animVals[1] = this.add(this.normalize(curVal), this.normalize(this.by));
} else {
for (var i=0; i<this.animVals.length; ++i)
this.animVals[i] = this.add(this.normalize(curVal), this.normalize(this.animVals[i]));
}
this.freezed = this.animVals[this.animVals.length-1];
}
this.iterBegin = now;
for (var i=0; i<this.repeatIterations.length; ++i) {
if (this.repeatIterations[i]==this.iteration)
this.repeatListeners[i].call();
}
var onrepeat = this.anim.getAttribute("onrepeat");
if (onrepeat)
eval(onrepeat);
}
}
return true;
},
/**
* Really stop of the animation (it doesn't repeat).
* Freezes or removes the animated value.
*/
finish : function(offset) {
if (this.min && this.min!="indefinite") {
var now = new Date();
if ((now-this.startTime)>=this.computedMin)
return true;
}
if (offset && offset>0) {
var me = this;
var myself = this.finish;
var call = function() {myself.call(me)};
window.setTimeout(call, offset);
return true;
}
if (offset && offset<0) {
var now = new Date();
now.setTime(now.getTime()+offset);
if (now<this.startTime)
return true;
}
var fill = this.fill;
var kept = true;
if (fill=="freeze") {
this.freeze();
} else {
this.stop();
this.step(this.realInitVal);
kept = false;
}
if (this.running) {
for (var i=0; i<this.endListeners.length; ++i)
this.endListeners[i].call();
var onend = this.anim.getAttribute("onend");
if (onend)
eval(onend);
this.running = false;
}
return kept;
},
/**
* Removes this animation from the running array.
*/
stop : function() {
for (var i=0, j=animations.length; i<j; ++i)
if (animations[i]==this) {
animations.splice(i, 1);
// if this is the last running animator, stop the rendering loop
if (!animations.length && animTimer) {
window.clearInterval(animTimer);
animTimer = null;
}
break;
}
},
/**
* Freezes the attribute value to the ending value.
*/
freeze : function() {
this.step(this.freezed);
},
/**
* Adds a listener to this animation beginning or ending.
*/
addEventListener : function(event, func, b) {
if (event=="begin")
this.beginListeners.push(func);
else if (event=="end")
this.endListeners.push(func);
else if (event.length>7 && event.substring(0,6)=="repeat") {
var iteration = event.substring(7,event.length-1);
this.repeatListeners.push(func);
this.repeatIterations.push(iteration);
}
},
/**
* Returns the path linked to this animateMotion.
*/
getPath : function() {
var mpath = this.anim.getElementsByTagNameNS(svgns,"mpath")[0];
if (mpath) {
var pathHref = mpath.getAttributeNS(xlinkns, "href");
return document.getElementById(pathHref.substring(1));
} else {
var d = this.anim.getAttribute("path");
if (d) {
var pathEl = createPath(d);
//pathEl.setAttribute("display", "none");
//this.anim.parentNode.appendChild(pathEl);
return pathEl;
}
}
return null;
},
/**
* Initializes this animator as a translation (x,y):
* <animateTransform type="translate"> or
* <animateMotion> without a path.
*/
translation : function() {
if (this.by && this.by.indexOf(",")==-1)
this.by = this.by+",0";
this.normalize = function(value) {
var coords = value.replace(/,/g," ").replace(/ +/," ").split(/ /);
coords[0] = parseFloat(coords[0]);
if (coords.length==1)
coords[1] = 0;
//coords[1] = this.initVal.split(",")[1];
else
coords[1] = parseFloat(coords[1]);
return coords;
};
this.add = function(a, b) {
var x = a[0]+b[0];
var y = a[1]+b[1];
return x+","+y;
};
this.isInterpolable = function(from, to) { return true; };
this.interpolate = function(from, to, percent) {
var x = from[0]+((to[0]-from[0])*percent);
var y = from[1]+((to[1]-from[1])*percent);
return x+","+y;
};
},
/**
* Initializes this animator as a color animation:
* <animateColor> or
* <animate> on a color attribute.
*/
color : function() {
this.isInterpolable = function(from, to) { return true; };
this.interpolate = function(from, to, percent) {
var r = Math.round(from[0]+((to[0]-from[0])*percent));
var g = Math.round(from[1]+((to[1]-from[1])*percent));
var b = Math.round(from[2]+((to[2]-from[2])*percent));
var val = "rgb("+r+","+g+","+b+")";
return val;
};
this.normalize = function(value) {
var rgb = toRGB(value);
if (rgb==null)
return toRGB(propDefaults[this.attributeName]);
return rgb;
};
this.add = function(a, b) {
var ret = new Array();
for (var i=0; i<a.length; ++i)
ret.push(Math.min(a[i],255)+Math.min(b[i],255));
return ret.join(",");
};
},
d : function() {
this.isInterpolable = function(from, to) { return true; };
this.interpolate = function(from, to, percent) {
var path = "";
var listFrom = from.myNormalizedPathSegList;
var listTo = to.myNormalizedPathSegList;
var segFrom, segTo, typeFrom, typeTo;
for (var i=0, j=Math.min(listFrom.numberOfItems, listTo.numberOfItems); i<j; ++i) {
segFrom = listFrom.getItem(i);
segTo = listTo.getItem(i);
typeFrom = segFrom.pathSegType;
typeTo = segTo.pathSegType;
// NOTE: in 'normalizedPathSegList', only 'M', 'L', 'C' and 'z' path data commands are expected
if (typeFrom==1 || typeTo==1) // PATHSEG_CLOSEPATH
path += " z ";
else {
var x = segFrom.x+((segTo.x-segFrom.x)*percent);
var y = segFrom.y+((segTo.y-segFrom.y)*percent);
if (typeFrom==2 || typeTo==2) // PATHSEG_MOVETO_ABS
path += " M ";
else if (typeFrom==4 || typeTo==4) // PATHSEG_LINETO_ABS
path += " L ";
// NOTE: need to be more strict here, as interpolating a 'C' command with an 'M' or an 'L' isn't yet supported
// (additional trickery is required for dealing with different DOM interfaces and interpolating them)
else if (typeFrom==6 && typeTo==6) { // PATHSEG_CURVETO_CUBIC_ABS
var x1 = segFrom.x1+((segTo.x1-segFrom.x1)*percent);
var y1 = segFrom.y1+((segTo.y1-segFrom.y1)*percent);
var x2 = segFrom.x2+((segTo.x2-segFrom.x2)*percent);
var y2 = segFrom.y2+((segTo.y2-segFrom.y2)*percent);
path += " C "+x1+","+y1+" "+x2+","+y2+" ";
} else
// "unexpected" type found, which means that 'pathSegList' is being used
// (incomplete support for segment interpolation therefore switch to a discrete approach)
return (percent<.5? from: to).getAttribute("d");
path += x+","+y;
}
}
return path;
};
this.normalize = function(value) {
var path = createPath(value);
return path;
};
}
};
/**
* Constructor:
* - initializes
* - gets the attributes
* - corrects and precomputes some values
* - specializes some functions
*/
function Animator(anim, target, index) {
this.anim = anim;
this.target = target;
this.index = index;
anim.targetElement = target;
this.attributeType = anim.getAttribute("attributeType");
this.attributeName = anim.getAttribute("attributeName");
if (this.attributeType!="CSS" && this.attributeType!="XML") {
// attributeType not specified, default stands for "auto"
// "The implementation must first search through the list of CSS properties for a matching property name"
// http://www.w3.org/TR/SVG11/animate.html#AttributeTypeAttribute
if (propDefaults[this.attributeName] && this.target.style.getPropertyValue(this.attributeName))
this.attributeType = "CSS";
else
this.attributeType = "XML";
}
if (this.attributeType=="XML" && this.attributeName) {
this.namespace = null;
var chColon = this.attributeName.indexOf(":");
if (chColon != -1) {
var prefix = this.attributeName.substring(0,chColon);
this.attributeName = this.attributeName.substring(chColon+1);
var node = target;
while(node && node.nodeType==1) {
var ns = node.getAttributeNS("http://www.w3.org/2000/xmlns/", prefix);
if (ns) {
this.namespace = ns;
break;
}
node = node.parentNode;
}
}
}
if (this.attributeName=="d")
this.d();
else if (this.attributeName=="points") {
this.isInterpolable = function(from, to) { return true; };
this.interpolate = function(from, to, percent) {
var ret = new Array();
var xyFrom, xyTo, x, y;
for (var i=0, j=Math.min(from.length, to.length); i<j; ++i) {
xyFrom = from[i].split(",");
xyTo = to[i].split(",");
x = parseFloat(xyFrom[0])+((parseFloat(xyTo[0])-xyFrom[0])*percent);
y = parseFloat(xyFrom[1])+((parseFloat(xyTo[1])-xyFrom[1])*percent);
ret.push(x+","+y);
}
return ret.join(" ");
};
this.normalize = function(value) {
var ar = value.split(" ");
for (var i=ar.length-1; i>=0; --i)
if (ar[i]=="")
ar.splice(i,1);
return ar;
};
}
this.from = anim.getAttribute("from");
this.to = anim.getAttribute("to");
this.by = anim.getAttribute("by");
this.values = anim.getAttribute("values");
if (this.values) {
this.values = this.values.trim();
if (this.values[this.values.length-1]==";")
this.values = this.values.substring(0, this.values.length-1);
}
this.calcMode = anim.getAttribute("calcMode");
this.keyTimes = anim.getAttribute("keyTimes");
if (this.keyTimes) {
this.keyTimes = this.keyTimes.split(";");
for (var i=0; i<this.keyTimes.length; ++i)
this.keyTimes[i] = parseFloat(this.keyTimes[i]);
this.keyPoints = anim.getAttribute("keyPoints");
if (this.keyPoints) {
this.keyPoints = this.keyPoints.split(";");
for (var i=0; i<this.keyPoints.length; ++i)
this.keyPoints[i] = parseFloat(this.keyPoints[i]);
}
}
this.keySplines = anim.getAttribute("keySplines");
if (this.keySplines) {
this.keySplines = this.keySplines.split(";");
for (var i=0; i<this.keySplines.length; ++i)
this.keySplines[i] = createPath("M 0 0 C "+this.keySplines[i]+" 1 1");
}
this.dur = anim.getAttribute("dur");
if (this.dur && this.dur!="indefinite")
this.computedDur = toMillis(this.dur);
this.max = anim.getAttribute("max");
if (this.max && this.max!="indefinite") {
this.computedMax = toMillis(this.max);
if (!isNaN(this.computedMax) && this.computedMax>0 && (!this.computedDur || this.computedDur>this.computedMax))
this.computedDur = this.computedMax;
}
this.min = anim.getAttribute("min");
if (this.min) {
this.computedMin = toMillis(this.min);
if (!this.computedDur || this.computedDur<this.computedMin)
this.computedDur = this.computedMin;
}
this.fill = anim.getAttribute("fill");
this.type = anim.getAttribute("type");
this.repeatCount = anim.getAttribute("repeatCount");
this.repeatDur = anim.getAttribute("repeatDur");
this.accumulate = anim.getAttribute("accumulate");
this.additive = anim.getAttribute("additive");
this.restart = anim.getAttribute("restart");
if (!this.restart)
this.restart = "always";
this.beginListeners = new Array();
this.endListeners = new Array();
this.repeatListeners = new Array();
this.repeatIterations = new Array();
var nodeName = anim.localName;
if (nodeName=="animateColor") {
this.color();
} else if (nodeName=="animateMotion") {
this.isInterpolable = function(from, to) { return true; };
this.getCurVal = function() {
var curTrans = this.target.transform;
if (curTrans && curTrans.animVal.numberOfItems>0) {
var transList = curTrans.animVal;
return decompose(transList.getItem(0).matrix, "translate");
} else
return "0,0";
};
this.path = this.getPath();
if (this.path) {
this.valueAt = function(percent) {
var length = this.path.getTotalLength();
var point = this.path.getPointAtLength(percent*length);
return point.x+","+point.y;
};
} else {
this.translation();
}
this.freeze = function() {
var val = this.valueAt(1);
this.step(val);
};
if (this.keyPoints && this.keyTimes) {
this.pathKeyTimes = this.keyTimes;
this.keyTimes = null;
this.superValueAt = this.valueAt;
this.valueAt = function(percent) {
for (var i=1; i<this.keyPoints.length; ++i) {
var fakePC = this.keyPoints[this.keyPoints.length-1]
if (this.pathKeyTimes[i]>percent) {