Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

rendering

Back

Expression code and comments

var s = 1.70158; ///< transcendental quantity used for "return"

/// @note return function
function outBack(t, b, c, d) {
    if (s == null)
        s = 1.70158;
    return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
}

/// @note actually calls the lazy function of the elastic function
function easeAndWizz() {

    var n = 0;
    
    /// @note ensures that the key frame number is greater than zero
    if (numKeys > 0) {
        n = nearestKey(time).index; ///< get the latest keyframe index
        if (key(n).time > time) { ///< if the last keyframe is after the current time (i.e., the time is not up yet)
            n--; ///< takes the index of the previous key frame}}try {
        /// @note two keyframes before and after
        var key1 = key(n);
        var key2 = key(n + 1);
    } catch (e) {
        return null;
    }

    /// @note determines the data dimension required by the keyframe
    var dim = 1; ///< This attribute is at least one-dimensional
    try {
        key(1) [1]; ///< data has a second dimension
        dim = 2;
        key(1) [2]; ///< data has a third dimension
        dim = 3;
    } catch (e) { }
    t = time - key1.time; ///< the time difference between the current time and the previous keyframe
    d = key2.time - key1.time; ///< the time difference between two key frames
    
    /// @note computes the attributes on the keyframe for later elastic calculation
    / / / d
    sX = key1[0];
    eX = key2[0] - key1[0];
    / / / @ note 2 d
    if (dim >= 2) {
        sY = key1[1];
        eY = key2[1] - key1[1];
        / / / @ note 3 d
        if (dim >= 3) {
            sZ = key1[2];
            eZ = key2[2] - key1[2]; }}if ((time < key1.time) || (time > key2.time)) {
        return value;
    } else {
        /// @note does the homing calculation
        val1 = outBack(t, sX, eX, d);
        
        /// @note is also divided into three dimensions for calculation
        switch (dim) {
            case 1:
                return val1;
                break;
            case 2:
                val2 = outBack(t, sY, eY, d);
                return [val1, val2];
                break;
            case 3:
                val2 = outBack(t, sY, eY, d);
                val3 = outBack(t, sZ, eZ, d);
                return [val1, val2, val3];
                break;
            default:
                return null;
        }
    }
}
(easeAndWizz() || value);
Copy the code