UNPKG

5.31 kBJavaScriptView Raw
1import { calculateActiveDuration, calculateIterationProgress } from '../utils/animation';
2import { AnimationEffectTiming } from './AnimationEffectTiming';
3import { normalizeKeyframes } from './KeyframeList';
4import { convertEffectInput } from '../utils/interpolation';
5var fills = 'backwards|forwards|both|none'.split('|');
6var directions = 'reverse|alternate|alternate-reverse'.split('|');
7export 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 } // @ts-ignore
33
34
35 timing[property] = timingInput[property];
36 }
37 });
38 }
39
40 return timing;
41}
42export function normalizeTimingInput(timingInput, forGroup) {
43 timingInput = numericTimingToObject(timingInput !== null && timingInput !== void 0 ? timingInput : {
44 duration: 'auto'
45 });
46 return makeTiming(timingInput, forGroup);
47}
48export 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 * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect
65 * @example
66 const circleDownKeyframes = new KeyframeEffect(
67 circle, // element to animate
68 [
69 { transform: 'translateY(0)' }, // keyframe
70 { transform: 'translateY(100)' } // keyframe
71 ],
72 { duration: 3000, fill: 'forwards' } // keyframe options
73 );
74 *
75 */
76
77var KeyframeEffect =
78/** @class */
79function () {
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 * @see https://developer.mozilla.org/en-US/docs/Web/API/AnimationEffect/getComputedTiming
144 */
145
146
147 KeyframeEffect.prototype.getComputedTiming = function () {
148 return this.computedTiming;
149 };
150 /**
151 * @see https://developer.mozilla.org/en-US/docs/Web/API/AnimationEffect/getTiming
152 */
153
154
155 KeyframeEffect.prototype.getTiming = function () {
156 return this.timing;
157 };
158 /**
159 * @see https://developer.mozilla.org/en-US/docs/Web/API/AnimationEffect/updateTiming
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
174export { KeyframeEffect };
\No newline at end of file