-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonFunction.js
209 lines (205 loc) · 5.7 KB
/
commonFunction.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
//动画函数 0.3
//获取颜色的16位进制的字符串
function getColorValue(color){
var colorObj={
r:0,
g:0,
b:0
}
var colorValue=color;
if(color.indexOf('gb')==0||color.indexOf('GB')===0){
if(color.match(/[0-9]{1,}/g)){
var colorValue=color.match(/[0-9]{1,}/g).join('');
}
}
var r=parseInt(colorValue.slice(0,2),16),
g=parseInt(colorValue.slice(2,4),16),
b=parseInt(colorValue.slice(4,6),16);
colorObj.r=r;
colorObj.g=g;
colorObj.b=b;
return colorObj;
}
//处理颜色单位
function colorHandler(self,startValue,endValue,curTime,duration){
var curR=Math.floor(self.easing(curTime,startValue.r,endValue.r,duration)),
curG=Math.floor(self.easing(curTime,startValue.g,endValue.g,duration)),
curB=Math.floor(self.easing(curTime,startValue.b,endValue.b,duration)),
cur=curR.toString(16)+curG.toString(16)+curB.toString(16);
return cur;
}
//获取动画执行的当前阶段的对应的属性值
function curHandler(self,prefix,curTime,startValue,endValue,duration){
if(prefix==='#'){
return colorHandler(self,getColorValue(startValue),getColorValue(endValue),curTime,duration);
}
else{
return self.easing(curTime,startValue,endValue,duration);
}
}
//处理样式规则
function ruleHandler(style,rule){
var ruleObj=new Object(),
rules=rule.split(':'),
suffix;
if(rules[0]=='color'||rules[0]=='backgroundColor'){
ruleObj.prefix='#';
ruleObj.suffix='';
ruleObj.endValue=rules[1].slice(1);
ruleObj.startValue=style[rules[0]].slice(1)||'000000';
}
else{
if(suffix=rules[1].match(/[a-zA-Z%]{1,}/)){
suffix=rules[1].match(/[a-zA-Z%]{1,}/)[0];
}
else{
suffix='';
}
ruleObj.prefix='';
ruleObj.startValue=parseInt(style[rules[0]])||0;
ruleObj.endValue=parseInt(rules[1]);
ruleObj.suffix=suffix;
}
ruleObj.name=rules[0];
return ruleObj;
}
//贝塞尔动画时间曲线函数,使用的时候调用其中的solve(x,epsilon)方法,参数x为当前动画执行的时间,参数eplsilon为精确度
function UnitBezier(p1x, p1y, p2x, p2y) {
this.cx = 3.0 * p1x;
this.bx = 3.0 * (p2x - p1x) - this.cx;
this.ax = 1.0 - this.cx -this.bx;
this.cy = 3.0 * p1y;
this.by = 3.0 * (p2y - p1y) - this.cy;
this.ay = 1.0 - this.cy - this.by;//之后再研究
}
UnitBezier.prototype = {
epsilon : 1e-2, // 默认精度
sampleCurveX : function(t) {//贝赛尔曲线t时刻的坐标点的X坐标
return ((this.ax * t + this.bx) * t + this.cx) * t;
},
sampleCurveY : function(t) {
return ((this.ay * t + this.by) * t + this.cy) * t;
},
sampleCurveDerivativeX : function(t) {//贝赛尔曲线t时刻的坐标点的Y坐标
return (3.0 * this.ax * t + 2.0 * this.bx) * t + this.cx;
},
solveCurveX : function(x, epsilon) {
var t0,
t1,
t2,
x2,
d2,
i;
for (t2 = x, i = 0; i < 8; i++) {
x2 = this.sampleCurveX(t2) - x;
if (Math.abs (x2) < epsilon)
return t2;
d2 = this.sampleCurveDerivativeX(t2);
if (Math.abs(d2) < epsilon)
break;
t2 = t2 - x2 / d2;
}
t0 = 0.0;
t1 = 1.0;
t2 = x;
if (t2 < t0) return t0;
if (t2 > t1) return t1;
while (t0 < t1) {
x2 = this.sampleCurveX(t2);
if (Math.abs(x2 - x) < epsilon)
return t2;
if (x > x2) t0 = t2;
else t1 = t2;
t2 = (t1 - t0) * .5 + t0;
}
return t2;
},
solve:function(x,epsilon) {
return this.sampleCurveY(this.solveCurveX(x,epsilon));
}
}
var defaultPattern={
ease:new UnitBezier(0.25,0.1,0.25,1),
linear:new UnitBezier(0,0,1,1),
easeIn:new UnitBezier(0.42,0,1,1),
easeOut:new UnitBezier(0, 0, 0.58, 1),
easeInOut:new UnitBezier(0.42, 0, 0.58, 1)
}
var speedPattern=function(){
if(arguments.length===1&&typeof(arguments[0])==='string'){
var Bezier=defaultPattern[arguments[0]];
}
else if(arguments[0].length===4){
var Bezier=new UnitBezier(arguments[0][0],arguments[0][1],arguments[0][2],arguments[0][3]);
for(var i=0;i<4;i++){
if(typeof(arguments[0][i])!='number'||arguments[0][i]<0||arguments[0][i]>1){
var Bezier=defaultPattern['linear'];
}
}
}
return function(curTime,startValue,endValue,duration){
return Bezier.solve(curTime/duration,UnitBezier.prototype.epsilon)*(endValue-startValue)+startValue;
}
}
function animate(){
this.dom=null;
this.startTime=0;
this.startValue=[];
this.endValue=[];
this.propertyName=[];
this.suffix=[];
this.easing=null;
this.duration=null;
this.func=null;
this.args=null;
this.prefix=[];
}
animate.prototype.init=function(dom){
this.dom=dom;
}
animate.prototype.start=function(rules,duration,easing){
this.startTime=+new Date;
this.duration=duration;
this.easing=speedPattern(easing);
var self=this;
for(var i=0,len=rules.length;i<len;i++){
(function(t){
var ruleDate=ruleHandler(self.dom.style,rules[t]);
self.prefix[t]=ruleDate.prefix;
self.suffix[t]=ruleDate.suffix;
self.propertyName[t]=ruleDate.name;
self.endValue[t]=ruleDate.endValue;
self.startValue[t]=ruleDate.startValue;
})(i);
}
var go=function(){
var t=+new Date;
if(t>=self.startTime+self.duration){
for(var i=0,len=rules.length;i<len;i++){
self.dom.style[self.propertyName[i]]=self.prefix[i]+self.endValue[i]+self.suffix[i];
}
if(self.func){
self.func(self.args);
}
return false;
}
else{
var cur=[];
for(var i=0,len=rules.length;i<len;i++){
cur=curHandler(self,self.prefix[i],t-self.startTime,self.startValue[i],self.endValue[i],self.duration);
self.update(cur,self.propertyName[i],self.suffix[i],self.prefix[i]);
}
setTimeout(function(){
go();
},13);
}
};
go();
}
animate.prototype.update=function(cur,propertyName,suffix,prefix){
this.dom.style[propertyName]=prefix+cur+suffix;
}
animate.prototype.callback=function(){
this.func=Array.prototype.shift.apply(arguments);
this.args=arguments;
};