-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebkit.tweener.js
executable file
·341 lines (319 loc) · 10.5 KB
/
webkit.tweener.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
/**
* animation kit for webkit with jQuery
*
* Copyright (c) 2010 Juan Carlos del Valle (imekinox.com) <jc.ekinox@gmail.com>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @license http://www.gnu.org/licenses/gpl.html
* @project jquery.doTween
*/
// Main Tweening function
jQuery.fn.doTween = function(settings){
settings = jQuery.extend({
time: 1,
transition: "linear",
callback: function(_this){
},
keyframes: {}
}, settings);
$(this).attr("is_tweening", true);
this.each(function(){
// Remove any webkit_style that wasn't deleted by callback
//$(".webkit_style").remove();
var unique_id = new Date().getTime();
// Generate unique style for this tween
var theCss = "<style id='style_" + unique_id + "' class='webkit_style'>";
// Generate Webkit keyframes
theCss += "@-webkit-keyframes 'tweener_" + unique_id + "' {";
for (var key in settings.keyframes) {
theCss += key + " { ";
for (var attribute in settings.keyframes[key]) {
if (attribute == "transform") {
theCss += "-webkit-transform: ";
var tmp = "";
var arr_b = [];
var arr_a = [];
current_transform = $(this).css("-webkit-transform");
// get current transformation and order it
if (current_transform != "none")
arr_a = current_transform.split(" ").sort();
for (var transform in settings.keyframes[key][attribute])
arr_b.push(transform + "(" + settings.keyframes[key][attribute][transform] + ")");
// If there is a setting not defined in the next frame and is part of the current transform append it to the animation (so it wont break)
for (var i in arr_a) {
var matched = false;
for (var j in arr_b) {
if (arr_a[i].split("(")[0] == arr_b[j].split("(")[0]) {
matched = true;
}
}
if (matched == false)
arr_b.push(arr_a[i]);
}
// Order transform (order between frames must match)
arr_b.sort();
for (var i in arr_b) {
theCss += arr_b[i] + " ";
tmp += arr_b[i] + " ";
}
theCss += ";";
// Apply defined settings to object so it wont get back to previous position when animation ends
$(this).css("-webkit-transform", tmp);
}
else {
// Apply defined settings to object so it wont get back to previous position when animation ends
$(this).css(attribute, settings.keyframes[key][attribute]);
theCss += attribute + ": " + settings.keyframes[key][attribute] + "; ";
}
}
theCss += "}";
};
theCss += "}";
theCss += "</style>";
// Callback execution and removing inserted style
jQuery(this).bind("webkitAnimationEnd", function(event){
$("#style_" + unique_id).remove();
$(this).attr("is_tweening", false);
settings.callback(this);
this.style.removeProperty("-webkit-animation-name");
this.style.removeProperty("-webkit-animation-duration");
this.style.removeProperty("-webkit-transition-timing-function");
event.stopPropagation();
$(this).unbind("webkitAnimationEnd");
});
// Append style to the head tag
$("head").append(theCss);
$(this).css({
"-webkit-animation-name": "tweener_" + unique_id,
"-webkit-animation-duration": settings.time + "s",
"-webkit-transition-timing-function": "'" + settings.transition + "'"
});
});
return jQuery(this);
};
jQuery.fn.isTweening = function(){
return (jQuery(this).attr("is_tweening") == "true");
};
jQuery.fn.removeAllTweens = function(){
$(".webkit_style").remove();
};
// AlphaTo function
jQuery.fn.alphaTo = function(alpha, time, transition, callback){
//setting proper display when changing alpha (on callback)
var new_callback = function(_this){
if(callback !== undefined) callback();
$(_this).css("display", (alpha == 0)?"none":"block");
};
if(alpha > 0) $(this).css("display", "block");
$(this).doTween({
time: time,
transition: transition,
callback: new_callback,
keyframes: {
from: {
"-webkit-transform-style": "preserve-3d",
opacity: $(this).css("opacity")
},
to: {
"-webkit-transform-style": "preserve-3d",
opacity: alpha
}
}
});
return jQuery(this);
};
// SlideTo function
jQuery.fn.slideTo = function(_x, _y, time, transition, callback){
$(this).doTransform({
translateX: _x + "px",
translateY: _y + "px"
}, {
translateX: "0px",
translateY: "0px"
}, time, transition, callback);
return jQuery(this);
};
//GlowTo function
jQuery.fn.glowTo = function(size, color, time, transition, callback){
$(this).doTween({
time: time,
transition: transition,
callback: callback,
keyframes: {
from: {
"-webkit-box-shadow": $(this).css("-webkit-box-shadow")
},
to: {
"-webkit-box-shadow": "0px 0px " + size + "px " + color
}
}
});
return jQuery(this);
};
jQuery.fn.doTransform = function(transform, default_o, time, transition, callback){
$(this).doTween({
time: time,
transition: transition,
callback: callback,
keyframes: {
from: {
"-webkit-transform": $(this).lastTransformOr(default_o)
},
to: {
transform: transform
}
}
});
return jQuery(this);
};
// Rotate function
jQuery.fn.rotate = function(deg, time, transition, callback){
$(this).doTransform({
rotate: deg + "deg"
}, {
rotate: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// RotateX function
jQuery.fn.rotateX = function(deg, time, transition, callback){
$(this).doTransform({
rotateX: deg + "deg"
}, {
rotateX: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// RotateY function
jQuery.fn.rotateY = function(deg, time, transition, callback){
$(this).doTransform({
rotateY: deg + "deg"
}, {
rotateY: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// Scale function
jQuery.fn.scale = function(scale, time, transition, callback){
$(this).doTransform({
scale: scale
}, {
scale: 1
}, time, transition, callback);
return jQuery(this);
};
// ScaleX function
jQuery.fn.scaleX = function(scale, time, transition, callback){
$(this).doTransform({
scaleX: scale
}, {
scaleX: 1
}, time, transition, callback);
return jQuery(this);
};
// ScaleY function
jQuery.fn.scaleY = function(scale, time, transition, callback){
$(this).doTransform({
scaleY: scale
}, {
scaleY: 1
}, time, transition, callback);
return jQuery(this);
};
// Skew function
jQuery.fn.skew = function(deg, time, transition, callback){
$(this).doTransform({
skew: deg + "deg"
}, {
skew: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// SkewX function
jQuery.fn.skewX = function(deg, time, transition, callback){
$(this).doTransform({
skewX: deg + "deg"
}, {
skewY: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// SkewY function
jQuery.fn.skewY = function(deg, time, transition, callback){
$(this).doTransform({
skewX: deg + "deg"
}, {
skewY: "0deg"
}, time, transition, callback);
return jQuery(this);
};
// lastTransformOr sets the default from value for the transform(s) that will be used in case
// previous transformation does not has a default value for those transforms and orders them
jQuery.fn.lastTransformOr = function(arr_b){
var new_transform = "";
var current_transform = $(this).css("-webkit-transform");
if (current_transform != "none") {
var arr_a = current_transform.split(" ").sort();
var match = false;
for (var j in arr_b) {
for (var i in arr_a) {
if (arr_a[i].split("(")[0] == j)
match = true
}
if (!match)
arr_a.push(j + "(" + arr_b[j] + ")");
}
arr_a.sort();
for (var i in arr_a)
new_transform += arr_a[i] + " ";
}
else {
new_transform = "none";
}
return new_transform;
};
// AlphaTo function
jQuery.fn.growX = function(_width, time, transition, callback){
$(this).doTween({
time: time,
transition: transition,
callback: callback,
keyframes: {
from: {
"-webkit-transform-style": "preserve-3d",
width: $(this).css("width")
},
to: {
"-webkit-transform-style": "preserve-3d",
width: _width
}
}
});
return jQuery(this);
};
jQuery.fn.growY = function(_height, time, transition, callback){
$(this).doTween({
time: time,
transition: transition,
callback: callback,
keyframes: {
from: {
"-webkit-transform-style": "preserve-3d",
height: $(this).css("height")
},
to: {
"-webkit-transform-style": "preserve-3d",
height: _height
}
}
});
return jQuery(this);
};