blob: f4ecb34743d6b7b96af09a1c368d3d0cb5775bdc (
plain)
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
|
export type OnFrameFn = (frame: number) => void;
export type OnFinishFn = () => void;
export interface IKeyframeAnimationOptions {
start?: number;
end: number;
}
export type KeyframeAnimationRange = [number, number];
export default class KeyframeAnimation {
private speedValue = 200; // ms
private onFrameValue?: OnFrameFn;
private onFinishValue?: OnFinishFn;
private currentFrameValue = 0;
private targetFrame = 0;
private isRunningValue = false;
private isFinishedValue = false;
private timeout?: NodeJS.Timeout;
get currentFrame(): number {
return this.currentFrameValue;
}
// This setter is only meant to be used when running tests
// @internal
set currentFrame(newValue: number) {
if (process.env.NODE_ENV === 'test') {
this.currentFrameValue = newValue;
} else {
throw new Error('The setter for currentFrame is only available in test environment.');
}
}
set onFrame(newValue: OnFrameFn | undefined) {
this.onFrameValue = newValue;
}
get onFrame(): OnFrameFn | undefined {
return this.onFrameValue;
}
// called when animation finished
set onFinish(newValue: OnFinishFn | undefined) {
this.onFinishValue = newValue;
}
get onFinish(): OnFinishFn | undefined {
return this.onFinishValue;
}
// pace per frame in ms
set speed(newValue: number) {
this.speedValue = newValue;
}
get speed(): number {
return this.speedValue;
}
get isRunning(): boolean {
return this.isRunningValue;
}
get isFinished(): boolean {
return this.isFinishedValue;
}
public play(options: IKeyframeAnimationOptions) {
const { start, end } = options;
if (start !== undefined) {
this.currentFrameValue = start;
}
this.targetFrame = end;
this.isRunningValue = true;
this.isFinishedValue = false;
this.unscheduleUpdate();
this.render();
this.scheduleUpdate();
}
public stop() {
this.isRunningValue = false;
this.unscheduleUpdate();
}
private unscheduleUpdate() {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
}
private scheduleUpdate() {
this.timeout = global.setTimeout(() => this.onUpdateFrame(), this.speedValue);
}
private render() {
if (this.onFrameValue) {
this.onFrameValue(this.currentFrameValue);
}
}
private didFinish() {
this.isFinishedValue = true;
this.isRunningValue = false;
if (this.onFinishValue) {
this.onFinishValue();
}
}
private onUpdateFrame() {
this.advanceFrame();
if (!this.isFinishedValue) {
this.render();
// check once again since onFrame() may stop animation
if (this.isRunningValue) {
this.scheduleUpdate();
}
}
}
private advanceFrame() {
if (this.isFinishedValue) {
return;
}
if (this.currentFrameValue === this.targetFrame) {
this.didFinish();
} else if (this.currentFrameValue < this.targetFrame) {
this.currentFrameValue += 1;
} else {
this.currentFrameValue -= 1;
}
}
}
|