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

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

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