2018-03-02 20:42:12 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-03-02 20:42:12 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var Curve = require('./Curve');
|
2018-03-05 01:15:18 +00:00
|
|
|
var QuadraticBezierInterpolation = require('../math/interpolation/QuadraticBezierInterpolation');
|
2018-03-02 20:42:12 +00:00
|
|
|
var Vector2 = require('../math/Vector2');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class QuadraticBezier
|
|
|
|
* @extends Phaser.Curves.Curve
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Curves
|
2018-03-02 20:42:12 +00:00
|
|
|
* @constructor
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @param {(Phaser.Math.Vector2|number[])} p0 - Start point, or an array of point pairs.
|
2018-03-02 20:42:12 +00:00
|
|
|
* @param {Phaser.Math.Vector2} p1 - Control Point 1.
|
|
|
|
* @param {Phaser.Math.Vector2} p2 - Control Point 2.
|
|
|
|
*/
|
|
|
|
var QuadraticBezier = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function QuadraticBezier (p0, p1, p2)
|
|
|
|
{
|
|
|
|
Curve.call(this, 'QuadraticBezier');
|
|
|
|
|
|
|
|
if (Array.isArray(p0))
|
|
|
|
{
|
|
|
|
p2 = new Vector2(p0[4], p0[5]);
|
|
|
|
p1 = new Vector2(p0[2], p0[3]);
|
|
|
|
p0 = new Vector2(p0[0], p0[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Curves.QuadraticBezier#p0
|
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
this.p0 = p0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Curves.QuadraticBezier#p1
|
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
this.p1 = p1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Curves.QuadraticBezier#p2
|
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
this.p2 = p2;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Gets the starting point on the curve.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.QuadraticBezier#getStartPoint
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @generic {Phaser.Math.Vector2} O - [out,$return]
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
getStartPoint: function (out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
return out.copy(this.p0);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.QuadraticBezier#getResolution
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-05 01:15:18 +00:00
|
|
|
* @param {number} divisions - [description]
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-05 01:15:18 +00:00
|
|
|
* @return {number} [description]
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Get point at relative position in curve according to length.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.QuadraticBezier#getPoint
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @generic {Phaser.Math.Vector2} O - [out,$return]
|
|
|
|
*
|
2018-06-26 22:19:14 +00:00
|
|
|
* @param {number} t - The position along the curve to return. Where 0 is the start and 1 is the end.
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
getPoint: function (t, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
var p0 = this.p0;
|
|
|
|
var p1 = this.p1;
|
|
|
|
var p2 = this.p2;
|
|
|
|
|
2018-03-05 01:15:18 +00:00
|
|
|
return out.set(
|
|
|
|
QuadraticBezierInterpolation(t, p0.x, p1.x, p2.x),
|
|
|
|
QuadraticBezierInterpolation(t, p0.y, p1.y, p2.y)
|
|
|
|
);
|
2018-03-02 20:42:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.QuadraticBezier#draw
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-03-27 12:06:24 +00:00
|
|
|
* @generic {Phaser.GameObjects.Graphics} G - [graphics,$return]
|
|
|
|
*
|
2018-09-24 22:20:43 +00:00
|
|
|
* @param {Phaser.GameObjects.Graphics} graphics - `Graphics` object to draw onto.
|
|
|
|
* @param {integer} [pointsTotal=32] - Number of points to be used for drawing the curve. Higher numbers result in smoother curve but require more processing.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2018-09-24 22:20:43 +00:00
|
|
|
* @return {Phaser.GameObjects.Graphics} `Graphics` object that was drawn to.
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
draw: function (graphics, pointsTotal)
|
|
|
|
{
|
|
|
|
if (pointsTotal === undefined) { pointsTotal = 32; }
|
|
|
|
|
|
|
|
var points = this.getPoints(pointsTotal);
|
|
|
|
|
|
|
|
graphics.beginPath();
|
|
|
|
graphics.moveTo(this.p0.x, this.p0.y);
|
|
|
|
|
|
|
|
for (var i = 1; i < points.length; i++)
|
|
|
|
{
|
|
|
|
graphics.lineTo(points[i].x, points[i].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
graphics.strokePath();
|
|
|
|
|
|
|
|
// So you can chain graphics calls
|
|
|
|
return graphics;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-09-24 22:20:43 +00:00
|
|
|
* Converts the curve into a JSON compatible object.
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.QuadraticBezier#toJSON
|
2018-03-05 01:15:18 +00:00
|
|
|
* @since 3.2.0
|
2018-03-02 20:42:12 +00:00
|
|
|
*
|
2019-05-09 10:52:07 +00:00
|
|
|
* @return {Phaser.Types.Curves.JSONCurve} The JSON object containing this curve data.
|
2018-03-02 20:42:12 +00:00
|
|
|
*/
|
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
type: this.type,
|
|
|
|
points: [
|
|
|
|
this.p0.x, this.p0.y,
|
|
|
|
this.p1.x, this.p1.y,
|
|
|
|
this.p2.x, this.p2.y
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-03-18 13:43:37 +00:00
|
|
|
/**
|
2018-09-24 22:20:43 +00:00
|
|
|
* Creates a curve from a JSON object, e. g. created by `toJSON`.
|
2018-03-18 13:43:37 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Curves.QuadraticBezier.fromJSON
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
2019-05-09 10:52:07 +00:00
|
|
|
* @param {Phaser.Types.Curves.JSONCurve} data - The JSON object containing this curve data.
|
2018-03-18 13:43:37 +00:00
|
|
|
*
|
2018-09-24 22:20:43 +00:00
|
|
|
* @return {Phaser.Curves.QuadraticBezier} The created curve instance.
|
2018-03-18 13:43:37 +00:00
|
|
|
*/
|
2018-03-02 20:42:12 +00:00
|
|
|
QuadraticBezier.fromJSON = function (data)
|
|
|
|
{
|
|
|
|
var points = data.points;
|
|
|
|
|
|
|
|
var p0 = new Vector2(points[0], points[1]);
|
|
|
|
var p1 = new Vector2(points[2], points[3]);
|
|
|
|
var p2 = new Vector2(points[4], points[5]);
|
|
|
|
|
|
|
|
return new QuadraticBezier(p0, p1, p2);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = QuadraticBezier;
|