diff --git a/packages/animate/src/api.ts b/packages/animate/src/api.ts index 537700a..cb0a0bc 100644 --- a/packages/animate/src/api.ts +++ b/packages/animate/src/api.ts @@ -1,9 +1,15 @@ import { EventEmitter, TypeEventInput, TypeListenerCallback } from "@okikio/emitter"; import { Manager } from "@okikio/manager"; + +import { TypeAnimationTarget, TypeAnimationOptionTypes, TypeCallbackArgs, TypeComputedAnimationOptions, IAnimationOptions, TypeComputedOptions, TypeKeyFrameOptionsType, TypeCSSLikeKeyframe, ICSSComputedTransformableProperties, TypeAnimationEvents, TypePlayStates } from "./types"; import { KeyframeParse, parseOffset } from "./builtin-effects"; +import { isValid, ParseTransformableCSSProperties, ParseTransformableCSSKeyframes } from "./css-properties"; + +export * from "./types"; +export * from "./builtin-effects"; +export * from "./css-properties"; /* DOM */ -export type TypeAnimationTarget = string | Node | NodeList | HTMLCollection | HTMLElement[] | TypeAnimationTarget[]; export const getElements = (selector: string | Node): Node[] => { return typeof selector === "string" ? Array.from(document.querySelectorAll(selector as string)) : [selector]; }; @@ -21,36 +27,31 @@ export const getTargets = (targets: TypeAnimationTarget): Node[] => { }; /* VALUES */ -// Types -export type TypeCallbackArgs = [number, number, HTMLElement]; -export type TypeGeneric = boolean | object | string | number; -export type TypeCSSLikeKeyframe = { [key: string]: Keyframe & ICSSComputedTransformableProperties }; -export type TypeKeyFrameOptionsType = TypeCSSLikeKeyframe | Keyframe[] | PropertyIndexedKeyframes; -export type TypeCSSPropertyValue = (string | number)[]; -export type TypeComputedAnimationOptions = TypeGeneric | TypeGeneric[] | TypeKeyFrameOptionsType | KeyframeEffectOptions | ICSSComputedTransformableProperties; -export type TypeCallback = (index?: number, total?: number, element?: HTMLElement) => TypeComputedAnimationOptions; -export type TypeAnimationOptionTypes = TypeCallback | TypeComputedAnimationOptions; -export type TypeComputedOptions = { [key: string]: TypeComputedAnimationOptions }; - -// Functions export const computeOption = (value: TypeAnimationOptionTypes, args: TypeCallbackArgs, context: Animate): TypeComputedAnimationOptions => { if (typeof value === "function") { return value.apply(context, args); } else return value; }; -export const mapAnimationOptions = (obj: IAnimationOptions, args: TypeCallbackArgs, options: Animate) => { - let key: string, value: TypeAnimationOptionTypes, result: TypeComputedOptions = {}; +/** + * Acts like array.map(...) but for functions + */ +export const mapObject = (obj: object, fn: (value, key, obj) => any) => { let keys = Object.keys(obj); + let key, value, result = {}; for (let i = 0, len = keys.length; i < len; i++) { key = keys[i]; value = obj[key]; - result[key] = computeOption(value, args, options); + result[key] = fn(value, key, obj); } return result; }; +export const mapAnimationOptions = (options: IAnimationOptions, args: TypeCallbackArgs, animate: Animate): TypeComputedOptions => { + return mapObject(options, (value) => computeOption(value, args, animate)); +}; + /** * From: [https://easings.net] * @@ -168,529 +169,6 @@ export const GetEase = (ease: keyof typeof EASINGS | string): string => { let search = ease.replace(/^ease-/, ""); // Remove the "ease-" keyword return EasingKeys.includes(search) ? EASINGS[search] : ease; }; - -export type TypeSingleValueCSSProperty = string | number | TypeCSSPropertyValue; - -/** - * CSS properties the `ParseTransformableCSSProperties` can parse - */ -export interface ICSSTransformableProperties { - perspective?: TypeSingleValueCSSProperty | TypeCallback, - rotate?: TypeSingleValueCSSProperty | Array | TypeCallback, - rotate3d?: TypeSingleValueCSSProperty | Array | TypeCallback, - rotateX?: TypeSingleValueCSSProperty | TypeCallback, - rotateY?: TypeSingleValueCSSProperty | TypeCallback, - rotateZ?: TypeSingleValueCSSProperty | TypeCallback, - translate?: TypeSingleValueCSSProperty | Array | TypeCallback, - translate3d?: TypeSingleValueCSSProperty | Array | TypeCallback, - translateX?: TypeSingleValueCSSProperty | TypeCallback, - translateY?: TypeSingleValueCSSProperty | TypeCallback, - translateZ?: TypeSingleValueCSSProperty | TypeCallback, - scale?: TypeSingleValueCSSProperty | Array | TypeCallback, - scale3d?: TypeSingleValueCSSProperty | Array | TypeCallback, - scaleX?: TypeSingleValueCSSProperty | TypeCallback, - scaleY?: TypeSingleValueCSSProperty | TypeCallback, - scaleZ?: TypeSingleValueCSSProperty | TypeCallback, - skew?: TypeSingleValueCSSProperty | Array | TypeCallback, - skewX?: TypeSingleValueCSSProperty | TypeCallback, - skewY?: TypeSingleValueCSSProperty | TypeCallback, - opacity?: TypeSingleValueCSSProperty | TypeCallback, -} - -/** - * Animation options control how an animation is produced, it shouldn't be too different for those who have used `animejs`, or `jquery`'s animate method. - * - * @remark - * An animation option is an object with keys and values that are computed and passed to the `Animate` class to create animations that match the specified options. - */ -export interface IAnimationOptions extends ICSSTransformableProperties { - /** - * Determines the DOM elements to animate. You can pass it a CSS selector, DOM elements, or an Array of DOM Elements and/or CSS Selectors. - */ - target?: TypeAnimationTarget, - - /** - * Alias of `target` - * {@link AnimationOptions.target | target } - */ - targets?: TypeAnimationTarget, - - /** - * Determines the acceleration curve of your animation. Based on the easings of [easings.net](https://easings.net) - * - * Read More about easings on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EffectTiming/easing) - * - * - * | constant | accelerate | decelerate | accelerate-decelerate | - * | :------- | :--------- | :--------- | :-------------------- | - * | linear | in-cubic | out-cubic | in-out-cubic | - * | ease | in-quart | out-quart | in-out-quart | - * | | in-quint | out-quint | in-out-quint | - * | | in-expo | out-expo | in-out-expo | - * | | in-circ | out-circ | in-out-circ | - * | | in-back | out-back | in-out-back | - * - * You can create your own custom cubic-bezier easing curves. Similar to css you type `cubic-bezier(...)` with 4 numbers representing the shape of the bezier curve, for example, `cubic-bezier(0.47, 0, 0.745, 0.715)` this is the bezier curve for `in-sine`. - * - * *Note: the `easing` property supports the original values and functions for easing as well, for example, `steps(1)`, and etc... are supported.* - * - * @example - * ```ts - * // cubic-bezier easing - * animate({ - * target: ".div", - * easing: "cubic-bezier(0.47, 0, 0.745, 0.715)", - * - * // or - * easing: "in-sine", - * transform: ["translate(0px)", "translate(500px)"], - * }); - * ``` - */ - easing?: keyof typeof EASINGS | TypeCallback | string | string[], - - /** - * Determines the duration of your animation in milliseconds. By passing it a callback, you can define a different duration for each element. The callback takes the index of each element, the target dom element, and the total number of target elements as its argument and returns a number. - * - * @example - * ```ts - * // First element fades out in 1s, second element in 2s, etc. - * animate({ - * target: ".div", - * easing: "linear", - * duration: 1000, - * // or - * duration: (index) => (index + 1) * 1000, - * opacity: [1, 0], - * }); - * ``` - */ - duration?: number | string | TypeCallback, - - /** - * Determines the delay of your animation in milliseconds. By passing it a callback, you can define a different delay for each element. The callback takes the index of each element, the target dom element, and the total number of target elements as its argument and returns a number. - * - * @example - * ```ts - * // First element starts fading out after 1s, second element after 2s, etc. - * animate({ - * target: ".div", - * easing: "linear", - * delay: 5, - * // or - * delay: (index) => (index + 1) * 1000, - * opacity: [1, 0], - * }); - * ``` - */ - delay?: number | TypeCallback, - - /** - * Adds an offset ammount to the `delay`, for creating a timeline similar to `animejs` - */ - timelineOffset?: number | TypeCallback, - - /** - * Similar to delay but it indicates the number of milliseconds to delay **after** the full animation has played **not before**. - * - * _**Note**: `endDelay` will delay the `onfinish` method and event, but will not reserve the current state of the CSS animation, if you need to use endDelay you may need to use the `fillMode` property to reserve the changes to the animation target._ - - * @example - * ```ts - * // First element fades out but then after 1s finishes, the second element after 2s, etc. - * animate({ - * target: ".div", - * easing: "linear", - * endDelay: 1000, - * // or - * endDelay: (index) => (index + 1) * 1000, - * opacity: [1, 0], - * }); - * ``` - */ - endDelay?: number | TypeCallback, - - /** - * - * This ensures all `animations` match up to the total duration, and don't finish too early, if animations finish too early when the `.play()` method is called specific animations that are finished will restart while the rest of the animations will continue playing. - * - * _**Note**: you cannot use the `padEndDelay` option and set a value for `endDelay`, the `endDelay` value will replace the padded endDelay_ - */ - padEndDelay?: Boolean, - - /** - * Determines if the animation should repeat, and how many times it should repeat. - * - * @example - * ```ts - * // Loop forever - * animate({ - * target: ".div", - * easing: "linear", - * loop: true, // If you want it to continously loop - * // or - * // loop: 5, // If you want the animation to loop 5 times - * opacity: [1, 0], - * }); - * ``` - */ - loop?: number | boolean | TypeCallback, - - /** - * Occurs when the animation for one of the targets completes, meaning when animating many targets that finish at different times this will run multiple times. The arguments it takes is slightly different from the rest of the animation options. - * - * The animation argument represents the animation for the current target. - * - * @param element - the current target element - * @param index - the index of the current target element in `Animate.prototype.targets` - * @param total - the total number of target elements - * @param animation - the animation of the current target element - * - * **Warning**: the order of the callback's arguments are in a different order, with the target element first, and the index second. - */ - onfinish?: (element?: HTMLElement, index?: number, total?: number, animation?: Animation) => void, - - /** - * Occurs when the animation for one of the targets is cancelled, meaning when animating many targets that are cancelled at different times this will run multiple times. The arguments it takes is slightly different from the rest of the animation options. - * - * The animation argument represents the animation for the current target. - * - * @param element - the current target element - * @param index - the index of the current target element in `Animate.prototype.targets` - * @param total - the total number of target elements - * @param animation - the animation of the current target element - * - * - * **Warning**: the order of the callback's arguments are in a different order, with the target element first, and the index second. - */ - oncancel?: (element?: HTMLElement, index?: number, total?: number, animation?: Animation) => void, - - /** - * - * Determines if the animation should automatically play immediately after being instantiated. - */ - autoplay?: boolean, - - /** - * Determines the direction of the animation; - * - `reverse` runs the animation backwards, - * - `alternate` switches direction after each iteration if the animation loops. - * - `alternate-reverse` starts the animation at what would be the end of the animation if the direction were - * - `normal` but then when the animation reaches the beginning of the animation it alternates going back to the position it started at. - */ - direction?: PlaybackDirection, - - /** - * Determines the animation playback rate. Useful in the authoring process to speed up some parts of a long sequence (value above 1) or slow down a specific animation to observe it (value between 0 to 1). - * - * _**Note**: negative numbers reverse the animation._ - */ - speed?: number | TypeCallback, - - /** - * Defines how an element should look after the animation. - * - * @remark - * fillMode of: - * - `none` means the animation's effects are only visible while the animation is playing. - * - `forwards` the affected element will continue to be rendered in the state of the final animation frame. - * - `backwards` the animation's effects should be reflected by the element(s) state prior to playing. - * - `both` combining the effects of both forwards and backwards; The animation's effects should be reflected by the element(s) state prior to playing and retained after the animation has completed playing. - * - `auto` if the animation effect fill mode is being applied to is a keyframe effect. "auto" is equivalent to "none". Otherwise, the result is "both". - * - * You can learn more here on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EffectTiming/fill). - - * _Be careful when using fillMode, it has some problems when it comes to concurrency of animations read more on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EffectTiming/fill), if browser support were better I would remove fillMode and use Animation.commitStyles, I'll have to change the way `fillMode` functions later. Use the onfinish method to commit styles [onfinish](#onfinish)._ - */ - fillMode?: FillMode, - - /** - * Another way to input options for an animation, it's also used to chain animations. - * - * @remarks - * The `options` animation option is another way to declare options, it can take an instance of `Animate`, a single `Animate` instance in an Array, e.g. `[Animate]` or an object containing animation options. - * - * - * `options` extends the animation properties of an animation, but more importance is given to the actual animation options object, so, the properties from `options` will be ignored if there is already an animation option with the same name declared. - * - * - * _**Note**: you can't use this property as a method._ - * - * @example - * ```ts - * (async () => { - * // animate is Promise-like, as in it has a then() method like a Promise but it isn't a Promise. - * // animate resolves to an Array that contains the Animate instance, e.g. [Animate] - * let [options] = await animate({ - * target: ".div", - * opacity: [0, 1], - * }); - * - * animate({ - * options, - * - * // opacity overrides the opacity property from `options` - * opacity: [1, 0], - * }); - * - * console.log(options); //= Animate - * })(); - * - * // or - * (async () => { - * let options = await animate({ - * target: ".div", - * opacity: [0, 1], - * duration: 2000, - * - * }); - * - * // Remeber, the `options` animation option can handle Arrays with an Animate instance, e.g. [Animate] - * // Also, remeber that Animate resolves to an Arrays with an Animate instance, e.g. [Animate] - * // Note: the `options` animation option can only handle one Animate instance in an Array and that is alway the first element in the Array - * animate({ - * options, - * opacity: [1, 0], - * }); - * - * console.log(options); //= [Animate] - * })(); - * - * // or - * (async () => { - * let options = { - * target: ".div", - * opacity: [0, 1], - * }; - * - * await animate(options); - * animate({ - * options, - * opacity: [1, 0], - * }); - * - * console.log(options); //= { ... } - * })(); - * ``` - */ - options?: IAnimationOptions, - - /** - * Contols the starting point of certain parts of an animation - * - * @remark - * The offset of the keyframe specified as a number between `0.0` and `1.0` inclusive or null. This is equivalent to specifying start and end states in percentages in CSS stylesheets using @keyframes. If this value is null or missing, the keyframe will be evenly spaced between adjacent keyframes. - * - * Read more on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) - * - * @example - * ```ts - * animate({ - * duration: 2000, - * opacity: [ 0, 0.9, 1 ], - * easing: [ 'ease-in', 'ease-out' ], - * - * offset: [ "from", 0.8 ], // Shorthand for [ 0, 0.8, 1 ] - * // or - * offset: [ 0, "80%", "to" ], // Shorthand for [ 0, 0.8, 1 ] - * // or - * offset: [ "0", "0.8", "to" ], // Shorthand for [ 0, 0.8, 1 ] - * }); - * ``` - */ - offset?: (number | string)[] | TypeCallback, - - /** - * Represents the timeline of animation. It exists to pass timeline features to Animations (default is [DocumentTimeline](https://developer.mozilla.org/en-US/docs/Web/API/DocumentTimeline)). - * - * As of right now it doesn't contain any features but in the future when other timelines like the [ScrollTimeline](https://drafts.csswg.org/scroll-animations-1/#scrolltimeline), read the Google Developer article for [examples and demos of ScrollTimeLine](https://developers.google.com/web/updates/2018/10/animation-worklet#hooking_into_the_space-time_continuum_scrolltimeline) - */ - timeline?: AnimationTimeline, - - /** - * Allows you to manually set keyframes using a `keyframe` array - * - * Read more on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/setKeyframes) - * - * @remark - * An `array` of objects (keyframes) consisting of properties and values to iterate over. This is the canonical format returned by the [getKeyframes()](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/getKeyframes) method. - * - * @example - * ```ts - * element.animate([ - * { // from - * opacity: 0, - * color: "#fff" - * }, - * { // to - * opacity: 1, - * color: "#000" - * } - * ], 2000); - * ``` - * - * Offsets for each keyframe can be specified by providing an `offset` value. - - * @example - * ```ts - * element.animate([ { opacity: 1 }, - * { opacity: 0.1, offset: 0.7 }, - * { opacity: 0 } ], - * 2000); - * ``` - * - * _**Note**: `offset` values, if provided, must be between 0.0 and 1.0 (inclusive) and arranged in ascending order._ - * - * It is not necessary to specify an offset for every keyframe. Keyframes without a specified offset will be evenly spaced between adjacent keyframes. - * - * --- - * - * The easing to apply between keyframes can be specified by providing an easing `value` as illustrated below. - * - * _**Note**: the values for easing in keyframes are limited to "ease", "ease-in", "ease-out", "ease-in-out", "linear", "steps(...)", and "cubic-bezier(...)", to get access to the predefined {@link EASINGS | easings}, you will need to use the function {@link GetEase}._ - - * @example - * ```ts - * element.animate([ { opacity: 1, easing: 'ease-out' }, - * { opacity: 0.1, easing: 'ease-in' }, - * { opacity: 0 } ], - * 2000); - * ``` - * - * In this example, the specified easing only applies from the keyframe where it is specified until the next keyframe. Any easing value specified on the options argument, however, applies across a single iteration of the animation — for the entire duration. - * - * - * `@okikio/animate` also offers another format called `CSSLikeKeyframe`, - * it basically functions the same way CSS `@keyframe` functions - * - * @example - * ```ts - * animate({ - * keyframes: { - * "from, 50%, to": { - * opacity: 1 - * }, - * - * "25%, 0.7": { - * opacity: 0 - * } - * } - * }) - * // Results in a keyframe array like this - * //= [ - * //= { opacity: 1, offset: 0 }, - * //= { opacity: 0, offset: 0.25 }, - * //= { opacity: 1, offset: 0.5 }, - * //= { opacity: 0, offset: 0.7 }, - * //= { opacity: 1, offset: 1 } - * //= ] - * ``` - */ - keyframes?: TypeCSSLikeKeyframe | ICSSComputedTransformableProperties[] & Keyframe[] | object[] | TypeCallback, - - /** - * The properties of the `extend` animation option are not interperted or computed, they are given directly to the `Web Animation API`, as way to access features that haven't been implemented in `@okikio/animate`, for example, `iterationStart`. - * - * `extend` is supposed to future proof the library if new features are added to the `Web Animation API` that you want to use, but that has not been implemented yet. - * - * _**Note**: it doesn't allow for declaring actual animation keyframes; it's just for animation timing options, and it overrides all other animation timing options that accomplish the same goal, e.g. `loop` & `iterations`, if `iterations` is a property of `extend` then `iterations` will override `loop`._ - * - * @example - * ```ts - * animate({ - * target: ".div", - * opacity: [0, 1], - * loop: 5, - * extend: { - * iterationStart: 0.5, - * // etc... - * fill: "both", // This overrides fillMode - * iteration: 2, // This overrides loop - * composite: "add" - * } - * }); - * ``` - */ - extend?: KeyframeEffectOptions | TypeCallback, - - /** - * Theses are the CSS properties to be animated as Keyframes - * - * Read more on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/setKeyframes) - * - * @remark - * An `object` containing key-value pairs consisting of the property to animate and an `array` of values to iterate over. - * - * @example - * ```ts - * element.animate({ - * opacity: [ 0, 1 ], // [ from, to ] - * color: [ "#fff", "#000" ] // [ from, to ] - * }, 2000); - * ``` - * - * Using this format, the number of elements in each array does not need to be equal. The provided values will be spaced out independently. - - * @example - * ```ts - * element.animate({ - * opacity: [ 0, 1 ], // offset: 0, 1 - * backgroundColor: [ "red", "yellow", "green" ], // offset: 0, 0.5, 1 - * }, 2000); - * ``` - * - * The special keys `offset`, `easing`, and `composite` (described below) may be specified alongside the property values. - - * @example - * ```ts - * element.animate({ - * opacity: [ 0, 0.9, 1 ], - * offset: [ 0, 0.8 ], // Shorthand for [ 0, 0.8, 1 ] - * easing: [ 'ease-in', 'ease-out' ], - * }, 2000); - * ``` - * - * After generating a suitable set of keyframes from the property value lists, each supplied offset is applied to the corresponding keyframe. If there are insufficient values, or if the list contains `null` values, the keyframes without specified offsets will be evenly spaced as with the array format described above. - * - * If there are too few `easing` or `composite` values, the corresponding list will be repeated as needed. - * - * _**Note**: to use `composite` you will need to add it to the {@link AnimationOptions.extend | extend} object as an option_ - */ - [property: string]: TypeAnimationOptionTypes, -}; - -/** - * CSS properties - */ -export interface ICSSProperties { - [key: string]: TypeSingleValueCSSProperty | Array -}; - -/** - * After being computed as an animation option - */ -export interface ICSSComputedTransformableProperties extends ICSSProperties { - perspective?: TypeSingleValueCSSProperty, - rotate?: TypeSingleValueCSSProperty | Array, - rotate3d?: TypeSingleValueCSSProperty | Array, - rotateX?: TypeSingleValueCSSProperty, - rotateY?: TypeSingleValueCSSProperty, - rotateZ?: TypeSingleValueCSSProperty, - translate?: TypeSingleValueCSSProperty | Array, - translate3d?: TypeSingleValueCSSProperty | Array, - translateX?: TypeSingleValueCSSProperty, - translateY?: TypeSingleValueCSSProperty, - translateZ?: TypeSingleValueCSSProperty, - scale?: TypeSingleValueCSSProperty | Array, - scale3d?: TypeSingleValueCSSProperty | Array, - scaleX?: TypeSingleValueCSSProperty, - scaleY?: TypeSingleValueCSSProperty, - scaleZ?: TypeSingleValueCSSProperty, - skew?: TypeSingleValueCSSProperty | Array, - skewX?: TypeSingleValueCSSProperty, - skewY?: TypeSingleValueCSSProperty, -} - /** * The default options for every Animate instance * @@ -730,403 +208,28 @@ export const DefaultAnimationOptions: IAnimationOptions = { extend: {} }; -export type TypeAnimationEvents = "update" | "play" | "pause" | "begin" | "cancel" | "finish" | "error" | "stop" | "playstate-change"; -export type TypePlayStates = "idle" | "running" | "paused" | "finished"; export const parseOptions = (options: IAnimationOptions): IAnimationOptions => { let { options: animation, ...rest } = options; - let oldOptions = animation instanceof Animate ? animation.getOptions() : (Array.isArray(animation) ? animation?.[0]?.getOptions() : animation); + let oldOptions = animation instanceof Animate ? animation.options : (Array.isArray(animation) ? animation?.[0]?.options : animation); return Object.assign({}, oldOptions, rest); } /** - * Returns a closure Function, which adds a unit to numbers but simply returns strings with no edits assuming the value has a unit if it's a string - * - * @param unit - the default unit to give the CSS Value - * @returns - * if value already has a unit (we assume the value has a unit if it's a string), we return it; - * else return the value plus the default unit - */ -export const addCSSUnit = (unit: string = "") => { - return (value: string | number) => typeof value == "string" ? value : `${value}${unit}`; -} - -/** Function doesn't add any units by default */ -export const UnitLess = addCSSUnit(); - -/** Function adds "px" unit to numbers */ -export const UnitPX = addCSSUnit("px"); - -/** Function adds "deg" unit to numbers */ -export const UnitDEG = addCSSUnit("deg"); - -/** - * Convert the input to an array - * For strings split them at commas - * For array do nothing - * For everything else wrap the input in an array - */ -export const toArr = (input: any): any[] => { - if (Array.isArray(input) || typeof input == "string") { - if (typeof input == "string") input = input.split(","); - return input; - } - - return [input]; -} - -/** - * Checks if value is valid/truthy; it counts empty arrays and strings as falsey, - * as well as null and undefined everything else is valid - * - * _**Note:** 0 counts as valid_ - * - * @param value - anything - * @returns true or false - */ -export const isValid = (value: any) => { - if (Array.isArray(value) || typeof value == "string") - return Boolean(value.length); - return value != null && value != undefined; -} - -/** - * Returns a closure function, which adds units to numbers, strings or arrays of both - * - * @param unit - a unit function to use to add units to {@link TypeSingleValueCSSProperty | TypeSingleValueCSSProperty's } - * @returns - * if input is a string split it into an array at the comma's, and add units - * else if the input is a number add the default units - * otherwise if the input is an array of both add units according to {@link addCSSUnit} - */ -export const CSSValue = (unit: typeof UnitLess) => { - return (input: TypeSingleValueCSSProperty) => { - return isValid(input) ? toArr(input).map(val => { - if (typeof val != "number" && typeof val != "string") - return val; - - // Basically if you can convert it to a number try to, - // otherwise just return whatever you can - let num = Number(val); - let value = Number.isNaN(num) ? (typeof val == "string" ? val.trim() : val) : num; - return unit(value); // Add the default units - }) : []; - }; -} - -/** - * Flips the rows and columns of 2-dimensional arrays + * Return a copy of the object without the keys specified in the keys argument * - * Read more on [underscorejs.org](https://underscorejs.org/#zip) & [lodash.com](https://lodash.com/docs/4.17.15#zip) - * - * @example - * ```ts - * transpose( - * ['moe', 'larry', 'curly'], - * [30, 40, 50], - * [true, false, false] - * ); - * // [ - * // ["moe", 30, true], - * // ["larry", 40, false], - * // ["curly", 50, false] - * // ] - * ``` - * @param [...args] - the arrays to process as a set of arguments + * @param keys - arrays of keys to remove from the object + * @param obj - the object in question * @returns - * returns the new array of grouped elements + * a copy of the object without certain key */ -export const transpose = (...args: TypeSingleValueCSSProperty[] | TypeSingleValueCSSProperty[][]) => { - let largestArrLen = 0; - args = args.map(arr => { - // Convert all values in arrays to an array - // This ensures that `arrays` is an array of arrays - let result = toArr(arr); - - // Finds the largest array - let len = result.length; - if (len > largestArrLen) largestArrLen = len; - return result; - }); - - // Flip the rows and columns of arrays - let result = []; - let len = args.length; - for (let col = 0; col < largestArrLen; col++) { - result[col] = []; - - for (let row = 0; row < len; row++) { - let val = args[row][col]; - if (isValid(val)) - result[col][row] = val; - } +export const omit = (keys: string[], obj: { [key: string]: any }) => { + let rest = { ...obj }; + while (keys.length) { + let { [keys.pop()]: omitted, ...remaining } = rest; + rest = remaining; } - - return result; -} - -/** - * Takes `TypeSingleValueCSSProperty` or an array of `TypeSingleValueCSSProperty` and adds units approriately - * - * @param arr - array of numbers, strings and/or an array of array of both e.g. ```[[25, "50px", "60%"], "25, 35, 60%", 50]``` - * @param unit - a unit function to use to add units to {@link TypeSingleValueCSSProperty | TypeSingleValueCSSProperty's } - * @returns - * an array of an array of strings with units - * e.g. - * ```ts - * [ - * [ '25px', '35px', ' 60%' ], - * [ '50px', '60px', '70px' ] - * ] - * ``` - */ -export const CSSArrValue = (arr: TypeSingleValueCSSProperty | TypeSingleValueCSSProperty[], unit: typeof UnitLess) => { - // This is for the full varients of the transform function as well as the 3d varients - // zipping the `CSSValue` means if a user enters a string, it will treat each value (seperated by a comma) in that - // string as a seperate transition state - return toArr(arr).map(CSSValue(unit)) as TypeSingleValueCSSProperty[]; -} - -export const TransformFunctionNames = [ - "translate", - "translate3d", - "translateX", - "translateY", - "translateZ", - "rotate", - "rotate3d", - "rotateX", - "rotateY", - "rotateZ", - "scale", - "scale3d", - "scaleX", - "scaleY", - "scaleZ", - "skew", - "skewX", - "skewY", - "perspective" -]; - -/** - * Creates the transform property text - */ -export const createTransformProperty = (arr) => { - let result = ""; - let len = TransformFunctionNames.length; - for (let i = 0; i < len; i++) { - let name = TransformFunctionNames[i]; - let value = arr[i]; - if (isValid(value)) - result += `${name}(${Array.isArray(value) ? value.join(", ") : value}) `; - } - - return result.trim(); -} - -/** Parses CSSValues without adding any units */ -export const UnitLessCSSValue = CSSValue(UnitLess); - -/** Parses CSSValues and adds the "px" unit if required */ -export const UnitPXCSSValue = CSSValue(UnitPX); - -/** Parses CSSValues and adds the "deg" unit if required */ -export const UnitDEGCSSValue = CSSValue(UnitDEG); - -/** - * Wrap non array CSS property values in an array - */ -export const CSSPropertiesToArr = (properties: ICSSProperties) => { - for (let [key, value] of Object.entries(properties)) { - // Wrap non array values in arrays - properties[key] = [].concat(value).map(value => `` + value); - } - - return properties; -}; - -/** - * Removes the need for the full transform statement in order to use translate, rotate, scale, skew, or perspective including their X, Y, Z, and 3d varients - * Also, adds the ability to use single string or number values for transform functions - * - * _**Note**: the `transform` animation option will override all transform function properties_ - * - * @param properties - the CSS properties to transform - * - * @example - * ```ts - * ParseTransformableCSSProperties({ - * // It will automatically add the "px" units for you, or you can write a string with the units you want - * translate3d: [ - * "25, 35, 60%", - * [50, "60px", 70], - * ["70", 50] - * ], - * translate: "25, 35, 60%", - * translateX: [50, "60px", "70"], - * translateY: ["50, 60", "60"], // Note: this will actually result in an error, make sure to pay attention to where you are putting strings and commas - * translateZ: 0, - * perspective: 0, - * opacity: "0, 5", - * scale: [ - * [1, "2"], - * ["2", 1] - * ], - * rotate3d: [ - * [1, 2, 5, "3deg"], // The last value in the array must be a string with units for rotate3d - * [2, "4", 6, "45turn"], - * ["2", "4", "6", "-1rad"] - * ] - * }) - * - * //= { - * //= transform: [ - * //= // `translateY(50, 60)` will actually result in an error - * //= 'translate(25px) translate3d(25px, 35px, 60%) translateX(50px) translateY(50, 60) translateZ(0px) rotate3d(1, 2, 5, 3deg) scale(1, 2) perspective(0px)', - * //= 'translate(35px) translate3d(50px, 60px, 70px) translateX(60px) translateY(60px) rotate3d(2, 4, 6, 45turn) scale(2, 1)', - * //= 'translate(60%) translate3d(70px, 50px) translateX(70px) rotate3d(2, 4, 6, -1rad)' - * //= ], - * //= opacity: [ '0', '5' ] - * //= } - * ``` - * - * @returns - * an object with a properly formatted `transform` and `opactity`, as well as other unformatted CSS properties - * ``` - */ -export const ParseTransformableCSSProperties = (properties: ICSSComputedTransformableProperties) => { - let { - perspective, - rotate, - rotate3d, - rotateX, - rotateY, - rotateZ, - translate, - translate3d, - translateX, - translateY, - translateZ, - scale, - scale3d, - scaleX, - scaleY, - scaleZ, - skew, - skewX, - skewY, - ...rest - } = properties; - - translate = CSSArrValue(translate, UnitPX); - translate3d = CSSArrValue(translate3d, UnitPX); - translateX = UnitPXCSSValue(translateX); - translateY = UnitPXCSSValue(translateY); - translateZ = UnitPXCSSValue(translateZ); - - rotate = CSSArrValue(rotate, UnitDEG); - rotate3d = CSSArrValue(rotate3d, UnitLess); - rotateX = UnitDEGCSSValue(rotateX); - rotateY = UnitDEGCSSValue(rotateY); - rotateZ = UnitDEGCSSValue(rotateZ); - - scale = CSSArrValue(scale, UnitLess); - scale3d = CSSArrValue(scale3d, UnitLess); - scaleX = UnitLessCSSValue(scaleX); - scaleY = UnitLessCSSValue(scaleY); - scaleZ = UnitLessCSSValue(scaleZ); - - skew = CSSArrValue(skew, UnitDEG); - skewX = UnitDEGCSSValue(skewX); - skewY = UnitDEGCSSValue(skewY); - - perspective = UnitPXCSSValue(perspective); - - let transform = transpose( - translate, translate3d, translateX, translateY, translateZ, - rotate, rotate3d, rotateX, rotateY, rotateZ, - scale, scale3d, scaleX, scaleY, scaleZ, - skew, skewX, skewY, - perspective - ).map(createTransformProperty); - - rest = CSSPropertiesToArr(rest); - return Object.assign({}, - isValid(transform) ? { transform } : null, - rest); -} - -/** - * Similar to {@link ParseTransformableCSSProperties} except it transforms the CSS properties in each Keyframe - * @param keyframes - an array of keyframes with transformable CSS properties - * @returns - * an array of keyframes, with transformed CSS properties - */ -export const ParseTransformableCSSKeyframes = (keyframes: ICSSComputedTransformableProperties[]) => { - return keyframes.map(properties => { - let { - translate, - translate3d, - translateX, - translateY, - translateZ, - rotate, - rotate3d, - rotateX, - rotateY, - rotateZ, - scale, - scale3d, - scaleX, - scaleY, - scaleZ, - skew, - skewX, - skewY, - perspective, - ...rest - } = properties; - - translate = UnitPXCSSValue(translate as TypeSingleValueCSSProperty); - translate3d = UnitPXCSSValue(translate3d as TypeSingleValueCSSProperty); - translateX = UnitPXCSSValue(translateX)[0]; - translateY = UnitPXCSSValue(translateY)[0]; - translateZ = UnitPXCSSValue(translateZ)[0]; - - rotate = UnitDEGCSSValue(rotate as TypeSingleValueCSSProperty); - rotate3d = UnitLessCSSValue(rotate3d as TypeSingleValueCSSProperty); - rotateX = UnitDEGCSSValue(rotateX)[0]; - rotateY = UnitDEGCSSValue(rotateY)[0]; - rotateZ = UnitDEGCSSValue(rotateZ)[0]; - - scale = UnitLessCSSValue(scale as TypeSingleValueCSSProperty); - scale3d = UnitLessCSSValue(scale3d as TypeSingleValueCSSProperty); - scaleX = UnitLessCSSValue(scaleX)[0]; - scaleY = UnitLessCSSValue(scaleY)[0]; - scaleZ = UnitLessCSSValue(scaleZ)[0]; - - skew = UnitDEGCSSValue(skew as TypeSingleValueCSSProperty); - skewX = UnitDEGCSSValue(skewX)[0]; - skewY = UnitDEGCSSValue(skewY)[0]; - - perspective = UnitPXCSSValue(perspective)[0]; - - return [ - rest, - translate, translate3d, translateX, translateY, translateZ, - rotate, rotate3d, rotateX, rotateY, rotateZ, - scale, scale3d, scaleX, scaleY, scaleZ, - skew, skewX, skewY, - perspective - ]; - }).map(([rest, ...transformFunctions]) => { - let transform = createTransformProperty(transformFunctions); - rest = CSSPropertiesToArr(rest as ICSSProperties); - return Object.assign({}, - isValid(transform) ? { transform } : null, - rest); - }); + return rest; } /** @@ -1142,26 +245,11 @@ export class Animate { */ public options: IAnimationOptions = {}; - /** - * The list of Elements to Animate - */ - public targets: Manager = new Manager(); - /** * The properties to animate */ public properties: object = {}; - /** - * A Manager of Animations - */ - public animations: Manager = new Manager(); - - /** - * A Manager of KeyFrameEffects - */ - public keyframeEffects: Manager = new Manager(); - /** * The total duration of all Animation's */ @@ -1170,32 +258,12 @@ export class Animate { /** * The smallest delay out of all Animation's */ - public minDelay: number = 0; - - /** - * The largest speed out of all Animation's - */ - public maxSpeed: number = 0; - - /** - * The options for individual animations - * - * A Manager that stores all the fully calculated options for individual Animation instances. - * - * _**Note**: the computedOptions are changed to their proper Animation instance options, so, some of the names are different, and options that can't be computed are not present. E.g. fillMode in the animation options is now just fill in the computedOptions.*_ - * - * _**Note**: keyframes are not included, both the array form and the object form; the options, speed, extend, padEndDelay, and autoplay animation options are not included_ - */ - public computedOptions: Manager = new Manager(); + public minDelay: number; /** - * The keyframes for individual animations - * - * A Manager that stores all the fully calculated keyframes for individual Animation instances. - * - * _**Note**: the computedKeyframes are changed to their proper Animation instance options, so, some of the names are different, and options that can't be computed are not present. E.g. translateX, skew, etc..., they've all been turned into the transform property.*_ + * The smallest speed out of all Animation's */ - public computedKeyframes: Manager = new Manager(); + public maxSpeed: number; /** * The Element the mainAnimation runs on @@ -1226,15 +294,58 @@ export class Animate { * Returns a promise that is fulfilled when the mainAnimation is finished */ public promise: Promise; + + /** + * The list of Elements to Animate + */ + public targets: Manager = new Manager(); + + /** + * The indexs of target Elements in Animate + */ + public targetIndexes: WeakMap = new WeakMap(); + + /** + * A WeakMap of KeyFrameEffects + */ + public keyframeEffects: WeakMap = new WeakMap(); + + /** + * The options for individual animations + * + * A WeakMap that stores all the fully calculated options for individual Animation instances. + * + * _**Note**: the computedOptions are changed to their proper Animation instance options, so, some of the names are different, and options that can't be computed are not present. E.g. fillMode in the animation options is now just fill in the computedOptions.*_ + * + * _**Note**: keyframes are not included, both the array form and the object form; the options, speed, extend, padEndDelay, and autoplay animation options are not included_ + */ + public computedOptions: WeakMap = new WeakMap(); + + /** + * A WeakMap of Animations + */ + public animations: WeakMap = new WeakMap(); + + /** + * The keyframes for individual animations + * + * A WeakMap that stores all the fully calculated keyframes for individual Animation instances. + * + * _**Note**: the computedKeyframes are changed to their proper Animation instance options, so, some of the names are different, and options that can't be computed are not present. E.g. translateX, skew, etc..., they've all been turned into the transform property.*_ + */ + public computedKeyframes: WeakMap = new WeakMap(); constructor(options: IAnimationOptions) { this.loop = this.loop.bind(this); this.onVisibilityChange = this.onVisibilityChange.bind(this); + this.on("error", (err) => console.error(err)); this.updateOptions(options); this.visibilityPlayState = this.getPlayState(); if (Animate.pauseOnPageHidden) { - document.addEventListener('visibilitychange', this.onVisibilityChange); + document.addEventListener('visibilitychange', this.onVisibilityChange, false); } + + this.newPromise(); } /** @@ -1253,7 +364,7 @@ export class Animate { * @type {TypePlayStates} * @memberof Animate */ - protected visibilityPlayState: TypePlayStates; + protected visibilityPlayState: TypePlayStates; /** * document `visibilitychange` event handler @@ -1342,7 +453,11 @@ export class Animate { * Calls a method that affects all animations **excluding** the mainAnimation; the method only allows the animation parameter */ public allAnimations(method: (animation?: Animation, target?: HTMLElement) => void) { - this.animations.forEach(method); + this.targets.forEach((target: HTMLElement) => { + let keyframeEffect = this.keyframeEffects.get(target); + let animation = this.animations.get(keyframeEffect); + return method(animation, target); + }); return this; } @@ -1387,7 +502,6 @@ export class Animate { if (!this.is(playstate)) this.emit("playstate-change", playstate, this); this.stopLoop(); - this.animationFrame = undefined; return this; } @@ -1431,71 +545,45 @@ export class Animate { */ public stop() { this.cancel(); + this.stopLoop(); + document.removeEventListener('visibilitychange', this.onVisibilityChange, false); - if (Animate.pauseOnPageHidden) { - document.removeEventListener('visibilitychange', this.onVisibilityChange); - } - - this.computedOptions.clear(); - this.animations.clear(); - this.keyframeEffects.clear(); - this.targets.clear(); - - this.mainkeyframeEffect = undefined; - this.mainAnimation = undefined; - this.mainElement = undefined; + this.targets.forEach((target: HTMLElement) => this.removeTarget(target)); this.emit("stop"); this.emitter.clear(); - this.promise = undefined; - this.computedOptions = undefined; - this.animations = undefined; - this.keyframeEffects = undefined; - this.emitter = undefined; - this.targets = undefined; - this.options = undefined; - } - - /** - * Returns an Manager instance for targets - */ - public getTargets() { - return this.targets.values(); + this.mainkeyframeEffect = null; + this.mainAnimation = null; + this.mainElement = null; + + this.promise = null; + this.computedOptions = null; + this.animations = null; + this.keyframeEffects = null; + this.emitter = null; + this.targets = null; + this.options = null; + this.properties = null; } /** * Get a specific Animation from an Animate instance */ - public getAnimation(element: HTMLElement): Animation { - return this.animations.get(element); - } - - /** - * Get a specific Animation's KeyframeEffect from an Animate instance - */ - public getKeyframeEffect(animation: Animation): KeyframeEffect { - return this.keyframeEffects.get(animation); + public getAnimation(target: HTMLElement) { + let keyframeEffect = this.keyframeEffects.get(target); + return this.animations.get(keyframeEffect); } /** * Returns the timings of an Animation, given a target * E.g. { duration, endDelay, delay, iterations, iterationStart, direction, easing, fill, etc... } */ - public getTiming(target: HTMLElement | Animation): TypeComputedAnimationOptions { - let animation = target instanceof Animation ? target : this.getAnimation(target); - let keyframeOptions = this.computedOptions.get(animation) ?? {}; - let timings = this.getKeyframeEffect(animation).getTiming?.() ?? {}; - let options = this.getOptions(); - - return { ...DefaultAnimationOptions, ...options, ...timings, ...keyframeOptions }; - } + public getTiming(target: HTMLElement): TypeComputedAnimationOptions { + let keyframeOptions = this.computedOptions.get(target) ?? {}; + let timings = this.keyframeEffects.get(target).getTiming?.() ?? {}; - /** - * Returns the total duration of Animation - */ - public getTotalDuration(): number { - return this.totalDuration; + return { ...keyframeOptions, ...timings }; } /** @@ -1526,21 +614,6 @@ export class Animate { return this.mainAnimation.playState; } - /** - * Get the options of an Animate instance - */ - public getOptions(): IAnimationOptions { - return this.options; - } - - /** - * Get the computed options object for a specific target - */ - public getComputedOption(target: HTMLElement | Animation): IAnimationOptions { - let animation = target instanceof Animation ? target : this.getAnimation(target); - return this.computedOptions.get(animation); - } - /** * Returns a boolean determining if the `animate` instances playstate is equal to the `playstate` parameter. */ @@ -1552,7 +625,7 @@ export class Animate { * Set the current time of the Main Animation */ public setCurrentTime(time: number): Animate { - this.all(anim => { anim.currentTime = time; }); + this.all(anim => (anim.currentTime = time)); this.emit("update", this.getProgress()); return this; } @@ -1580,179 +653,256 @@ export class Animate { } /** - * Update the options for all targets set in the constructor - * - * _**Note**: `KeyframeEffect` support is really low, so, I am suggest that you avoid using the `updateOptions` method, until browser support for `KeyframeEffect.updateTiming(...)` and `KeyframeEffefct.setKeyframes(...)` is better_ - * - * @beta + * Returns an array of computed options */ - public updateOptions(options: IAnimationOptions = {}) { - try { - this.options = Object.assign({}, DefaultAnimationOptions, this.options, parseOptions(options)); + protected createArrayOfComputedOptions(optionsFromParam: IAnimationOptions, len: number) { + let result: TypeComputedAnimationOptions = []; + this.targets.forEach((target: HTMLElement, i) => { + // Basically if there is already a computedOption for the target element use it, but don't ovveride any new options + let oldComputedOptions: IAnimationOptions = this.computedOptions.get(target) ?? {}; + let getOption = (key: string) => { + let computedKey = key; + if (key == "loop") computedKey = "iterations"; + if (key == "fillMode") computedKey = "fill"; + return optionsFromParam[key] ?? oldComputedOptions[computedKey] ?? this.options[key]; + }; + let animationOptions = Object.assign({ + easing: getOption("easing"), + iterations: getOption("loop"), + direction: getOption("direction"), + endDelay: getOption("endDelay"), + duration: getOption("duration"), + speed: getOption("speed"), + delay: getOption("delay"), + timelineOffset: getOption("timelineOffset"), + keyframes: getOption("keyframes"), + }, getOption("extend") ?? {}); + + // let oldComputedOptions = this.computedOptions.get(target) + // Allows the use of functions as the values, for both the keyframes and the animation object + // It adds the capability of advanced stagger animation, similar to the animejs stagger functions + let computedOptions = mapAnimationOptions(animationOptions as IAnimationOptions, [i, len, target], this); + + if (typeof computedOptions.easing == "string") + computedOptions.easing = GetEase(computedOptions.easing); + + if (computedOptions.iterations === true) + computedOptions.iterations = Infinity; + + computedOptions.fill = getOption("fillMode"); + + // Add timelineOffset to delay, this is future proofing; + // if you want to create a custom timeline similar to animejs this will help you + // I don't intend to make a timeline function for this project let { - loop, - delay, - speed, - easing, timelineOffset, + speed, endDelay, + delay, duration, - direction, - fillMode, - onfinish, - oncancel, - keyframes, - autoplay, - target, - targets: _targets, - padEndDelay, - extend, + iterations, + ...remainingComputedOptions + } = computedOptions; - /** - * Theses are the CSS properties to be animated as Keyframes - */ - ...properties - } = this.options; + iterations = Number(iterations); + duration = Number(duration); + endDelay = Number(endDelay); + speed = Number(speed); + delay = Number(delay) + Number(timelineOffset); - let targets = new Set([...this.getTargets(), ...getTargets(_targets), ...getTargets(target)]); - this.targets = new Manager(); - this.properties = properties; + let tempDurations = delay + (duration * iterations) + endDelay; - targets.forEach(value => this.targets.add(value)); + // Set the totalDuration to be the Animation with the largest totalDuration + if (this.totalDuration < tempDurations) + this.totalDuration = tempDurations; - let delayArr = []; - let speedArr = []; - let len = this.targets.size; - let listofComputedOptions = []; - let oldTotalDuration = this.totalDuration; - let animationOptions: IAnimationOptions = { - easing: typeof easing == "string" ? GetEase(easing) : easing, - iterations: loop === true ? Infinity : (loop as number), - direction, - endDelay, - duration, + result[i] = { + ...remainingComputedOptions, speed, + tempDurations, + endDelay, delay, - timelineOffset, - fill: fillMode, - ...extend + duration, + iterations, }; - this.targets.forEach((target: HTMLElement, i) => { - // Allows the use of functions as the values, for both the keyframes and the animation object - // It adds the capability of advanced stagger animation, similar to the animejs stagger functions - let computedOptions = mapAnimationOptions(animationOptions, [i, len, target], this); + if (!isValid(this.minDelay) || delay < this.minDelay) this.minDelay = delay; + if (!isValid(this.maxSpeed) || speed < this.maxSpeed) this.maxSpeed = speed; + }); + return result; + } - // Add timelineOffset to delay, this is future proofing; - // if you want to create a custom timeline similar to animejs this will help you - // I don't intend to make a timeline function for this project - let { timelineOffset, ...remainingComputedOptions } = computedOptions; + /** + * Creates animations out of an array of computed options + */ + protected createAnimations(param: { arrOfComputedOptions: any; padEndDelay: any; oldCSSProperties: any; onfinish: any; oncancel: any; }, len: number) { + let { + arrOfComputedOptions, + padEndDelay, + oldCSSProperties, + onfinish, + oncancel + } = param; + + this.targets.forEach((target: HTMLElement, i) => { + let { speed, keyframes, tempDurations, ...computedOptions } = arrOfComputedOptions[i]; + + // You cannot use the `padEndDelay` option and set a value for `endDelay`, the `endDelay` value will + // replace the padded endDelay + + // This ensures all `animations` match up to the total duration, and don't finish too early, + // if animations finish too early, when the `.play()` method is called, some animations + // that are finished will restart, while the rest will continue playing. + // This is mostly for progress control, but depending on your usage may truly benefit you + if (padEndDelay && computedOptions.endDelay == 0 && + Math.abs(computedOptions.iterations) != Math.abs(Infinity)) { + computedOptions.endDelay = this.totalDuration - tempDurations; + } - (remainingComputedOptions.delay as number) += timelineOffset as number; - remainingComputedOptions.tempDurations = +remainingComputedOptions.delay + - (+remainingComputedOptions.duration * +remainingComputedOptions.iterations) + - +remainingComputedOptions.endDelay; + let computedKeyframes: Keyframe[] | PropertyIndexedKeyframes; + let animationKeyframe: TypeKeyFrameOptionsType; + + // Accept keyframes as a keyframes Object, or a method, + // if there are no animations in the keyframes array, + // uses css properties from the options object + let arrKeyframes = keyframes as (Keyframe[] | TypeCSSLikeKeyframe); + if (typeof arrKeyframes == "object") arrKeyframes = KeyframeParse(arrKeyframes); + + // If `computedKeyframes` have been previously computed for this target element replace + // the old uncomputed CSS properties with it, otherwise, use the uncomputed property + let oldComputedKeyframe = this.computedKeyframes.get(target) ?? {}; + let fullProperties = Object.assign({}, oldCSSProperties, oldComputedKeyframe); + + // Replace old CSS properties with new CSS properties if there is a new value for the CSS property + // As in the new CSS property is not null or null + let properties = mapObject(fullProperties, (value, key) => (this.properties[key] ?? value)); + + // Prefer arrays of keyframes over pure CSS Properties + animationKeyframe = isValid(arrKeyframes) ? arrKeyframes : properties as PropertyIndexedKeyframes; + + if (!Array.isArray(animationKeyframe)) { + // Remove `keyframes` animation option, it's not a valid CSS property + let remaining: IAnimationOptions = omit(["keyframes"], animationKeyframe); + let { offset, ...CSSProperties } = mapAnimationOptions(remaining, [i, len, target], this); + + // transform, is often used so, to make them easier to use we parse them for strings, number, and/or arrays of both; + // for transform we parse the translate, skew, scale, and perspective functions (including all their varients) as CSS properties; + // it then turns these properties into valid `PropertyIndexedKeyframes` + // Read the documentation for `ParseTransformableCSSProperties` + CSSProperties = ParseTransformableCSSProperties(CSSProperties as ICSSComputedTransformableProperties); + + let _offset = offset as (string | number)[]; + computedKeyframes = Object.assign({}, + CSSProperties, + !isValid(_offset) ? null : { offset: _offset.map(parseOffset) } + ) as PropertyIndexedKeyframes; + } else { + computedKeyframes = animationKeyframe.map((keyframe: Keyframe & { loop: boolean | number }) => { + // Remove `speed`, it's not a valid CSS property + let { loop, easing, offset, ...remaining } = omit(["speed"], keyframe); + + return { + easing: GetEase(easing), + iterations: (loop as boolean) === true ? Infinity : Number(loop), + offset: parseOffset(offset), + ...remaining + }; + }); - // Set the Animate class's duration to be the Animation with the largest totalDuration - if (this.totalDuration < +remainingComputedOptions.tempDurations) { - this.totalDuration = +remainingComputedOptions.tempDurations; - } + // Transform transformable CSS properties in each keyframe of the keyframe array + computedKeyframes = ParseTransformableCSSKeyframes(computedKeyframes) as Keyframe[]; + } - listofComputedOptions[i] = remainingComputedOptions; - delayArr.push(remainingComputedOptions.delay); - speedArr.push(remainingComputedOptions.speed); - }); + let animation: Animation, keyFrameEffect: KeyframeEffect; + if (this.keyframeEffects.has(target)) { + // Update the animation, if the target already is already being animated + keyFrameEffect = this.keyframeEffects.get(target); + animation = this.animations.get(keyFrameEffect); + + keyFrameEffect?.setKeyframes?.(computedKeyframes); + keyFrameEffect?.updateTiming?.(computedOptions as KeyframeAnimationOptions); + } else { + // Create animation and add it to the Animations Set + keyFrameEffect = new KeyframeEffect(target, computedKeyframes, computedOptions as KeyframeAnimationOptions); + animation = new Animation(keyFrameEffect, computedOptions.timeline); + + this.keyframeEffects.set(target, keyFrameEffect); + this.animations.set(keyFrameEffect, animation); + } - this.targets.forEach((target: HTMLElement, i) => { - let { speed: $speed, tempDurations, ...computedOptions }: IAnimationOptions = listofComputedOptions[i]; - let animationKeyframe: TypeKeyFrameOptionsType; - let computedKeyframes: Keyframe[] | PropertyIndexedKeyframes; + animation.playbackRate = speed; - // You cannot use the `padEndDelay` option and set a value for `endDelay`, the `endDelay` value will - // replace the padded endDelay + // Support for on finish + animation.onfinish = () => { + typeof onfinish == "function" && onfinish.call(this, target, i, len, animation); + }; - // This ensures all `animations` match up to the total duration, and don't finish too early, - // if animations finish too early, when the `.play()` method is called, some animations - // that are finished will restart, while the rest will continue playing. - // This is mostly for progress control, but depending on your usage may truly benefit you - if (padEndDelay && computedOptions.endDelay == 0 && Math.abs(+computedOptions.iterations) != Math.abs(Infinity)) { - computedOptions.endDelay = this.totalDuration - (tempDurations as number); - } + // // Support for on cancel + animation.oncancel = () => { + typeof oncancel == "function" && oncancel.call(this, target, i, len, animation); + }; - // Accept keyframes as a keyframes Object, or a method, - // if there are no animations in the keyframes array, - // uses css properties from the options object - let arrKeyframes = computeOption(keyframes, [i, len, target], this) as (Keyframe[] | TypeCSSLikeKeyframe); - if (typeof arrKeyframes == "object") - arrKeyframes = KeyframeParse(arrKeyframes); - animationKeyframe = isValid(arrKeyframes) ? arrKeyframes as Keyframe[] : (properties as PropertyIndexedKeyframes); - - // If keyframes isn't defined as an animation option, use CSS properties as keyframes - if (!Array.isArray(animationKeyframe)) { - let { offset, ...CSSProperties } = mapAnimationOptions(animationKeyframe as IAnimationOptions, [i, len, target], this); - let $offset = offset as (string | number)[]; - - // transform, is often used so, to make them easier to use we parse them for strings, number, and/or arrays of both; - // for transform we parse the translate, skew, scale, and perspective functions (including all their varients) as CSS properties; - // it then turns these properties into valid `PropertyIndexedKeyframes` - // Read the documentation for `ParseTransformableCSSProperties` - CSSProperties = ParseTransformableCSSProperties(CSSProperties as ICSSComputedTransformableProperties); - computedKeyframes = Object.assign({}, - // Remove the offset effect option if it's not valid - isValid($offset) ? { offset: $offset.map(parseOffset) } : null, - CSSProperties - ) as PropertyIndexedKeyframes; - } else { - // Validate keyframe effect options - computedKeyframes = animationKeyframe.map((keyframe: Keyframe & { loop: boolean | number }) => { - let { speed, loop, easing, offset, ...rest } = keyframe; // Remove `speed`, it's not a valid CSS property - - return { - easing: typeof easing == "string" ? GetEase(easing) : easing, - iterations: (loop as boolean) === true ? Infinity : (loop as number), - offset: parseOffset(offset), - ...rest - }; - }); - - // Transform transformable CSS properties in each keyframe of the keyframe array - computedKeyframes = ParseTransformableCSSKeyframes(computedKeyframes) as Keyframe[]; - } + // Set the calculated options & keyframes for each individual animation + this.computedOptions.set(target, computedOptions); + this.computedKeyframes.set(target, computedKeyframes); + }); + } - let animation: Animation, keyFrameEffect: KeyframeEffect; - if (!this.animations.has(target)) { - // Create animation and add it to the Animations Set - keyFrameEffect = new KeyframeEffect(target, computedKeyframes, computedOptions as KeyframeAnimationOptions); - animation = new Animation(keyFrameEffect, computedOptions.timeline); - - this.animations.set(target, animation); - this.keyframeEffects.set(animation, keyFrameEffect); - } else { - // Update the animation - animation = this.getAnimation(target); - keyFrameEffect = this.getKeyframeEffect(animation); - - keyFrameEffect?.setKeyframes?.(computedKeyframes); - keyFrameEffect?.updateTiming?.(computedOptions as KeyframeAnimationOptions); - } + /** + * Update the options for all targets + * + * _**Note**: `KeyframeEffect` support is really low, so, I am suggest that you avoid using the `updateOptions` method, until browser support for `KeyframeEffect.updateTiming(...)` and `KeyframeEffefct.setKeyframes(...)` is better_ + * + * @beta + */ + public updateOptions(options: IAnimationOptions = {}) { + try { + let optionsFromParam = parseOptions(options); + this.options = Object.assign({}, DefaultAnimationOptions, this.options, optionsFromParam); - animation.playbackRate = $speed as number; + // This removes all none CSS properties from `properties` + let sharedTimingKeys = ["easing", "loop", "endDelay", "duration", "speed", "delay", "timelineOffset", "direction", "extend", "fillMode", "offset"]; + let { + // These values cannot be functions + padEndDelay, + onfinish, + oncancel, + autoplay, + target, + targets, - // Support for on finish - animation.onfinish = () => { - typeof onfinish == "function" && onfinish.call(this, target, i, len, animation); - }; + /** + * Theses are the CSS properties to be animated as Keyframes + */ + ...oldCSSProperties + } = omit(sharedTimingKeys, this.options); + + // This removes all none CSS properties from `optionsFromParam` + this.properties = omit([...sharedTimingKeys, "keyframes", "padEndDelay", "onfinish", "oncancel", "autoplay", "target", "targets"], optionsFromParam); + + // Avoid duplicate elements + let oldTargets = this.targets.values(); + let targetSet = [...new Set([...oldTargets, ...getTargets(targets), ...getTargets(target)])]; + this.targets.clear(); + targetSet.forEach((value, i) => { + this.targets.set(i, value); + this.targetIndexes.set(value, i); + }); - // Support for on cancel - animation.oncancel = () => { - typeof oncancel == "function" && oncancel.call(this, target, i, len, animation); - }; + let len = this.targets.size; + let arrOfComputedOptions = this.createArrayOfComputedOptions(optionsFromParam, len); + this.createAnimations({ + arrOfComputedOptions, + padEndDelay, + oldCSSProperties, + onfinish, + oncancel + }, len); - // Set the calculated options & keyframes for each individual animation - this.computedOptions.set(animation, computedOptions); - this.computedKeyframes.set(animation, computedKeyframes); - }); + this.maxSpeed = this.maxSpeed ?? this.options.speed as number; + this.minDelay = this.minDelay ?? this.options.delay as number; + this.totalDuration = this.totalDuration ?? this.options.duration as number; if (!this.mainAnimation) { this.mainkeyframeEffect = new KeyframeEffect(this.mainElement, [ @@ -1765,33 +915,35 @@ export class Animate { }); this.mainAnimation = new Animation(this.mainkeyframeEffect, this.options.timeline); - this.mainAnimation.onfinish = () => { + } else { + this.mainkeyframeEffect?.updateTiming?.({ + duration: this.totalDuration + }); + + if (!this.mainkeyframeEffect.setKeyframes || !this.mainkeyframeEffect.updateTiming) + console.warn("@okikio/animate - `KeyframeEffect.setKeyframes` and/or `KeyframeEffect.updateTiming` are not supported in this browser."); + } + + this.mainAnimation.playbackRate = this.maxSpeed; + this.mainAnimation.onfinish = () => { + this.emit("finish", this); + if (this.mainAnimation) { let playstate = this.getPlayState(); - this.emit("finish", playstate, this); if (!this.is(playstate)) this.emit("playstate-change", playstate, this); this.stopLoop(); - }; + } + }; - this.mainAnimation.oncancel = () => { + this.mainAnimation.oncancel = () => { + this.emit("cancel", this); + if (this.mainAnimation) { let playstate = this.getPlayState(); - this.emit("cancel", playstate, this); if (!this.is(playstate)) this.emit("playstate-change", playstate, this); this.stopLoop(); - }; - } else if (oldTotalDuration !== this.totalDuration) { - this.mainkeyframeEffect?.updateTiming?.({ - duration: this.totalDuration - }); - - if (!this.mainkeyframeEffect.setKeyframes || !this.mainkeyframeEffect.updateTiming) - console.error("@okikio/animate - `KeyframeEffect.setKeyframes` and/or `KeyframeEffect.updateTiming` are not supported in this browser."); - } - - this.minDelay = Math.min(...delayArr); - this.maxSpeed = Math.min(...speedArr);; - this.mainAnimation.playbackRate = this.maxSpeed; + } + }; if (autoplay) { // By the time events are registered the animation would have started and there wouldn't have be a `begin` event listener to actually emit @@ -1804,13 +956,78 @@ export class Animate { this.play(); } else this.pause(); - - this.newPromise(); } catch (err) { this.emit("error", err); } } + /** + * Adds a target to the Animate instance, and update the animation options with the change + * + * _**Note**: `KeyframeEffect` support is really low, so, I am suggest that you avoid using the `add` method, until browser support for `KeyframeEffect.updateTiming(...)` and `KeyframeEffefct.setKeyframes(...)` is better_ + * + * @beta + */ + public add(target: HTMLElement) { + let progress = this.getProgress(); + let running = this.is("running"); + let paused = this.is("paused"); + + this.updateOptions({ target }); + this.setProgress(progress); + + if (running) this.play(); + else if (paused) this.pause(); + } + + /** + * Removes a target from an Animate instance + * + * _**Note**: it doesn't update the current running options, you need to use the `Animate.prototype.remove(...)` method if you want to also update the running options_ + */ + public removeTarget(target: HTMLElement) { + let keyframeEffect = this.keyframeEffects.get(target); + this.animations.delete(keyframeEffect); + keyframeEffect = null; + + this.computedKeyframes.delete(target); + this.computedOptions.delete(target); + this.keyframeEffects.delete(target); + + let index = this.targetIndexes.get(target); + this.targets.delete(index); + this.targetIndexes.delete(target); + } + + /** + * Removes a target from an Animate instance, and update the animation options with the change + * + * _**Note**: `KeyframeEffect` support is really low, so, I am suggest that you avoid using the `remove` method, until browser support for `KeyframeEffect.updateTiming(...)` and `KeyframeEffefct.setKeyframes(...)` is better_ + * + * @beta + */ + public remove(target: HTMLElement) { + this.removeTarget(target); + + let targetSet = new Set([].concat(this.targets.values())); + + this.options.target = [...targetSet]; + this.options.targets = []; + targetSet.clear(); + targetSet = null; + + let progress = this.getProgress(); + let running = this.is("running"); + let paused = this.is("paused"); + + this.updateOptions(); + + if (running) this.play(); + else if (paused) this.pause(); + + this.setProgress(progress); + } + /** * Adds a listener for a given event */ @@ -1837,7 +1054,7 @@ export class Animate { /** Returns the Animate options, as JSON */ public toJSON(): IAnimationOptions { - return this.getOptions(); + return this.options; } /** @@ -1939,5 +1156,4 @@ export const animate = (options: IAnimationOptions = {}): Animate => { return new Animate(options); }; -export * from "./builtin-effects"; -export default animate; +// export default animate;