all files / montage/core/geometry/ cubic-bezier.js

100% Statements 31/31
80% Branches 8/10
100% Functions 5/5
100% Lines 31/31
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                                                                                                                                                                                                                                                                                 
/**
 * @module montage/core/geometry/cubic-bezier
 * @requires montage/core/core
 * @requires montage/core/geometry/point
 */
var Montage = require("../core").Montage;
var Point = require("./point").Point;
 
/**
 * @class CubicBezier
 * @extends Montage
 */
var CubicBezier = exports.CubicBezier = Montage.specialize( /** @lends CubicBezier# */{
 
    /**
     * @function
     * @param {Array} controlPoints Control points.
     * @returns itself
     */
    init: {
        enumerable: false,
        value: function (controlPoints) {
            Eif (controlPoints !== null) {
                if (controlPoints.length === 2) {
                    this.p1 = controlPoints[0];
                    this.p2 = controlPoints[1];
                } else Eif (controlPoints.length === 4) {
                    this.p0 = controlPoints[0];
                    this.p1 = controlPoints[1];
                    this.p2 = controlPoints[2];
                    this.p3 = controlPoints[3];
                }
            }
            return this;
        }
    },
 
    /**
     * @function
     * @param {number} t Control point.
     * @returns itself or `new Point().init(this.p0.x * b1 + this.p1.x * b2 +
     * this.p2.x * b3 + this.p3.x * b4, this.p0.y * b1 + this.p1.y * b2 +
     * this.p2.y * b3 + this.p3.y * b4)`
    */
    position: {
        enumerable: false,
        value: function (t) {
            if (t < 0 || t > 1) {
                return;
            }
 
            t = 1 - t;
 
            var b1 = t * t * t,
                b2 = 3 * t * t * (1 - t),
                b3 = 3 * t * (1 - t) * (1 - t),
                b4 = (1 - t) * (1 - t) * (1 - t);
            return new Point().init(this.p0.x * b1 + this.p1.x * b2 + this.p2.x * b3 + this.p3.x * b4,
                this.p0.y * b1 + this.p1.y * b2 + this.p2.y * b3 + this.p3.y * b4);
        }
    },
 
    /**
     * @function
     * @param {number} t Control point.
     * @returns CubicBezier.create(CubicBezier).init([this.p0, this.p01, this.p012, this.p0123])
     */
    split: {
        enumerable: false,
        value: function (t) {
            this.makeScaffolding(t);
            return (new CubicBezier()).init([this.p0, this.p01, this.p012, this.p0123]);
        }
    },
 
    /**
     * @function
     * @param {number} t Control point.
     * @returns `CubicBezier.create(CubicBezier).init([new
     * Point().init(this.p01.x / xScale, this.p01.y / yScale), new
     * Point().init(this.p012.x / xScale, this.p012.y / yScale)])`
     */
    splitToTimingFunction: {
        enumerable: false,
        value: function (t) {
            this.makeScaffolding(t);
            // p0123 x and y are the scale
            var xScale = this.p0123.x,
                yScale = this.p0123.y;
            return (new CubicBezier()).init([new Point().init(this.p01.x / xScale, this.p01.y / yScale), new Point().init(this.p012.x / xScale, this.p012.y / yScale)]);
        }
    },
 
    /**
     * @function
     * @param {number} t Control point.
     */
    makeScaffolding: {
        enumerable: false,
        value: function (t) {
 
            t = 1 - t;
 
            var precision = 1000000;
            Montage.defineProperty(this, 'p01', {
                value: Point.interpolate(t, this.p0, this.p1, precision)
            });
            Montage.defineProperty(this, 'p12', {
                value: Point.interpolate(t, this.p1, this.p2, precision)
            });
            Montage.defineProperty(this, 'p23', {
                value: Point.interpolate(t, this.p2, this.p3, precision)
            });
            Montage.defineProperty(this, 'p012', {
                value: Point.interpolate(t, this.p01, this.p12, precision)
            });
            Montage.defineProperty(this, 'p123', {
                value: Point.interpolate(t, this.p12, this.p23, precision)
            });
            Montage.defineProperty(this, 'p0123', {
                value: Point.interpolate(t, this.p012, this.p123, precision)
            });
        }
    },
 
    /**
     * First control point in bezier curve.
     * @type {Property}
     * @default {number} new Point().init(0, 0)
     */
    p0: {
        enumerable: true,
        value: new Point().init(0, 0)
    },
 
    /**
     * Second control point in bezier curve.
     * @type {Property}
     * @default {number} new Point().init(0, 0)
     */
    p1: {
        enumerable: true,
        value: new Point().init(0, 0)
    },
 
    /**
     * Third control point in bezier curve.
     * @type {Property}
     * @default {number} new Point().init(1, 1)
     */
    p2: {
        enumerable: true,
        value: new Point().init(1, 1)
    },
 
    /**
     * Fourth control point in bezier curve.
     * @type {Property}
     * @default {number}M ontage.create(Point).init(1, 1)
     */
    p3: {
        enumerable: true,
        value: new Point().init(1, 1)
    }
 
});