|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +var Class = require('../utils/Class'); |
| 9 | +var QuadraticBezierInter = require('../math/interpolation/QuadraticBezierInterpolation'); |
| 10 | +var Curve = require('./Curve'); |
| 11 | +var Vector2 = require('../math/Vector2'); |
| 12 | + |
| 13 | +/** |
| 14 | + * @classdesc |
| 15 | + * [description] |
| 16 | + * |
| 17 | + * @class QuadraticBezier |
| 18 | + * @extends Phaser.Curves.Curve |
| 19 | + * @memberOf Phaser.Curves |
| 20 | + * @constructor |
| 21 | + * @since 3.0.0 |
| 22 | + * |
| 23 | + * @param {Phaser.Math.Vector2|Phaser.Math.Vector2[]} p0 - Start point, or an array of point pairs. |
| 24 | + * @param {Phaser.Math.Vector2} p1 - Control Point 1. |
| 25 | + * @param {Phaser.Math.Vector2} p2 - Control Point 2. |
| 26 | + * @param {Phaser.Math.Vector2} p3 - End Point. |
| 27 | + */ |
| 28 | +var QuadraticBezier = new Class({ |
| 29 | + |
| 30 | + Extends: Curve, |
| 31 | + |
| 32 | + initialize: |
| 33 | + |
| 34 | + function QuadraticBezier (p0, p1, p2) |
| 35 | + { |
| 36 | + Curve.call(this, 'QuadraticBezier'); |
| 37 | + |
| 38 | + if (Array.isArray(p0)) |
| 39 | + { |
| 40 | + p2 = new Vector2(p0[4], p0[5]); |
| 41 | + p1 = new Vector2(p0[2], p0[3]); |
| 42 | + p0 = new Vector2(p0[0], p0[1]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * [description] |
| 47 | + * |
| 48 | + * @name Phaser.Curves.QuadraticBezier#p0 |
| 49 | + * @type {Phaser.Math.Vector2} |
| 50 | + * @since 3.0.0 |
| 51 | + */ |
| 52 | + this.p0 = p0; |
| 53 | + |
| 54 | + /** |
| 55 | + * [description] |
| 56 | + * |
| 57 | + * @name Phaser.Curves.QuadraticBezier#p1 |
| 58 | + * @type {Phaser.Math.Vector2} |
| 59 | + * @since 3.0.0 |
| 60 | + */ |
| 61 | + this.p1 = p1; |
| 62 | + |
| 63 | + /** |
| 64 | + * [description] |
| 65 | + * |
| 66 | + * @name Phaser.Curves.QuadraticBezier#p2 |
| 67 | + * @type {Phaser.Math.Vector2} |
| 68 | + * @since 3.0.0 |
| 69 | + */ |
| 70 | + this.p2 = p2; |
| 71 | + }, |
| 72 | + |
| 73 | + /** |
| 74 | + * [description] |
| 75 | + * |
| 76 | + * @method Phaser.Curves.QuadraticBezier#getStartPoint |
| 77 | + * @since 3.0.0 |
| 78 | + * |
| 79 | + * @param {Phaser.Math.Vector2} out - [description] |
| 80 | + * |
| 81 | + * @return {Phaser.Math.Vector2} [description] |
| 82 | + */ |
| 83 | + getStartPoint: function (out) |
| 84 | + { |
| 85 | + if (out === undefined) { out = new Vector2(); } |
| 86 | + |
| 87 | + return out.copy(this.p0); |
| 88 | + }, |
| 89 | + |
| 90 | + /** |
| 91 | + * [description] |
| 92 | + * |
| 93 | + * @method Phaser.Curves.QuadraticBezier#getResolution |
| 94 | + * @since 3.0.0 |
| 95 | + * |
| 96 | + * @param {[type]} divisions - [description] |
| 97 | + * |
| 98 | + * @return {[type]} [description] |
| 99 | + */ |
| 100 | + getResolution: function (divisions) |
| 101 | + { |
| 102 | + return divisions; |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * [description] |
| 107 | + * |
| 108 | + * @method Phaser.Curves.QuadraticBezier#getPoint |
| 109 | + * @since 3.0.0 |
| 110 | + * |
| 111 | + * @param {[type]} t - [description] |
| 112 | + * @param {[type]} out - [description] |
| 113 | + * |
| 114 | + * @return {[type]} [description] |
| 115 | + */ |
| 116 | + getPoint: function (t, out) |
| 117 | + { |
| 118 | + if (out === undefined) { out = new Vector2(); } |
| 119 | + |
| 120 | + var p0 = this.p0; |
| 121 | + var p1 = this.p1; |
| 122 | + var p2 = this.p2; |
| 123 | + |
| 124 | + return out.set(QuadraticBezierInter(t, p0.x, p1.x, p2.x), QuadraticBezierInter(t, p0.y, p1.y, p2.y)); |
| 125 | + }, |
| 126 | + |
| 127 | + /** |
| 128 | + * [description] |
| 129 | + * |
| 130 | + * @method Phaser.Curves.QuadraticBezier#draw |
| 131 | + * @since 3.0.0 |
| 132 | + * |
| 133 | + * @param {Phaser.GameObjects.Graphics} graphics - [description] |
| 134 | + * @param {integer} [pointsTotal=32] - [description] |
| 135 | + * |
| 136 | + * @return {Phaser.GameObjects.Graphics} [description] |
| 137 | + */ |
| 138 | + draw: function (graphics, pointsTotal) |
| 139 | + { |
| 140 | + if (pointsTotal === undefined) { pointsTotal = 32; } |
| 141 | + |
| 142 | + var points = this.getPoints(pointsTotal); |
| 143 | + |
| 144 | + graphics.beginPath(); |
| 145 | + graphics.moveTo(this.p0.x, this.p0.y); |
| 146 | + |
| 147 | + for (var i = 1; i < points.length; i++) |
| 148 | + { |
| 149 | + graphics.lineTo(points[i].x, points[i].y); |
| 150 | + } |
| 151 | + |
| 152 | + graphics.strokePath(); |
| 153 | + |
| 154 | + // So you can chain graphics calls |
| 155 | + return graphics; |
| 156 | + }, |
| 157 | + |
| 158 | + /** |
| 159 | + * [description] |
| 160 | + * |
| 161 | + * @method Phaser.Curves.QuadraticBezier#toJSON |
| 162 | + * @since 3.0.0 |
| 163 | + * |
| 164 | + * @return {object} [description] |
| 165 | + */ |
| 166 | + toJSON: function () |
| 167 | + { |
| 168 | + return { |
| 169 | + type: this.type, |
| 170 | + points: [ |
| 171 | + this.p0.x, this.p0.y, |
| 172 | + this.p1.x, this.p1.y, |
| 173 | + this.p2.x, this.p2.y |
| 174 | + ] |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | +}); |
| 179 | + |
| 180 | +QuadraticBezier.fromJSON = function (data) |
| 181 | +{ |
| 182 | + var points = data.points; |
| 183 | + |
| 184 | + var p0 = new Vector2(points[0], points[1]); |
| 185 | + var p1 = new Vector2(points[2], points[3]); |
| 186 | + var p2 = new Vector2(points[4], points[5]); |
| 187 | + |
| 188 | + return new QuadraticBezier(p0, p1, p2); |
| 189 | +}; |
| 190 | + |
| 191 | +module.exports = QuadraticBezier; |
0 commit comments