Object2D.js.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 2D object class, implements all the object positioning and scalling features.
  25. *
  26. * @class
  27. */
  28. function Object2D()
  29. {
  30. /**
  31. * UUID of the object.
  32. */
  33. this.uuid = UUID.generate();
  34. /**
  35. * List of children objects attached to the object.
  36. */
  37. this.children = [];
  38. /**
  39. * Parent object, the object position is affected by its parent position.
  40. */
  41. this.parent = null;
  42. /**
  43. * Position of the object.
  44. */
  45. this.position = new Vector2(0, 0);
  46. /**
  47. * Origin of the object used as point of rotation.
  48. */
  49. this.origin = new Vector2(0, 0);
  50. /**
  51. * Scale of the object.
  52. */
  53. this.scale = new Vector2(1, 1);
  54. /**
  55. * Rotation of the object relative to its center.
  56. */
  57. this.rotation = 0.0;
  58. /**
  59. * Indicates if the object is visible.
  60. */
  61. this.visible = true;
  62. /**
  63. * Layer of this object, objects are sorted by layer value.
  64. *
  65. * Lower layer value is draw first.
  66. */
  67. this.layer = 0;
  68. /**
  69. * Local transformation matrix applied to the object.
  70. */
  71. this.matrix = new Matrix();
  72. /**
  73. * Global transformation matrix multiplied by the parent matrix.
  74. *
  75. * Used to transform the object before projecting into screen coordinates.
  76. */
  77. this.globalMatrix = new Matrix();
  78. /**
  79. * Inverse of the global matrix.
  80. *
  81. * Used to convert pointer input points into object coordinates.
  82. */
  83. this.inverseGlobalMatrix = new Matrix();
  84. /**
  85. * Masks being applied to this object.
  86. *
  87. * Multiple masks can be used simultaneously.
  88. */
  89. this.masks = [];
  90. /**
  91. * If true the matrix is updated before rendering the object.
  92. */
  93. this.matrixNeedsUpdate = true;
  94. /**
  95. * Indicates if its possible to drag the object around.
  96. *
  97. * If true the onPointerDrag callback is used to update the state of the object.
  98. */
  99. this.draggable = false;
  100. /**
  101. * Indicates if this object uses pointer events.
  102. *
  103. * Can be set false to skip the pointer interaction events.
  104. */
  105. this.pointerEvents = true;
  106. /**
  107. * Flag to indicate wheter this objet ignores the viewport transformation.
  108. */
  109. this.ignoreViewport = false;
  110. /**
  111. * Flag to indicate if the context of canvas should be saved before render.
  112. */
  113. this.saveContextState = true;
  114. /**
  115. * Flag to indicate if the context of canvas should be restored after render.
  116. */
  117. this.restoreContextState = true;
  118. /**
  119. * Flag indicating if the pointer is inside of the element.
  120. *
  121. * Used to control object event.
  122. */
  123. this.pointerInside = false;
  124. /**
  125. * Flag to indicate if the object is currently being dragged.
  126. */
  127. this.beingDragged = false;
  128. }
  129. /**
  130. * Traverse the object tree and run a function for all objects.
  131. *
  132. * @param callback Callback function that receives the object as parameter.
  133. */
  134. Object2D.prototype.traverse = function(callback)
  135. {
  136. callback(this);
  137. var children = this.children;
  138. for(var i = 0; i &lt; children.length; i++)
  139. {
  140. children[i].traverse(callback);
  141. }
  142. };
  143. /**
  144. * Attach a children to the object.
  145. *
  146. * @param object Object to attach to this object.
  147. */
  148. Object2D.prototype.add = function(object)
  149. {
  150. object.parent = this;
  151. this.children.push(object);
  152. };
  153. /**
  154. * Remove object from the children list.
  155. *
  156. * @param object Object to be removed.
  157. */
  158. Object2D.prototype.remove = function(object)
  159. {
  160. var index = this.children.indexOf(object);
  161. if(index !== -1)
  162. {
  163. this.children[index].parent = null;
  164. this.children.splice(index, 1)
  165. }
  166. };
  167. /**
  168. * Check if a point is inside of the object.
  169. */
  170. Object2D.prototype.isInside = function(point)
  171. {
  172. return false;
  173. };
  174. /**
  175. * Update the transformation matrix of the object.
  176. */
  177. Object2D.prototype.updateMatrix = function(context)
  178. {
  179. if(this.matrixNeedsUpdate)
  180. {
  181. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  182. this.globalMatrix.copy(this.matrix);
  183. if(this.parent !== null)
  184. {
  185. this.globalMatrix.premultiply(this.parent.globalMatrix);
  186. }
  187. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  188. //this.matrixNeedsUpdate = false;
  189. }
  190. };
  191. /**
  192. * Apply the transform to the rendering context.
  193. *
  194. * It is assumed that the viewport transform is pre-applied to the context.
  195. *
  196. * Can also be used for pre rendering logic.
  197. *
  198. * @param {CanvasContext} context Canvas 2d drawing context.
  199. * @param {Viewport} viewport Viewport applied to the canvas.
  200. */
  201. Object2D.prototype.transform = function(context, viewport)
  202. {
  203. this.globalMatrix.tranformContext(context);
  204. };
  205. /**
  206. * Draw the object into the canvas.
  207. *
  208. * Has to be implemented by underlying classes.
  209. *
  210. * @param {CanvasContext} context Canvas 2d drawing context.
  211. * @param {Viewport} viewport Viewport applied to the canvas.
  212. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  213. */
  214. Object2D.prototype.draw = function(context, viewport, canvas){};
  215. /**
  216. * Callback method called every time before the object is draw into the canvas.
  217. *
  218. * Can be used to run preparation code, move the object, etc.
  219. */
  220. Object2D.prototype.onUpdate = null;
  221. /**
  222. * Callback method called when the pointer enters the object.
  223. *
  224. * Receives (pointer, viewport) as arguments.
  225. */
  226. Object2D.prototype.onPointerEnter = null;
  227. /**
  228. * Callback method called when the was inside of the object and leaves the object.
  229. *
  230. * Receives (pointer, viewport) as arguments.
  231. */
  232. Object2D.prototype.onPointerLeave = null;
  233. /**
  234. * Callback method while the pointer is over (inside) of the object.
  235. *
  236. * Receives (pointer, viewport) as arguments.
  237. */
  238. Object2D.prototype.onPointerOver = null;
  239. /**
  240. * Callback method while the object is being dragged across the screen.
  241. *
  242. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  243. */
  244. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  245. {
  246. this.position.add(delta);
  247. };
  248. /**
  249. * Callback method called while the pointer button is pressed.
  250. *
  251. * Receives (pointer, viewport) as arguments.
  252. */
  253. Object2D.prototype.onButtonPressed = null;
  254. /**
  255. * Callback method called when the pointer button is pressed down (single time).
  256. */
  257. Object2D.prototype.onButtonDown = null;
  258. /**
  259. * Callback method called when the pointer button is released (single time).
  260. */
  261. Object2D.prototype.onButtonUp = null;
  262. export {Object2D};
  263. </code></pre>
  264. </article>
  265. </section>
  266. </div>
  267. <nav>
  268. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><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="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></ul>
  269. </nav>
  270. <br class="clear">
  271. <footer>
  272. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Fri Jun 07 2019 11:44:40 GMT+0100 (Western European Summer Time)
  273. </footer>
  274. <script> prettyPrint(); </script>
  275. <script src="scripts/linenumber.js"> </script>
  276. </body>
  277. </html>