* Define the animation. * * @param definition Definition (i.e. `AnimationRule` array) * @returns The animation
(definition: AnimationDefinition)
| 94 | * @returns The animation |
| 95 | */ |
| 96 | define(definition: AnimationDefinition) { |
| 97 | this.reset(); |
| 98 | this.extractObjectsAndValidateRules(definition); |
| 99 | |
| 100 | // Parse the rules. |
| 101 | // IMPORTANT: Parsed rules use milliseconds instead of seconds. |
| 102 | this.rules = definition |
| 103 | .sort((a, b) => getStartTime(a) - getStartTime(b)) |
| 104 | .map((rule) => { |
| 105 | if (rule.timeframe) { |
| 106 | const from: ParsedStyles = {}; |
| 107 | const to: ParsedStyles = {}; |
| 108 | |
| 109 | for (const [prop, val] of Object.entries(rule.from)) { |
| 110 | from[prop] = cssValueParser(val); |
| 111 | } |
| 112 | for (const [prop, val] of Object.entries(rule.to)) { |
| 113 | to[prop] = cssValueParser(val); |
| 114 | } |
| 115 | // Convert to milliseconds. |
| 116 | const msTimeframe = rule.timeframe.map((t) => t * MS) as [number, number]; |
| 117 | |
| 118 | return {...rule, from, to, timeframe: msTimeframe}; |
| 119 | } else { |
| 120 | const styles: ParsedStyles = {}; |
| 121 | |
| 122 | for (const [prop, val] of Object.entries(rule.styles)) { |
| 123 | styles[prop] = cssValueParser(val); |
| 124 | } |
| 125 | // Convert to milliseconds. |
| 126 | const msAt = rule.at * MS; |
| 127 | |
| 128 | return {...rule, styles, at: msAt}; |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | // Calculate the duration of the animation. |
| 133 | // IMPORTANT: Use parsed rules with milliseconds. |
| 134 | this._duration = Math.max(...this.rules.map((r) => getEndTime(r))); |
| 135 | |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** Play the animation. */ |
| 140 | play() { |