UNPKG

addimated

Version:

An always interruptable, declarative animation library for React

167 lines (145 loc) 4.29 kB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread"; import { AnimatedInterpolation } from "./AnimatedInterpolation"; import { AnimatedValue } from "./AnimatedValue"; import { AnimatedValueXY } from "./AnimatedValueXY"; import { createAnimatedComponent } from "./createAnimatedComponent"; import * as Easing from "./Easing"; import { Interpolation } from "./Interpolation"; import { Manager } from "./Manager"; import { SpringAnimation } from "./SpringAnimation"; import { TimingAnimation } from "./TimingAnimation"; var globalManager; function maybeVectorAnim(value, config, anim) { if (value instanceof AnimatedValueXY) { var configX = _objectSpread({}, config); var configY = _objectSpread({}, config); for (var key in config) { var _config$key = config[key], x = _config$key.x, y = _config$key.y; if (x !== undefined && y !== undefined) { configX[key] = x; configY[key] = y; } } var aX = anim(value.x, configX); var aY = anim(value.y, configY); return parallel([aX, aY]); } else { return null; } } function spring(value, config) { var vectorAnim = maybeVectorAnim(value, config, spring); if (vectorAnim) return vectorAnim; var singleValue = value; var singleConfig = config; return { start: function start(callback) { singleValue.animate(new SpringAnimation(singleConfig), callback); } }; } function timing(value, config) { var vectorAnim = maybeVectorAnim(value, config, timing); if (vectorAnim) return vectorAnim; var singleValue = value; var singleConfig = config; return { start: function start(callback) { singleValue.animate(new TimingAnimation(singleConfig), callback); } }; } function sequence(animations) { var current = 0; return { start: function start(callback) { var onComplete = function onComplete(result) { if (!result.finished) { callback && callback(result); return; } current++; if (current === animations.length) { callback && callback(result); return; } animations[current].start(onComplete); }; if (animations.length === 0) { callback && callback({ finished: true }); } else { animations[current].start(onComplete); } } }; } function parallel(animations) { var doneCount = 0; var result = { start: function start(callback) { if (doneCount === animations.length) { callback && callback({ finished: true }); return; } animations.forEach(function (anim) { var cb = function cb(endResult) { doneCount++; if (doneCount === animations.length) { doneCount = 0; callback && callback(endResult); return; } }; if (!anim) { cb({ finished: true }); } else { anim.start(cb); } }); } }; return result; } function delay(time) { return timing(createAnimatedValue(0), { toValue: 0, duration: time }); } function stagger(time, animations) { return parallel(animations.map(function (animation, i) { return sequence([delay(time * i), animation]); })); } function createAnimatedValue(value) { globalManager = globalManager || new Manager(); return new AnimatedValue(value, globalManager); } function createAnimatedValueXY(valueIn) { globalManager = globalManager || new Manager(); return new AnimatedValueXY(valueIn, globalManager); } function interpolate(value, config) { return new AnimatedInterpolation(value, Interpolation.create(config)); } var a = createAnimatedComponent("a"); var button = createAnimatedComponent("button"); var div = createAnimatedComponent("div"); var span = createAnimatedComponent("span"); var img = createAnimatedComponent("img"); export { // Animated Values createAnimatedValue, createAnimatedValueXY, // Value Mutators interpolate, // Animations spring, timing, // Animation Monitors delay, sequence, parallel, stagger, // Components createAnimatedComponent, a, button, div, span, img, // Easing Functions Easing }; // types //# sourceMappingURL=index.js.map