This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (166 loc) · 5.07 KB
/
index.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
import easing from './src/easing.js'
import util from './src/util.js'
import draw from './src/draw.js'
// to be done
// 适配
window.Canimation = function (canvas, width, height) {
this.w = canvas.width = width + '';
this.h = canvas.height = height + '';
this.ctx = canvas.getContext('2d');
this.t = 0;
this.cacheCanvas = document.createElement('canvas');
this.cacheCtx = this.cacheCanvas.getContext('2d');
this.cacheCanvas.width = this.w + '';
this.cacheCanvas.height = this.h + '';
}
Canimation.prototype.now = window.performance.now ?
window.performance.now.bind(window.performance)
:
function () {
return new Date().getTime();
}
Canimation.prototype.aSin = function (x, y, cx, cy) {
var r = Math.pow((y - cy) * (y - cy) + (x - cx) * (x - cx), .5);
if (x - cx >= 0) {
return (Math.asin((y - cy) / r));
} else {
return Math.PI - Math.asin((y - cy) / r);
}
}
Canimation.prototype.createAnimation = function (option, loop) {
this.iterator = [];
var maxDuration = 1000;
var _this = this;
for (var key in option) {
var action = option[key];
if (action.duration && action.duration > maxDuration) {
maxDuration = action.duration;
}
// 将终点状态统一到 to 属性上
if (action.arcTo) {
action.to = action.arcTo;
action.arcTo = true;
}
// 将对象包裹为数组形式
if (this.util.isObject(action.from)) {
action.from = [action.from];
}
if (this.util.isObject(action.to)) {
action.to = [action.to];
}
// 处理数组中每个对象的属性,将需要改变的属性设为存取器属性
for (var i = 0; i < action.from.length; i++) {
let from = action.from[i];
let to = action.to[i];
let dur = action.duration || 1000;
let easing = action.easing || 'linear';
let obj = {
name: key,
dur: dur,
easing: easing
};
for (var prop in from) {
if (!to[prop] || from[prop] === to[prop]) {
obj[prop] = from[prop]
} else {
if (!this.util.isNum(from[prop]) || !this.util.isNum(to[prop])) {
throw new Error('the variable param must be number')
}
let b = from[prop];
let c = to[prop] - from[prop];
Object.defineProperty(obj, prop, {
get: function () {
return _this.easing[obj.easing](_this.t, b, c, obj.dur)
}
});
}
}
if (action.arcTo) {
let cx = to._x;
let cy = to._y;
let rad = from.rad || this.aSin(from.x, from.y, cx, cy) / Math.PI;
let R = to._R;
let r = to._r;
let e = to._e || 2;
Object.defineProperty(obj, 'x', {
get: function () {
var radian = _this.easing[obj.easing](_this.t, rad, e, obj.dur)
return cx + R * Math.cos(Math.PI * radian)
}
})
Object.defineProperty(obj, 'y', {
get: function () {
var radian = _this.easing[obj.easing](_this.t, rad, e, obj.dur)
return cy + r * Math.sin(Math.PI * radian)
}
})
}
// 参数再加工
obj = this.processParam[key](obj);
this.iterator.push(obj);
}
}
loop = loop || false;
this.animate(maxDuration, loop);
};
Canimation.prototype.animate = function (maxDuration, loop) {
var start = this.now();
var _this = this;
if (window.requestAnimationFrame) {
var animate = function () {
if (_this.t > maxDuration) {
if (loop === true) {
start = _this.now();
_this.t = 0;
} else {
return;
}
}
render();
requestAnimationFrame(animate)
}
animate();
} else {
var id = setInterval(function () {
if (_this.t > maxDuration) {
clearInterval(id);
return;
}
render();
}, 19)
}
function render () {
_this.ctx.clearRect(0, 0, _this.w, _this.h);
_this.cacheCtx.clearRect(0, 0, _this.w, _this.h);
// 绘制图形到离屏canvas
for (var i = 0, len = _this.iterator.length; i < len; i++) {
if (_this.t < _this.iterator[i].dur) {
_this.draw[_this.iterator[i].name](_this.iterator[i], _this.cacheCtx);
}
}
// 绘制到canvas
_this.ctx.drawImage(_this.cacheCanvas, 0, 0);
// 更新时间参数
_this.t = _this.now() - start;
}
}
Canimation.prototype.processParam = {
circle: function (param) {
param.fill = param.fill || '#000';
return param
},
arc: function (param) {
param.fill = param.fill || 'transparent';
param.stroke = param.stroke || '#000';
param.width = param.width || 2;
return param
},
text: function (param) {
param.font = param.font || '14px Arial';
param.fill = param.fill || '#000';
return param
}
}
Canimation.prototype.draw = draw;
Canimation.prototype.easing = easing;
Canimation.prototype.util = util;