Skip to content

Commit c3883a1

Browse files
committed
Testing old anims
1 parent c843009 commit c3883a1

File tree

9 files changed

+183
-250
lines changed

9 files changed

+183
-250
lines changed

Example/Reanimated/Animated.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ import createAnimatedComponent from './createAnimatedComponent';
88
import decay from './animations/decay';
99
import timing from './animations/timing';
1010
import spring from './animations/spring';
11+
import TimingAnimation from './animations/TimingAnimation';
12+
import SpringAnimation from './animations/SpringAnimation';
13+
import DecayAnimation from './animations/DecayAnimation';
14+
15+
function backwardsCompatibleAnim(node, AnimationClass) {
16+
return (clock, state, config) => {
17+
if (config !== undefined) {
18+
return node(clock, state, config);
19+
}
20+
// reassign to match spec of old Animated lib where first arg was value
21+
// and second arg was animation config
22+
const value = clock;
23+
config = state;
24+
return {
25+
start: () => {
26+
value.animate(new AnimationClass(config));
27+
},
28+
};
29+
};
30+
}
1131

1232
const Animated = {
1333
// components
@@ -26,9 +46,9 @@ const Animated = {
2646
...derived,
2747

2848
// animations
29-
decay,
30-
timing,
31-
spring,
49+
decay: backwardsCompatibleAnim(decay, DecayAnimation),
50+
timing: backwardsCompatibleAnim(timing, TimingAnimation),
51+
spring: backwardsCompatibleAnim(spring, SpringAnimation),
3252
};
3353

3454
export default Animated;

Example/Reanimated/CoreAnimated.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

Example/Reanimated/animations/DecayAnimation.js

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,40 @@
11
import Animation from './Animation';
22

3-
import { shouldUseNativeDriver } from '../NativeAnimatedHelper';
3+
import decay from './decay';
4+
import { block, clockRunning, startClock, stopClock, cond } from '../base';
5+
import Clock from '../core/AnimatedClock';
6+
import AnimatedValue from '../core/AnimatedValue';
47

58
class DecayAnimation extends Animation {
69
constructor(config) {
710
super();
811
this._deceleration =
912
config.deceleration !== undefined ? config.deceleration : 0.998;
1013
this._velocity = config.velocity;
11-
this._useNativeDriver = shouldUseNativeDriver(config);
12-
this.__isInteraction =
13-
config.isInteraction !== undefined ? config.isInteraction : true;
14-
this.__iterations = config.iterations !== undefined ? config.iterations : 1;
1514
}
1615

17-
__getNativeAnimationConfig() {
18-
return {
19-
type: 'decay',
20-
deceleration: this._deceleration,
21-
velocity: this._velocity,
22-
iterations: this.__iterations,
16+
start(value) {
17+
this._clock = new Clock();
18+
const state = {
19+
finished: new AnimatedValue(0),
20+
velocity: new AnimatedValue(this._velocity),
21+
position: value,
22+
time: new AnimatedValue(0),
2323
};
24-
}
25-
26-
start(fromValue, onUpdate, onEnd, previousAnimation, animatedValue) {
27-
this.__active = true;
28-
this._lastValue = fromValue;
29-
this._fromValue = fromValue;
30-
this._onUpdate = onUpdate;
31-
this.__onEnd = onEnd;
32-
this._startTime = Date.now();
33-
if (this._useNativeDriver) {
34-
this.__startNativeAnimation(animatedValue);
35-
} else {
36-
this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this));
37-
}
38-
}
39-
40-
onUpdate() {
41-
const now = Date.now();
4224

43-
const value =
44-
this._fromValue +
45-
this._velocity /
46-
(1 - this._deceleration) *
47-
(1 - Math.exp(-(1 - this._deceleration) * (now - this._startTime)));
48-
49-
this._onUpdate(value);
50-
51-
if (Math.abs(this._lastValue - value) < 0.1) {
52-
this.__debouncedOnEnd({ finished: true });
53-
return;
54-
}
25+
const config = {
26+
deceleration: this._deceleration,
27+
};
5528

56-
this._lastValue = value;
57-
if (this.__active) {
58-
this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this));
59-
}
29+
return block([
30+
cond(clockRunning(this._clock), 0, [startClock(this._clock)]),
31+
decay(this._clock, state, config),
32+
cond(state.finished, stopClock(this._clock)),
33+
]);
6034
}
6135

6236
stop() {
63-
super.stop();
64-
this.__active = false;
65-
global.cancelAnimationFrame(this._animationFrame);
66-
this.__debouncedOnEnd({ finished: false });
37+
// not implemented yet
6738
}
6839
}
6940

Example/Reanimated/animations/SpringAnimation.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import AnimatedValue from '../core/AnimatedValue';
22
import Animation from './Animation';
33
import SpringConfig from '../SpringConfig';
4-
import SpringNode from '../nodes/SpringNode';
5-
import AnimatedOnChange from '../core/AnimatedOnChange';
6-
import AnimatedDetach from '../core/AnimatedDetach';
7-
import AnimatedOp from '../nodes/AnimatedOp';
4+
import spring from './spring';
85

9-
import { clock } from '../core/AnimatedClock';
6+
import { block, clockRunning, startClock, stopClock, cond } from '../base';
7+
import Clock from '../core/AnimatedClock';
108

119
import invariant from 'fbjs/lib/invariant';
12-
import { shouldUseNativeDriver } from '../NativeAnimatedHelper';
1310

