-
Notifications
You must be signed in to change notification settings - Fork 3
/
example3-animation.js
executable file
·234 lines (196 loc) · 6.09 KB
/
example3-animation.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
var myAnimation = new function () {
var me = this,
$myShape,
$paths,
pathAttr,
$text;
me.init = function () {
// caching jQuery objects.
$myShape = $('#myShape');
$paths = $('#myShape .path');
$text = $('#myShape text, #myShape .path')
/*
* For each of the textpaths in the page, set
* the path to be the value of the data-begin
* attribute.
*/
$paths.each(function(i, el) {
$(el).attr('data-begin', el.d);
});
/*
* We get all the SVG textpath elements and find
* out what paths they link to by looking at their
* xlink:href attribute. We then find that
* path and set it's data() to the textpath
* element.
*
* Note that we don't use $('<textpath>'), since
* jQuery can't query on SVG node names. :-/
*/
$('.textpath').each(function(i, el) {
var href = el.getAttribute('xlink:href');
$(href).data('textpath', el)
});
/*
* Make sure we get the right attribute to change
* in the paths depending
* on if the browser is an SVG or VML one.
*/
if ($paths[0].getAttribute('d')) {
pathAttr = 'd';
} else {
pathAttr = 'path';
}
/*
* Use jQuery animate using values 0 to 1 on the 'text-indent'
* attribute (which neither VML or SVG use).
*/
$myShape.css({
textIndent: 0
}).animate({
textIndent: 1
}, {
duration: 1000,
step: function (now) {
$paths.each(function(i, el) {
// VML's document object model for paths is different than SVG's.
var tween = tweenPath(now, $(el));
if (pathAttr == 'd') {
el.setAttribute(pathAttr, tween);
// updating a path does not trigger a redraw in older WebKit browsers
// so we force the issue that repeatedly sets the textpath's xlink:href attribute.
$(el).data('textpath').setAttribute('xlink:href', $(el).data('textpath').getAttribute('xlink:href'));
} else {
el.path.v = tween;
}
});
/*
* For older IE, we have to do some extra magic to
* deal with IE's crappy bug with Visual Filters
* and CLearType. We only fade in for half the animation
* to avoid blocky looking text. For other browsers, we just
* set the opacity to 'now'.
*/
if ($text[0].nodeName == 'shape') {
// IE <= 8
if (now < 0.5) {
$text.css({
filter: 'progid:DXImageTransform.Microsoft.Chroma(color="#666666") ' +
'progid:DXImageTransform.Microsoft.Alpha(opacity=' + (now * 100) + ');'
});
} else {
$text.css({
filter: 'none',
background: 'none'
});
}
} else {
// Modern Browsers.
$text.css({
opacity: now
});
}
}
});
}
/*
* tweenPath(): returns the tweened path
* of an SVG/VML path at time 't'.
*
* t is assumed to be a number between 0 and 1.
*
* $node is a jQuery node of a SVG <path> or VML
* <v:shape> which alredy has a path assigned to
* it. It also must have a 'data-end-d' attribute
* that is set to the path we want to tween to.
* This path must have the same amount of curve
* and lines (and in the same order) as the original
* path. For example, you can tween between:
*
* M 5, 182 C 9, 489, 9, 479, 10, 537 L 5, 732
*
* and
*
* M 5 182 C 201, 479, 233, 478, 232, 529 L 232, 732
*
* but not:
*
* M 5 182 C 201, 479, 233, 478, 232, 529 L 232, 732
*
* and
*
* M 27, 32 C 190, 69, 378, 73, 508, 21
*
*/
function tweenPath(t, $node) {
var startPath ,
endPath,
startPathArr,
endPathArr,
tweenPathArr = [], // return value
startVal, endVal, val;
// cache the array values if needed
if (!(startPathArr = $node.data('startPathAttr'))) {
// VML's document object model for paths is different than SVG's.
if (pathAttr == 'd') {
startPath = $node[0].getAttribute(pathAttr);
} else {
startPath = $node[0].path.v;
}
endPath = $node[0].getAttribute('data-end-d');
startPathArr = getSplitPath(startPath);
endPathArr = getSplitPath(endPath);
} else {
endPathArr = $node.data('endPathAttr');
}
for (var i=0; i<startPathArr.length; i++) {
var isStartNum = isNumber(startPathArr[i]),
isEndNum = isNumber(endPathArr[i]);
if (isStartNum && isEndNum) {
x = tweenLinear(t, startPathArr[i], endPathArr[i]);
tweenPathArr[i] = x;
} else if (isStartNum || isEndNum){
throw('Error: startPath "' + startPath + '" and endPath "' +
endPath + '" do not match up for tweening.')
} else {
tweenPathArr[i] = startPathArr[i];
}
}
return tweenPathArr.join(' ');
}
/*
* getSplitPath(): splits an VML/SVG path into an
* array split by the white space and commas
* between in the path string.
*/
function getSplitPath(path) {
path = path.replace(/([mMcClL])([0-9])/g, '$1 $2').replace(/[ \n\t]+/g, ' ');
r = path.split(/[,\s]+/);
if ($.trim(r[0]) == '') {
r.shift();
}
// let's change the values to integers if possible
for (var i=0; i<r[i].length; i++) {
var num = parseInt(r[i]);
if (!isNaN(num)) {
r[i] = num;
}
}
return r;
}
/*
* isNumber(): retuns true if the string represents
* a number.
*/
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
/*
* tweenLinear(): Classic tweening function.
* Needed by the tweenPath() function.
*/
function tweenLinear(t, start, end){
return Math.round(t * end + (1 - t) * start);
}
}
$(window).load(myAnimation.init)