1 | import { calculateActiveDuration, calculateIterationProgress } from '../utils/animation';
|
2 | import { AnimationEffectTiming } from './AnimationEffectTiming';
|
3 | import { normalizeKeyframes } from './KeyframeList';
|
4 | import { convertEffectInput } from '../utils/interpolation';
|
5 | var fills = 'backwards|forwards|both|none'.split('|');
|
6 | var directions = 'reverse|alternate|alternate-reverse'.split('|');
|
7 | export function makeTiming(timingInput, forGroup) {
|
8 | var timing = new AnimationEffectTiming();
|
9 |
|
10 | if (forGroup) {
|
11 | timing.fill = 'both';
|
12 | timing.duration = 'auto';
|
13 | }
|
14 |
|
15 | if (typeof timingInput === 'number' && !isNaN(timingInput)) {
|
16 | timing.duration = timingInput;
|
17 | } else if (timingInput !== undefined) {
|
18 | Object.keys(timingInput).forEach(function (property) {
|
19 | if (timingInput[property] !== undefined && timingInput[property] !== null && timingInput[property] !== 'auto') {
|
20 | if (typeof timing[property] === 'number' || property === 'duration') {
|
21 | if (typeof timingInput[property] !== 'number' || isNaN(timingInput[property])) {
|
22 | return;
|
23 | }
|
24 | }
|
25 |
|
26 | if (property === 'fill' && fills.indexOf(timingInput[property]) === -1) {
|
27 | return;
|
28 | }
|
29 |
|
30 | if (property === 'direction' && directions.indexOf(timingInput[property]) === -1) {
|
31 | return;
|
32 | }
|
33 |
|
34 |
|
35 | timing[property] = timingInput[property];
|
36 | }
|
37 | });
|
38 | }
|
39 |
|
40 | return timing;
|
41 | }
|
42 | export function normalizeTimingInput(timingInput, forGroup) {
|
43 | timingInput = numericTimingToObject(timingInput !== null && timingInput !== void 0 ? timingInput : {
|
44 | duration: 'auto'
|
45 | });
|
46 | return makeTiming(timingInput, forGroup);
|
47 | }
|
48 | export function numericTimingToObject(timingInput) {
|
49 | if (typeof timingInput === 'number') {
|
50 | if (isNaN(timingInput)) {
|
51 | timingInput = {
|
52 | duration: 'auto'
|
53 | };
|
54 | } else {
|
55 | timingInput = {
|
56 | duration: timingInput
|
57 | };
|
58 | }
|
59 | }
|
60 |
|
61 | return timingInput;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | var KeyframeEffect =
|
78 |
|
79 | function () {
|
80 | function KeyframeEffect(target, effectInput, timingInput) {
|
81 | var _this = this;
|
82 |
|
83 | this.composite = 'replace';
|
84 | this.iterationComposite = 'replace';
|
85 | this.target = target;
|
86 | this.timing = normalizeTimingInput(timingInput, false);
|
87 | this.timing.effect = this;
|
88 | this.timing.activeDuration = calculateActiveDuration(this.timing);
|
89 | this.timing.endTime = Math.max(0, this.timing.delay + this.timing.activeDuration + this.timing.endDelay);
|
90 | this.normalizedKeyframes = normalizeKeyframes(effectInput, this.timing);
|
91 | this.interpolations = convertEffectInput(this.normalizedKeyframes, this.timing, this.target);
|
92 | this.computedTiming = new Proxy(this.timing, {
|
93 | get: function get(target, prop) {
|
94 | if (prop === 'duration') {
|
95 | return target.duration === 'auto' ? 0 : target.duration;
|
96 | } else if (prop === 'fill') {
|
97 | return target.fill === 'auto' ? 'none' : target.fill;
|
98 | } else if (prop === 'localTime') {
|
99 | return _this.animation && _this.animation.currentTime || null;
|
100 | } else if (prop === 'currentIteration') {
|
101 | if (!_this.animation || _this.animation.playState !== 'running') {
|
102 | return null;
|
103 | }
|
104 |
|
105 | return target.currentIteration || 0;
|
106 | } else if (prop === 'progress') {
|
107 | if (!_this.animation || _this.animation.playState !== 'running') {
|
108 | return null;
|
109 | }
|
110 |
|
111 | return target.progress || 0;
|
112 | }
|
113 |
|
114 | return target[prop];
|
115 | },
|
116 | set: function set() {
|
117 | return true;
|
118 | }
|
119 | });
|
120 | }
|
121 |
|
122 | KeyframeEffect.prototype.applyInterpolations = function () {
|
123 | this.interpolations(this.target, Number(this.timeFraction));
|
124 | };
|
125 |
|
126 | KeyframeEffect.prototype.update = function (localTime) {
|
127 | if (localTime === null) {
|
128 | return false;
|
129 | }
|
130 |
|
131 | this.timeFraction = calculateIterationProgress(this.timing.activeDuration, localTime, this.timing);
|
132 | return this.timeFraction !== null;
|
133 | };
|
134 |
|
135 | KeyframeEffect.prototype.getKeyframes = function () {
|
136 | return this.normalizedKeyframes;
|
137 | };
|
138 |
|
139 | KeyframeEffect.prototype.setKeyframes = function (keyframes) {
|
140 | this.normalizedKeyframes = normalizeKeyframes(keyframes);
|
141 | };
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | KeyframeEffect.prototype.getComputedTiming = function () {
|
148 | return this.computedTiming;
|
149 | };
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | KeyframeEffect.prototype.getTiming = function () {
|
156 | return this.timing;
|
157 | };
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 | KeyframeEffect.prototype.updateTiming = function (timing) {
|
164 | var _this = this;
|
165 |
|
166 | Object.keys(timing || {}).forEach(function (name) {
|
167 | _this.timing = timing[name];
|
168 | });
|
169 | };
|
170 |
|
171 | return KeyframeEffect;
|
172 | }();
|
173 |
|
174 | export { KeyframeEffect }; |
\ | No newline at end of file |