Object2D.js.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: Object2D.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: Object2D.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. import {Vector2} from "./math/Vector2.js";
  21. import {Matrix} from "./math/Matrix.js";
  22. import {UUID} from "./math/UUID.js";
  23. /**
  24. * Base object class, implements all the object positioning and scalling features.
  25. *
  26. * Stores all the base properties shared between all objects as the position, transformation properties, children etc.
  27. *
  28. * Object2D should be used as a group to store all the other objects drawn.
  29. *
  30. * @class
  31. */
  32. function Object2D()
  33. {
  34. /**
  35. * UUID of the object.
  36. */
  37. this.uuid = UUID.generate();
  38. /**
  39. * List of children objects attached to the object.
  40. */
  41. this.children = [];
  42. /**
  43. * Parent object, the object position is affected by its parent position.
  44. */
  45. this.parent = null;
  46. /**
  47. * Depth level in the object tree, objects with higher depth are drawn on top.
  48. *
  49. * The layer value is considered first.
  50. */
  51. this.level = 0;
  52. /**
  53. * Position of the object.
  54. */
  55. this.position = new Vector2(0, 0);
  56. /**
  57. * Origin of the object used as point of rotation.
  58. */
  59. this.origin = new Vector2(0, 0);
  60. /**
  61. * Scale of the object.
  62. */
  63. this.scale = new Vector2(1, 1);
  64. /**
  65. * Rotation of the object relative to its center.
  66. */
  67. this.rotation = 0.0;
  68. /**
  69. * Indicates if the object is visible.
  70. */
  71. this.visible = true;
  72. /**
  73. * Layer of this object, objects are sorted by layer value.
  74. *
  75. * Lower layer value is draw first.
  76. */
  77. this.layer = 0;
  78. /**
  79. * Local transformation matrix applied to the object.
  80. */
  81. this.matrix = new Matrix();
  82. /**
  83. * Global transformation matrix multiplied by the parent matrix.
  84. *
  85. * Used to transform the object before projecting into screen coordinates.
  86. */
  87. this.globalMatrix = new Matrix();
  88. /**
  89. * Inverse of the global matrix.
  90. *
  91. * Used to convert pointer input points into object coordinates.
  92. */
  93. this.inverseGlobalMatrix = new Matrix();
  94. /**
  95. * Masks being applied to this object.
  96. *
  97. * Multiple masks can be used simultaneously.
  98. */
  99. this.masks = [];
  100. /**
  101. * If true the matrix is updated before rendering the object.
  102. */
  103. this.matrixNeedsUpdate = true;
  104. /**
  105. * Indicates if its possible to drag the object around.
  106. *
  107. * If true the onPointerDrag callback is used to update the state of the object.
  108. */
  109. this.draggable = false;
  110. /**
  111. * Indicates if this object uses pointer events.
  112. *
  113. * Can be set false to skip the pointer interaction events.
  114. */
  115. this.pointerEvents = true;
  116. /**
  117. * Flag to indicate wheter this objet ignores the viewport transformation.
  118. */
  119. this.ignoreViewport = false;
  120. /**
  121. * Flag to indicate if the context of canvas should be saved before render.
  122. */
  123. this.saveContextState = true;
  124. /**
  125. * Flag to indicate if the context of canvas should be restored after render.
  126. */
  127. this.restoreContextState = true;
  128. /**
  129. * Flag indicating if the pointer is inside of the element.
  130. *
  131. * Used to control object event.
  132. */
  133. this.pointerInside = false;
  134. /**
  135. * Flag to indicate if the object is currently being dragged.
  136. */
  137. this.beingDragged = false;
  138. }
  139. /**
  140. * Traverse the object tree and run a function for all objects.
  141. *
  142. * @param {Function} callback Callback function that receives the object as parameter.
  143. */
  144. Object2D.prototype.traverse = function(callback)
  145. {
  146. callback(this);
  147. var children = this.children;
  148. for(var i = 0; i &lt; children.length; i++)
  149. {
  150. children[i].traverse(callback);
  151. }
  152. };
  153. /**
  154. * Get a object from its children list by its UUID.
  155. *
  156. * @param {String} uuid UUID of the object to get.
  157. * @return {Object2D} The object that has the UUID specified, null if the object was not found.
  158. */
  159. Object2D.prototype.getChildByUUID = function(uuid)
  160. {
  161. var object = null;
  162. this.traverse(function(child)
  163. {
  164. if(child.uuid === uuid)
  165. {
  166. object = child;
  167. }
  168. });
  169. return object;
  170. };
  171. /**
  172. * Attach a children to this object.
  173. *
  174. * The object is set as children of this object and the transformations applied to this object are traversed to its children.
  175. *
  176. * @param {Object2D} object Object to attach to this object.
  177. */
  178. Object2D.prototype.add = function(object)
  179. {
  180. object.parent = this;
  181. object.level = this.level + 1;
  182. object.traverse(function(child)
  183. {
  184. if(child.onAdd !== null)
  185. {
  186. child.onAdd(this);
  187. }
  188. });
  189. this.children.push(object);
  190. };
  191. /**
  192. * Remove object from the children list.
  193. *
  194. * @param {Object2D} object Object to be removed.
  195. */
  196. Object2D.prototype.remove = function(object)
  197. {
  198. var index = this.children.indexOf(object);
  199. if(index !== -1)
  200. {
  201. var object = this.children[index];
  202. object.parent = null;
  203. object.level = 0;
  204. object.traverse(function(child)
  205. {
  206. if(child.onRemove !== null)
  207. {
  208. child.onRemove(this);
  209. }
  210. });
  211. this.children.splice(index, 1)
  212. }
  213. };
  214. /**
  215. * Check if a point is inside of the object.
  216. *
  217. * Used to update the point events attached to the object.
  218. *
  219. * @return {boolean} True if the point is inside of the object.
  220. */
  221. Object2D.prototype.isInside = function(point)
  222. {
  223. return false;
  224. };
  225. /**
  226. * Update the transformation matrix of the object.
  227. *
  228. * @param {CanvasContext} context
  229. */
  230. Object2D.prototype.updateMatrix = function(context)
  231. {
  232. if(this.matrixNeedsUpdate)
  233. {
  234. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  235. this.globalMatrix.copy(this.matrix);
  236. if(this.parent !== null)
  237. {
  238. this.globalMatrix.premultiply(this.parent.globalMatrix);
  239. }
  240. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  241. //this.matrixNeedsUpdate = false;
  242. }
  243. };
  244. /**
  245. * Apply the transform to the rendering context.
  246. *
  247. * It is assumed that the viewport transform is pre-applied to the context.
  248. *
  249. * Can also be used for pre rendering logic.
  250. *
  251. * @param {CanvasContext} context Canvas 2d drawing context.
  252. * @param {Viewport} viewport Viewport applied to the canvas.
  253. */
  254. Object2D.prototype.transform = function(context, viewport)
  255. {
  256. this.globalMatrix.tranformContext(context);
  257. };
  258. /**
  259. * Draw the object into the canvas.
  260. *
  261. * Has to be implemented by underlying classes.
  262. *
  263. * @param {CanvasContext} context Canvas 2d drawing context.
  264. * @param {Viewport} viewport Viewport applied to the canvas.
  265. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  266. */
  267. Object2D.prototype.draw = function(context, viewport, canvas){};
  268. /**
  269. * Callback method while the object is being dragged across the screen.
  270. *
  271. * By default is adds the delta value to the object position (making it follow the mouse movement).
  272. *
  273. * Delta is the movement of the pointer already translated into local object coordinates.
  274. *
  275. * Receives (pointer, viewport, delta) as arguments.
  276. *
  277. * @param {Pointer} pointer Pointer object that receives the user input.
  278. * @param {Viewport} viewport Viewport where the object is drawn.
  279. * @param {Vector2} delta Pointer movement in world space.
  280. */
  281. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  282. {
  283. this.position.add(delta);
  284. };
  285. /**
  286. * Method called when the object its added to a parent.
  287. *
  288. * @param {Object2D} parent Parent object were it was added.
  289. */
  290. Object2D.prototype.onAdd = null;
  291. /**
  292. * Method called when the object gets removed from its parent
  293. *
  294. * @param {Object2D} parent Parent object from were the object is being removed.
  295. */
  296. Object2D.prototype.onRemove = null;
  297. /**
  298. * Callback method called every time before the object is draw into the canvas.
  299. *
  300. * Can be used to run preparation code, move the object, etc.
  301. */
  302. Object2D.prototype.onUpdate = null;
  303. /**
  304. * Callback method called when the pointer enters the object.
  305. *
  306. * Receives (pointer, viewport) as arguments.
  307. *
  308. * @param {Pointer} pointer Pointer object that receives the user input.
  309. * @param {Viewport} viewport Viewport where the object is drawn.
  310. */
  311. Object2D.prototype.onPointerEnter = null;
  312. /**
  313. * Callback method called when the was inside of the object and leaves the object.
  314. *
  315. * Receives (pointer, viewport) as arguments.
  316. *
  317. * @param {Pointer} pointer Pointer object that receives the user input.
  318. * @param {Viewport} viewport Viewport where the object is drawn.
  319. */
  320. Object2D.prototype.onPointerLeave = null;
  321. /**
  322. * Callback method while the pointer is over (inside) of the object.
  323. *
  324. * Receives (pointer, viewport) as arguments.
  325. *
  326. * @param {Pointer} pointer Pointer object that receives the user input.
  327. * @param {Viewport} viewport Viewport where the object is drawn.
  328. */
  329. Object2D.prototype.onPointerOver = null;
  330. /**
  331. * Callback method called while the pointer button is pressed.
  332. *
  333. * Receives (pointer, viewport) as arguments.
  334. *
  335. * @param {Pointer} pointer Pointer object that receives the user input.
  336. * @param {Viewport} viewport Viewport where the object is drawn.
  337. */
  338. Object2D.prototype.onButtonPressed = null;
  339. /**
  340. * Callback method called while the pointer button is double clicked.
  341. *
  342. * Receives (pointer, viewport) as arguments.
  343. *
  344. * @param {Pointer} pointer Pointer object that receives the user input.
  345. * @param {Viewport} viewport Viewport where the object is drawn.
  346. */
  347. Object2D.prototype.onDoubleClick = null;
  348. /**
  349. * Callback method called when the pointer button is pressed down (single time).
  350. *
  351. * Receives (pointer, viewport) as arguments.
  352. *
  353. * @param {Pointer} pointer Pointer object that receives the user input.
  354. * @param {Viewport} viewport Viewport where the object is drawn.
  355. */
  356. Object2D.prototype.onButtonDown = null;
  357. /**
  358. * Callback method called when the pointer button is released (single time).
  359. *
  360. * Receives (pointer, viewport) as arguments.
  361. *
  362. * @param {Pointer} pointer Pointer object that receives the user input.
  363. * @param {Viewport} viewport Viewport where the object is drawn.
  364. */
  365. Object2D.prototype.onButtonUp = null;
  366. export {Object2D};
  367. </code></pre>
  368. </article>
  369. </section>
  370. </div>
  371. <nav>
  372. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul>
  373. </nav>
  374. <br class="clear">
  375. <footer>
  376. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Tue Jun 25 2019 14:27:21 GMT+0100 (Western European Summer Time)
  377. </footer>
  378. <script> prettyPrint(); </script>
  379. <script src="scripts/linenumber.js"> </script>
  380. </body>
  381. </html>