All files / lib easing.js

94.11% Statements 16/17
90.9% Branches 10/11
75% Functions 3/4
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49    1x           1x         1x       1x 9x             9x   9x         9x 9x   9x 2x     9x 1x 1x   8x          
import between from 'between.js';
 
export const LOOPING = {
    BOUNCE: 'bounce',
    REPEAT: 'repeat',
    NONE: false
};
 
export const EASING_EVENTS = {
    UPDATE: 'update',
    COMPLETE: 'complete'
};
 
export const FUNCTIONS = {
    ...between.Easing
};
 
export const tweenTo = (origin, target, options = {}) => (
    new Promise((resolve) => {
        const {
            time,
            easing = FUNCTIONS.Linear.None,
            loop = LOOPING.NONE,
            onUpdate = f => f,
            repeat = undefined
        } = options;
 
        const tween = new between(origin, target)
            .time(time)
            .easing(easing)
            .on(EASING_EVENTS.UPDATE, onUpdate)
 
        const infinite = loop && !repeat;
        const onComplete = () => resolve(tween, infinite);
 
        if (loop) {
            tween.loop(loop, repeat);
        }
 
        if (infinite) {
            const timeToCompleteLoop = time * 2;
            setTimeout(onComplete, timeToCompleteLoop);
        } else {
            tween.on(EASING_EVENTS.COMPLETE, onComplete);
        }
    })
);