aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/trails/SlideEffect.tsx
blob: 89abdd12dab39b5cc88585800c0e7ce239d7132a (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { animated, to, useInView, useSpring } from '@react-spring/web';
import React, { useEffect } from 'react';
import { Doc } from '../../../../fields/Doc';
import { NumCast } from '../../../../fields/Types';
import { PresEffect, PresEffectDirection } from './PresEnums';
import './SlideEffect.scss';
import { emptyFunction } from '../../../../Utils';

interface SlideEffectProps {
    doc?: Doc; // pass in doc to extract width, height, bg
    dir: PresEffectDirection;
    presEffect: PresEffect;
    springSettings: {
        stiffness: number;
        damping: number;
        mass: number;
    };
    children: React.ReactNode;
    infinite?: boolean;
    startOpacity?: number; // set to zero to linearly fade in while animating
}

const DEFAULT_WIDTH = 40;
const PREVIEW_OFFSET = 60;
const ACTUAL_OFFSET = 200;

/**
 * This component wraps around the doc to create an effect animation, and also wraps the preview animations
 * for the effects as well.
 */
export default function SpringAnimation({ doc, dir, springSettings, presEffect, children, infinite, startOpacity }: SlideEffectProps) {
    const expandConfig = {
        to: { scale: 1, x: 0, y: 0 },
        from: { scale: 0, x: 0, y: 0 },
    };
    const fadeConfig = {
        to: { x: 0, y: 0 },
        from: { x: 0, y: 0 },
    };
    const rotateConfig = {
        to: { x: 360, y: 0 },
        from: { x: 0, y: 0 },
    };
    const flipConfig = {
        to: { x: 180, y: 0 },
        from: { x: 0, y: 0 },
    };
    const bounceConfig = {
        to: { x: 0, y: 0 },
        from: (() => {
            const offset = infinite ? PREVIEW_OFFSET : ACTUAL_OFFSET;
            switch (dir) {
                case PresEffectDirection.Left:  return { x: -offset, y:       0, };
                case PresEffectDirection.Right: return { x:  offset, y:       0, };
                case PresEffectDirection.Top:   return { x:       0, y: -offset, };
                case PresEffectDirection.Bottom:return { x:       0, y:  offset, };
                default:                        return { x:       0, y:       0, }; // no movement for center
            }})(), // prettier-ignore
    };
    const rollConfig = {
        to: { x: 0, y: 0 },
        from: (() => {
            switch (dir) {
                case PresEffectDirection.Left:   return { x: -100, y: -120, };
                case PresEffectDirection.Right:  return { x:  100, y:  120, };
                case PresEffectDirection.Top:    return { x: -100, y: -120, };
                case PresEffectDirection.Bottom: return { x: -100, y: -120, };
                default:                         return { x:    0, y:    0, };  // no movement for center
            }})(), // prettier-ignore
    };

    // prettier-ignore
    const effectConfig = (() => {
        switch (presEffect) {
            case PresEffect.Fade:       return fadeConfig;
            case PresEffect.Bounce:     return bounceConfig;
            case PresEffect.Rotate:     return rotateConfig;
            case PresEffect.Flip:       return flipConfig;
            case PresEffect.Roll:       return rollConfig;
            case PresEffect.Lightspeed: return { from: {}, to: {} };
            case PresEffect.Expand:     
            default:                    return expandConfig;
        } // prettier-ignore
    })();

    const [springs, api] = useSpring(
        () => ({
            to: { ...effectConfig.to, opacity: 1 },
            from: { ...effectConfig.from, opacity: startOpacity ?? 1 },
            config: { tension: springSettings.stiffness, friction: springSettings.damping, mass: springSettings.mass },
            onStart: emptyFunction,
            onRest: emptyFunction,
        }),
        [springSettings]
    );

    const [ref, inView] = useInView({
        once: true,
    });
    useEffect(() => {
        if (inView) {
            api.start({ loop: infinite, delay: infinite ? 500 : 0 });
        }
    }, [inView]);
    const animatedDiv = (style: object) => (
        <animated.div ref={ref} style={{ ...style, opacity: to(springs.opacity, val => `${val}`) }}>
            {children}
        </animated.div>
    );
    const [width, height] = [NumCast(doc?.width, DEFAULT_WIDTH), NumCast(doc?.height, DEFAULT_WIDTH)];
    const flipAxis = dir === PresEffectDirection.Bottom || dir === PresEffectDirection.Top ? 'X' : 'Y';
    const [rotateX, rotateY] = flipAxis === 'X' ? ['180deg', undefined] : [undefined, '180deg'];
    switch (presEffect) {
        case PresEffect.Flip:  return animatedDiv({ transform: to(springs.x, val => `perspective(600px) rotate${flipAxis}(${val}deg)`), width, height, rotateX, rotateY })
        case PresEffect.Rotate:return animatedDiv({ transform: to(springs.x, val => `rotate(${val}deg)`) });
        case PresEffect.Roll:  return animatedDiv({ transform: to([springs.x, springs.y], (val, val2) => `translate3d(${val}%, 0, 0) rotate3d(0, 0, 1, ${val2}deg)`) });
        default:               return animatedDiv(springs);
    } // prettier-ignore
}