Skip to content

Commit 2743c6f

Browse files
authored
Merge pull request #14 from fram-x/Feature-12/EnhancedTimingFunction
Feature 12/enhanced timing function
2 parents e62c4a1 + 50e0d1b commit 2743c6f

File tree

3 files changed

+39
-66
lines changed

3 files changed

+39
-66
lines changed

src/packages/animated/src/Types/IAnimationProvider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ export interface IAnimationProvider {
2828
/**
2929
* @description Creates a timing based animation
3030
*/
31-
runTiming: (
32-
master: IAnimationValue,
33-
duration: number,
34-
callback?: () => void,
35-
) => void;
31+
runTiming: (master: IAnimationValue, duration: number) => void;
3632

3733
/**
3834
* @description Creates an animated value
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { Animated, Easing } from "react-native";
22
import { IAnimationValue } from "../../Types";
33

4-
export const runTiming = (
5-
master: IAnimationValue,
6-
duration: number,
7-
callback?: () => void
8-
): void => {
4+
export const runTiming = (master: IAnimationValue, duration: number): void => {
95
Animated.timing(master as Animated.Value, {
106
toValue: duration,
117
duration,
128
easing: Easing.linear,
13-
isInteraction: false
9+
isInteraction: false,
1410
}).start(() => {
1511
(master as Animated.Value).removeAllListeners();
16-
callback && callback();
1712
});
1813
};

src/packages/animated/src/react-native-reanimated/Implementation/runTiming.ts

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,53 @@ import Animated, { Easing } from "react-native-reanimated";
22
import { IAnimationValue } from "../../Types";
33
// @ts-ignore
44
import { always } from "react-native-reanimated/src/base";
5+
56
const {
67
Clock,
78
Value,
8-
block,
9-
timing,
10-
call,
9+
sub,
1110
cond,
11+
greaterOrEq,
12+
call,
1213
stopClock,
1314
set,
1415
startClock,
1516
clockRunning,
16-
onChange
1717
} = Animated;
1818

19-
export const runTiming = (
20-
master: IAnimationValue,
21-
duration: number,
22-
callback?: () => void
23-
) => {
19+
export const runTiming = (master: IAnimationValue, duration: number) => {
2420
const clock = new Clock();
25-
let animation: Animated.Node<number>;
26-
animation = always(
27-
internalRunTiming(
28-
clock,
29-
master as Animated.Value<number>,
30-
{
31-
toValue: duration,
32-
duration,
33-
easing: Easing.linear
34-
},
35-
() => {
36-
// @ts-ignore
37-
animation.__detach();
38-
callback && callback();
39-
}
40-
)
21+
let timing: Animated.Node<number>;
22+
23+
const startTime = new Value(0);
24+
const frameTime = sub(clock, startTime);
25+
const value = master as Animated.Value<number>;
26+
27+
timing = always(
28+
cond(
29+
clockRunning(clock),
30+
[
31+
cond(greaterOrEq(frameTime, duration), [
32+
// Stop animation (duration has been reached)
33+
stopClock(clock),
34+
set(value, duration),
35+
call([], () =>
36+
// @ts-ignore
37+
timing.__detach(),
38+
),
39+
]),
40+
// Update animation value
41+
set(value, frameTime),
42+
],
43+
[
44+
// Start (clock is not running)
45+
set(startTime, clock),
46+
set(value, frameTime),
47+
startClock(clock),
48+
],
49+
),
4150
);
51+
4252
// @ts-ignore
43-
animation.__attach();
53+
timing.__attach();
4454
};
45-
46-
function internalRunTiming(
47-
clock: Animated.Clock,
48-
value: Animated.Value<number>,
49-
config: Animated.TimingConfig,
50-
callback: () => void
51-
) {
52-
const state = {
53-
finished: new Value(0),
54-
position: new Value(0),
55-
time: new Value(0),
56-
frameTime: new Value(0)
57-
};
58-
59-
return block([
60-
onChange(config.toValue, set(state.frameTime, 0)),
61-
cond(clockRunning(clock), 0, [
62-
set(state.finished, 0),
63-
set(state.time, 0),
64-
set(value, state.position),
65-
set(state.frameTime, 0),
66-
startClock(clock)
67-
]),
68-
timing(clock, state, config),
69-
set(value, state.position),
70-
cond(state.finished, [stopClock(clock), call([], callback)])
71-
]);
72-
}

0 commit comments

Comments
 (0)