All files _animation.js

95.95% Statements 237/247
95.45% Branches 21/22
91.3% Functions 21/23
95.95% Lines 237/247

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 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 2701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x   1x 1x 1x 31x 31x 31x 15x 15x 15x 31x 31x 31x   1x 1x 1x       1x 1x 1x 1x 1x 1x   1x 1x 31x 31x 31x   1x 1x           1x 1x           1x 1x 3x 3x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x   1x 1x 2x 2x 1x  
/**
 * Opzioni per l'animazione.
 */
export class AnimationOption {
    /**
     * @param {number} duration - Durata dell'animazione in millisecondi.
     * @default 250
     *
     * @param {'linear'|'ease'|'ease-in'|'ease-out'|'ease-in-out'} easing - Funzione di easing dell'animazione.
     * @default 'easeInOut'
     *
     * @param {'none'|'forwards'|'backwards'|'both'|'auto'} fill - Stile applicato fuori dall'intervallo dell'animazione.
     * @default 'forwards'
     *
     * @param {number} delay - Ritardo dell'inizio dell'animazione in millisecondi.
     * @default 0
     *
     * @param {'replace'|'add'|'accumulate'} composite - Modalità di composizione dell’animazione.
     * @default 'replace'
     *
     * @param {'normal'|'reverse'|'alternate'|'alternate-reverse'} direction - Direzione di riproduzione dell'animazione.
     * @default 'normal'
     *
     * @param {number|string} iterations - Numero di volte che l'animazione verrà ripetuta.
     * @default 1
     */
    constructor(duration = 250, easing = "ease-in-out", fill = "forwards", delay = 0, composite = "replace", direction = "normal", iterations = 1) {
        this.duration = duration;
        this.easing = easing;
        this.fill = fill;
        this.delay = delay;
        this.composite = composite;
        this.direction = direction;
        this.iterations = iterations;
    }
}
 
export const keyframe = {
    fadeIn: [{ opacity: 0 }, { opacity: 1 }],
    fadeInDown: [
        { opacity: 0, transform: "translate3d(0, -100%, 0)" },
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
    ],
    fadeInUp: [
        { opacity: 0, transform: "translate3d(0, 100%, 0)" },
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
    ],
    fadeInLeft: [
        { opacity: 0, transform: "translate3d(-100%, 0, 0)" },
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
    ],
    fadeInRight: [
        { opacity: 0, transform: "translate3d(100%, 0, 0)" },
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
    ],
    fadeOut: [{ opacity: 1 }, { opacity: 0 }],
    fadeOutDown: [
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
        { opacity: 0, transform: "translate3d(0, 100%, 0)" },
    ],
    fadeOutUp: [
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
        { opacity: 0, transform: "translate3d(0, -100%, 0)" },
    ],
    fadeOutLeft: [
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
        { opacity: 0, transform: "translate3d(-100%, 0, 0)" },
    ],
    fadeOutRight: [
        { opacity: 1, transform: "translate3d(0, 0, 0)" },
        { opacity: 0, transform: "translate3d(100%, 0, 0)" },
    ],
    bounce: [
        { transform: "translateY(0)", easing: "ease" },
        { transform: "translateY(-30px)", easing: "ease-in" },
        { transform: "translateY(15px)", easing: "ease-out" },
        { transform: "translateY(-10px)", easing: "ease-in-out" },
        { transform: "translateY(0)", easing: "ease-out" },
    ],
    tada: [
        {
            transform: "scale(1) rotate(0) translateX(0)",
            transformOrigin: "center center",
            easing: "ease",
        },
        {
            transform: "scale(1.1) rotate(-3deg) translateX(-0.1%)",
            transformOrigin: "center center",
            easing: "ease-in-out",
        },
        {
            transform: "scale(1.1) rotate(3deg) translateX(-0.1%)",
            transformOrigin: "center center",
            easing: "ease-in-out",
        },
        {
            transform: "scale(1.1) rotate(-3deg) translateX(-0.1%)",
            transformOrigin: "center center",
            easing: "ease-in-out",
        },
        {
            transform: "scale(1.1) rotate(3deg) translateX(-0.1%)",
            transformOrigin: "center center",
            easing: "ease-in-out",
        },
        {
            transform: "scale(1) rotate(0) translateX(0)",
            transformOrigin: "center center",
            easing: "ease-out",
        },
    ],
    zoomIn: [
        {
            transform: "scale(0)",
            opacity: 0,
            transformOrigin: "center center",
            easing: "ease",
        },
        {
            transform: "scale(1)",
            opacity: 1,
            transformOrigin: "center center",
            easing: "ease-out",
        },
    ],
    zoomOut: [
        {
            transform: "scale(1)",
            opacity: 1,
            transformOrigin: "center center",
            easing: "ease-out",
        },
        {
            transform: "scale(0)",
            opacity: 0,
            transformOrigin: "center center",
            easing: "ease",
        },
    ],
    rotation: deg => [
        {
            transform: `rotate(${deg}deg)`,
            transformOrigin: "center center",
            easing: "linear",
        },
    ],
};
 
export class _animation {
    /** @this {Jdm} */
    static #animate(keyframe, option, callbackFn) {
        option = { ...new AnimationOption(), ...option };
        const animation = this.node.animate(keyframe, option);
        animation.onfinish = () => {
            if (typeof callbackFn === "function") {
                callbackFn();
            }
        };
        return animation;
    }
 
    /** @this {Jdm} */
    static jdm_clearAnimations() {
        if (typeof this.node.getAnimations === "function") {
            this.node.getAnimations().forEach(animation => animation.cancel());
        }
 
        this.node.style.animation = "none";
        this.node.style.transition = "none";
        this.node.style.transform = "";
        this.node.style.opacity = "";
        return this.node;
    }
 
    /** @this {Jdm} */
    static jdm_animation(keyframe, callbackFn, option = new AnimationOption()) {
        _animation.#animate.apply(this, [keyframe, option, callbackFn]);
        return this.node;
    }
 
    /** @this {Jdm} */
    static jdm_hide() {
        this.jdm_setStyle("visibility", "hide");
        this.jdm_setStyle("opacity", 0);
        return this.node;
    }
 
    /** @this {Jdm} */
    static jdm_show() {
        this.jdm_setStyle("visibility", "visible");
        this.jdm_setStyle("opacity", 1);
        return this.node;
    }
 
    /** @this {Jdm} */
    static jdm_fadeIn(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeIn, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeInDown(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeInDown, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeInUp(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeInUp, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeInLeft(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeInLeft, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeInRight(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeInRight, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeOut(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeOut, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeOutDown(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeOutDown, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeOutUp(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeOutUp, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeOutLeft(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeOutLeft, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_fadeOutRight(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.fadeOutRight, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_bounce(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.bounce, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_tada(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.tada, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_zoomIn(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.zoomIn, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_zoomOut(callbackFn, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.zoomOut, callbackFn, option]);
    }
 
    /** @this {Jdm} */
    static jdm_rotation(callbackFn, deg = 360, option = new AnimationOption()) {
        return _animation.jdm_animation.apply(this, [keyframe.rotation(deg), callbackFn, option]);
    }
}