UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

46 lines (45 loc) 1.78 kB
import { clamp } from "../utilities.mjs"; /* @license * Copyright 2019 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const easeInOutQuad = (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; const interpolate = (start, end, ease = easeInOutQuad) => (time) => start + (end - start) * ease(time); const sequence = (tracks, weights) => { const cumulativeSum = (sum) => (value) => sum += value; const times = weights.map(cumulativeSum(0)); return (time) => { time = clamp(time, 0, 1); time *= times[times.length - 1]; const i = times.findIndex((val) => val >= time); const start = i < 1 ? 0 : times[i - 1]; const end = times[i]; return tracks[i]((time - start) / (end - start)); }; }; const timeline = (path) => { const tracks = []; const weights = []; let lastValue = path.initialValue; for (let i = 0; i < path.keyframes.length; ++i) { const keyframe = path.keyframes[i]; const { value, frames } = keyframe; const ease = keyframe.ease || easeInOutQuad; const track = interpolate(lastValue, value, ease); tracks.push(track); weights.push(frames); lastValue = value; } return sequence(tracks, weights); }; export { easeInOutQuad, interpolate, sequence, timeline };