diagram.module.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. /**
  2. * EventManager is used to manager DOM events creationg and destruction in a single function call.
  3. *
  4. * It is used by objects to make it easier to add and remove events from global DOM objects.
  5. *
  6. * @class
  7. */
  8. function EventManager()
  9. {
  10. /**
  11. * Stores all events in the manager, their target and callback.
  12. *
  13. * Format [target, event, callback, active]
  14. *
  15. * @type {Array}
  16. */
  17. this.events = [];
  18. }
  19. /**
  20. * Add new event to the manager.
  21. *
  22. * @param {DOM} target Event target element.
  23. * @param {String} event Event name.
  24. * @param {Function} callback Callback function.
  25. */
  26. EventManager.prototype.add = function(target, event, callback)
  27. {
  28. this.events.push([target, event, callback, false]);
  29. };
  30. /**
  31. * Destroys this manager and remove all events.
  32. */
  33. EventManager.prototype.clear = function()
  34. {
  35. this.destroy();
  36. this.events = [];
  37. };
  38. /**
  39. * Creates all events in this manager.
  40. */
  41. EventManager.prototype.create = function()
  42. {
  43. for(var i = 0; i < this.events.length; i++)
  44. {
  45. var event = this.events[i];
  46. event[0].addEventListener(event[1], event[2]);
  47. event[3] = true;
  48. }
  49. };
  50. /**
  51. * Removes all events in this manager.
  52. */
  53. EventManager.prototype.destroy = function()
  54. {
  55. for(var i = 0; i < this.events.length; i++)
  56. {
  57. var event = this.events[i];
  58. event[0].removeEventListener(event[1], event[2]);
  59. event[3] = false;
  60. }
  61. };
  62. function Vector2(x, y)
  63. {
  64. this.x = x || 0;
  65. this.y = y || 0;
  66. }
  67. Object.assign(Vector2.prototype,
  68. {
  69. set: function(x, y)
  70. {
  71. this.x = x;
  72. this.y = y;
  73. },
  74. setScalar: function(scalar)
  75. {
  76. this.x = scalar;
  77. this.y = scalar;
  78. },
  79. clone: function()
  80. {
  81. return new Vector2(this.x, this.y);
  82. },
  83. copy: function(v)
  84. {
  85. this.x = v.x;
  86. this.y = v.y;
  87. },
  88. add: function(v, w)
  89. {
  90. this.x += v.x;
  91. this.y += v.y;
  92. },
  93. addScalar: function(s)
  94. {
  95. this.x += s;
  96. this.y += s;
  97. },
  98. addVectors: function(a, b)
  99. {
  100. this.x = a.x + b.x;
  101. this.y = a.y + b.y;
  102. },
  103. addScaledVector: function(v, s)
  104. {
  105. this.x += v.x * s;
  106. this.y += v.y * s;
  107. },
  108. sub: function(v, w)
  109. {
  110. this.x -= v.x;
  111. this.y -= v.y;
  112. },
  113. subScalar: function(s)
  114. {
  115. this.x -= s;
  116. this.y -= s;
  117. },
  118. subVectors: function(a, b)
  119. {
  120. this.x = a.x - b.x;
  121. this.y = a.y - b.y;
  122. },
  123. multiply: function(v)
  124. {
  125. this.x *= v.x;
  126. this.y *= v.y;
  127. },
  128. multiplyScalar: function(scalar)
  129. {
  130. this.x *= scalar;
  131. this.y *= scalar;
  132. },
  133. divide: function(v)
  134. {
  135. this.x /= v.x;
  136. this.y /= v.y;
  137. },
  138. divideScalar: function(scalar)
  139. {
  140. return this.multiplyScalar(1 / scalar);
  141. },
  142. min: function(v)
  143. {
  144. this.x = Math.min(this.x, v.x);
  145. this.y = Math.min(this.y, v.y);
  146. },
  147. max: function(v)
  148. {
  149. this.x = Math.max(this.x, v.x);
  150. this.y = Math.max(this.y, v.y);
  151. },
  152. clamp: function(min, max)
  153. {
  154. // assumes min < max, componentwise
  155. this.x = Math.max(min.x, Math.min(max.x, this.x));
  156. this.y = Math.max(min.y, Math.min(max.y, this.y));
  157. },
  158. clampScalar: function(minVal, maxVal)
  159. {
  160. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  161. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  162. },
  163. clampLength: function(min, max)
  164. {
  165. var length = this.length();
  166. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  167. },
  168. floor: function()
  169. {
  170. this.x = Math.floor(this.x);
  171. this.y = Math.floor(this.y);
  172. },
  173. ceil: function()
  174. {
  175. this.x = Math.ceil(this.x);
  176. this.y = Math.ceil(this.y);
  177. },
  178. round: function()
  179. {
  180. this.x = Math.round(this.x);
  181. this.y = Math.round(this.y);
  182. },
  183. roundToZero: function()
  184. {
  185. this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
  186. this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
  187. },
  188. negate: function()
  189. {
  190. this.x = -this.x;
  191. this.y = -this.y;
  192. return this;
  193. },
  194. dot: function(v)
  195. {
  196. return this.x * v.x + this.y * v.y;
  197. },
  198. cross: function(v)
  199. {
  200. return this.x * v.y - this.y * v.x;
  201. },
  202. lengthSq: function()
  203. {
  204. return this.x * this.x + this.y * this.y;
  205. },
  206. length: function()
  207. {
  208. return Math.sqrt(this.x * this.x + this.y * this.y);
  209. },
  210. manhattanLength: function()
  211. {
  212. return Math.abs(this.x) + Math.abs(this.y);
  213. },
  214. normalize: function()
  215. {
  216. return this.divideScalar(this.length() || 1);
  217. },
  218. /**
  219. * Computes the angle in radians with respect to the positive x-axis
  220. */
  221. angle: function()
  222. {
  223. var angle = Math.atan2(this.y, this.x);
  224. if(angle < 0) angle += 2 * Math.PI;
  225. return angle;
  226. },
  227. distanceTo: function(v)
  228. {
  229. return Math.sqrt(this.distanceToSquared(v));
  230. },
  231. distanceToSquared: function(v)
  232. {
  233. var dx = this.x - v.x,
  234. dy = this.y - v.y;
  235. return dx * dx + dy * dy;
  236. },
  237. manhattanDistanceTo: function(v)
  238. {
  239. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  240. },
  241. setLength: function(length)
  242. {
  243. return this.normalize().multiplyScalar(length);
  244. },
  245. lerp: function(v, alpha)
  246. {
  247. this.x += (v.x - this.x) * alpha;
  248. this.y += (v.y - this.y) * alpha;
  249. },
  250. lerpVectors: function(v1, v2, alpha)
  251. {
  252. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  253. },
  254. equals: function(v)
  255. {
  256. return ((v.x === this.x) && (v.y === this.y));
  257. },
  258. fromArray: function(array, offset)
  259. {
  260. if(offset === undefined) offset = 0;
  261. this.x = array[offset];
  262. this.y = array[offset + 1];
  263. },
  264. toArray: function(array, offset)
  265. {
  266. if(array === undefined) array = [];
  267. if(offset === undefined) offset = 0;
  268. array[offset] = this.x;
  269. array[offset + 1] = this.y;
  270. return array;
  271. },
  272. rotateAround: function(center, angle)
  273. {
  274. var c = Math.cos(angle),
  275. s = Math.sin(angle);
  276. var x = this.x - center.x;
  277. var y = this.y - center.y;
  278. this.x = x * c - y * s + center.x;
  279. this.y = x * s + y * c + center.y;
  280. }
  281. });
  282. /**
  283. * 2D 3x2 transformation matrix, applied to the canvas elements.
  284. *
  285. * @class
  286. */
  287. function Matrix(values)
  288. {
  289. if(values !== undefined)
  290. {
  291. this.m = values;
  292. }
  293. else
  294. {
  295. this.identity();
  296. }
  297. }
  298. /**
  299. * Copy the content of another matrix and store in this one.
  300. */
  301. Matrix.prototype.copy = function(mat)
  302. {
  303. this.m = mat.m.slice(0);
  304. };
  305. /**
  306. * Create a new matrix object with a copy of the content of this one.
  307. */
  308. Matrix.prototype.clone = function()
  309. {
  310. return new Matrix(this.m.slice(0))
  311. };
  312. /**
  313. * Reset this matrix to indentity.
  314. */
  315. Matrix.prototype.identity = function()
  316. {
  317. this.m = [1, 0, 0, 1, 0, 0];
  318. };
  319. /**
  320. * Multiply another matrix by this one and store the result.
  321. *
  322. * @param mat Matrix array.
  323. */
  324. Matrix.prototype.multiply = function(mat)
  325. {
  326. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  327. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  328. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  329. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  330. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  331. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  332. this.m = [m0, m1, m2, m3, m4, m5];
  333. };
  334. /**
  335. * Premultiply another matrix by this one and store the result.
  336. *
  337. * @param mat Matrix array to multiply.
  338. */
  339. Matrix.prototype.premultiply = function(mat)
  340. {
  341. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  342. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  343. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  344. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  345. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  346. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  347. this.m = [m0, m1, m2, m3, m4, m5];
  348. };
  349. /**
  350. * Compose this transformation matrix with position scale and rotation.
  351. */
  352. Matrix.prototype.compose = function(px, py, sx, sy, a)
  353. {
  354. this.m = [1, 0, 0, 1, px, py];
  355. var c = Math.cos(a);
  356. var s = Math.sin(a);
  357. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  358. this.scale(sx, sy);
  359. };
  360. /**
  361. * Apply translation to this matrix.
  362. */
  363. Matrix.prototype.translate = function(x, y)
  364. {
  365. this.m[4] += this.m[0] * x + this.m[2] * y;
  366. this.m[5] += this.m[1] * x + this.m[3] * y;
  367. };
  368. /**
  369. * Apply rotation to this matrix.
  370. *
  371. * @param angle Angle in radians.
  372. */
  373. Matrix.prototype.rotate = function(rad)
  374. {
  375. var c = Math.cos(rad);
  376. var s = Math.sin(rad);
  377. var m11 = this.m[0] * c + this.m[2] * s;
  378. var m12 = this.m[1] * c + this.m[3] * s;
  379. var m21 = this.m[0] * -s + this.m[2] * c;
  380. var m22 = this.m[1] * -s + this.m[3] * c;
  381. this.m[0] = m11;
  382. this.m[1] = m12;
  383. this.m[2] = m21;
  384. this.m[3] = m22;
  385. };
  386. /**
  387. * Apply scale to this matrix.
  388. */
  389. Matrix.prototype.scale = function(sx, sy)
  390. {
  391. this.m[0] *= sx;
  392. this.m[1] *= sx;
  393. this.m[2] *= sy;
  394. this.m[3] *= sy;
  395. };
  396. /**
  397. * Set the position of the transformation matrix.
  398. */
  399. Matrix.prototype.setPosition = function(x, y)
  400. {
  401. this.m[4] = x;
  402. this.m[5] = y;
  403. };
  404. /**
  405. * Get the scale from the transformation matrix.
  406. */
  407. Matrix.prototype.getScale = function()
  408. {
  409. return new Vector2(this.m[0], this.m[3]);
  410. };
  411. /**
  412. * Get the position from the transformation matrix.
  413. */
  414. Matrix.prototype.getPosition = function()
  415. {
  416. return new Vector2(this.m[4], this.m[5]);
  417. };
  418. /**
  419. * Apply skew to this matrix.
  420. */
  421. Matrix.prototype.skew = function(radianX, radianY)
  422. {
  423. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  424. };
  425. /**
  426. * Get the matrix determinant.
  427. */
  428. Matrix.prototype.determinant = function()
  429. {
  430. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  431. };
  432. /**
  433. * Get the inverse matrix.
  434. */
  435. Matrix.prototype.getInverse = function()
  436. {
  437. var d = this.determinant();
  438. return new Matrix([this.m[3] * d, -this.m[1] * d, -this.m[2] * d, this.m[0] * d, d * (this.m[2] * this.m[5] - this.m[3] * this.m[4]), d * (this.m[1] * this.m[4] - this.m[0] * this.m[5])]);
  439. };
  440. /**
  441. * Transform a point using this matrix.
  442. */
  443. Matrix.prototype.transformPoint = function(p)
  444. {
  445. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  446. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  447. return new Vector2(px, py);
  448. };
  449. /**
  450. * Set a canvas context to use this transformation.
  451. */
  452. Matrix.prototype.setContextTransform = function(context)
  453. {
  454. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  455. };
  456. /**
  457. * Transform on top of the current context transformation.
  458. */
  459. Matrix.prototype.tranformContext = function(context)
  460. {
  461. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  462. };
  463. /**
  464. * Implements all UUID related methods.
  465. *
  466. * @class
  467. */
  468. function UUID(){}
  469. /**
  470. * Generate new random UUID v4 as string.
  471. *
  472. * http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  473. */
  474. UUID.generate = (function ()
  475. {
  476. var lut = [];
  477. for(var i = 0; i < 256; i++)
  478. {
  479. lut[i] = (i < 16 ? "0" : "") + (i).toString(16);
  480. }
  481. return function generateUUID()
  482. {
  483. var d0 = Math.random() * 0XFFFFFFFF | 0;
  484. var d1 = Math.random() * 0XFFFFFFFF | 0;
  485. var d2 = Math.random() * 0XFFFFFFFF | 0;
  486. var d3 = Math.random() * 0XFFFFFFFF | 0;
  487. var uuid = lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + "-" +
  488. lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + "-" + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + "-" +
  489. lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + "-" + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
  490. lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
  491. return uuid.toUpperCase();
  492. };
  493. })();
  494. /**
  495. * Base 2D object class, implements all the object positioning and scalling features.
  496. *
  497. * @class
  498. */
  499. function Object2D()
  500. {
  501. /**
  502. * UUID of the object.
  503. */
  504. this.uuid = UUID.generate();
  505. /**
  506. * List of children objects attached to the object.
  507. */
  508. this.children = [];
  509. /**
  510. * Parent object, the object position is affected by its parent position.
  511. */
  512. this.parent = null;
  513. /**
  514. * Position of the object.
  515. */
  516. this.position = new Vector2(0, 0);
  517. /**
  518. * Scale of the object.
  519. */
  520. this.scale = new Vector2(1, 1);
  521. /**
  522. * Rotation of the object relative to its center.
  523. */
  524. this.rotation = 0.0;
  525. /**
  526. * Indicates if the object is visible.
  527. */
  528. this.visible = true;
  529. /**
  530. * Layer of this object, objects are sorted by layer value.
  531. *
  532. * Lower layer value is draw first.
  533. */
  534. this.layer = 0;
  535. /**
  536. * Local transformation matrix applied to the object.
  537. */
  538. this.matrix = new Matrix();
  539. /**
  540. * Global transformation matrix multiplied by the parent matrix.
  541. *
  542. * Used to transform the object before projecting into screen coordinates.
  543. */
  544. this.globalMatrix = new Matrix();
  545. /**
  546. * Inverse of the global matrix.
  547. *
  548. * Used to convert pointer input points into object coordinates.
  549. */
  550. this.inverseGlobalMatrix = new Matrix();
  551. /**
  552. * If true the matrix is updated before rendering the object.
  553. */
  554. this.matrixNeedsUpdate = true;
  555. /**
  556. * Indicates if its possible to drag the object around.
  557. *
  558. * If true the onPointerDrag callback is used to update the state of the object.
  559. */
  560. this.draggable = true;
  561. /**
  562. * Flag indicating if the pointer is inside of the element.
  563. *
  564. * Used to control object event.
  565. */
  566. this.pointerInside = false;
  567. /**
  568. * Flag to indicate if the object is currently being dragged.
  569. */
  570. this.beingDragged = false;
  571. }
  572. /**
  573. * Traverse the object tree and run a function for all objects.
  574. *
  575. * @param callback Callback function that receives the object as parameter.
  576. */
  577. Object2D.prototype.traverse = function(callback)
  578. {
  579. callback(this);
  580. var children = this.children;
  581. for(var i = 0; i < children.length; i++)
  582. {
  583. children[i].traverse(callback);
  584. }
  585. };
  586. /**
  587. * Attach a children to the object.
  588. *
  589. * @param object Object to attach to this object.
  590. */
  591. Object2D.prototype.add = function(object)
  592. {
  593. object.parent = this;
  594. this.children.push(object);
  595. };
  596. /**
  597. * Remove object from the children list.
  598. *
  599. * @param object Object to be removed.
  600. */
  601. Object2D.prototype.remove = function(object)
  602. {
  603. var index = this.children.indexOf(object);
  604. if(index !== -1)
  605. {
  606. this.children[index].parent = null;
  607. this.children.splice(index, 1);
  608. }
  609. };
  610. /**
  611. * Check if a point is inside of the object.
  612. */
  613. Object2D.prototype.isInside = function(point)
  614. {
  615. return false;
  616. };
  617. /**
  618. * Update the transformation matrix of the object.
  619. */
  620. Object2D.prototype.updateMatrix = function(context)
  621. {
  622. if(this.matrixNeedsUpdate)
  623. {
  624. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.rotation);
  625. this.globalMatrix.copy(this.matrix);
  626. if(this.parent !== null)
  627. {
  628. this.globalMatrix.premultiply(this.parent.globalMatrix);
  629. }
  630. this.inverseGlobalMatrix = this.globalMatrix.getInverse();
  631. //this.matrixNeedsUpdate = false;
  632. }
  633. };
  634. /**
  635. * Draw the object into the canvas.
  636. *
  637. * Has to be implemented by underlying classes.
  638. *
  639. * @param context Canvas 2d drawing context.
  640. */
  641. Object2D.prototype.draw = function(context){};
  642. /**
  643. * Callback method called every time before the object is draw into the canvas.
  644. *
  645. * Can be used to run preparation code, move the object, etc.
  646. */
  647. Object2D.prototype.onUpdate = null;
  648. /**
  649. * Callback method called when the pointer enters the object.
  650. *
  651. * Receives (pointer, viewport) as arguments.
  652. */
  653. Object2D.prototype.onPointerEnter = null;
  654. /**
  655. * Callback method called when the was inside of the object and leaves the object.
  656. *
  657. * Receives (pointer, viewport) as arguments.
  658. */
  659. Object2D.prototype.onPointerLeave = null;
  660. /**
  661. * Callback method while the pointer is over (inside) of the object.
  662. *
  663. * Receives (pointer, viewport) as arguments.
  664. */
  665. Object2D.prototype.onPointerOver = null;
  666. /**
  667. * Callback method while the object is being dragged across the screen.
  668. *
  669. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  670. */
  671. Object2D.prototype.onPointerDrag = null;
  672. /**
  673. * Callback method called while the pointer button is pressed.
  674. *
  675. * Receives (pointer, viewport) as arguments.
  676. */
  677. Object2D.prototype.onButtonPressed = null;
  678. /**
  679. * Callback method called when the pointer button is pressed down (single time).
  680. */
  681. Object2D.prototype.onButtonDown = null;
  682. /**
  683. * Callback method called when the pointer button is released (single time).
  684. */
  685. Object2D.prototype.onButtonUp = null;
  686. /**
  687. * Key is used by Keyboard, Pointer, etc, to represent a key state.
  688. *
  689. * @class
  690. */
  691. function Key()
  692. {
  693. /**
  694. * Indicates if this key is currently pressed.
  695. */
  696. this.pressed = false;
  697. /**
  698. * Indicates if this key was just pressed.
  699. */
  700. this.justPressed = false;
  701. /**
  702. * Indicates if this key was just released.
  703. */
  704. this.justReleased = false;
  705. }
  706. Key.DOWN = -1;
  707. Key.UP = 1;
  708. Key.RESET = 0;
  709. Key.prototype.constructor = Key;
  710. /**
  711. * Update Key status based on new key state.
  712. */
  713. Key.prototype.update = function(action)
  714. {
  715. this.justPressed = false;
  716. this.justReleased = false;
  717. if(action === Key.DOWN)
  718. {
  719. if(this.pressed === false)
  720. {
  721. this.justPressed = true;
  722. }
  723. this.pressed = true;
  724. }
  725. else if(action === Key.UP)
  726. {
  727. if(this.pressed)
  728. {
  729. this.justReleased = true;
  730. }
  731. this.pressed = false;
  732. }
  733. else if(action === Key.RESET)
  734. {
  735. this.justReleased = false;
  736. this.justPressed = false;
  737. }
  738. };
  739. /**
  740. * Set this key attributes manually.
  741. */
  742. Key.prototype.set = function(justPressed, pressed, justReleased)
  743. {
  744. this.justPressed = justPressed;
  745. this.pressed = pressed;
  746. this.justReleased = justReleased;
  747. };
  748. /**
  749. * Reset key to default values.
  750. */
  751. Key.prototype.reset = function()
  752. {
  753. this.justPressed = false;
  754. this.pressed = false;
  755. this.justReleased = false;
  756. };
  757. /**
  758. * Pointer instance for input in sync with the running 3D application.
  759. *
  760. * The pointer object provided by scripts is automatically updated by the runtime handler.
  761. *
  762. * @class
  763. * @param {DOM} domElement DOM element to craete the pointer events.
  764. * @param {Boolean} dontInitialize If true the pointer events are not created.
  765. */
  766. function Pointer(domElement)
  767. {
  768. //Raw data
  769. this._keys = new Array(5);
  770. this._position = new Vector2(0, 0);
  771. this._positionUpdated = false;
  772. this._delta = new Vector2(0, 0);
  773. this._wheel = 0;
  774. this._wheelUpdated = false;
  775. this._doubleClicked = new Array(5);
  776. /**
  777. * Array with pointer buttons status.
  778. */
  779. this.keys = new Array(5);
  780. /**
  781. * Pointer position inside of the window (coordinates in window space).
  782. */
  783. this.position = new Vector2(0, 0);
  784. /**
  785. * Pointer movement (coordinates in window space).
  786. */
  787. this.delta = new Vector2(0, 0);
  788. /**
  789. * Pointer scroll wheel movement.
  790. */
  791. this.wheel = 0;
  792. /**
  793. * Indicates a button of the pointer was double clicked.
  794. */
  795. this.doubleClicked = new Array(5);
  796. /**
  797. * DOM element where to attach the pointer events.
  798. */
  799. this.domElement = (domElement !== undefined) ? domElement : window;
  800. /**
  801. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  802. */
  803. this.canvas = null;
  804. /**
  805. * Event manager responsible for updating the raw data variables.
  806. *
  807. * Diferent events are used depending on the host platform.
  808. *
  809. * When the update method is called the raw data is reset.
  810. */
  811. this.events = new EventManager();
  812. //Initialize key instances
  813. for(var i = 0; i < 5; i++)
  814. {
  815. this._doubleClicked[i] = false;
  816. this.doubleClicked[i] = false;
  817. this._keys[i] = new Key();
  818. this.keys[i] = new Key();
  819. }
  820. //Self pointer
  821. var self = this;
  822. //Scroll wheel
  823. if(window.onmousewheel !== undefined)
  824. {
  825. //Chrome, edge
  826. this.events.add(this.domElement, "mousewheel", function(event)
  827. {
  828. self._wheel = event.deltaY;
  829. self._wheelUpdated = true;
  830. });
  831. }
  832. else if(window.addEventListener !== undefined)
  833. {
  834. //Firefox
  835. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  836. {
  837. self._wheel = event.detail * 30;
  838. self._wheelUpdated = true;
  839. });
  840. }
  841. else
  842. {
  843. this.events.add(this.domElement, "wheel", function(event)
  844. {
  845. self._wheel = event.deltaY;
  846. self._wheelUpdated = true;
  847. });
  848. }
  849. //Touchscreen input events
  850. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  851. {
  852. //Auxiliar variables to calculate touch delta
  853. var lastTouch = new Vector2(0, 0);
  854. //Touch start event
  855. this.events.add(this.domElement, "touchstart", function(event)
  856. {
  857. var touch = event.touches[0];
  858. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  859. self.updateKey(Pointer.LEFT, Key.DOWN);
  860. lastTouch.set(touch.clientX, touch.clientY);
  861. });
  862. //Touch end event
  863. this.events.add(this.domElement, "touchend", function(event)
  864. {
  865. self.updateKey(Pointer.LEFT, Key.UP);
  866. });
  867. //Touch cancel event
  868. this.events.add(this.domElement, "touchcancel", function(event)
  869. {
  870. self.updateKey(Pointer.LEFT, Key.UP);
  871. });
  872. //Touch move event
  873. this.events.add(document.body, "touchmove", function(event)
  874. {
  875. var touch = event.touches[0];
  876. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  877. lastTouch.set(touch.clientX, touch.clientY);
  878. });
  879. }
  880. //Move
  881. this.events.add(this.domElement, "mousemove", function(event)
  882. {
  883. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  884. });
  885. //Button pressed
  886. this.events.add(this.domElement, "mousedown", function(event)
  887. {
  888. self.updateKey(event.which - 1, Key.DOWN);
  889. });
  890. //Button released
  891. this.events.add(this.domElement, "mouseup", function(event)
  892. {
  893. self.updateKey(event.which - 1, Key.UP);
  894. });
  895. //Drag start
  896. this.events.add(this.domElement, "dragstart", function(event)
  897. {
  898. self.updateKey(event.which - 1, Key.UP);
  899. });
  900. //Pointer double click
  901. this.events.add(this.domElement, "dblclick", function(event)
  902. {
  903. self._doubleClicked[event.which - 1] = true;
  904. });
  905. this.create();
  906. }
  907. Pointer.prototype = Pointer;
  908. Pointer.prototype.constructor = Pointer;
  909. /**
  910. * Left pointer button.
  911. */
  912. Pointer.LEFT = 0;
  913. /**
  914. * Middle pointer button.
  915. */
  916. Pointer.MIDDLE = 1;
  917. /**
  918. * Right pointer button.
  919. */
  920. Pointer.RIGHT = 2;
  921. /**
  922. * Back pointer navigation button.
  923. */
  924. Pointer.BACK = 3;
  925. /**
  926. * Forward pointer navigation button.
  927. */
  928. Pointer.FORWARD = 4;
  929. /**
  930. * Element to be used for coordinates calculation relative to that canvas.
  931. *
  932. * @param {DOM} canvas Canvas to be attached to the Pointer instance
  933. */
  934. Pointer.setCanvas = function(element)
  935. {
  936. this.canvas = element;
  937. element.pointerInside = false;
  938. element.addEventListener("mouseenter", function()
  939. {
  940. this.pointerInside = true;
  941. });
  942. element.addEventListener("mouseleave", function()
  943. {
  944. this.pointerInside = false;
  945. });
  946. };
  947. /**
  948. * Check if pointer is inside attached canvas (updated async).
  949. *
  950. * @return {boolean} True if pointer is currently inside the canvas
  951. */
  952. Pointer.insideCanvas = function()
  953. {
  954. return this.canvas !== null && this.canvas.pointerInside;
  955. };
  956. /**
  957. * Check if pointer button is currently pressed.
  958. *
  959. * @param {Number} button Button to check status of
  960. * @return {boolean} True if button is currently pressed
  961. */
  962. Pointer.buttonPressed = function(button)
  963. {
  964. return this.keys[button].pressed;
  965. };
  966. /**
  967. * Check if pointer button was double clicked.
  968. *
  969. * @param {Number} button Button to check status of
  970. * @return {boolean} True if some pointer button was just double clicked
  971. */
  972. Pointer.buttonDoubleClicked = function(button)
  973. {
  974. return this.doubleClicked[button];
  975. };
  976. /**
  977. * Check if a pointer button was just pressed.
  978. *
  979. * @param {Number} button Button to check status of
  980. * @return {boolean} True if button was just pressed
  981. */
  982. Pointer.buttonJustPressed = function(button)
  983. {
  984. return this.keys[button].justPressed;
  985. };
  986. /**
  987. * Check if a pointer button was just released.
  988. *
  989. * @param {Number} button Button to check status of
  990. * @return {boolean} True if button was just released
  991. */
  992. Pointer.buttonJustReleased = function(button)
  993. {
  994. return this.keys[button].justReleased;
  995. };
  996. /**
  997. * Update pointer position.
  998. *
  999. * Automatically called by the runtime.
  1000. *
  1001. * @param {Number} x
  1002. * @param {Number} y
  1003. * @param {Number} xDiff
  1004. * @param {Number} yDiff
  1005. */
  1006. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  1007. {
  1008. if(this.canvas !== null)
  1009. {
  1010. var rect = this.canvas.getBoundingClientRect();
  1011. x -= rect.left;
  1012. y -= rect.top;
  1013. }
  1014. this._position.set(x, y);
  1015. this._delta.x += xDiff;
  1016. this._delta.y += yDiff;
  1017. this._positionUpdated = true;
  1018. };
  1019. /**
  1020. * Update a pointer button.
  1021. *
  1022. * Automatically called by the runtime.
  1023. *
  1024. * @param {Number} button
  1025. * @param {Number} action
  1026. */
  1027. Pointer.updateKey = function(button, action)
  1028. {
  1029. if(button > -1)
  1030. {
  1031. this._keys[button].update(action);
  1032. }
  1033. };
  1034. /**
  1035. * Update pointer buttons state, position, wheel and delta synchronously.
  1036. */
  1037. Pointer.update = function()
  1038. {
  1039. //Update pointer keys state
  1040. for(var i = 0; i < 5; i++)
  1041. {
  1042. if(this._keys[i].justPressed && this.keys[i].justPressed)
  1043. {
  1044. this._keys[i].justPressed = false;
  1045. }
  1046. if(this._keys[i].justReleased && this.keys[i].justReleased)
  1047. {
  1048. this._keys[i].justReleased = false;
  1049. }
  1050. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  1051. //Update pointer double click
  1052. if(this._doubleClicked[i] === true)
  1053. {
  1054. this.doubleClicked[i] = true;
  1055. this._doubleClicked[i] = false;
  1056. }
  1057. else
  1058. {
  1059. this.doubleClicked[i] = false;
  1060. }
  1061. }
  1062. //Update pointer wheel
  1063. if(this._wheelUpdated)
  1064. {
  1065. this.wheel = this._wheel;
  1066. this._wheelUpdated = false;
  1067. }
  1068. else
  1069. {
  1070. this.wheel = 0;
  1071. }
  1072. //Update pointer Position if needed
  1073. if(this._positionUpdated)
  1074. {
  1075. this.delta.copy(this._delta);
  1076. this.position.copy(this._position);
  1077. this._delta.set(0,0);
  1078. this._positionUpdated = false;
  1079. }
  1080. else
  1081. {
  1082. this.delta.x = 0;
  1083. this.delta.y = 0;
  1084. }
  1085. };
  1086. /**
  1087. * Create pointer events.
  1088. */
  1089. Pointer.create = function()
  1090. {
  1091. this.events.create();
  1092. };
  1093. /**
  1094. * Dispose pointer events.
  1095. */
  1096. Pointer.dispose = function()
  1097. {
  1098. this.events.destroy();
  1099. };
  1100. /**
  1101. * The renderer is resposible for drawing the structure into the canvas element.
  1102. *
  1103. * Its also resposible for managing the canvas state.
  1104. *
  1105. * @class
  1106. */
  1107. function Renderer(canvas)
  1108. {
  1109. /**
  1110. * Canvas DOM element, has to be managed by the user.
  1111. */
  1112. this.canvas = canvas;
  1113. /**
  1114. * Canvas 2D rendering context used to draw content.
  1115. */
  1116. this.context = canvas.getContext("2d");
  1117. this.context.imageSmoothingEnabled = true;
  1118. this.context.globalCompositeOperation = "source-over";
  1119. /**
  1120. * Pointer input handler object.
  1121. */
  1122. this.pointer = new Pointer();
  1123. this.pointer.setCanvas(canvas);
  1124. }
  1125. /**
  1126. * Update the renderer state, update the input handlers, calculate the object and viewport transformation matrices.
  1127. *
  1128. * @param object Object to be updated.
  1129. * @param viewport Viewport to be updated (should be the one where the objects will be rendered after).
  1130. */
  1131. Renderer.prototype.update = function(object, viewport)
  1132. {
  1133. this.pointer.update();
  1134. var pointer = this.pointer;
  1135. // Viewport transform matrix
  1136. viewport.updateControls(pointer);
  1137. viewport.updateMatrix();
  1138. // Project pointer coordinates
  1139. var point = pointer.position.clone();
  1140. var viewportPoint = viewport.inverseMatrix.transformPoint(point);
  1141. // Object transformation matrices
  1142. object.traverse(function(child)
  1143. {
  1144. var childPoint = child.inverseGlobalMatrix.transformPoint(viewportPoint);
  1145. // Check if the pointer pointer is inside
  1146. if(child.isInside(childPoint))
  1147. {
  1148. // Pointer enter
  1149. if(!child.pointerInside && child.onPointerEnter !== null)
  1150. {
  1151. child.onPointerEnter(pointer, viewport);
  1152. }
  1153. // Pointer over
  1154. if(child.onPointerOver !== null)
  1155. {
  1156. child.onPointerOver(pointer, viewport);
  1157. }
  1158. // Pointer just pressed
  1159. if(pointer.buttonJustPressed(Pointer.LEFT))
  1160. {
  1161. if(child.onButtonDown !== null)
  1162. {
  1163. child.onButtonDown(pointer, viewport);
  1164. }
  1165. if(child.draggable)
  1166. {
  1167. child.beingDragged = true;
  1168. }
  1169. }
  1170. // Pointer pressed
  1171. if(pointer.buttonPressed(Pointer.LEFT) && child.onButtonPressed !== null)
  1172. {
  1173. child.onButtonPressed(pointer, viewport);
  1174. }
  1175. // Just released
  1176. if(pointer.buttonJustReleased(Pointer.LEFT) && child.onButtonUp !== null)
  1177. {
  1178. child.onButtonUp(pointer, viewport);
  1179. }
  1180. child.pointerInside = true;
  1181. }
  1182. else if(child.pointerInside)
  1183. {
  1184. // Pointer leave
  1185. if(child.onPointerLeave !== null)
  1186. {
  1187. child.onPointerLeave(pointer, viewport);
  1188. }
  1189. child.pointerInside = false;
  1190. }
  1191. // Stop object drag
  1192. if(pointer.buttonJustReleased(Pointer.LEFT))
  1193. {
  1194. if(child.draggable)
  1195. {
  1196. child.beingDragged = false;
  1197. }
  1198. }
  1199. // Pointer drag event
  1200. if(child.beingDragged)
  1201. {
  1202. var matrix = viewport.inverseMatrix.clone();
  1203. matrix.multiply(child.inverseGlobalMatrix);
  1204. matrix.setPosition(0, 0);
  1205. var delta = matrix.transformPoint(pointer.delta);
  1206. child.position.add(delta);
  1207. if(child.onPointerDrag !== null)
  1208. {
  1209. child.onPointerDrag(pointer, viewport, delta);
  1210. }
  1211. }
  1212. // On update
  1213. if(child.onUpdate !== null)
  1214. {
  1215. child.onUpdate();
  1216. }
  1217. child.updateMatrix();
  1218. });
  1219. };
  1220. /**
  1221. * Render the object using the viewport into a canvas element.
  1222. *
  1223. * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
  1224. *
  1225. * @param object Object to be rendered.
  1226. * @param viewport Viewport to render the objects.
  1227. */
  1228. Renderer.prototype.render = function(object, viewport)
  1229. {
  1230. var context = this.context;
  1231. // Clear canvas
  1232. context.setTransform(1, 0, 0, 1, 0, 0);
  1233. context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  1234. // Set viewport matrix transform
  1235. viewport.matrix.setContextTransform(context);
  1236. // Get objects to be rendered
  1237. var objects = [];
  1238. object.traverse(function(child)
  1239. {
  1240. if(child.visible)
  1241. {
  1242. objects.push(child);
  1243. }
  1244. });
  1245. // Sort objects by layer
  1246. objects.sort(function(a, b)
  1247. {
  1248. return a.layer - b.layer;
  1249. });
  1250. // Render into the canvas
  1251. for(var i = 0; i < objects.length; i++)
  1252. {
  1253. context.save();
  1254. objects[i].globalMatrix.tranformContext(context);
  1255. objects[i].draw(context);
  1256. context.restore();
  1257. }
  1258. };
  1259. /**
  1260. * Used to indicate how the user views the content inside of the canvas.
  1261. *
  1262. * @class
  1263. */
  1264. function Viewport()
  1265. {
  1266. /**
  1267. * UUID of the object.
  1268. */
  1269. this.uuid = UUID.generate();
  1270. /**
  1271. * Position of the object.
  1272. */
  1273. this.position = new Vector2(0, 0);
  1274. /**
  1275. * Scale of the object.
  1276. */
  1277. this.scale = 1.0;
  1278. /**
  1279. * Rotation of the object relative to its center.
  1280. */
  1281. this.rotation = 0.0;
  1282. /**
  1283. * Local transformation matrix applied to the object.
  1284. */
  1285. this.matrix = new Matrix();
  1286. /**
  1287. * Inverse of the local transformation matrix.
  1288. */
  1289. this.inverseMatrix = new Matrix();
  1290. /**
  1291. * If true the matrix is updated before rendering the object.
  1292. */
  1293. this.matrixNeedsUpdate = true;
  1294. /**
  1295. * Flag to indicate if the viewport should move when scalling.
  1296. *
  1297. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  1298. */
  1299. this.moveOnScale = true;
  1300. }
  1301. /**
  1302. * Update the viewport controls using the pointer object.
  1303. */
  1304. Viewport.prototype.updateControls = function(pointer)
  1305. {
  1306. if(pointer.wheel !== 0)
  1307. {
  1308. this.scale -= pointer.wheel * 1e-3 * this.scale;
  1309. if(this.moveOnScale)
  1310. {
  1311. var speed = pointer.wheel / this.scale;
  1312. var halfWidth = pointer.canvas.width / 2;
  1313. var halfWeight = pointer.canvas.height / 2;
  1314. this.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
  1315. this.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
  1316. }
  1317. }
  1318. if(pointer.buttonPressed(Pointer.RIGHT))
  1319. {
  1320. this.position.x += pointer.delta.x;
  1321. this.position.y += pointer.delta.y;
  1322. }
  1323. };
  1324. /**
  1325. * Calculate and update the viewport transformation matrix.
  1326. */
  1327. Viewport.prototype.updateMatrix = function()
  1328. {
  1329. if(this.matrixNeedsUpdate)
  1330. {
  1331. this.matrix.compose(this.position.x, this.position.y, this.scale, this.scale, this.rotation);
  1332. this.inverseMatrix = this.matrix.getInverse();
  1333. //this.matrixNeedsUpdate = false;
  1334. }
  1335. };
  1336. /**
  1337. * Box is described by a minimum and maximum points.
  1338. *
  1339. * Can be used for collision detection with points and other boxes.
  1340. */
  1341. function Box2(min, max)
  1342. {
  1343. this.min = (min !== undefined) ? min : new Vector2();
  1344. this.max = (max !== undefined) ? max : new Vector2();
  1345. }
  1346. Object.assign(Box2.prototype,
  1347. {
  1348. set: function(min, max)
  1349. {
  1350. this.min.copy(min);
  1351. this.max.copy(max);
  1352. return this;
  1353. },
  1354. setFromPoints: function(points)
  1355. {
  1356. this.min = new Vector2(+Infinity, +Infinity);
  1357. this.max = new Vector2(-Infinity, -Infinity);
  1358. for(var i = 0, il = points.length; i < il; i++)
  1359. {
  1360. this.expandByPoint(points[i]);
  1361. }
  1362. return this;
  1363. },
  1364. setFromCenterAndSize: function(center, size)
  1365. {
  1366. var v1 = new Vector2();
  1367. var halfSize = v1.copy(size).multiplyScalar(0.5);
  1368. this.min.copy(center).sub(halfSize);
  1369. this.max.copy(center).add(halfSize);
  1370. return this;
  1371. },
  1372. clone: function()
  1373. {
  1374. var box = new Box2();
  1375. box.copy(this);
  1376. return box;
  1377. },
  1378. copy: function(box)
  1379. {
  1380. this.min.copy(box.min);
  1381. this.max.copy(box.max);
  1382. return this;
  1383. },
  1384. isEmpty: function()
  1385. {
  1386. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  1387. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  1388. },
  1389. getCenter: function(target)
  1390. {
  1391. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  1392. },
  1393. getSize: function(target)
  1394. {
  1395. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  1396. },
  1397. expandByPoint: function(point)
  1398. {
  1399. this.min.min(point);
  1400. this.max.max(point);
  1401. return this;
  1402. },
  1403. expandByVector: function(vector)
  1404. {
  1405. this.min.sub(vector);
  1406. this.max.add(vector);
  1407. return this;
  1408. },
  1409. expandByScalar: function(scalar)
  1410. {
  1411. this.min.addScalar(-scalar);
  1412. this.max.addScalar(scalar);
  1413. return this;
  1414. },
  1415. containsPoint: function(point)
  1416. {
  1417. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  1418. },
  1419. containsBox: function(box)
  1420. {
  1421. return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
  1422. },
  1423. getParameter: function(point, target)
  1424. {
  1425. // This can potentially have a divide by zero if the box
  1426. // has a size dimension of 0.
  1427. return target.set(
  1428. (point.x - this.min.x) / (this.max.x - this.min.x),
  1429. (point.y - this.min.y) / (this.max.y - this.min.y)
  1430. );
  1431. },
  1432. intersectsBox: function(box)
  1433. {
  1434. // using 4 splitting planes to rule out intersections
  1435. return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
  1436. },
  1437. clampPoint: function(point, target)
  1438. {
  1439. return target.copy(point).clamp(this.min, this.max);
  1440. },
  1441. distanceToPoint: function(point)
  1442. {
  1443. var v = new Vector2();
  1444. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  1445. return clampedPoint.sub(point).length();
  1446. },
  1447. intersect: function(box)
  1448. {
  1449. this.min.max(box.min);
  1450. this.max.min(box.max);
  1451. return this;
  1452. },
  1453. union: function(box)
  1454. {
  1455. this.min.min(box.min);
  1456. this.max.max(box.max);
  1457. return this;
  1458. },
  1459. translate: function(offset)
  1460. {
  1461. this.min.add(offset);
  1462. this.max.add(offset);
  1463. return this;
  1464. },
  1465. equals: function(box)
  1466. {
  1467. return box.min.equals(this.min) && box.max.equals(this.max);
  1468. }
  1469. });
  1470. /**
  1471. * Circle object draw a circular object.
  1472. */
  1473. function Circle()
  1474. {
  1475. Object2D.call(this);
  1476. /**
  1477. * Radius of the circle.
  1478. */
  1479. this.radius = 10.0;
  1480. /**
  1481. * Color of the circle border line.
  1482. */
  1483. this.strokeStyle = "#000000";
  1484. /**
  1485. * Background color of the circle.
  1486. */
  1487. this.fillStyle = "#FFFFFF";
  1488. }
  1489. Circle.prototype = Object.create(Object2D.prototype);
  1490. Circle.prototype.isInside = function(point)
  1491. {
  1492. return point.length() <= this.radius;
  1493. };
  1494. Circle.prototype.onPointerEnter = function(pointer, viewport)
  1495. {
  1496. this.fillStyle = "#CCCCCC";
  1497. };
  1498. Circle.prototype.onPointerLeave = function(pointer, viewport)
  1499. {
  1500. this.fillStyle = "#FFFFFF";
  1501. };
  1502. Circle.prototype.draw = function(context)
  1503. {
  1504. context.fillStyle = this.fillStyle;
  1505. context.beginPath();
  1506. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  1507. context.fill();
  1508. context.lineWidth = 1;
  1509. context.strokeStyle = this.strokeStyle;
  1510. context.beginPath();
  1511. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  1512. context.stroke();
  1513. };
  1514. /**
  1515. * Box object draw a box.
  1516. */
  1517. function Box(resizable)
  1518. {
  1519. Object2D.call(this);
  1520. /**
  1521. * Box object containing the size of the object.
  1522. */
  1523. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  1524. /**
  1525. * Color of the box border line.
  1526. */
  1527. this.strokeStyle = "#000000";
  1528. /**
  1529. * Background color of the box.
  1530. */
  1531. this.fillStyle = "#FFFFFF";
  1532. if(resizable)
  1533. {
  1534. this.createResizeHelpers();
  1535. }
  1536. }
  1537. Box.prototype = Object.create(Object2D.prototype);
  1538. Box.prototype.createResizeHelpers = function(first_argument)
  1539. {
  1540. var self = this;
  1541. function updateHelpers()
  1542. {
  1543. topRight.position.copy(self.box.min);
  1544. bottomLeft.position.copy(self.box.max);
  1545. topLeft.position.set(self.box.max.x, self.box.min.y);
  1546. bottomRight.position.set(self.box.min.x, self.box.max.y);
  1547. }
  1548. var topRight = new Circle();
  1549. topRight.radius = 4;
  1550. topRight.onPointerDrag = function(pointer, viewport, delta)
  1551. {
  1552. self.box.min.copy(topRight.position);
  1553. updateHelpers();
  1554. };
  1555. this.add(topRight);
  1556. var topLeft = new Circle();
  1557. topLeft.radius = 4;
  1558. topLeft.onPointerDrag = function(pointer, viewport, delta)
  1559. {
  1560. self.box.max.x = topLeft.position.x;
  1561. self.box.min.y = topLeft.position.y;
  1562. updateHelpers();
  1563. };
  1564. this.add(topLeft);
  1565. var bottomLeft = new Circle();
  1566. bottomLeft.radius = 4;
  1567. bottomLeft.onPointerDrag = function(pointer, viewport, delta)
  1568. {
  1569. self.box.max.copy(bottomLeft.position);
  1570. updateHelpers();
  1571. };
  1572. this.add(bottomLeft);
  1573. var bottomRight = new Circle();
  1574. bottomRight.radius = 4;
  1575. bottomRight.onPointerDrag = function(pointer, viewport, delta)
  1576. {
  1577. self.box.min.x = bottomRight.position.x;
  1578. self.box.max.y = bottomRight.position.y;
  1579. updateHelpers();
  1580. };
  1581. this.add(bottomRight);
  1582. updateHelpers();
  1583. };
  1584. Box.prototype.onPointerEnter = function(pointer, viewport)
  1585. {
  1586. this.fillStyle = "#CCCCCC";
  1587. };
  1588. Box.prototype.onPointerLeave = function(pointer, viewport)
  1589. {
  1590. this.fillStyle = "#FFFFFF";
  1591. };
  1592. Box.prototype.isInside = function(point)
  1593. {
  1594. return this.box.containsPoint(point);
  1595. };
  1596. Box.prototype.draw = function(context)
  1597. {
  1598. var width = this.box.max.x - this.box.min.x;
  1599. var height = this.box.max.y - this.box.min.y;
  1600. context.fillStyle = this.fillStyle;
  1601. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  1602. context.lineWidth = 1;
  1603. context.strokeStyle = this.strokeStyle;
  1604. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  1605. };
  1606. /**
  1607. * Line object draw a line from one point to another.
  1608. */
  1609. function Line()
  1610. {
  1611. Object2D.call(this);
  1612. /**
  1613. * Initial point of the line.
  1614. *
  1615. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  1616. */
  1617. this.from = new Vector2();
  1618. /**
  1619. * Final point of the line.
  1620. *
  1621. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  1622. */
  1623. this.to = new Vector2();
  1624. /**
  1625. * Color of the line.
  1626. */
  1627. this.strokeStyle = "#000000";
  1628. }
  1629. Line.prototype = Object.create(Object2D.prototype);
  1630. Line.prototype.draw = function(context)
  1631. {
  1632. context.lineWidth = 1;
  1633. context.strokeStyle = this.strokeStyle;
  1634. context.setLineDash([5, 5]);
  1635. context.beginPath();
  1636. context.moveTo(this.from.x, this.from.y);
  1637. context.lineTo(this.to.x, this.to.y);
  1638. context.stroke();
  1639. };
  1640. function Text()
  1641. {
  1642. Object2D.call(this);
  1643. /**
  1644. * Text value.
  1645. */
  1646. this.text = "";
  1647. /**
  1648. * Font of the text.
  1649. */
  1650. this.font = "16px Arial";
  1651. /**
  1652. * Color (style) of the text.
  1653. */
  1654. this.color = "#000000";
  1655. }
  1656. Text.prototype = Object.create(Object2D.prototype);
  1657. Text.prototype.draw = function(context)
  1658. {
  1659. context.font = this.font;
  1660. context.textAlign = "center";
  1661. context.fillStyle = this.color;
  1662. context.fillText(this.text, 0, 0);
  1663. };
  1664. function Image(src)
  1665. {
  1666. Object2D.call(this);
  1667. this.image = document.createElement("img");
  1668. this.image.src = src;
  1669. }
  1670. Image.prototype = Object.create(Object2D.prototype);
  1671. Image.prototype.draw = function(context)
  1672. {
  1673. context.drawImage(this.image, 0, 0);
  1674. };
  1675. export { Box, Box2, Circle, EventManager, Image, Key, Line, Matrix, Object2D, Pointer, Renderer, Text, UUID, Vector2, Viewport };