1411
function withDefault(value, defaultValue) {
1512
if (value === undefined || value === null) {
@@ -32,10 +29,6 @@ export default class SpringAnimation extends Animation {
3229
this._lastVelocity = withDefault(config.velocity, 0);
3330
this._toValue = config.toValue;
3431
this._delay = withDefault(config.delay, 0);
35-
this._useNativeDriver = shouldUseNativeDriver(config);
36-
this.__isInteraction =
37-
config.isInteraction !== undefined ? config.isInteraction : true;
38-
this.__iterations = config.iterations !== undefined ? config.iterations : 1;
3932

4033
if (
4134
config.stiffness !== undefined ||
@@ -88,12 +81,12 @@ export default class SpringAnimation extends Animation {
8881
}
8982

9083
start(value) {
91-
this._finished = new AnimatedValue(0);
84+
this._clock = new Clock();
9285
const state = {
93-
finished: this._finished,
94-
velocity: this._initialVelocity,
86+
finished: new AnimatedValue(0),
87+
velocity: new AnimatedValue(this._initialVelocity),
9588
position: value,
96-
time: 0,
89+
time: new AnimatedValue(0),
9790
};
9891

9992
const config = {
@@ -106,20 +99,14 @@ export default class SpringAnimation extends Animation {
10699
restDisplacementThreshold: this._restDisplacementThreshold,
107100
};
108101

109-
const step = new SpringNode(clock, state, config);
110-
const detach = new AnimatedDetach(step);
111-
const clb = finished => {
112-
console.log('FINISHED', finished);
113-
};
114-
const call = new AnimatedOp([this._finished], ([finished]) =>
115-
clb(finished)
116-
);
117-
const block = new AnimatedOp([detach, call], () => {});
118-
new AnimatedOnChange(this._finished, block).__attach();
119-
step.__attach();
102+
return block([
103+
cond(clockRunning(this._clock), 0, [startClock(this._clock)]),
104+
spring(this._clock, state, config),
105+
cond(state.finished, stopClock(this._clock)),
106+
]);
120107
}
121108

122109
stop() {
123-
this._finished && this._finished.setValue(1);
110+
// this._finished && this._finished.setValue(1);
124111
}
125112
}
Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,35 @@
11
import AnimatedValue from '../core/AnimatedValue';
2-
import TimingNode from '../nodes/TimingNode';
3-
import Animation from './Animation';
4-
5-
import { clock } from '../core/AnimatedClock';
6-
import AnimatedOnChange from '../core/AnimatedOnChange';
7-
import AnimatedDetach from '../core/AnimatedDetach';
2+
import timing from './timing';
3+
import { block, clockRunning, startClock, stopClock, cond } from '../base';
4+
import Clock from '../core/AnimatedClock';
5+
import Easing from '../Easing';
86

9-
import { shouldUseNativeDriver } from '../NativeAnimatedHelper';
7+
import Animation from './Animation';
108

11-
let _easeInOut;
12-
function easeInOut() {
13-
if (!_easeInOut) {
14-
const Easing = require('Easing');
15-
_easeInOut = Easing.inOut(Easing.ease);
16-
}
17-
return _easeInOut;
18-
}
9+
const easeInOut = Easing.inOut(Easing.ease);
1910

2011
export default class TimingAnimation extends Animation {
21-
_startTime;
22-
_fromValue;
2312
_toValue;
2413
_duration;
25-
_delay;
2614
_easing;
27-
_onUpdate;
28-
_animationFrame;
29-
_timeout;
30-
_useNativeDriver;
3115

32-
_finished;
16+
_clock;
3317

3418
constructor(config) {
3519
super();
36-
this._config = { ...config };
3720
this._toValue = config.toValue;
38-
this._easing = config.easing !== undefined ? config.easing : easeInOut();
21+
this._easing = config.easing !== undefined ? config.easing : easeInOut;
3922
this._duration = config.duration !== undefined ? config.duration : 500;
40-
this._delay = config.delay !== undefined ? config.delay : 0;
41-
this.__iterations = config.iterations !== undefined ? config.iterations : 1;
42-
this.__isInteraction =
43-
config.isInteraction !== undefined ? config.isInteraction : true;
44-
this._useNativeDriver = shouldUseNativeDriver(config);
4523
}
4624

4725
start(value) {
48-
this._finished = new AnimatedValue(0);
26+
this._clock = new Clock();
27+
4928
const state = {
50-
finished: this._finished,
29+
finished: new AnimatedValue(0),
5130
position: value,
52-
time: 0,
53-
frameTime: 0,
31+
time: new AnimatedValue(0),
32+
frameTime: new AnimatedValue(0),
5433
};
5534

5635
const config = {
@@ -59,12 +38,14 @@ export default class TimingAnimation extends Animation {
5938
easing: this._easing,
6039
};
6140

62-
const step = new TimingNode(clock, state, config);
63-
new AnimatedOnChange(this._finished, new AnimatedDetach(step)).__attach();
64-
step.__attach();
41+
return block([
42+
cond(clockRunning(this._clock), 0, [startClock(this._clock)]),
43+
timing(this._clock, state, config),
44+
cond(state.finished, stopClock(this._clock)),
45+
]);
6546
}
6647

6748
stop() {
68-
this._finished && this._finished.setValue(1);
49+
// this._finished && this._finished.setValue(1);
6950
}
7051
}

Example/Reanimated/core/AnimatedDetach.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)