diagram.module.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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. Matrix.prototype.cssTransform = function()
  464. {
  465. return "matrix3d(" + this.m[0] + "," + this.m[2] + "," + this.m[4] + ",0," + this.m[1] + "," + this.m[3] + "," + this.m[5] + ",0,0,0,1,0,0,0,0,1)";
  466. };
  467. /**
  468. * Implements all UUID related methods.
  469. *
  470. * @class
  471. */
  472. function UUID(){}
  473. /**
  474. * Generate new random UUID v4 as string.
  475. *
  476. * http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  477. */
  478. UUID.generate = (function ()
  479. {
  480. var lut = [];
  481. for(var i = 0; i < 256; i++)
  482. {
  483. lut[i] = (i < 16 ? "0" : "") + (i).toString(16);
  484. }
  485. return function generateUUID()
  486. {
  487. var d0 = Math.random() * 0XFFFFFFFF | 0;
  488. var d1 = Math.random() * 0XFFFFFFFF | 0;
  489. var d2 = Math.random() * 0XFFFFFFFF | 0;
  490. var d3 = Math.random() * 0XFFFFFFFF | 0;
  491. var uuid = lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + "-" +
  492. lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + "-" + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + "-" +
  493. lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + "-" + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
  494. lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
  495. return uuid.toUpperCase();
  496. };
  497. })();
  498. /**
  499. * Base 2D object class, implements all the object positioning and scalling features.
  500. *
  501. * @class
  502. */
  503. function Object2D()
  504. {
  505. /**
  506. * UUID of the object.
  507. */
  508. this.uuid = UUID.generate();
  509. /**
  510. * List of children objects attached to the object.
  511. */
  512. this.children = [];
  513. /**
  514. * Parent object, the object position is affected by its parent position.
  515. */
  516. this.parent = null;
  517. /**
  518. * Position of the object.
  519. */
  520. this.position = new Vector2(0, 0);
  521. /**
  522. * Scale of the object.
  523. */
  524. this.scale = new Vector2(1, 1);
  525. /**
  526. * Rotation of the object relative to its center.
  527. */
  528. this.rotation = 0.0;
  529. /**
  530. * Indicates if the object is visible.
  531. */
  532. this.visible = true;
  533. /**
  534. * Layer of this object, objects are sorted by layer value.
  535. *
  536. * Lower layer value is draw first.
  537. */
  538. this.layer = 0;
  539. /**
  540. * Local transformation matrix applied to the object.
  541. */
  542. this.matrix = new Matrix();
  543. /**
  544. * Global transformation matrix multiplied by the parent matrix.
  545. *
  546. * Used to transform the object before projecting into screen coordinates.
  547. */
  548. this.globalMatrix = new Matrix();
  549. /**
  550. * Inverse of the global matrix.
  551. *
  552. * Used to convert pointer input points into object coordinates.
  553. */
  554. this.inverseGlobalMatrix = new Matrix();
  555. /**
  556. * If true the matrix is updated before rendering the object.
  557. */
  558. this.matrixNeedsUpdate = true;
  559. /**
  560. * Indicates if its possible to drag the object around.
  561. *
  562. * If true the onPointerDrag callback is used to update the state of the object.
  563. */
  564. this.draggable = true;
  565. /**
  566. * Flag indicating if the pointer is inside of the element.
  567. *
  568. * Used to control object event.
  569. */
  570. this.pointerInside = false;
  571. /**
  572. * Flag to indicate if the object is currently being dragged.
  573. */
  574. this.beingDragged = false;
  575. }
  576. /**
  577. * Traverse the object tree and run a function for all objects.
  578. *
  579. * @param callback Callback function that receives the object as parameter.
  580. */
  581. Object2D.prototype.traverse = function(callback)
  582. {
  583. callback(this);
  584. var children = this.children;
  585. for(var i = 0; i < children.length; i++)
  586. {
  587. children[i].traverse(callback);
  588. }
  589. };
  590. /**
  591. * Attach a children to the object.
  592. *
  593. * @param object Object to attach to this object.
  594. */
  595. Object2D.prototype.add = function(object)
  596. {
  597. object.parent = this;
  598. this.children.push(object);
  599. };
  600. /**
  601. * Remove object from the children list.
  602. *
  603. * @param object Object to be removed.
  604. */
  605. Object2D.prototype.remove = function(object)
  606. {
  607. var index = this.children.indexOf(object);
  608. if(index !== -1)
  609. {
  610. this.children[index].parent = null;
  611. this.children.splice(index, 1);
  612. }
  613. };
  614. /**
  615. * Check if a point is inside of the object.
  616. */
  617. Object2D.prototype.isInside = function(point)
  618. {
  619. return false;
  620. };
  621. /**
  622. * Update the transformation matrix of the object.
  623. */
  624. Object2D.prototype.updateMatrix = function(context)
  625. {
  626. if(this.matrixNeedsUpdate)
  627. {
  628. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.rotation);
  629. this.globalMatrix.copy(this.matrix);
  630. if(this.parent !== null)
  631. {
  632. this.globalMatrix.premultiply(this.parent.globalMatrix);
  633. }
  634. this.inverseGlobalMatrix = this.globalMatrix.getInverse();
  635. //this.matrixNeedsUpdate = false;
  636. }
  637. };
  638. /**
  639. * Draw the object into the canvas.
  640. *
  641. * Has to be implemented by underlying classes.
  642. *
  643. * @param context Canvas 2d drawing context.
  644. */
  645. Object2D.prototype.draw = function(context){};
  646. /**
  647. * Callback method called every time before the object is draw into the canvas.
  648. *
  649. * Can be used to run preparation code, move the object, etc.
  650. */
  651. Object2D.prototype.onUpdate = null;
  652. /**
  653. * Callback method called when the pointer enters the object.
  654. *
  655. * Receives (pointer, viewport) as arguments.
  656. */
  657. Object2D.prototype.onPointerEnter = null;
  658. /**
  659. * Callback method called when the was inside of the object and leaves the object.
  660. *
  661. * Receives (pointer, viewport) as arguments.
  662. */
  663. Object2D.prototype.onPointerLeave = null;
  664. /**
  665. * Callback method while the pointer is over (inside) of the object.
  666. *
  667. * Receives (pointer, viewport) as arguments.
  668. */
  669. Object2D.prototype.onPointerOver = null;
  670. /**
  671. * Callback method while the object is being dragged across the screen.
  672. *
  673. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  674. */
  675. Object2D.prototype.onPointerDrag = null;
  676. /**
  677. * Callback method called while the pointer button is pressed.
  678. *
  679. * Receives (pointer, viewport) as arguments.
  680. */
  681. Object2D.prototype.onButtonPressed = null;
  682. /**
  683. * Callback method called when the pointer button is pressed down (single time).
  684. */
  685. Object2D.prototype.onButtonDown = null;
  686. /**
  687. * Callback method called when the pointer button is released (single time).
  688. */
  689. Object2D.prototype.onButtonUp = null;
  690. /**
  691. * Key is used by Keyboard, Pointer, etc, to represent a key state.
  692. *
  693. * @class
  694. */
  695. function Key()
  696. {
  697. /**
  698. * Indicates if this key is currently pressed.
  699. */
  700. this.pressed = false;
  701. /**
  702. * Indicates if this key was just pressed.
  703. */
  704. this.justPressed = false;
  705. /**
  706. * Indicates if this key was just released.
  707. */
  708. this.justReleased = false;
  709. }
  710. Key.DOWN = -1;
  711. Key.UP = 1;
  712. Key.RESET = 0;
  713. Key.prototype.constructor = Key;
  714. /**
  715. * Update Key status based on new key state.
  716. */
  717. Key.prototype.update = function(action)
  718. {
  719. this.justPressed = false;
  720. this.justReleased = false;
  721. if(action === Key.DOWN)
  722. {
  723. if(this.pressed === false)
  724. {
  725. this.justPressed = true;
  726. }
  727. this.pressed = true;
  728. }
  729. else if(action === Key.UP)
  730. {
  731. if(this.pressed)
  732. {
  733. this.justReleased = true;
  734. }
  735. this.pressed = false;
  736. }
  737. else if(action === Key.RESET)
  738. {
  739. this.justReleased = false;
  740. this.justPressed = false;
  741. }
  742. };
  743. /**
  744. * Set this key attributes manually.
  745. */
  746. Key.prototype.set = function(justPressed, pressed, justReleased)
  747. {
  748. this.justPressed = justPressed;
  749. this.pressed = pressed;
  750. this.justReleased = justReleased;
  751. };
  752. /**
  753. * Reset key to default values.
  754. */
  755. Key.prototype.reset = function()
  756. {
  757. this.justPressed = false;
  758. this.pressed = false;
  759. this.justReleased = false;
  760. };
  761. /**
  762. * Pointer instance for input in sync with the running 3D application.
  763. *
  764. * The pointer object provided by scripts is automatically updated by the runtime handler.
  765. *
  766. * @class
  767. * @param {DOM} domElement DOM element to craete the pointer events.
  768. * @param {Boolean} dontInitialize If true the pointer events are not created.
  769. */
  770. function Pointer(domElement)
  771. {
  772. //Raw data
  773. this._keys = new Array(5);
  774. this._position = new Vector2(0, 0);
  775. this._positionUpdated = false;
  776. this._delta = new Vector2(0, 0);
  777. this._wheel = 0;
  778. this._wheelUpdated = false;
  779. this._doubleClicked = new Array(5);
  780. /**
  781. * Array with pointer buttons status.
  782. */
  783. this.keys = new Array(5);
  784. /**
  785. * Pointer position inside of the window (coordinates in window space).
  786. */
  787. this.position = new Vector2(0, 0);
  788. /**
  789. * Pointer movement (coordinates in window space).
  790. */
  791. this.delta = new Vector2(0, 0);
  792. /**
  793. * Pointer scroll wheel movement.
  794. */
  795. this.wheel = 0;
  796. /**
  797. * Indicates a button of the pointer was double clicked.
  798. */
  799. this.doubleClicked = new Array(5);
  800. /**
  801. * DOM element where to attach the pointer events.
  802. */
  803. this.domElement = (domElement !== undefined) ? domElement : window;
  804. /**
  805. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  806. */
  807. this.canvas = null;
  808. /**
  809. * Event manager responsible for updating the raw data variables.
  810. *
  811. * Diferent events are used depending on the host platform.
  812. *
  813. * When the update method is called the raw data is reset.
  814. */
  815. this.events = new EventManager();
  816. //Initialize key instances
  817. for(var i = 0; i < 5; i++)
  818. {
  819. this._doubleClicked[i] = false;
  820. this.doubleClicked[i] = false;
  821. this._keys[i] = new Key();
  822. this.keys[i] = new Key();
  823. }
  824. //Self pointer
  825. var self = this;
  826. //Scroll wheel
  827. if(window.onmousewheel !== undefined)
  828. {
  829. //Chrome, edge
  830. this.events.add(this.domElement, "mousewheel", function(event)
  831. {
  832. self._wheel = event.deltaY;
  833. self._wheelUpdated = true;
  834. });
  835. }
  836. else if(window.addEventListener !== undefined)
  837. {
  838. //Firefox
  839. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  840. {
  841. self._wheel = event.detail * 30;
  842. self._wheelUpdated = true;
  843. });
  844. }
  845. else
  846. {
  847. this.events.add(this.domElement, "wheel", function(event)
  848. {
  849. self._wheel = event.deltaY;
  850. self._wheelUpdated = true;
  851. });
  852. }
  853. //Touchscreen input events
  854. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  855. {
  856. //Auxiliar variables to calculate touch delta
  857. var lastTouch = new Vector2(0, 0);
  858. //Touch start event
  859. this.events.add(this.domElement, "touchstart", function(event)
  860. {
  861. var touch = event.touches[0];
  862. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  863. self.updateKey(Pointer.LEFT, Key.DOWN);
  864. lastTouch.set(touch.clientX, touch.clientY);
  865. });
  866. //Touch end event
  867. this.events.add(this.domElement, "touchend", function(event)
  868. {
  869. self.updateKey(Pointer.LEFT, Key.UP);
  870. });
  871. //Touch cancel event
  872. this.events.add(this.domElement, "touchcancel", function(event)
  873. {
  874. self.updateKey(Pointer.LEFT, Key.UP);
  875. });
  876. //Touch move event
  877. this.events.add(document.body, "touchmove", function(event)
  878. {
  879. var touch = event.touches[0];
  880. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  881. lastTouch.set(touch.clientX, touch.clientY);
  882. });
  883. }
  884. //Move
  885. this.events.add(this.domElement, "mousemove", function(event)
  886. {
  887. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  888. });
  889. //Button pressed
  890. this.events.add(this.domElement, "mousedown", function(event)
  891. {
  892. self.updateKey(event.which - 1, Key.DOWN);
  893. });
  894. //Button released
  895. this.events.add(this.domElement, "mouseup", function(event)
  896. {
  897. self.updateKey(event.which - 1, Key.UP);
  898. });
  899. //Drag start
  900. this.events.add(this.domElement, "dragstart", function(event)
  901. {
  902. self.updateKey(event.which - 1, Key.UP);
  903. });
  904. //Pointer double click
  905. this.events.add(this.domElement, "dblclick", function(event)
  906. {
  907. self._doubleClicked[event.which - 1] = true;
  908. });
  909. this.create();
  910. }
  911. Pointer.prototype = Pointer;
  912. Pointer.prototype.constructor = Pointer;
  913. /**
  914. * Left pointer button.
  915. */
  916. Pointer.LEFT = 0;
  917. /**
  918. * Middle pointer button.
  919. */
  920. Pointer.MIDDLE = 1;
  921. /**
  922. * Right pointer button.
  923. */
  924. Pointer.RIGHT = 2;
  925. /**
  926. * Back pointer navigation button.
  927. */
  928. Pointer.BACK = 3;
  929. /**
  930. * Forward pointer navigation button.
  931. */
  932. Pointer.FORWARD = 4;
  933. /**
  934. * Element to be used for coordinates calculation relative to that canvas.
  935. *
  936. * @param {DOM} canvas Canvas to be attached to the Pointer instance
  937. */
  938. Pointer.setCanvas = function(element)
  939. {
  940. this.canvas = element;
  941. element.pointerInside = false;
  942. element.addEventListener("mouseenter", function()
  943. {
  944. this.pointerInside = true;
  945. });
  946. element.addEventListener("mouseleave", function()
  947. {
  948. this.pointerInside = false;
  949. });
  950. };
  951. /**
  952. * Check if pointer is inside attached canvas (updated async).
  953. *
  954. * @return {boolean} True if pointer is currently inside the canvas
  955. */
  956. Pointer.insideCanvas = function()
  957. {
  958. return this.canvas !== null && this.canvas.pointerInside;
  959. };
  960. /**
  961. * Check if pointer button is currently pressed.
  962. *
  963. * @param {Number} button Button to check status of
  964. * @return {boolean} True if button is currently pressed
  965. */
  966. Pointer.buttonPressed = function(button)
  967. {
  968. return this.keys[button].pressed;
  969. };
  970. /**
  971. * Check if pointer button was double clicked.
  972. *
  973. * @param {Number} button Button to check status of
  974. * @return {boolean} True if some pointer button was just double clicked
  975. */
  976. Pointer.buttonDoubleClicked = function(button)
  977. {
  978. return this.doubleClicked[button];
  979. };
  980. /**
  981. * Check if a pointer button was just pressed.
  982. *
  983. * @param {Number} button Button to check status of
  984. * @return {boolean} True if button was just pressed
  985. */
  986. Pointer.buttonJustPressed = function(button)
  987. {
  988. return this.keys[button].justPressed;
  989. };
  990. /**
  991. * Check if a pointer button was just released.
  992. *
  993. * @param {Number} button Button to check status of
  994. * @return {boolean} True if button was just released
  995. */
  996. Pointer.buttonJustReleased = function(button)
  997. {
  998. return this.keys[button].justReleased;
  999. };
  1000. /**
  1001. * Update pointer position.
  1002. *
  1003. * Automatically called by the runtime.
  1004. *
  1005. * @param {Number} x
  1006. * @param {Number} y
  1007. * @param {Number} xDiff
  1008. * @param {Number} yDiff
  1009. */
  1010. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  1011. {
  1012. if(this.canvas !== null)
  1013. {
  1014. var rect = this.canvas.getBoundingClientRect();
  1015. x -= rect.left;
  1016. y -= rect.top;
  1017. }
  1018. this._position.set(x, y);
  1019. this._delta.x += xDiff;
  1020. this._delta.y += yDiff;
  1021. this._positionUpdated = true;
  1022. };
  1023. /**
  1024. * Update a pointer button.
  1025. *
  1026. * Automatically called by the runtime.
  1027. *
  1028. * @param {Number} button
  1029. * @param {Number} action
  1030. */
  1031. Pointer.updateKey = function(button, action)
  1032. {
  1033. if(button > -1)
  1034. {
  1035. this._keys[button].update(action);
  1036. }
  1037. };
  1038. /**
  1039. * Update pointer buttons state, position, wheel and delta synchronously.
  1040. */
  1041. Pointer.update = function()
  1042. {
  1043. //Update pointer keys state
  1044. for(var i = 0; i < 5; i++)
  1045. {
  1046. if(this._keys[i].justPressed && this.keys[i].justPressed)
  1047. {
  1048. this._keys[i].justPressed = false;
  1049. }
  1050. if(this._keys[i].justReleased && this.keys[i].justReleased)
  1051. {
  1052. this._keys[i].justReleased = false;
  1053. }
  1054. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  1055. //Update pointer double click
  1056. if(this._doubleClicked[i] === true)
  1057. {
  1058. this.doubleClicked[i] = true;
  1059. this._doubleClicked[i] = false;
  1060. }
  1061. else
  1062. {
  1063. this.doubleClicked[i] = false;
  1064. }
  1065. }
  1066. //Update pointer wheel
  1067. if(this._wheelUpdated)
  1068. {
  1069. this.wheel = this._wheel;
  1070. this._wheelUpdated = false;
  1071. }
  1072. else
  1073. {
  1074. this.wheel = 0;
  1075. }
  1076. //Update pointer Position if needed
  1077. if(this._positionUpdated)
  1078. {
  1079. this.delta.copy(this._delta);
  1080. this.position.copy(this._position);
  1081. this._delta.set(0,0);
  1082. this._positionUpdated = false;
  1083. }
  1084. else
  1085. {
  1086. this.delta.x = 0;
  1087. this.delta.y = 0;
  1088. }
  1089. };
  1090. /**
  1091. * Create pointer events.
  1092. */
  1093. Pointer.create = function()
  1094. {
  1095. this.events.create();
  1096. };
  1097. /**
  1098. * Dispose pointer events.
  1099. */
  1100. Pointer.dispose = function()
  1101. {
  1102. this.events.destroy();
  1103. };
  1104. /**
  1105. * The renderer is resposible for drawing the structure into the canvas element.
  1106. *
  1107. * Its also resposible for managing the canvas state.
  1108. *
  1109. * @class
  1110. */
  1111. function Renderer(canvas)
  1112. {
  1113. /**
  1114. * Canvas DOM element, has to be managed by the user.
  1115. */
  1116. this.canvas = canvas;
  1117. /**
  1118. * Canvas 2D rendering context used to draw content.
  1119. */
  1120. this.context = canvas.getContext("2d");
  1121. this.context.imageSmoothingEnabled = true;
  1122. this.context.globalCompositeOperation = "source-over";
  1123. /**
  1124. * Pointer input handler object.
  1125. */
  1126. this.pointer = new Pointer();
  1127. this.pointer.setCanvas(canvas);
  1128. }
  1129. /**
  1130. * Update the renderer state, update the input handlers, calculate the object and viewport transformation matrices.
  1131. *
  1132. * @param object Object to be updated.
  1133. * @param viewport Viewport to be updated (should be the one where the objects will be rendered after).
  1134. */
  1135. Renderer.prototype.update = function(object, viewport)
  1136. {
  1137. this.pointer.update();
  1138. var pointer = this.pointer;
  1139. // Viewport transform matrix
  1140. viewport.updateControls(pointer);
  1141. viewport.updateMatrix();
  1142. // Project pointer coordinates
  1143. var point = pointer.position.clone();
  1144. var viewportPoint = viewport.inverseMatrix.transformPoint(point);
  1145. // Object transformation matrices
  1146. object.traverse(function(child)
  1147. {
  1148. var childPoint = child.inverseGlobalMatrix.transformPoint(viewportPoint);
  1149. // Check if the pointer pointer is inside
  1150. if(child.isInside(childPoint))
  1151. {
  1152. // Pointer enter
  1153. if(!child.pointerInside && child.onPointerEnter !== null)
  1154. {
  1155. child.onPointerEnter(pointer, viewport);
  1156. }
  1157. // Pointer over
  1158. if(child.onPointerOver !== null)
  1159. {
  1160. child.onPointerOver(pointer, viewport);
  1161. }
  1162. // Pointer just pressed
  1163. if(pointer.buttonJustPressed(Pointer.LEFT))
  1164. {
  1165. if(child.onButtonDown !== null)
  1166. {
  1167. child.onButtonDown(pointer, viewport);
  1168. }
  1169. if(child.draggable)
  1170. {
  1171. child.beingDragged = true;
  1172. }
  1173. }
  1174. // Pointer pressed
  1175. if(pointer.buttonPressed(Pointer.LEFT) && child.onButtonPressed !== null)
  1176. {
  1177. child.onButtonPressed(pointer, viewport);
  1178. }
  1179. // Just released
  1180. if(pointer.buttonJustReleased(Pointer.LEFT) && child.onButtonUp !== null)
  1181. {
  1182. child.onButtonUp(pointer, viewport);
  1183. }
  1184. child.pointerInside = true;
  1185. }
  1186. else if(child.pointerInside)
  1187. {
  1188. // Pointer leave
  1189. if(child.onPointerLeave !== null)
  1190. {
  1191. child.onPointerLeave(pointer, viewport);
  1192. }
  1193. child.pointerInside = false;
  1194. }
  1195. // Stop object drag
  1196. if(pointer.buttonJustReleased(Pointer.LEFT))
  1197. {
  1198. if(child.draggable)
  1199. {
  1200. child.beingDragged = false;
  1201. }
  1202. }
  1203. // Pointer drag event
  1204. if(child.beingDragged)
  1205. {
  1206. var matrix = viewport.inverseMatrix.clone();
  1207. matrix.multiply(child.inverseGlobalMatrix);
  1208. matrix.setPosition(0, 0);
  1209. var delta = matrix.transformPoint(pointer.delta);
  1210. child.position.add(delta);
  1211. if(child.onPointerDrag !== null)
  1212. {
  1213. child.onPointerDrag(pointer, viewport, delta);
  1214. }
  1215. }
  1216. // On update
  1217. if(child.onUpdate !== null)
  1218. {
  1219. child.onUpdate();
  1220. }
  1221. child.updateMatrix();
  1222. });
  1223. };
  1224. /**
  1225. * Render the object using the viewport into a canvas element.
  1226. *
  1227. * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
  1228. *
  1229. * @param object Object to be rendered.
  1230. * @param viewport Viewport to render the objects.
  1231. */
  1232. Renderer.prototype.render = function(object, viewport)
  1233. {
  1234. var context = this.context;
  1235. // Clear canvas
  1236. context.setTransform(1, 0, 0, 1, 0, 0);
  1237. context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  1238. // Set viewport matrix transform
  1239. viewport.matrix.setContextTransform(context);
  1240. // Get objects to be rendered
  1241. var objects = [];
  1242. object.traverse(function(child)
  1243. {
  1244. if(child.visible)
  1245. {
  1246. objects.push(child);
  1247. }
  1248. });
  1249. // Sort objects by layer
  1250. objects.sort(function(a, b)
  1251. {
  1252. return a.layer - b.layer;
  1253. });
  1254. // Render into the canvas
  1255. for(var i = 0; i < objects.length; i++)
  1256. {
  1257. context.save();
  1258. objects[i].globalMatrix.tranformContext(context);
  1259. objects[i].draw(context);
  1260. context.restore();
  1261. }
  1262. };
  1263. /**
  1264. * Used to indicate how the user views the content inside of the canvas.
  1265. *
  1266. * @class
  1267. */
  1268. function Viewport()
  1269. {
  1270. /**
  1271. * UUID of the object.
  1272. */
  1273. this.uuid = UUID.generate();
  1274. /**
  1275. * Position of the object.
  1276. */
  1277. this.position = new Vector2(0, 0);
  1278. /**
  1279. * Scale of the object.
  1280. */
  1281. this.scale = 1.0;
  1282. /**
  1283. * Rotation of the object relative to its center.
  1284. */
  1285. this.rotation = 0.0;
  1286. /**
  1287. * Local transformation matrix applied to the object.
  1288. */
  1289. this.matrix = new Matrix();
  1290. /**
  1291. * Inverse of the local transformation matrix.
  1292. */
  1293. this.inverseMatrix = new Matrix();
  1294. /**
  1295. * If true the matrix is updated before rendering the object.
  1296. */
  1297. this.matrixNeedsUpdate = true;
  1298. /**
  1299. * Flag to indicate if the viewport should move when scalling.
  1300. *
  1301. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  1302. */
  1303. this.moveOnScale = true;
  1304. }
  1305. /**
  1306. * Update the viewport controls using the pointer object.
  1307. */
  1308. Viewport.prototype.updateControls = function(pointer)
  1309. {
  1310. if(pointer.wheel !== 0)
  1311. {
  1312. this.scale -= pointer.wheel * 1e-3 * this.scale;
  1313. if(this.moveOnScale)
  1314. {
  1315. var speed = pointer.wheel / this.scale;
  1316. var halfWidth = pointer.canvas.width / 2;
  1317. var halfWeight = pointer.canvas.height / 2;
  1318. this.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
  1319. this.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
  1320. }
  1321. }
  1322. if(pointer.buttonPressed(Pointer.RIGHT))
  1323. {
  1324. this.position.x += pointer.delta.x;
  1325. this.position.y += pointer.delta.y;
  1326. }
  1327. };
  1328. /**
  1329. * Calculate and update the viewport transformation matrix.
  1330. */
  1331. Viewport.prototype.updateMatrix = function()
  1332. {
  1333. if(this.matrixNeedsUpdate)
  1334. {
  1335. this.matrix.compose(this.position.x, this.position.y, this.scale, this.scale, this.rotation);
  1336. this.inverseMatrix = this.matrix.getInverse();
  1337. //this.matrixNeedsUpdate = false;
  1338. }
  1339. };
  1340. /**
  1341. * Box is described by a minimum and maximum points.
  1342. *
  1343. * Can be used for collision detection with points and other boxes.
  1344. */
  1345. function Box2(min, max)
  1346. {
  1347. this.min = (min !== undefined) ? min : new Vector2();
  1348. this.max = (max !== undefined) ? max : new Vector2();
  1349. }
  1350. Object.assign(Box2.prototype,
  1351. {
  1352. set: function(min, max)
  1353. {
  1354. this.min.copy(min);
  1355. this.max.copy(max);
  1356. return this;
  1357. },
  1358. setFromPoints: function(points)
  1359. {
  1360. this.min = new Vector2(+Infinity, +Infinity);
  1361. this.max = new Vector2(-Infinity, -Infinity);
  1362. for(var i = 0, il = points.length; i < il; i++)
  1363. {
  1364. this.expandByPoint(points[i]);
  1365. }
  1366. return this;
  1367. },
  1368. setFromCenterAndSize: function(center, size)
  1369. {
  1370. var v1 = new Vector2();
  1371. var halfSize = v1.copy(size).multiplyScalar(0.5);
  1372. this.min.copy(center).sub(halfSize);
  1373. this.max.copy(center).add(halfSize);
  1374. return this;
  1375. },
  1376. clone: function()
  1377. {
  1378. var box = new Box2();
  1379. box.copy(this);
  1380. return box;
  1381. },
  1382. copy: function(box)
  1383. {
  1384. this.min.copy(box.min);
  1385. this.max.copy(box.max);
  1386. return this;
  1387. },
  1388. isEmpty: function()
  1389. {
  1390. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  1391. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  1392. },
  1393. getCenter: function(target)
  1394. {
  1395. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  1396. },
  1397. getSize: function(target)
  1398. {
  1399. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  1400. },
  1401. expandByPoint: function(point)
  1402. {
  1403. this.min.min(point);
  1404. this.max.max(point);
  1405. return this;
  1406. },
  1407. expandByVector: function(vector)
  1408. {
  1409. this.min.sub(vector);
  1410. this.max.add(vector);
  1411. return this;
  1412. },
  1413. expandByScalar: function(scalar)
  1414. {
  1415. this.min.addScalar(-scalar);
  1416. this.max.addScalar(scalar);
  1417. return this;
  1418. },
  1419. containsPoint: function(point)
  1420. {
  1421. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  1422. },
  1423. containsBox: function(box)
  1424. {
  1425. 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;
  1426. },
  1427. getParameter: function(point, target)
  1428. {
  1429. // This can potentially have a divide by zero if the box
  1430. // has a size dimension of 0.
  1431. return target.set(
  1432. (point.x - this.min.x) / (this.max.x - this.min.x),
  1433. (point.y - this.min.y) / (this.max.y - this.min.y)
  1434. );
  1435. },
  1436. intersectsBox: function(box)
  1437. {
  1438. // using 4 splitting planes to rule out intersections
  1439. 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;
  1440. },
  1441. clampPoint: function(point, target)
  1442. {
  1443. return target.copy(point).clamp(this.min, this.max);
  1444. },
  1445. distanceToPoint: function(point)
  1446. {
  1447. var v = new Vector2();
  1448. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  1449. return clampedPoint.sub(point).length();
  1450. },
  1451. intersect: function(box)
  1452. {
  1453. this.min.max(box.min);
  1454. this.max.min(box.max);
  1455. return this;
  1456. },
  1457. union: function(box)
  1458. {
  1459. this.min.min(box.min);
  1460. this.max.max(box.max);
  1461. return this;
  1462. },
  1463. translate: function(offset)
  1464. {
  1465. this.min.add(offset);
  1466. this.max.add(offset);
  1467. return this;
  1468. },
  1469. equals: function(box)
  1470. {
  1471. return box.min.equals(this.min) && box.max.equals(this.max);
  1472. }
  1473. });
  1474. /**
  1475. * Circle object draw a circular object.
  1476. */
  1477. function Circle()
  1478. {
  1479. Object2D.call(this);
  1480. /**
  1481. * Radius of the circle.
  1482. */
  1483. this.radius = 10.0;
  1484. /**
  1485. * Color of the circle border line.
  1486. */
  1487. this.strokeStyle = "#000000";
  1488. /**
  1489. * Background color of the circle.
  1490. */
  1491. this.fillStyle = "#FFFFFF";
  1492. }
  1493. Circle.prototype = Object.create(Object2D.prototype);
  1494. Circle.prototype.isInside = function(point)
  1495. {
  1496. return point.length() <= this.radius;
  1497. };
  1498. Circle.prototype.onPointerEnter = function(pointer, viewport)
  1499. {
  1500. this.fillStyle = "#CCCCCC";
  1501. };
  1502. Circle.prototype.onPointerLeave = function(pointer, viewport)
  1503. {
  1504. this.fillStyle = "#FFFFFF";
  1505. };
  1506. Circle.prototype.draw = function(context)
  1507. {
  1508. context.fillStyle = this.fillStyle;
  1509. context.beginPath();
  1510. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  1511. context.fill();
  1512. context.lineWidth = 1;
  1513. context.strokeStyle = this.strokeStyle;
  1514. context.beginPath();
  1515. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  1516. context.stroke();
  1517. };
  1518. /**
  1519. * Box object draw a box.
  1520. */
  1521. function Box(resizable)
  1522. {
  1523. Object2D.call(this);
  1524. /**
  1525. * Box object containing the size of the object.
  1526. */
  1527. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  1528. /**
  1529. * Color of the box border line.
  1530. */
  1531. this.strokeStyle = "#000000";
  1532. /**
  1533. * Background color of the box.
  1534. */
  1535. this.fillStyle = "#FFFFFF";
  1536. if(resizable)
  1537. {
  1538. this.createResizeHelpers();
  1539. }
  1540. }
  1541. Box.prototype = Object.create(Object2D.prototype);
  1542. Box.prototype.createResizeHelpers = function(first_argument)
  1543. {
  1544. var self = this;
  1545. function updateHelpers()
  1546. {
  1547. topRight.position.copy(self.box.min);
  1548. bottomLeft.position.copy(self.box.max);
  1549. topLeft.position.set(self.box.max.x, self.box.min.y);
  1550. bottomRight.position.set(self.box.min.x, self.box.max.y);
  1551. }
  1552. var topRight = new Circle();
  1553. topRight.radius = 4;
  1554. topRight.onPointerDrag = function(pointer, viewport, delta)
  1555. {
  1556. self.box.min.copy(topRight.position);
  1557. updateHelpers();
  1558. };
  1559. this.add(topRight);
  1560. var topLeft = new Circle();
  1561. topLeft.radius = 4;
  1562. topLeft.onPointerDrag = function(pointer, viewport, delta)
  1563. {
  1564. self.box.max.x = topLeft.position.x;
  1565. self.box.min.y = topLeft.position.y;
  1566. updateHelpers();
  1567. };
  1568. this.add(topLeft);
  1569. var bottomLeft = new Circle();
  1570. bottomLeft.radius = 4;
  1571. bottomLeft.onPointerDrag = function(pointer, viewport, delta)
  1572. {
  1573. self.box.max.copy(bottomLeft.position);
  1574. updateHelpers();
  1575. };
  1576. this.add(bottomLeft);
  1577. var bottomRight = new Circle();
  1578. bottomRight.radius = 4;
  1579. bottomRight.onPointerDrag = function(pointer, viewport, delta)
  1580. {
  1581. self.box.min.x = bottomRight.position.x;
  1582. self.box.max.y = bottomRight.position.y;
  1583. updateHelpers();
  1584. };
  1585. this.add(bottomRight);
  1586. updateHelpers();
  1587. };
  1588. Box.prototype.onPointerEnter = function(pointer, viewport)
  1589. {
  1590. this.fillStyle = "#CCCCCC";
  1591. };
  1592. Box.prototype.onPointerLeave = function(pointer, viewport)
  1593. {
  1594. this.fillStyle = "#FFFFFF";
  1595. };
  1596. Box.prototype.isInside = function(point)
  1597. {
  1598. return this.box.containsPoint(point);
  1599. };
  1600. Box.prototype.draw = function(context)
  1601. {
  1602. var width = this.box.max.x - this.box.min.x;
  1603. var height = this.box.max.y - this.box.min.y;
  1604. context.fillStyle = this.fillStyle;
  1605. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  1606. context.lineWidth = 1;
  1607. context.strokeStyle = this.strokeStyle;
  1608. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  1609. };
  1610. /**
  1611. * Line object draw a line from one point to another.
  1612. */
  1613. function Line()
  1614. {
  1615. Object2D.call(this);
  1616. /**
  1617. * Initial point of the line.
  1618. *
  1619. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  1620. */
  1621. this.from = new Vector2();
  1622. /**
  1623. * Final point of the line.
  1624. *
  1625. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  1626. */
  1627. this.to = new Vector2();
  1628. /**
  1629. * Color of the line.
  1630. */
  1631. this.strokeStyle = "#000000";
  1632. }
  1633. Line.prototype = Object.create(Object2D.prototype);
  1634. Line.prototype.draw = function(context)
  1635. {
  1636. context.lineWidth = 1;
  1637. context.strokeStyle = this.strokeStyle;
  1638. context.setLineDash([5, 5]);
  1639. context.beginPath();
  1640. context.moveTo(this.from.x, this.from.y);
  1641. context.lineTo(this.to.x, this.to.y);
  1642. context.stroke();
  1643. };
  1644. function Text()
  1645. {
  1646. Object2D.call(this);
  1647. /**
  1648. * Text value.
  1649. */
  1650. this.text = "";
  1651. /**
  1652. * Font of the text.
  1653. */
  1654. this.font = "16px Arial";
  1655. /**
  1656. * Color (style) of the text.
  1657. */
  1658. this.color = "#000000";
  1659. }
  1660. Text.prototype = Object.create(Object2D.prototype);
  1661. Text.prototype.draw = function(context)
  1662. {
  1663. context.font = this.font;
  1664. context.textAlign = "center";
  1665. context.fillStyle = this.color;
  1666. context.fillText(this.text, 0, 0);
  1667. };
  1668. function Image(src)
  1669. {
  1670. Object2D.call(this);
  1671. this.image = document.createElement("img");
  1672. this.image.src = src;
  1673. }
  1674. Image.prototype = Object.create(Object2D.prototype);
  1675. Image.prototype.draw = function(context)
  1676. {
  1677. context.drawImage(this.image, 0, 0);
  1678. };
  1679. export { Box, Box2, Circle, EventManager, Image, Key, Line, Matrix, Object2D, Pointer, Renderer, Text, UUID, Vector2, Viewport };