Skip to content

Commit ce02edb

Browse files
committed
2344234
1 parent 65ab970 commit ce02edb

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
🔸Matches client's monitor refresh rate.
88

9-
🔸Contains most popular easing timing functions.
9+
🔸Contains the most popular easing timing functions.
1010

11-
### 📖 [View the documentation site](https://alabsi91.github.io/animare/)
11+
### 📖 [View the documentation site](https://alabsi91.github.io/animare/) 📖

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "animare",
3-
"version": "0.0.0-beta.0",
3+
"version": "0.0.0-beta.2",
44
"description": "Light animation library based on requestanimationframe ",
55
"author": "Ahmed Alabsi <alabsi91@gmail.com",
66
"license": "MIT",

src/animare.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { colorToArr } from './methods/colorToRgbArray';
2+
export { organize } from './methods/organize';
23
export { ease } from './methods/ease';
34

45
export { animare } from './methods/animare';
56
import animare from './methods/animare';
6-
export default animare;
7+
export default animare;

src/methods/animare.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { animareOptions, cbInfo, DIRECTION, Ilisteners, returnedObject, TIMELINE_TYPE } from './types';
1+
import { animareOnUpdate, animareOptions, cbInfo, DIRECTION, Ilisteners, returnedObject, TIMELINE_TYPE } from './types';
22

3-
// todo fix value's default when waiting for delay
3+
///// todo fix value's default when waiting for delay
44
// todo react useAnimare hook
55
// todo play , reverse and stop at progress
66

7-
export function animare(options: animareOptions, callback: (values: number[], info: cbInfo) => void) {
7+
export function animare(options: animareOptions, callback: animareOnUpdate) {
88
if (typeof options !== 'object' || Array.isArray(options)) throw new Error('animare: expects an object as the first argument.');
99
options.to = Array.isArray(options.to) ? options.to : [options.to];
1010
const userInput = { ...options };
@@ -104,7 +104,7 @@ export function animare(options: animareOptions, callback: (values: number[], in
104104
// finished animations indexes will be stored here.
105105
const finished = new Set<number>();
106106
// used to fill the animation value when it's finished and other animations still playing to keep the passed array of values at the same length
107-
const lastKnownValue = [...options.to];
107+
const lastKnownValue: number[] = [];
108108

109109
const timeline = [
110110
{

src/methods/organize.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { DIRECTION } from "./types";
2+
3+
type organizeOptions = {
4+
from?: number;
5+
to: number;
6+
duration?: number;
7+
delay?: number;
8+
delayOnce?: boolean;
9+
repeat?: number;
10+
direction?: keyof typeof DIRECTION;
11+
ease?: (x: number) => number;
12+
};
13+
14+
/**
15+
* - A helper function to organize the options for the animation.
16+
* @param ob - options: `{name: {from: number, to: number, ...}, ...}`
17+
* @param defaults - changes the default values for each option.
18+
*/
19+
export function organize<T extends { [key in keyof T]: organizeOptions }>(ob: T, defaults: Omit<organizeOptions, 'to'> = {}) {
20+
defaults.from ??= 0;
21+
defaults.duration ??= 350;
22+
defaults.delay ??= 0;
23+
defaults.delayOnce ??= false;
24+
defaults.repeat ??= 0;
25+
defaults.direction ??= 'normal';
26+
defaults.ease ??= x => x;
27+
28+
const ent = Object.entries<organizeOptions>(ob),
29+
from = ent.map(e => e[1].from ?? defaults.from),
30+
to = ent.map(e => e[1].to),
31+
duration = ent.map(e => e[1].duration ?? defaults.duration),
32+
delay = ent.map(e => e[1].delay ?? defaults.delay),
33+
delayOnce = ent.map(e => e[1].delayOnce ?? defaults.delayOnce),
34+
repeat = ent.map(e => e[1].repeat ?? defaults.repeat),
35+
direction = ent.map(e => e[1].direction ?? defaults.direction),
36+
ease = ent.map(e => e[1].ease ?? defaults.ease),
37+
names = Object.keys(ob) as (keyof T)[];
38+
39+
const get = (animareArray: number[]) => {
40+
const res: Record<keyof T, number> = Object.create(null);
41+
for (let i = 0; i < animareArray.length; i++) res[names[i]] = animareArray[i];
42+
return res;
43+
};
44+
45+
const patch = (patchOb?: Partial<T>) => organize(Object.assign({}, ob, patchOb));
46+
47+
return {
48+
from,
49+
to,
50+
duration,
51+
delay,
52+
delayOnce,
53+
repeat,
54+
direction,
55+
ease,
56+
/**
57+
* - Takes an array of numbers and retruns an object
58+
* - with the same keys as the original object, but with the values of the array.
59+
*/
60+
get,
61+
/** - Creates a new copy with patched options.*/
62+
patch,
63+
};
64+
}

src/methods/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,5 @@ export interface Ilisteners {
288288
onFinish: { id: string; cb: Function }[];
289289
onProgress: { at: number | string; id: string; cb: Function }[];
290290
}
291+
292+
export type animareOnUpdate = (values: number[], info: cbInfo) => void;

0 commit comments

Comments
 (0)