Skip to content

Commit 677d3e7

Browse files
committed
feat: add <Tween> component
1 parent ea1f812 commit 677d3e7

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/Tween/easing.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export type TEasing = (time: number) => number;
2+
3+
export interface IEasingMap {
4+
linear: TEasing;
5+
inQuad: TEasing;
6+
outQuad: TEasing;
7+
inOutQuad: TEasing;
8+
inCubic: TEasing;
9+
outCubic: TEasing;
10+
inOutCubic: TEasing;
11+
inQuart: TEasing;
12+
outQuart: TEasing;
13+
inOutQuart: TEasing;
14+
inQuint: TEasing;
15+
outQuint: TEasing;
16+
inOutQuint: TEasing;
17+
}
18+
19+
// From: https://gist.github.com/gre/1650294
20+
export const easing: IEasingMap = {
21+
// no easing, no acceleration
22+
linear: (t) => t,
23+
24+
// accelerating from zero velocity
25+
inQuad: (t) => t*t,
26+
27+
// decelerating to zero velocity
28+
outQuad: (t) => t*(2-t),
29+
30+
// acceleration until halfway, then deceleration
31+
inOutQuad: (t) => t<.5 ? 2*t*t : -1+(4-2*t)*t,
32+
33+
// accelerating from zero velocity
34+
inCubic: (t) => t*t*t,
35+
36+
// decelerating to zero velocity
37+
outCubic: (t) => (--t)*t*t+1,
38+
39+
// acceleration until halfway, then deceleration
40+
inOutCubic: (t) => t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1,
41+
42+
// accelerating from zero velocity
43+
inQuart: (t) => t*t*t*t,
44+
45+
// decelerating to zero velocity
46+
outQuart: (t) => 1-(--t)*t*t*t,
47+
48+
// acceleration until halfway, then deceleration
49+
inOutQuart: (t) => t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t,
50+
51+
// accelerating from zero velocity
52+
inQuint: (t) => t*t*t*t*t,
53+
54+
// decelerating to zero velocity
55+
outQuint: (t) => 1+(--t)*t*t*t*t,
56+
57+
// acceleration until halfway, then deceleration
58+
inOutQuint: (t) => t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t
59+
};

src/Tween/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {render, createEnhancer} from 'react-universal-interface';
2+
import {Render, IRenderProps} from '../Render';
3+
import {h} from '../util';
4+
import {easing, TEasing, IEasingMap} from './easing';
5+
6+
export interface ITweenProps extends IRenderProps {
7+
easing: (keyof IEasingMap) | [number, number, number, number] | ((time: number) => number);
8+
Render: React.SFC<IRenderProps> | React.ComponentClass<IRenderProps>;
9+
}
10+
11+
export const Tween: React.SFC<ITweenProps> = (props) => {
12+
let {easing: fn} = props;
13+
14+
switch (typeof props.easing) {
15+
case 'string':
16+
fn = easing[fn as string];
17+
break;
18+
case 'object':
19+
// TODO: Implement cubic-bezier
20+
}
21+
22+
if (process.env.NODE_ENV !== 'production') {
23+
if (typeof fn !== 'function') {
24+
console.error(
25+
'<Tween> expected "easing" property to be a valid easing function or a 4-tuple array or numbers ' +
26+
'specifying a Cubic Bezier curve, or a string specifying one of the built-int easing functions: ' +
27+
'"' + Object.keys(easing).join('", "') + '".'
28+
);
29+
console.trace();
30+
31+
return null;
32+
}
33+
}
34+
35+
return h(props.Render, props,
36+
({value}) => render(props, {value: (fn as TEasing)(value)})
37+
);
38+
};
39+
40+
Tween.defaultProps = {
41+
easing: 'linear',
42+
Render
43+
};
44+
45+
export const withTween = createEnhancer(Tween, 'tween');

0 commit comments

Comments
 (0)