-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathconfigUpdate.js
134 lines (113 loc) · 3.04 KB
/
configUpdate.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
import { getIntersectionKeys, mapObject } from './util';
export const alpha = (begin, end, k) => begin + (end - begin) * k;
const needContinue = ({ from, to }) => from !== to;
/*
* @description: cal new from value and velocity in each stepper
* @return: { [styleProperty]: { from, to, velocity } }
*/
const calStepperVals = (easing, preVals, steps) => {
const nextStepVals = mapObject((key, val) => {
if (needContinue(val)) {
const [newX, newV] = easing(val.from, val.to, val.velocity);
return {
...val,
from: newX,
velocity: newV,
};
}
return val;
}, preVals);
if (steps < 1) {
return mapObject((key, val) => {
if (needContinue(val)) {
return {
...val,
velocity: alpha(val.velocity, nextStepVals[key].velocity, steps),
from: alpha(val.from, nextStepVals[key].from, steps),
};
}
return val;
}, preVals);
}
return calStepperVals(easing, nextStepVals, steps - 1);
};
// configure update function
export default (from, to, easing, duration, render) => {
const interKeys = getIntersectionKeys(from, to);
const timingStyle = interKeys.reduce(
(res, key) => ({
...res,
[key]: [from[key], to[key]],
}),
{},
);
let stepperStyle = interKeys.reduce(
(res, key) => ({
...res,
[key]: {
from: from[key],
velocity: 0,
to: to[key],
},
}),
{},
);
let cafId = -1;
let preTime;
let beginTime;
let update = () => null;
const getCurrStyle = () => mapObject((key, val) => val.from, stepperStyle);
const shouldStopAnimation = () => !Object.values(stepperStyle).filter(needContinue).length;
// stepper timing function like spring
const stepperUpdate = now => {
if (!preTime) {
preTime = now;
}
const deltaTime = now - preTime;
const steps = deltaTime / easing.dt;
stepperStyle = calStepperVals(easing, stepperStyle, steps);
// get union set and add compatible prefix
render({
...from,
...to,
...getCurrStyle(stepperStyle),
});
preTime = now;
if (!shouldStopAnimation()) {
cafId = requestAnimationFrame(update);
}
};
// t => val timing function like cubic-bezier
const timingUpdate = now => {
if (!beginTime) {
beginTime = now;
}
const t = (now - beginTime) / duration;
const currStyle = mapObject((key, val) => alpha(...val, easing(t)), timingStyle);
// get union set and add compatible prefix
render({
...from,
...to,
...currStyle,
});
if (t < 1) {
cafId = requestAnimationFrame(update);
} else {
const finalStyle = mapObject((key, val) => alpha(...val, easing(1)), timingStyle);
render({
...from,
...to,
...finalStyle,
});
}
};
update = easing.isStepper ? stepperUpdate : timingUpdate;
// return start animation method
return () => {
requestAnimationFrame(update);
// return stop animation method
return () => {
cancelAnimationFrame(cafId);
};
};
};