/home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/curves/quadraticBezierCurve.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/examples/js/curves/quadraticBezierCurve.js

  1. /**
  2. A **QuadraticBezierCurve** is a {{#crossLink "Curve"}}{{/crossLink}} along which a 3D position can be animated.
  3.  
  4. ## Overview
  5.  
  6. <ul>
  7. <li>As shown in the diagram below, a QuadraticBezierCurve is defined by three control points.</li>
  8. <li>You can sample a {{#crossLink "QuadraticBezierCurve/point:property"}}{{/crossLink}} and a {{#crossLink "Curve/tangent:property"}}{{/crossLink}}
  9. vector on a QuadraticBezierCurve for any given value of {{#crossLink "QuadraticBezierCurve/t:property"}}{{/crossLink}} in the range [0..1].</li>
  10. <li>When you set {{#crossLink "QuadraticBezierCurve/t:property"}}{{/crossLink}} on a QuadraticBezierCurve, its
  11. {{#crossLink "QuadraticBezierCurve/point:property"}}{{/crossLink}} and {{#crossLink "Curve/tangent:property"}}{{/crossLink}} properties
  12. will update accordingly.</li>
  13. <li>To build a complex path, you can combine an unlimited combination of QuadraticBezierCurves,
  14. {{#crossLink "CubicBezierCurve"}}CubicBezierCurves{{/crossLink}} and {{#crossLink "SplineCurve"}}SplineCurves{{/crossLink}}
  15. into a {{#crossLink "Path"}}{{/crossLink}}.</li>
  16. </ul>
  17.  
  18. <img style="border:1px solid;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3d/B%C3%A9zier_2_big.gif/240px-B%C3%A9zier_2_big.gif"/><br>
  19. *[Quadratic Bezier Curve from WikiPedia](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)*
  20.  
  21. ## Examples
  22.  
  23. <ul>
  24. <li>[Tweening position along a QuadraticBezierCurve](../../examples/#animation_curves_quadraticBezier)</li>
  25. <li>[Tweening color along a QuadraticBezierCurve](../../examples/#animation_curves_quadraticBezier_color)</li>
  26. <li>[Path example](../../examples/#curves_Path)</li>
  27. </ul>
  28.  
  29. ## Usage
  30.  
  31. #### Animation along a QuadraticBezierCurve
  32.  
  33. Let's create a QuadraticBezierCurve, subscribe to updates on its {{#crossLink "QuadraticBezierCurve/point:property"}}{{/crossLink}},
  34. {{#crossLink "Curve/tangent:property"}}{{/crossLink}} and {{#crossLink "Curve/t:property"}}{{/crossLink}} properties,
  35. then vary its {{#crossLink "QuadraticBezierCurve/t:property"}}{{/crossLink}}
  36. property over time:
  37.  
  38. ````javascript
  39. var curve = new xeogl.QuadraticBezierCurve({
  40. v0: [10, 0, 0],
  41. v1: [20, 15, 0],
  42. v2: [10, 0, 0]
  43. });
  44.  
  45. curve.on("point", function(point) {
  46. this.log("curve.point=" + JSON.stringify(point));
  47. });
  48.  
  49. curve.on("tangent", function(tangent) {
  50. this.log("curve.tangent=" + JSON.stringify(tangent));
  51. });
  52.  
  53. curve.on("t", function(t) {
  54. this.log("curve.t=" + t);
  55. });
  56.  
  57. curve.scene.on("tick", function(e) {
  58. curve.t = (e.time - e.startTime) * 0.01;
  59. });
  60. ````
  61.  
  62. #### Randomly sampling points
  63.  
  64. Use QuadraticBezierCurve's {{#crossLink "QuadraticBezierCurve/getPoint:method"}}{{/crossLink}} and
  65. {{#crossLink "Curve/getTangent:method"}}{{/crossLink}} methods to sample the point and vector
  66. at a given **t**:
  67.  
  68. ````javascript
  69. curve.scene.on("tick", function(e) {
  70.  
  71. var t = (e.time - e.startTime) * 0.01;
  72.  
  73. var point = curve.getPoint(t);
  74. var tangent = curve.getTangent(t);
  75.  
  76. this.log("t=" + t + ", point=" + JSON.stringify(point) + ", tangent=" + JSON.stringify(tangent));
  77. });
  78. ````
  79.  
  80. #### Sampling multiple points
  81.  
  82. Use QuadraticBezierCurve's {{#crossLink "Curve/getPoints:method"}}{{/crossLink}} method to sample a list of equidistant points
  83. along it. In the snippet below, we'll build a {{#crossLink "Geometry"}}{{/crossLink}} that renders a line along the
  84. curve. Note that we need to flatten the points array for consumption by the {{#crossLink "Geometry"}}{{/crossLink}}.
  85.  
  86. ````javascript
  87. var geometry = new xeogl.Geometry({
  88. positions: xeogl.math.flatten(curve.getPoints(50))
  89. });
  90. ````
  91.  
  92. @class QuadraticBezierCurve
  93. @module xeogl
  94. @submodule curves
  95. @constructor
  96. @param [scene] {Scene} Parent {{#crossLink "Scene"}}Scene{{/crossLink}}.
  97. @param [cfg] {*} Configuration
  98. @param [cfg.id] {String} Optional ID, unique among all components in the parent {{#crossLink "Scene"}}Scene{{/crossLink}}, generated automatically when omitted.
  99. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this QuadraticBezierCurve.
  100. @param [cfg.v0=[0,0,0]] The starting point.
  101. @param [cfg.v1=[0,0,0]] The middle control point.
  102. @param [cfg.v2=[0,0,0]] The end point.
  103. @param [cfg.t=0] Current position on this QuadraticBezierCurve, in range between 0..1.
  104. @extends Curve
  105. */
  106. xeogl.QuadraticBezierCurve = class xeoglQuadraticBezierCurve extends xeogl.Curve {
  107.  
  108. init(cfg) {
  109. super.init(cfg);
  110. this.v0 = cfg.v0;
  111. this.v1 = cfg.v1;
  112. this.v2 = cfg.v2;
  113. this.t = cfg.t;
  114. }
  115.  
  116. /**
  117. Starting point on this QuadraticBezierCurve.
  118.  
  119. Fires a {{#crossLink "QuadraticBezierCurve/v0:event"}}{{/crossLink}} event on change.
  120.  
  121. @property v0
  122. @default [0.0, 0.0, 0.0]
  123. @type Float32Array
  124. */
  125. set v0(value) {
  126.  
  127. /**
  128. * Fired whenever this QuadraticBezierCurve's
  129. * {{#crossLink "QuadraticBezierCurve/v0:property"}}{{/crossLink}} property changes.
  130. * @event v0
  131. * @param value The property's new value
  132. */
  133. this.fire("v0", this._v0 = value || xeogl.math.vec3([0, 0, 0]));
  134. }
  135.  
  136. get v0() {
  137. return this._v0;
  138. }
  139.  
  140. /**
  141. Middle control point on this QuadraticBezierCurve.
  142.  
  143. Fires a {{#crossLink "QuadraticBezierCurve/v1:event"}}{{/crossLink}} event on change.
  144.  
  145. @property v1
  146. @default [0.0, 0.0, 0.0]
  147. @type Float32Array
  148. */
  149. set v1(value) {
  150.  
  151. /**
  152. * Fired whenever this QuadraticBezierCurve's
  153. * {{#crossLink "QuadraticBezierCurve/v1:property"}}{{/crossLink}} property changes.
  154. * @event v1
  155. * @param value The property's new value
  156. */
  157. this.fire("v1", this._v1 = value || xeogl.math.vec3([0, 0, 0]));
  158. }
  159.  
  160. get v1() {
  161. return this._v1;
  162. }
  163.  
  164. /**
  165. End point on this QuadraticBezierCurve.
  166.  
  167. Fires a {{#crossLink "QuadraticBezierCurve/v2:event"}}{{/crossLink}} event on change.
  168.  
  169. @property v2
  170. @default [0.0, 0.0, 0.0]
  171. @type Float32Array
  172. */
  173. set v2(value) {
  174. /**
  175. * Fired whenever this QuadraticBezierCurve's
  176. * {{#crossLink "QuadraticBezierCurve/v2:property"}}{{/crossLink}} property changes.
  177. * @event v2
  178. * @param value The property's new value
  179. */
  180. this.fire("v2", this._v2 = value || xeogl.math.vec3([0, 0, 0]));
  181. }
  182.  
  183. get v2() {
  184. return this._v2;
  185. }
  186.  
  187. /**
  188. Progress along this QuadraticBezierCurve.
  189.  
  190. Automatically clamps to range [0..1].
  191.  
  192. Fires a {{#crossLink "QuadraticBezierCurve/t:event"}}{{/crossLink}} event on change.
  193.  
  194. @property t
  195. @default 0
  196. @type Number
  197. */
  198. set t(value) {
  199. value = value || 0;
  200. this._t = value < 0.0 ? 0.0 : (value > 1.0 ? 1.0 : value);
  201. /**
  202. * Fired whenever this QuadraticBezierCurve's
  203. * {{#crossLink "QuadraticBezierCurve/t:property"}}{{/crossLink}} property changes.
  204. * @event t
  205. * @param value The property's new value
  206. */
  207. this.fire("t", this._t);
  208. }
  209.  
  210. get t() {
  211. return this._t;
  212. }
  213.  
  214. /**
  215. Point on this QuadraticBezierCurve at position {{#crossLink "QuadraticBezierCurve/t:property"}}{{/crossLink}}.
  216.  
  217. @property point
  218. @type {{Array of Number}}
  219. */
  220. get point() {
  221. return this.getPoint(this._t);
  222. }
  223.  
  224. /**
  225. * Returns point on this QuadraticBezierCurve at the given position.
  226. * @method getPoint
  227. * @param {Number} t Position to get point at.
  228. * @returns {{Array of Number}}
  229. */
  230. getPoint(t) {
  231. var math = xeogl.math;
  232. var vector = math.vec3();
  233. vector[0] = math.b2(t, this._v0[0], this._v1[0], this._v2[0]);
  234. vector[1] = math.b2(t, this._v0[1], this._v1[1], this._v2[1]);
  235. vector[2] = math.b2(t, this._v0[2], this._v1[2], this._v2[2]);
  236. return vector;
  237. }
  238.  
  239. getJSON() {
  240. return {
  241. v0: this._v0,
  242. v1: this._v1,
  243. v2: this._v2,
  244. t: this._t
  245. };
  246. }
  247. };
  248.