trenette.js 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = global || self, factory(global.Trenette = {}));
  5. }(this, function (exports) { 'use strict';
  6. /**
  7. * EventManager is used to manager DOM events creationg and destruction in a single function call.
  8. *
  9. * It is used by objects to make it easier to add and remove events from global DOM objects.
  10. *
  11. * @class
  12. */
  13. function EventManager()
  14. {
  15. /**
  16. * Stores all events in the manager, their target and callback.
  17. *
  18. * Format [target, event, callback, active]
  19. *
  20. * @type {Array}
  21. */
  22. this.events = [];
  23. }
  24. /**
  25. * Add new event to the manager.
  26. *
  27. * @param {DOM} target Event target element.
  28. * @param {String} event Event name.
  29. * @param {Function} callback Callback function.
  30. */
  31. EventManager.prototype.add = function(target, event, callback)
  32. {
  33. this.events.push([target, event, callback, false]);
  34. };
  35. /**
  36. * Destroys this manager and remove all events.
  37. */
  38. EventManager.prototype.clear = function()
  39. {
  40. this.destroy();
  41. this.events = [];
  42. };
  43. /**
  44. * Creates all events in this manager.
  45. */
  46. EventManager.prototype.create = function()
  47. {
  48. for(var i = 0; i < this.events.length; i++)
  49. {
  50. var event = this.events[i];
  51. event[0].addEventListener(event[1], event[2]);
  52. event[3] = true;
  53. }
  54. };
  55. /**
  56. * Removes all events in this manager.
  57. */
  58. EventManager.prototype.destroy = function()
  59. {
  60. for(var i = 0; i < this.events.length; i++)
  61. {
  62. var event = this.events[i];
  63. event[0].removeEventListener(event[1], event[2]);
  64. event[3] = false;
  65. }
  66. };
  67. /**
  68. * Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.
  69. *
  70. * @class
  71. * @param {number} x
  72. * @param {number} y
  73. */
  74. function Vector2(x, y)
  75. {
  76. this.x = x || 0;
  77. this.y = y || 0;
  78. }
  79. /**
  80. * Set vector x and y values.
  81. *
  82. * @param {number} x
  83. * @param {number} y
  84. */
  85. Vector2.prototype.set = function(x, y)
  86. {
  87. this.x = x;
  88. this.y = y;
  89. };
  90. /**
  91. * Set a scalar value into the x and y values.
  92. */
  93. Vector2.prototype.setScalar = function(scalar)
  94. {
  95. this.x = scalar;
  96. this.y = scalar;
  97. };
  98. /**
  99. * Create a clone of this vector object.
  100. */
  101. Vector2.prototype.clone = function()
  102. {
  103. return new Vector2(this.x, this.y);
  104. };
  105. /**
  106. * Copy the content of another vector into this one.
  107. *
  108. * @param {Vector2} v
  109. */
  110. Vector2.prototype.copy = function(v)
  111. {
  112. this.x = v.x;
  113. this.y = v.y;
  114. };
  115. /**
  116. * Add the content of another vector to this one.
  117. *
  118. * @param {Vector2} v
  119. */
  120. Vector2.prototype.add = function(v)
  121. {
  122. this.x += v.x;
  123. this.y += v.y;
  124. };
  125. /**
  126. * Add a scalar value to booth vector components.
  127. *
  128. * @param {number} s
  129. */
  130. Vector2.prototype.addScalar = function(s)
  131. {
  132. this.x += s;
  133. this.y += s;
  134. };
  135. /**
  136. * Add two vectors and store the result in this vector.
  137. *
  138. * @param {Vector2} a
  139. * @param {Vector2} b
  140. */
  141. Vector2.prototype.addVectors = function(a, b)
  142. {
  143. this.x = a.x + b.x;
  144. this.y = a.y + b.y;
  145. };
  146. /**
  147. * Scale a vector components and add the result to this vector.
  148. *
  149. * @param {Vector2} v
  150. * @param {number} s
  151. */
  152. Vector2.prototype.addScaledVector = function(v, s)
  153. {
  154. this.x += v.x * s;
  155. this.y += v.y * s;
  156. };
  157. /**
  158. * Subtract the content of another vector to this one.
  159. *
  160. * @param {Vector2} v
  161. */
  162. Vector2.prototype.sub = function(v)
  163. {
  164. this.x -= v.x;
  165. this.y -= v.y;
  166. };
  167. /**
  168. * Subtract a scalar value to booth vector components.
  169. *
  170. * @param {number} s
  171. */
  172. Vector2.prototype.subScalar = function(s)
  173. {
  174. this.x -= s;
  175. this.y -= s;
  176. };
  177. /**
  178. * Subtract two vectors and store the result in this vector.
  179. *
  180. * @param {Vector2} a
  181. * @param {Vector2} b
  182. */
  183. Vector2.prototype.subVectors = function(a, b)
  184. {
  185. this.x = a.x - b.x;
  186. this.y = a.y - b.y;
  187. };
  188. /**
  189. * Multiply the content of another vector to this one.
  190. *
  191. * @param {Vector2} v
  192. */
  193. Vector2.prototype.multiply = function(v)
  194. {
  195. this.x *= v.x;
  196. this.y *= v.y;
  197. };
  198. /**
  199. * Multiply a scalar value by booth vector components.
  200. *
  201. * @param {number} s
  202. */
  203. Vector2.prototype.multiplyScalar = function(scalar)
  204. {
  205. this.x *= scalar;
  206. this.y *= scalar;
  207. };
  208. /**
  209. * Divide the content of another vector from this one.
  210. *
  211. * @param {Vector2} v
  212. */
  213. Vector2.prototype.divide = function(v)
  214. {
  215. this.x /= v.x;
  216. this.y /= v.y;
  217. };
  218. /**
  219. * Divide a scalar value by booth vector components.
  220. *
  221. * @param {number} s
  222. */
  223. Vector2.prototype.divideScalar = function(scalar)
  224. {
  225. return this.multiplyScalar(1 / scalar);
  226. };
  227. /**
  228. * Set the minimum of x and y coordinates between two vectors.
  229. *
  230. * X is set as the min between this vector and the other vector.
  231. *
  232. * @param {Vector2} v
  233. */
  234. Vector2.prototype.min = function(v)
  235. {
  236. this.x = Math.min(this.x, v.x);
  237. this.y = Math.min(this.y, v.y);
  238. };
  239. /**
  240. * Set the maximum of x and y coordinates between two vectors.
  241. *
  242. * X is set as the max between this vector and the other vector.
  243. *
  244. * @param {Vector2} v
  245. */
  246. Vector2.prototype.max = function(v)
  247. {
  248. this.x = Math.max(this.x, v.x);
  249. this.y = Math.max(this.y, v.y);
  250. };
  251. Vector2.prototype.clamp = function(min, max)
  252. {
  253. // assumes min < max, componentwise
  254. this.x = Math.max(min.x, Math.min(max.x, this.x));
  255. this.y = Math.max(min.y, Math.min(max.y, this.y));
  256. };
  257. Vector2.prototype.clampScalar = function(minVal, maxVal)
  258. {
  259. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  260. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  261. };
  262. Vector2.prototype.clampLength = function(min, max)
  263. {
  264. var length = this.length();
  265. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  266. };
  267. /**
  268. * Round the vector coordinates to integer by flooring to the smaller integer.
  269. */
  270. Vector2.prototype.floor = function()
  271. {
  272. this.x = Math.floor(this.x);
  273. this.y = Math.floor(this.y);
  274. };
  275. /**
  276. * Round the vector coordinates to integer by ceiling to the bigger integer.
  277. */
  278. Vector2.prototype.ceil = function()
  279. {
  280. this.x = Math.ceil(this.x);
  281. this.y = Math.ceil(this.y);
  282. };
  283. /**
  284. * Round the vector coordinates to their closest integer.
  285. */
  286. Vector2.prototype.round = function()
  287. {
  288. this.x = Math.round(this.x);
  289. this.y = Math.round(this.y);
  290. };
  291. /**
  292. * Negate the coordinates of this vector.
  293. */
  294. Vector2.prototype.negate = function()
  295. {
  296. this.x = -this.x;
  297. this.y = -this.y;
  298. return this;
  299. };
  300. /**
  301. * Dot multiplication between this vector and another vector.
  302. *
  303. * @param {Vector2} vector
  304. * @return {number} Result of the dot multiplication.
  305. */
  306. Vector2.prototype.dot = function(v)
  307. {
  308. return this.x * v.x + this.y * v.y;
  309. };
  310. /**
  311. * Cross multiplication between this vector and another vector.
  312. *
  313. * @param {Vector2} vector
  314. * @return {number} Result of the cross multiplication.
  315. */
  316. Vector2.prototype.cross = function(v)
  317. {
  318. return this.x * v.y - this.y * v.x;
  319. };
  320. /**
  321. * Squared length of the vector.
  322. *
  323. * Faster for comparions.
  324. */
  325. Vector2.prototype.lengthSq = function()
  326. {
  327. return this.x * this.x + this.y * this.y;
  328. };
  329. /**
  330. * Length of the vector.
  331. */
  332. Vector2.prototype.length = function()
  333. {
  334. return Math.sqrt(this.x * this.x + this.y * this.y);
  335. };
  336. /**
  337. * Manhattan length of the vector.
  338. */
  339. Vector2.prototype.manhattanLength = function()
  340. {
  341. return Math.abs(this.x) + Math.abs(this.y);
  342. };
  343. /**
  344. * Normalize the vector (make it length one).
  345. */
  346. Vector2.prototype.normalize = function()
  347. {
  348. return this.divideScalar(this.length() || 1);
  349. };
  350. /**
  351. * Computes the angle in radians with respect to the positive x-axis
  352. */
  353. Vector2.prototype.angle = function()
  354. {
  355. var angle = Math.atan2(this.y, this.x);
  356. if(angle < 0)
  357. {
  358. angle += 2 * Math.PI;
  359. }
  360. return angle;
  361. };
  362. /**
  363. * Distance between two vector positions.
  364. */
  365. Vector2.prototype.distanceTo = function(v)
  366. {
  367. return Math.sqrt(this.distanceToSquared(v));
  368. };
  369. /**
  370. * Distance between two vector positions squared.
  371. *
  372. * Faster for comparisons.
  373. */
  374. Vector2.prototype.distanceToSquared = function(v)
  375. {
  376. var dx = this.x - v.x;
  377. var dy = this.y - v.y;
  378. return dx * dx + dy * dy;
  379. };
  380. /**
  381. * Manhattan distance between two vector positions.
  382. */
  383. Vector2.prototype.manhattanDistanceTo = function(v)
  384. {
  385. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  386. };
  387. /**
  388. * Scale the vector to have a defined length value.
  389. */
  390. Vector2.prototype.setLength = function(length)
  391. {
  392. return this.normalize().multiplyScalar(length);
  393. };
  394. Vector2.prototype.lerp = function(v, alpha)
  395. {
  396. this.x += (v.x - this.x) * alpha;
  397. this.y += (v.y - this.y) * alpha;
  398. };
  399. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  400. {
  401. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  402. };
  403. /**
  404. * Check if two vectors are equal.
  405. *
  406. * @param {Vector2} v
  407. */
  408. Vector2.prototype.equals = function(v)
  409. {
  410. return ((v.x === this.x) && (v.y === this.y));
  411. };
  412. /**
  413. * Set vector value from array with a offset.
  414. *
  415. * @param {array} array
  416. * @param {number} [offset]
  417. */
  418. Vector2.prototype.fromArray = function(array, offset)
  419. {
  420. if(offset === undefined) offset = 0;
  421. this.x = array[offset];
  422. this.y = array[offset + 1];
  423. };
  424. /**
  425. * Convert this vector to an array.
  426. *
  427. * @param {array} array
  428. * @param {number} [offset]
  429. */
  430. Vector2.prototype.toArray = function(array, offset)
  431. {
  432. if(array === undefined) array = [];
  433. if(offset === undefined) offset = 0;
  434. array[offset] = this.x;
  435. array[offset + 1] = this.y;
  436. return array;
  437. };
  438. /**
  439. * Rotate the vector around a central point.
  440. *
  441. * @param {Vector2} center
  442. * @param {number} angle
  443. */
  444. Vector2.prototype.rotateAround = function(center, angle)
  445. {
  446. var c = Math.cos(angle);
  447. var s = Math.sin(angle);
  448. var x = this.x - center.x;
  449. var y = this.y - center.y;
  450. this.x = x * c - y * s + center.x;
  451. this.y = x * s + y * c + center.y;
  452. };
  453. /**
  454. * 2D 3x2 transformation matrix, applied to the canvas elements.
  455. *
  456. * The values of the matrix are stored in a numeric array.
  457. *
  458. * @class
  459. * @param {array} [values]
  460. */
  461. function Matrix(values)
  462. {
  463. if(values !== undefined)
  464. {
  465. this.m = values;
  466. }
  467. else
  468. {
  469. this.identity();
  470. }
  471. }
  472. /**
  473. * Copy the content of another matrix and store in this one.
  474. */
  475. Matrix.prototype.copy = function(mat)
  476. {
  477. this.m = mat.m.slice(0);
  478. };
  479. /**
  480. * Create a new matrix object with a copy of the content of this one.
  481. */
  482. Matrix.prototype.clone = function()
  483. {
  484. return new Matrix(this.m.slice(0))
  485. };
  486. /**
  487. * Reset this matrix to indentity.
  488. */
  489. Matrix.prototype.identity = function()
  490. {
  491. this.m = [1, 0, 0, 1, 0, 0];
  492. };
  493. /**
  494. * Multiply another matrix by this one and store the result.
  495. *
  496. * @param mat Matrix array.
  497. */
  498. Matrix.prototype.multiply = function(mat)
  499. {
  500. var m0 = this.m[0] * mat.m[0] + this.m[2] * mat.m[1];
  501. var m1 = this.m[1] * mat.m[0] + this.m[3] * mat.m[1];
  502. var m2 = this.m[0] * mat.m[2] + this.m[2] * mat.m[3];
  503. var m3 = this.m[1] * mat.m[2] + this.m[3] * mat.m[3];
  504. var m4 = this.m[0] * mat.m[4] + this.m[2] * mat.m[5] + this.m[4];
  505. var m5 = this.m[1] * mat.m[4] + this.m[3] * mat.m[5] + this.m[5];
  506. this.m = [m0, m1, m2, m3, m4, m5];
  507. };
  508. /**
  509. * Premultiply another matrix by this one and store the result.
  510. *
  511. * @param mat Matrix array to multiply.
  512. */
  513. Matrix.prototype.premultiply = function(mat)
  514. {
  515. var m0 = mat.m[0] * this.m[0] + mat.m[2] * this.m[1];
  516. var m1 = mat.m[1] * this.m[0] + mat.m[3] * this.m[1];
  517. var m2 = mat.m[0] * this.m[2] + mat.m[2] * this.m[3];
  518. var m3 = mat.m[1] * this.m[2] + mat.m[3] * this.m[3];
  519. var m4 = mat.m[0] * this.m[4] + mat.m[2] * this.m[5] + mat.m[4];
  520. var m5 = mat.m[1] * this.m[4] + mat.m[3] * this.m[5] + mat.m[5];
  521. this.m = [m0, m1, m2, m3, m4, m5];
  522. };
  523. /**
  524. * Compose this transformation matrix with position scale and rotation and origin point.
  525. */
  526. Matrix.prototype.compose = function(px, py, sx, sy, ox, oy, a)
  527. {
  528. this.m = [1, 0, 0, 1, px, py];
  529. if(a !== 0)
  530. {
  531. var c = Math.cos(a);
  532. var s = Math.sin(a);
  533. this.multiply(new Matrix([c, s, -s, c, 0, 0]));
  534. }
  535. if(ox !== 0 || oy !== 0)
  536. {
  537. this.multiply(new Matrix([1, 0, 0, 1, -ox, -oy]));
  538. }
  539. if(sx !== 1 || sy !== 1)
  540. {
  541. this.scale(sx, sy);
  542. }
  543. };
  544. /**
  545. * Apply translation to this matrix.
  546. */
  547. Matrix.prototype.translate = function(x, y)
  548. {
  549. this.m[4] += this.m[0] * x + this.m[2] * y;
  550. this.m[5] += this.m[1] * x + this.m[3] * y;
  551. };
  552. /**
  553. * Apply rotation to this matrix.
  554. *
  555. * @param angle Angle in radians.
  556. */
  557. Matrix.prototype.rotate = function(rad)
  558. {
  559. var c = Math.cos(rad);
  560. var s = Math.sin(rad);
  561. var m11 = this.m[0] * c + this.m[2] * s;
  562. var m12 = this.m[1] * c + this.m[3] * s;
  563. var m21 = this.m[0] * -s + this.m[2] * c;
  564. var m22 = this.m[1] * -s + this.m[3] * c;
  565. this.m[0] = m11;
  566. this.m[1] = m12;
  567. this.m[2] = m21;
  568. this.m[3] = m22;
  569. };
  570. /**
  571. * Apply scale to this matrix.
  572. */
  573. Matrix.prototype.scale = function(sx, sy)
  574. {
  575. this.m[0] *= sx;
  576. this.m[1] *= sx;
  577. this.m[2] *= sy;
  578. this.m[3] *= sy;
  579. };
  580. /**
  581. * Set the position of the transformation matrix.
  582. */
  583. Matrix.prototype.setPosition = function(x, y)
  584. {
  585. this.m[4] = x;
  586. this.m[5] = y;
  587. };
  588. /**
  589. * Get the scale from the transformation matrix.
  590. */
  591. Matrix.prototype.getScale = function()
  592. {
  593. return new Vector2(this.m[0], this.m[3]);
  594. };
  595. /**
  596. * Get the position from the transformation matrix.
  597. */
  598. Matrix.prototype.getPosition = function()
  599. {
  600. return new Vector2(this.m[4], this.m[5]);
  601. };
  602. /**
  603. * Apply skew to this matrix.
  604. */
  605. Matrix.prototype.skew = function(radianX, radianY)
  606. {
  607. this.multiply(new Matrix([1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0]));
  608. };
  609. /**
  610. * Get the matrix determinant.
  611. */
  612. Matrix.prototype.determinant = function()
  613. {
  614. return 1 / (this.m[0] * this.m[3] - this.m[1] * this.m[2]);
  615. };
  616. /**
  617. * Get the inverse matrix.
  618. */
  619. Matrix.prototype.getInverse = function()
  620. {
  621. var d = this.determinant();
  622. 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])]);
  623. };
  624. /**
  625. * Transform a point using this matrix.
  626. */
  627. Matrix.prototype.transformPoint = function(p)
  628. {
  629. var px = p.x * this.m[0] + p.y * this.m[2] + this.m[4];
  630. var py = p.x * this.m[1] + p.y * this.m[3] + this.m[5];
  631. return new Vector2(px, py);
  632. };
  633. /**
  634. * Set a canvas context to use this transformation.
  635. */
  636. Matrix.prototype.setContextTransform = function(context)
  637. {
  638. context.setTransform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  639. };
  640. /**
  641. * Transform on top of the current context transformation.
  642. */
  643. Matrix.prototype.tranformContext = function(context)
  644. {
  645. context.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
  646. };
  647. Matrix.prototype.cssTransform = function()
  648. {
  649. return "matrix(" + this.m[0] + "," + this.m[1] + "," + this.m[2] + "," + this.m[3] + "," + this.m[4] + "," + this.m[5] + ")";
  650. };
  651. /**
  652. * Class to implement UUID generation methods.
  653. *
  654. * @class
  655. */
  656. function UUID(){}
  657. /**
  658. * Generate new random UUID v4 as string.
  659. *
  660. * http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  661. *
  662. * @static
  663. */
  664. UUID.generate = (function ()
  665. {
  666. var lut = [];
  667. for(var i = 0; i < 256; i++)
  668. {
  669. lut[i] = (i < 16 ? "0" : "") + (i).toString(16);
  670. }
  671. return function generateUUID()
  672. {
  673. var d0 = Math.random() * 0XFFFFFFFF | 0;
  674. var d1 = Math.random() * 0XFFFFFFFF | 0;
  675. var d2 = Math.random() * 0XFFFFFFFF | 0;
  676. var d3 = Math.random() * 0XFFFFFFFF | 0;
  677. var uuid = lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + "-" +
  678. lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + "-" + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + "-" +
  679. lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + "-" + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
  680. lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
  681. return uuid.toUpperCase();
  682. };
  683. })();
  684. /**
  685. * Base 2D object class, implements all the object positioning and scalling features.
  686. *
  687. * @class
  688. */
  689. function Object2D()
  690. {
  691. /**
  692. * UUID of the object.
  693. */
  694. this.uuid = UUID.generate();
  695. /**
  696. * List of children objects attached to the object.
  697. */
  698. this.children = [];
  699. /**
  700. * Parent object, the object position is affected by its parent position.
  701. */
  702. this.parent = null;
  703. /**
  704. * Depth level in the object tree, objects with higher depth are drawn on top.
  705. *
  706. * The layer value is considered first.
  707. */
  708. this.level = 0;
  709. /**
  710. * Position of the object.
  711. */
  712. this.position = new Vector2(0, 0);
  713. /**
  714. * Origin of the object used as point of rotation.
  715. */
  716. this.origin = new Vector2(0, 0);
  717. /**
  718. * Scale of the object.
  719. */
  720. this.scale = new Vector2(1, 1);
  721. /**
  722. * Rotation of the object relative to its center.
  723. */
  724. this.rotation = 0.0;
  725. /**
  726. * Indicates if the object is visible.
  727. */
  728. this.visible = true;
  729. /**
  730. * Layer of this object, objects are sorted by layer value.
  731. *
  732. * Lower layer value is draw first.
  733. */
  734. this.layer = 0;
  735. /**
  736. * Local transformation matrix applied to the object.
  737. */
  738. this.matrix = new Matrix();
  739. /**
  740. * Global transformation matrix multiplied by the parent matrix.
  741. *
  742. * Used to transform the object before projecting into screen coordinates.
  743. */
  744. this.globalMatrix = new Matrix();
  745. /**
  746. * Inverse of the global matrix.
  747. *
  748. * Used to convert pointer input points into object coordinates.
  749. */
  750. this.inverseGlobalMatrix = new Matrix();
  751. /**
  752. * Masks being applied to this object.
  753. *
  754. * Multiple masks can be used simultaneously.
  755. */
  756. this.masks = [];
  757. /**
  758. * If true the matrix is updated before rendering the object.
  759. */
  760. this.matrixNeedsUpdate = true;
  761. /**
  762. * Indicates if its possible to drag the object around.
  763. *
  764. * If true the onPointerDrag callback is used to update the state of the object.
  765. */
  766. this.draggable = false;
  767. /**
  768. * Indicates if this object uses pointer events.
  769. *
  770. * Can be set false to skip the pointer interaction events.
  771. */
  772. this.pointerEvents = true;
  773. /**
  774. * Flag to indicate wheter this objet ignores the viewport transformation.
  775. */
  776. this.ignoreViewport = false;
  777. /**
  778. * Flag to indicate if the context of canvas should be saved before render.
  779. */
  780. this.saveContextState = true;
  781. /**
  782. * Flag to indicate if the context of canvas should be restored after render.
  783. */
  784. this.restoreContextState = true;
  785. /**
  786. * Flag indicating if the pointer is inside of the element.
  787. *
  788. * Used to control object event.
  789. */
  790. this.pointerInside = false;
  791. /**
  792. * Flag to indicate if the object is currently being dragged.
  793. */
  794. this.beingDragged = false;
  795. }
  796. /**
  797. * Traverse the object tree and run a function for all objects.
  798. *
  799. * @param callback Callback function that receives the object as parameter.
  800. */
  801. Object2D.prototype.traverse = function(callback)
  802. {
  803. callback(this);
  804. var children = this.children;
  805. for(var i = 0; i < children.length; i++)
  806. {
  807. children[i].traverse(callback);
  808. }
  809. };
  810. /**
  811. * Attach a children to the object.
  812. *
  813. * @param object Object to attach to this object.
  814. */
  815. Object2D.prototype.add = function(object)
  816. {
  817. object.parent = this;
  818. object.level = this.level + 1;
  819. object.traverse(function(child)
  820. {
  821. if(child.onAdd !== null)
  822. {
  823. child.onAdd(this);
  824. }
  825. });
  826. this.children.push(object);
  827. };
  828. /**
  829. * Remove object from the children list.
  830. *
  831. * @param object Object to be removed.
  832. */
  833. Object2D.prototype.remove = function(object)
  834. {
  835. var index = this.children.indexOf(object);
  836. if(index !== -1)
  837. {
  838. var object = this.children[index];
  839. object.parent = null;
  840. object.level = 0;
  841. object.traverse(function(child)
  842. {
  843. if(child.onRemove !== null)
  844. {
  845. child.onRemove(this);
  846. }
  847. });
  848. this.children.splice(index, 1);
  849. }
  850. };
  851. /**
  852. * Check if a point is inside of the object.
  853. */
  854. Object2D.prototype.isInside = function(point)
  855. {
  856. return false;
  857. };
  858. /**
  859. * Update the transformation matrix of the object.
  860. */
  861. Object2D.prototype.updateMatrix = function(context)
  862. {
  863. if(this.matrixNeedsUpdate)
  864. {
  865. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  866. this.globalMatrix.copy(this.matrix);
  867. if(this.parent !== null)
  868. {
  869. this.globalMatrix.premultiply(this.parent.globalMatrix);
  870. }
  871. this.inverseGlobalMatrix = this.globalMatrix.getInverse();
  872. //this.matrixNeedsUpdate = false;
  873. }
  874. };
  875. /**
  876. * Apply the transform to the rendering context.
  877. *
  878. * It is assumed that the viewport transform is pre-applied to the context.
  879. *
  880. * Can also be used for pre rendering logic.
  881. *
  882. * @param {CanvasContext} context Canvas 2d drawing context.
  883. * @param {Viewport} viewport Viewport applied to the canvas.
  884. */
  885. Object2D.prototype.transform = function(context, viewport)
  886. {
  887. this.globalMatrix.tranformContext(context);
  888. };
  889. /**
  890. * Draw the object into the canvas.
  891. *
  892. * Has to be implemented by underlying classes.
  893. *
  894. * @param {CanvasContext} context Canvas 2d drawing context.
  895. * @param {Viewport} viewport Viewport applied to the canvas.
  896. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  897. */
  898. Object2D.prototype.draw = function(context, viewport, canvas){};
  899. /**
  900. * Callback method while the object is being dragged across the screen.
  901. *
  902. * By default is adds the delta value to the object position (making it follow the mouse movement).
  903. *
  904. * Delta is the movement of the pointer already translated into local object coordinates.
  905. *
  906. * Receives (pointer, viewport, delta) as arguments.
  907. */
  908. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  909. {
  910. this.position.add(delta);
  911. };
  912. /**
  913. * Method called when the object its added to a parent.
  914. *
  915. * Receives (parent) as arguments.
  916. */
  917. Object2D.prototype.onAdd = null;
  918. /**
  919. * Method called when the object gets removed from its parent
  920. *
  921. * Receives (parent) as arguments.
  922. */
  923. Object2D.prototype.onRemove = null;
  924. /**
  925. * Callback method called every time before the object is draw into the canvas.
  926. *
  927. * Can be used to run preparation code, move the object, etc.
  928. */
  929. Object2D.prototype.onUpdate = null;
  930. /**
  931. * Callback method called when the pointer enters the object.
  932. *
  933. * Receives (pointer, viewport) as arguments.
  934. */
  935. Object2D.prototype.onPointerEnter = null;
  936. /**
  937. * Callback method called when the was inside of the object and leaves the object.
  938. *
  939. * Receives (pointer, viewport) as arguments.
  940. */
  941. Object2D.prototype.onPointerLeave = null;
  942. /**
  943. * Callback method while the pointer is over (inside) of the object.
  944. *
  945. * Receives (pointer, viewport) as arguments.
  946. */
  947. Object2D.prototype.onPointerOver = null;
  948. /**
  949. * Callback method called while the pointer button is pressed.
  950. *
  951. * Receives (pointer, viewport) as arguments.
  952. */
  953. Object2D.prototype.onButtonPressed = null;
  954. /**
  955. * Callback method called when the pointer button is pressed down (single time).
  956. *
  957. * Receives (pointer, viewport) as arguments.
  958. */
  959. Object2D.prototype.onButtonDown = null;
  960. /**
  961. * Callback method called when the pointer button is released (single time).
  962. *
  963. * Receives (pointer, viewport) as arguments.
  964. */
  965. Object2D.prototype.onButtonUp = null;
  966. /**
  967. * Key is used by Keyboard, Pointer, etc, to represent a key state.
  968. *
  969. * @class
  970. */
  971. function Key()
  972. {
  973. /**
  974. * Indicates if this key is currently pressed.
  975. */
  976. this.pressed = false;
  977. /**
  978. * Indicates if this key was just pressed.
  979. */
  980. this.justPressed = false;
  981. /**
  982. * Indicates if this key was just released.
  983. */
  984. this.justReleased = false;
  985. }
  986. Key.DOWN = -1;
  987. Key.UP = 1;
  988. Key.RESET = 0;
  989. Key.prototype.constructor = Key;
  990. /**
  991. * Update Key status based on new key state.
  992. */
  993. Key.prototype.update = function(action)
  994. {
  995. this.justPressed = false;
  996. this.justReleased = false;
  997. if(action === Key.DOWN)
  998. {
  999. if(this.pressed === false)
  1000. {
  1001. this.justPressed = true;
  1002. }
  1003. this.pressed = true;
  1004. }
  1005. else if(action === Key.UP)
  1006. {
  1007. if(this.pressed)
  1008. {
  1009. this.justReleased = true;
  1010. }
  1011. this.pressed = false;
  1012. }
  1013. else if(action === Key.RESET)
  1014. {
  1015. this.justReleased = false;
  1016. this.justPressed = false;
  1017. }
  1018. };
  1019. /**
  1020. * Set this key attributes manually.
  1021. */
  1022. Key.prototype.set = function(justPressed, pressed, justReleased)
  1023. {
  1024. this.justPressed = justPressed;
  1025. this.pressed = pressed;
  1026. this.justReleased = justReleased;
  1027. };
  1028. /**
  1029. * Reset key to default values.
  1030. */
  1031. Key.prototype.reset = function()
  1032. {
  1033. this.justPressed = false;
  1034. this.pressed = false;
  1035. this.justReleased = false;
  1036. };
  1037. /**
  1038. * Pointer instance for input in sync with the running 3D application.
  1039. *
  1040. * The pointer object provided by scripts is automatically updated by the runtime handler.
  1041. *
  1042. * @class
  1043. * @param {DOM} domElement DOM element to craete the pointer events.
  1044. * @param {Boolean} dontInitialize If true the pointer events are not created.
  1045. */
  1046. function Pointer(domElement)
  1047. {
  1048. //Raw data
  1049. this._keys = new Array(5);
  1050. this._position = new Vector2(0, 0);
  1051. this._positionUpdated = false;
  1052. this._delta = new Vector2(0, 0);
  1053. this._wheel = 0;
  1054. this._wheelUpdated = false;
  1055. this._doubleClicked = new Array(5);
  1056. /**
  1057. * Array with pointer buttons status.
  1058. */
  1059. this.keys = new Array(5);
  1060. /**
  1061. * Pointer position inside of the window (coordinates in window space).
  1062. */
  1063. this.position = new Vector2(0, 0);
  1064. /**
  1065. * Pointer movement (coordinates in window space).
  1066. */
  1067. this.delta = new Vector2(0, 0);
  1068. /**
  1069. * Pointer scroll wheel movement.
  1070. */
  1071. this.wheel = 0;
  1072. /**
  1073. * Indicates a button of the pointer was double clicked.
  1074. */
  1075. this.doubleClicked = new Array(5);
  1076. /**
  1077. * DOM element where to attach the pointer events.
  1078. */
  1079. this.domElement = (domElement !== undefined) ? domElement : window;
  1080. /**
  1081. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  1082. */
  1083. this.canvas = null;
  1084. /**
  1085. * Event manager responsible for updating the raw data variables.
  1086. *
  1087. * Diferent events are used depending on the host platform.
  1088. *
  1089. * When the update method is called the raw data is reset.
  1090. */
  1091. this.events = new EventManager();
  1092. //Initialize key instances
  1093. for(var i = 0; i < 5; i++)
  1094. {
  1095. this._doubleClicked[i] = false;
  1096. this.doubleClicked[i] = false;
  1097. this._keys[i] = new Key();
  1098. this.keys[i] = new Key();
  1099. }
  1100. //Self pointer
  1101. var self = this;
  1102. //Scroll wheel
  1103. if(window.onmousewheel !== undefined)
  1104. {
  1105. //Chrome, edge
  1106. this.events.add(this.domElement, "mousewheel", function(event)
  1107. {
  1108. self._wheel = event.deltaY;
  1109. self._wheelUpdated = true;
  1110. });
  1111. }
  1112. else if(window.addEventListener !== undefined)
  1113. {
  1114. //Firefox
  1115. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  1116. {
  1117. self._wheel = event.detail * 30;
  1118. self._wheelUpdated = true;
  1119. });
  1120. }
  1121. else
  1122. {
  1123. this.events.add(this.domElement, "wheel", function(event)
  1124. {
  1125. self._wheel = event.deltaY;
  1126. self._wheelUpdated = true;
  1127. });
  1128. }
  1129. //Touchscreen input events
  1130. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  1131. {
  1132. //Auxiliar variables to calculate touch delta
  1133. var lastTouch = new Vector2(0, 0);
  1134. //Touch start event
  1135. this.events.add(this.domElement, "touchstart", function(event)
  1136. {
  1137. var touch = event.touches[0];
  1138. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  1139. self.updateKey(Pointer.LEFT, Key.DOWN);
  1140. lastTouch.set(touch.clientX, touch.clientY);
  1141. });
  1142. //Touch end event
  1143. this.events.add(this.domElement, "touchend", function(event)
  1144. {
  1145. self.updateKey(Pointer.LEFT, Key.UP);
  1146. });
  1147. //Touch cancel event
  1148. this.events.add(this.domElement, "touchcancel", function(event)
  1149. {
  1150. self.updateKey(Pointer.LEFT, Key.UP);
  1151. });
  1152. //Touch move event
  1153. this.events.add(document.body, "touchmove", function(event)
  1154. {
  1155. var touch = event.touches[0];
  1156. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  1157. lastTouch.set(touch.clientX, touch.clientY);
  1158. });
  1159. }
  1160. //Move
  1161. this.events.add(this.domElement, "mousemove", function(event)
  1162. {
  1163. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  1164. });
  1165. //Button pressed
  1166. this.events.add(this.domElement, "mousedown", function(event)
  1167. {
  1168. self.updateKey(event.which - 1, Key.DOWN);
  1169. });
  1170. //Button released
  1171. this.events.add(this.domElement, "mouseup", function(event)
  1172. {
  1173. self.updateKey(event.which - 1, Key.UP);
  1174. });
  1175. //Drag start
  1176. this.events.add(this.domElement, "dragstart", function(event)
  1177. {
  1178. self.updateKey(event.which - 1, Key.UP);
  1179. });
  1180. //Pointer double click
  1181. this.events.add(this.domElement, "dblclick", function(event)
  1182. {
  1183. self._doubleClicked[event.which - 1] = true;
  1184. });
  1185. this.create();
  1186. }
  1187. Pointer.prototype = Pointer;
  1188. Pointer.prototype.constructor = Pointer;
  1189. /**
  1190. * Left pointer button.
  1191. */
  1192. Pointer.LEFT = 0;
  1193. /**
  1194. * Middle pointer button.
  1195. */
  1196. Pointer.MIDDLE = 1;
  1197. /**
  1198. * Right pointer button.
  1199. */
  1200. Pointer.RIGHT = 2;
  1201. /**
  1202. * Back pointer navigation button.
  1203. */
  1204. Pointer.BACK = 3;
  1205. /**
  1206. * Forward pointer navigation button.
  1207. */
  1208. Pointer.FORWARD = 4;
  1209. /**
  1210. * Element to be used for coordinates calculation relative to that canvas.
  1211. *
  1212. * @param {DOM} canvas Canvas to be attached to the Pointer instance
  1213. */
  1214. Pointer.setCanvas = function(element)
  1215. {
  1216. this.canvas = element;
  1217. element.pointerInside = false;
  1218. element.addEventListener("mouseenter", function()
  1219. {
  1220. this.pointerInside = true;
  1221. });
  1222. element.addEventListener("mouseleave", function()
  1223. {
  1224. this.pointerInside = false;
  1225. });
  1226. };
  1227. /**
  1228. * Check if pointer is inside attached canvas (updated async).
  1229. *
  1230. * @return {boolean} True if pointer is currently inside the canvas
  1231. */
  1232. Pointer.insideCanvas = function()
  1233. {
  1234. return this.canvas !== null && this.canvas.pointerInside;
  1235. };
  1236. /**
  1237. * Check if pointer button is currently pressed.
  1238. *
  1239. * @param {Number} button Button to check status of
  1240. * @return {boolean} True if button is currently pressed
  1241. */
  1242. Pointer.buttonPressed = function(button)
  1243. {
  1244. return this.keys[button].pressed;
  1245. };
  1246. /**
  1247. * Check if pointer button was double clicked.
  1248. *
  1249. * @param {Number} button Button to check status of
  1250. * @return {boolean} True if some pointer button was just double clicked
  1251. */
  1252. Pointer.buttonDoubleClicked = function(button)
  1253. {
  1254. return this.doubleClicked[button];
  1255. };
  1256. /**
  1257. * Check if a pointer button was just pressed.
  1258. *
  1259. * @param {Number} button Button to check status of
  1260. * @return {boolean} True if button was just pressed
  1261. */
  1262. Pointer.buttonJustPressed = function(button)
  1263. {
  1264. return this.keys[button].justPressed;
  1265. };
  1266. /**
  1267. * Check if a pointer button was just released.
  1268. *
  1269. * @param {Number} button Button to check status of
  1270. * @return {boolean} True if button was just released
  1271. */
  1272. Pointer.buttonJustReleased = function(button)
  1273. {
  1274. return this.keys[button].justReleased;
  1275. };
  1276. /**
  1277. * Update pointer position.
  1278. *
  1279. * Automatically called by the runtime.
  1280. *
  1281. * @param {Number} x
  1282. * @param {Number} y
  1283. * @param {Number} xDiff
  1284. * @param {Number} yDiff
  1285. */
  1286. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  1287. {
  1288. if(this.canvas !== null)
  1289. {
  1290. var rect = this.canvas.getBoundingClientRect();
  1291. x -= rect.left;
  1292. y -= rect.top;
  1293. }
  1294. this._position.set(x, y);
  1295. this._delta.x += xDiff;
  1296. this._delta.y += yDiff;
  1297. this._positionUpdated = true;
  1298. };
  1299. /**
  1300. * Update a pointer button.
  1301. *
  1302. * Automatically called by the runtime.
  1303. *
  1304. * @param {Number} button
  1305. * @param {Number} action
  1306. */
  1307. Pointer.updateKey = function(button, action)
  1308. {
  1309. if(button > -1)
  1310. {
  1311. this._keys[button].update(action);
  1312. }
  1313. };
  1314. /**
  1315. * Update pointer buttons state, position, wheel and delta synchronously.
  1316. */
  1317. Pointer.update = function()
  1318. {
  1319. //Update pointer keys state
  1320. for(var i = 0; i < 5; i++)
  1321. {
  1322. if(this._keys[i].justPressed && this.keys[i].justPressed)
  1323. {
  1324. this._keys[i].justPressed = false;
  1325. }
  1326. if(this._keys[i].justReleased && this.keys[i].justReleased)
  1327. {
  1328. this._keys[i].justReleased = false;
  1329. }
  1330. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  1331. //Update pointer double click
  1332. if(this._doubleClicked[i] === true)
  1333. {
  1334. this.doubleClicked[i] = true;
  1335. this._doubleClicked[i] = false;
  1336. }
  1337. else
  1338. {
  1339. this.doubleClicked[i] = false;
  1340. }
  1341. }
  1342. //Update pointer wheel
  1343. if(this._wheelUpdated)
  1344. {
  1345. this.wheel = this._wheel;
  1346. this._wheelUpdated = false;
  1347. }
  1348. else
  1349. {
  1350. this.wheel = 0;
  1351. }
  1352. //Update pointer Position if needed
  1353. if(this._positionUpdated)
  1354. {
  1355. this.delta.copy(this._delta);
  1356. this.position.copy(this._position);
  1357. this._delta.set(0,0);
  1358. this._positionUpdated = false;
  1359. }
  1360. else
  1361. {
  1362. this.delta.x = 0;
  1363. this.delta.y = 0;
  1364. }
  1365. };
  1366. /**
  1367. * Create pointer events.
  1368. */
  1369. Pointer.create = function()
  1370. {
  1371. this.events.create();
  1372. };
  1373. /**
  1374. * Dispose pointer events.
  1375. */
  1376. Pointer.dispose = function()
  1377. {
  1378. this.events.destroy();
  1379. };
  1380. /**
  1381. * Used to indicate how the user views the content inside of the canvas.
  1382. *
  1383. * @class
  1384. */
  1385. function Viewport()
  1386. {
  1387. /**
  1388. * UUID of the object.
  1389. */
  1390. this.uuid = UUID.generate();
  1391. /**
  1392. * Position of the object.
  1393. */
  1394. this.position = new Vector2(0, 0);
  1395. /**
  1396. * Scale of the object.
  1397. */
  1398. this.scale = 1.0;
  1399. /**
  1400. * Rotation of the object relative to its center.
  1401. */
  1402. this.rotation = 0.0;
  1403. /**
  1404. * Local transformation matrix applied to the object.
  1405. */
  1406. this.matrix = new Matrix();
  1407. /**
  1408. * Inverse of the local transformation matrix.
  1409. */
  1410. this.inverseMatrix = new Matrix();
  1411. /**
  1412. * If true the matrix is updated before rendering the object.
  1413. */
  1414. this.matrixNeedsUpdate = true;
  1415. /**
  1416. * Flag to indicate if the viewport should move when scalling.
  1417. *
  1418. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  1419. */
  1420. this.moveOnScale = true;
  1421. /**
  1422. * Value of the initial point of rotation if the viewport is being rotated.
  1423. *
  1424. * Is set to null when the viewport is not being rotated.
  1425. */
  1426. this.rotationPoint = null;
  1427. }
  1428. /**
  1429. * Calculate and update the viewport transformation matrix.
  1430. *
  1431. * Also updates the inverse matrix of the viewport.
  1432. */
  1433. Viewport.prototype.updateMatrix = function()
  1434. {
  1435. if(this.matrixNeedsUpdate)
  1436. {
  1437. this.matrix.compose(this.position.x, this.position.y, this.scale, this.scale, 0, 0, this.rotation);
  1438. this.inverseMatrix = this.matrix.getInverse();
  1439. //this.matrixNeedsUpdate = false;
  1440. }
  1441. };
  1442. /**
  1443. * Center the viewport relative to a object.
  1444. *
  1445. * The position of the object is used a central point, this method does not consider "box" attributes or other strucures in the object.
  1446. *
  1447. * @param {Object2D} object Object to be centered on the viewport.
  1448. * @param {DOM} canvas Canvas element where the image is drawn.
  1449. */
  1450. Viewport.prototype.centerObject = function(object, canvas)
  1451. {
  1452. var position = object.globalMatrix.transformPoint(new Vector2());
  1453. position.multiplyScalar(-this.scale);
  1454. position.x += canvas.width / 2;
  1455. position.y += canvas.height / 2;
  1456. this.position.copy(position);
  1457. };
  1458. /**
  1459. * Viewport controls are used to allow the user to control the viewport.
  1460. *
  1461. * @class
  1462. * @param {Viewport} viewport
  1463. */
  1464. function ViewportControls(viewport)
  1465. {
  1466. /**
  1467. * Viewport being controlled by this object.
  1468. */
  1469. this.viewport = viewport;
  1470. /**
  1471. * Button used to drag and viewport around.
  1472. *
  1473. * On touch enabled devices the touch event is represented as a LEFT button.
  1474. */
  1475. this.dragButton = Pointer.RIGHT;
  1476. /**
  1477. * Is set to true allow the viewport to be scalled.
  1478. *
  1479. * Scaling is performed using the pointer scroll.
  1480. */
  1481. this.allowScale = true;
  1482. /**
  1483. * Flag to indicate if the viewport should move when scalling.
  1484. *
  1485. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  1486. */
  1487. this.moveOnScale = true;
  1488. /**
  1489. * If true allows the viewport to be rotated.
  1490. *
  1491. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  1492. */
  1493. this.allowRotation = false;
  1494. /**
  1495. * Value of the initial point of rotation if the viewport is being rotated.
  1496. *
  1497. * Is set to null when the viewport is not being rotated.
  1498. */
  1499. this.rotationPoint = null;
  1500. /**
  1501. * Initial rotation of the viewport.
  1502. */
  1503. this.rotationInitial = 0;
  1504. }
  1505. /**
  1506. * Update the viewport controls using the pointer object.
  1507. *
  1508. * Should be called every frame before rendering.
  1509. *
  1510. * @param {Pointer} pointer
  1511. */
  1512. ViewportControls.prototype.update = function(pointer)
  1513. {
  1514. if(this.allowScale && pointer.wheel !== 0)
  1515. {
  1516. this.viewport.scale -= pointer.wheel * 1e-3 * this.viewport.scale;
  1517. if(this.moveOnScale)
  1518. {
  1519. var speed = pointer.wheel;
  1520. var halfWidth = pointer.canvas.width / 2;
  1521. var halfWeight = pointer.canvas.height / 2;
  1522. this.viewport.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
  1523. this.viewport.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
  1524. }
  1525. }
  1526. if(this.allowRotation && pointer.buttonPressed(Pointer.RIGHT) && pointer.buttonPressed(Pointer.LEFT))
  1527. {
  1528. if(this.rotationPoint === null)
  1529. {
  1530. this.rotationPoint = pointer.position.clone();
  1531. this.rotationInitial = this.viewport.rotation;
  1532. }
  1533. else
  1534. {
  1535. var pointer = pointer.position.clone();
  1536. pointer.sub(this.rotationPoint);
  1537. this.viewport.rotation = this.rotationInitial + pointer.angle();
  1538. }
  1539. }
  1540. else
  1541. {
  1542. this.rotationPoint = null;
  1543. if(pointer.buttonPressed(this.dragButton))
  1544. {
  1545. this.viewport.position.x += pointer.delta.x;
  1546. this.viewport.position.y += pointer.delta.y;
  1547. }
  1548. }
  1549. };
  1550. /**
  1551. * The renderer is resposible for drawing the structure into the canvas element.
  1552. *
  1553. * Its also resposible for managing the canvas state.
  1554. *
  1555. * @class
  1556. */
  1557. function Renderer(canvas, options)
  1558. {
  1559. if(options === undefined)
  1560. {
  1561. options =
  1562. {
  1563. alpha: true
  1564. };
  1565. }
  1566. /**
  1567. * Canvas DOM element, has to be managed by the user.
  1568. */
  1569. this.canvas = canvas;
  1570. /**
  1571. * Canvas 2D rendering context used to draw content.
  1572. */
  1573. this.context = canvas.getContext("2d", {alpha: options.alpha});
  1574. this.context.imageSmoothingEnabled = true;
  1575. this.context.globalCompositeOperation = "source-over";
  1576. /**
  1577. * Pointer input handler object.
  1578. */
  1579. this.pointer = new Pointer();
  1580. this.pointer.setCanvas(canvas);
  1581. /**
  1582. * Indicates if the canvas should be automatically cleared on each new frame.
  1583. */
  1584. this.autoClear = true;
  1585. }
  1586. /**
  1587. * Creates a infinite render loop to render the group into a viewport each frame.
  1588. *
  1589. * The render loop cannot be destroyed, and it automatically creates a viewport controls object.
  1590. *
  1591. * @param {Object2D} group Group to be rendererd.
  1592. * @param {Viewport} viewport Viewport into the objects.
  1593. * @param {Function} onUpdate Function called before rendering the frame.
  1594. */
  1595. Renderer.prototype.createRenderLoop = function(group, viewport, onUpdate)
  1596. {
  1597. var self = this;
  1598. var controls = new ViewportControls(viewport);
  1599. function loop()
  1600. {
  1601. if(onUpdate !== undefined)
  1602. {
  1603. onUpdate();
  1604. }
  1605. controls.update(self.pointer);
  1606. self.update(group, viewport);
  1607. requestAnimationFrame(loop);
  1608. }
  1609. loop();
  1610. };
  1611. /**
  1612. * Update the renderer state, update the input handlers, calculate the object and viewport transformation matrices.
  1613. *
  1614. * Render the object using the viewport into a canvas element.
  1615. *
  1616. * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
  1617. *
  1618. * Should be called at a fixed rate preferably using the requestAnimationFrame() method, its also possible to use the createRenderLoop() method, that automatically creates a infinite render loop.
  1619. *
  1620. * @param object Object to be updated.
  1621. * @param viewport Viewport to be updated (should be the one where the objects will be rendered after).
  1622. */
  1623. Renderer.prototype.update = function(object, viewport)
  1624. {
  1625. // Get objects to be rendered
  1626. var objects = [];
  1627. object.traverse(function(child)
  1628. {
  1629. if(child.visible)
  1630. {
  1631. objects.push(child);
  1632. }
  1633. });
  1634. // Sort objects by layer
  1635. objects.sort(function(a, b)
  1636. {
  1637. if(b.layer === a.layer)
  1638. {
  1639. return b.level - a.level;
  1640. }
  1641. return b.layer - a.layer;
  1642. });
  1643. // Pointer object update
  1644. var pointer = this.pointer;
  1645. pointer.update();
  1646. // Viewport transform matrix
  1647. viewport.updateMatrix();
  1648. // Project pointer coordinates
  1649. var point = pointer.position.clone();
  1650. var viewportPoint = viewport.inverseMatrix.transformPoint(point);
  1651. // Object pointer events
  1652. for(var i = 0; i < objects.length; i++)
  1653. {
  1654. var child = objects[i];
  1655. //Process the
  1656. if(child.pointerEvents)
  1657. {
  1658. var childPoint = child.inverseGlobalMatrix.transformPoint(child.ignoreViewport ? point : viewportPoint);
  1659. // Check if the pointer pointer is inside
  1660. if(child.isInside(childPoint))
  1661. {
  1662. // Pointer enter
  1663. if(!child.pointerInside && child.onPointerEnter !== null)
  1664. {
  1665. child.onPointerEnter(pointer, viewport);
  1666. }
  1667. // Pointer over
  1668. if(child.onPointerOver !== null)
  1669. {
  1670. child.onPointerOver(pointer, viewport);
  1671. }
  1672. // Pointer pressed
  1673. if(pointer.buttonPressed(Pointer.LEFT) && child.onButtonPressed !== null)
  1674. {
  1675. child.onButtonPressed(pointer, viewport);
  1676. }
  1677. // Just released
  1678. if(pointer.buttonJustReleased(Pointer.LEFT) && child.onButtonUp !== null)
  1679. {
  1680. child.onButtonUp(pointer, viewport);
  1681. }
  1682. // Pointer just pressed
  1683. if(pointer.buttonJustPressed(Pointer.LEFT))
  1684. {
  1685. if(child.onButtonDown !== null)
  1686. {
  1687. child.onButtonDown(pointer, viewport);
  1688. }
  1689. // Drag object and break to only start a drag operation on the top element.
  1690. if(child.draggable)
  1691. {
  1692. child.beingDragged = true;
  1693. break;
  1694. }
  1695. }
  1696. child.pointerInside = true;
  1697. }
  1698. else if(child.pointerInside)
  1699. {
  1700. // Pointer leave
  1701. if(child.onPointerLeave !== null)
  1702. {
  1703. child.onPointerLeave(pointer, viewport);
  1704. }
  1705. child.pointerInside = false;
  1706. }
  1707. // Stop object drag
  1708. if(pointer.buttonJustReleased(Pointer.LEFT))
  1709. {
  1710. if(child.draggable)
  1711. {
  1712. child.beingDragged = false;
  1713. }
  1714. }
  1715. }
  1716. }
  1717. // Object drag events and update logic
  1718. for(var i = 0; i < objects.length; i++)
  1719. {
  1720. var child = objects[i];
  1721. // Pointer drag event
  1722. if(child.beingDragged)
  1723. {
  1724. var lastPosition = pointer.position.clone();
  1725. lastPosition.sub(pointer.delta);
  1726. var positionWorld = viewport.inverseMatrix.transformPoint(pointer.position);
  1727. var lastWorld = viewport.inverseMatrix.transformPoint(lastPosition);
  1728. // Mouse delta in world coordinates
  1729. positionWorld.sub(lastWorld);
  1730. if(child.onPointerDrag !== null)
  1731. {
  1732. child.onPointerDrag(pointer, viewport, positionWorld);
  1733. }
  1734. }
  1735. // On update
  1736. if(child.onUpdate !== null)
  1737. {
  1738. child.onUpdate();
  1739. }
  1740. }
  1741. // Update transformation matrices
  1742. object.traverse(function(child)
  1743. {
  1744. child.updateMatrix();
  1745. });
  1746. this.context.setTransform(1, 0, 0, 1, 0, 0);
  1747. // Clear canvas content
  1748. if(this.autoClear)
  1749. {
  1750. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  1751. }
  1752. // Render into the canvas
  1753. for(var i = objects.length - 1; i >= 0; i--)
  1754. {
  1755. if(objects[i].isMask)
  1756. {
  1757. continue;
  1758. }
  1759. if(objects[i].saveContextState)
  1760. {
  1761. this.context.save();
  1762. }
  1763. // Apply all masks
  1764. var masks = objects[i].masks;
  1765. for(var j = 0; j < masks.length; j++)
  1766. {
  1767. if(!masks[j].ignoreViewport)
  1768. {
  1769. viewport.matrix.setContextTransform(this.context);
  1770. }
  1771. masks[j].transform(this.context, viewport, this.canvas);
  1772. masks[j].clip(this.context, viewport, this.canvas);
  1773. }
  1774. // Set the viewport transform
  1775. if(!objects[i].ignoreViewport)
  1776. {
  1777. viewport.matrix.setContextTransform(this.context);
  1778. }
  1779. else if(masks.length > 0)
  1780. {
  1781. this.context.setTransform(1, 0, 0, 1, 0, 0);
  1782. }
  1783. // Apply the object transform to the canvas context
  1784. objects[i].transform(this.context, viewport, this.canvas);
  1785. objects[i].draw(this.context, viewport, this.canvas);
  1786. if(objects[i].restoreContextState)
  1787. {
  1788. this.context.restore();
  1789. }
  1790. }
  1791. };
  1792. /**
  1793. * Box is described by a minimum and maximum points.
  1794. *
  1795. * Can be used for collision detection with points and other boxes.
  1796. *
  1797. * @class
  1798. * @param {Vector2} min
  1799. * @param {Vector2} max
  1800. */
  1801. function Box2(min, max)
  1802. {
  1803. this.min = (min !== undefined) ? min : new Vector2();
  1804. this.max = (max !== undefined) ? max : new Vector2();
  1805. }
  1806. /**
  1807. * Set the box values.
  1808. *
  1809. * @param {Vector2} min
  1810. * @param {Vector2} max
  1811. */
  1812. Box2.prototype.set = function(min, max)
  1813. {
  1814. this.min.copy(min);
  1815. this.max.copy(max);
  1816. return this;
  1817. };
  1818. /**
  1819. * Set the box from a list of Vector2 points.
  1820. *
  1821. * @param {Array} points
  1822. */
  1823. Box2.prototype.setFromPoints = function(points)
  1824. {
  1825. this.min = new Vector2(+Infinity, +Infinity);
  1826. this.max = new Vector2(-Infinity, -Infinity);
  1827. for(var i = 0, il = points.length; i < il; i++)
  1828. {
  1829. this.expandByPoint(points[i]);
  1830. }
  1831. return this;
  1832. };
  1833. /**
  1834. * Set the box minimum and maximum from center point and size.
  1835. *
  1836. * @param {Vector2} center
  1837. * @param {Vector2} size
  1838. */
  1839. Box2.prototype.setFromCenterAndSize = function(center, size)
  1840. {
  1841. var v1 = new Vector2();
  1842. var halfSize = v1.copy(size).multiplyScalar(0.5);
  1843. this.min.copy(center).sub(halfSize);
  1844. this.max.copy(center).add(halfSize);
  1845. return this;
  1846. };
  1847. /**
  1848. * Clone the box into a new object.
  1849. *
  1850. * Should be used when it it necessary to make operations to this box.
  1851. *
  1852. * @return {Box2} New box object with the copy of this object.
  1853. */
  1854. Box2.prototype.clone = function()
  1855. {
  1856. var box = new Box2();
  1857. box.copy(this);
  1858. return box;
  1859. };
  1860. /**
  1861. * Copy the box value from another box.
  1862. *
  1863. * @param {Box2} point
  1864. */
  1865. Box2.prototype.copy = function(box)
  1866. {
  1867. this.min.copy(box.min);
  1868. this.max.copy(box.max);
  1869. };
  1870. /**
  1871. * Check if the box is empty (size equals zero or is negative).
  1872. *
  1873. * The box size is condireded valid on two negative axis.
  1874. *
  1875. * @return {boolean} True if the box is empty.
  1876. */
  1877. Box2.prototype.isEmpty = function()
  1878. {
  1879. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  1880. };
  1881. /**
  1882. * Calculate the center point of the box.
  1883. *
  1884. * @param {Vector2} [target] Vector to store the result.
  1885. * @return {Vector2} Central point of the box.
  1886. */
  1887. Box2.prototype.getCenter = function(target)
  1888. {
  1889. if(target === undefined)
  1890. {
  1891. target = new Vector2();
  1892. }
  1893. this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  1894. return target;
  1895. };
  1896. /**
  1897. * Get the size of the box from its min and max points.
  1898. *
  1899. * @param {Vector2} [target] Vector to store the result.
  1900. * @return {Vector2} Vector with the calculated size.
  1901. */
  1902. Box2.prototype.getSize = function(target)
  1903. {
  1904. if(target === undefined)
  1905. {
  1906. target = new Vector2();
  1907. }
  1908. this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  1909. return target;
  1910. };
  1911. /**
  1912. * Expand the box to contain a new point.
  1913. *
  1914. * @param {Vector2} point
  1915. */
  1916. Box2.prototype.expandByPoint = function(point)
  1917. {
  1918. this.min.min(point);
  1919. this.max.max(point);
  1920. return this;
  1921. };
  1922. /**
  1923. * Expand the box by adding a border with the vector size.
  1924. *
  1925. * Vector is subtracted from min and added to the max points.
  1926. *
  1927. * @param {Vector2} vector
  1928. */
  1929. Box2.prototype.expandByVector = function(vector)
  1930. {
  1931. this.min.sub(vector);
  1932. this.max.add(vector);
  1933. };
  1934. /**
  1935. * Expand the box by adding a border with the scalar value.
  1936. *
  1937. * @param {number} scalar
  1938. */
  1939. Box2.prototype.expandByScalar = function(scalar)
  1940. {
  1941. this.min.addScalar(-scalar);
  1942. this.max.addScalar(scalar);
  1943. };
  1944. /**
  1945. * Check if the box contains a point inside.
  1946. *
  1947. * @param {Vector2} point
  1948. * @return {boolean} True if the box contains point.
  1949. */
  1950. Box2.prototype.containsPoint = function(point)
  1951. {
  1952. return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
  1953. };
  1954. /**
  1955. * Check if the box fully contains another box inside (different from intersects box).
  1956. *
  1957. * Only returns true if the box is fully contained.
  1958. *
  1959. * @param {Box2} box
  1960. * @return {boolean} True if the box contains box.
  1961. */
  1962. Box2.prototype.containsBox = function(box)
  1963. {
  1964. 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;
  1965. };
  1966. /**
  1967. * Check if two boxes intersect each other, using 4 splitting planes to rule out intersections.
  1968. *
  1969. * @param {Box2} box
  1970. * @return {boolean} True if the boxes intersect each other.
  1971. */
  1972. Box2.prototype.intersectsBox = function(box)
  1973. {
  1974. 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;
  1975. };
  1976. /**
  1977. * Calculate the distance to a point.
  1978. *
  1979. * @param {Vector2} point
  1980. * @return {number} Distance to point calculated.
  1981. */
  1982. Box2.prototype.distanceToPoint = function(point)
  1983. {
  1984. var v = new Vector2();
  1985. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  1986. return clampedPoint.sub(point).length();
  1987. };
  1988. /**
  1989. * Make a intersection between this box and another box.
  1990. *
  1991. * Store the result in this object.
  1992. *
  1993. * @param {Box2} box
  1994. */
  1995. Box2.prototype.intersect = function(box)
  1996. {
  1997. this.min.max(box.min);
  1998. this.max.min(box.max);
  1999. };
  2000. /**
  2001. * Make a union between this box and another box.
  2002. *
  2003. * Store the result in this object.
  2004. *
  2005. * @param {Box2} box
  2006. */
  2007. Box2.prototype.union = function(box)
  2008. {
  2009. this.min.min(box.min);
  2010. this.max.max(box.max);
  2011. };
  2012. /**
  2013. * Translate the box by a offset value, adds the offset to booth min and max.
  2014. *
  2015. * @param {Vector2} offset
  2016. */
  2017. Box2.prototype.translate = function(offset)
  2018. {
  2019. this.min.add(offset);
  2020. this.max.add(offset);
  2021. };
  2022. /**
  2023. * Checks if two boxes are equal.
  2024. *
  2025. * @param {Box2} box
  2026. * @return {boolean} True if the two boxes are equal.
  2027. */
  2028. Box2.prototype.equals = function(box)
  2029. {
  2030. return box.min.equals(this.min) && box.max.equals(this.max);
  2031. };
  2032. /**
  2033. * A mask can be used to set the drawing region.
  2034. *
  2035. * Masks are treated as objects their shape is used to filter other objects shape.
  2036. *
  2037. * Multiple mask objects can be active simulatenously, they have to be attached to the object mask list to filter the render region.
  2038. *
  2039. * A mask objects is draw using the context.clip() method.
  2040. *
  2041. * @class
  2042. */
  2043. function Mask()
  2044. {
  2045. Object2D.call(this);
  2046. }
  2047. Mask.prototype = Object.create(Object2D.prototype);
  2048. Mask.prototype.isMask = true;
  2049. /**
  2050. * Clip the canvas context, to ensure that next objects being drawn are cliped to the path stored here.
  2051. *
  2052. * @param {CanvasContext} context Canvas 2d drawing context.
  2053. * @param {Viewport} viewport Viewport applied to the canvas.
  2054. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  2055. */
  2056. Mask.prototype.clip = function(context, viewport, canvas){};
  2057. /**
  2058. * Box mask can be used to clear a box mask region.
  2059. *
  2060. * It will limit the drwaing region to this box.
  2061. *
  2062. * @class
  2063. * @extends {Mask}
  2064. */
  2065. function BoxMask()
  2066. {
  2067. Mask.call(this);
  2068. /**
  2069. * Box object containing the size of the object.
  2070. */
  2071. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  2072. /**
  2073. * If inverted the mask considers the outside of the box instead of the inside.
  2074. */
  2075. this.invert = false;
  2076. }
  2077. BoxMask.prototype = Object.create(Mask.prototype);
  2078. BoxMask.prototype.isInside = function(point)
  2079. {
  2080. return this.box.containsPoint(point);
  2081. };
  2082. BoxMask.prototype.clip = function(context, viewport, canvas)
  2083. {
  2084. context.beginPath();
  2085. var width = this.box.max.x - this.box.min.x;
  2086. if(this.invert)
  2087. {
  2088. context.rect(this.box.min.x - 1e4, -5e3, 1e4, 1e4);
  2089. context.rect(this.box.max.x, -5e3, 1e4, 1e4);
  2090. context.rect(this.box.min.x, this.box.min.y - 1e4, width, 1e4);
  2091. context.rect(this.box.min.x, this.box.max.y, width, 1e4);
  2092. }
  2093. else
  2094. {
  2095. var height = this.box.max.y - this.box.min.y;
  2096. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  2097. }
  2098. context.clip();
  2099. };
  2100. /**
  2101. * Circle object draw a circular object, into the canvas.
  2102. *
  2103. * @class
  2104. */
  2105. function Circle()
  2106. {
  2107. Object2D.call(this);
  2108. /**
  2109. * Radius of the circle.
  2110. */
  2111. this.radius = 10.0;
  2112. /**
  2113. * Color of the circle border line.
  2114. */
  2115. this.strokeStyle = "#000000";
  2116. /**
  2117. * Line width.
  2118. */
  2119. this.lineWidth = 1;
  2120. /**
  2121. * Background color of the circle.
  2122. */
  2123. this.fillStyle = "#FFFFFF";
  2124. }
  2125. Circle.prototype = Object.create(Object2D.prototype);
  2126. Circle.prototype.isInside = function(point)
  2127. {
  2128. return point.length() <= this.radius;
  2129. };
  2130. Circle.prototype.onPointerEnter = function(pointer, viewport)
  2131. {
  2132. this.fillStyle = "#CCCCCC";
  2133. };
  2134. Circle.prototype.onPointerLeave = function(pointer, viewport)
  2135. {
  2136. this.fillStyle = "#FFFFFF";
  2137. };
  2138. Circle.prototype.draw = function(context, viewport, canvas)
  2139. {
  2140. context.beginPath();
  2141. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  2142. context.fillStyle = this.fillStyle;
  2143. context.fill();
  2144. context.lineWidth = this.lineWidth;
  2145. context.strokeStyle = this.strokeStyle;
  2146. context.stroke();
  2147. };
  2148. /**
  2149. * Class contains helper functions to create editing object tools.
  2150. *
  2151. * @class
  2152. */
  2153. function Helpers(){}
  2154. /**
  2155. * Create a rotation tool helper.
  2156. *
  2157. * When the object is dragged is changes the parent object rotation.
  2158. *
  2159. * @static
  2160. */
  2161. Helpers.rotateTool = function(object)
  2162. {
  2163. var tool = new Circle();
  2164. tool.radius = 4;
  2165. tool.layer = object.layer + 1;
  2166. tool.onPointerDrag = function(pointer, viewport, delta)
  2167. {
  2168. object.rotation += delta.x * 1e-3;
  2169. };
  2170. object.add(tool);
  2171. };
  2172. /**
  2173. * Create a box resize helper and attach it to an object to change the size of the object box.
  2174. *
  2175. * Each helper is positioned on one corner of the box, and the value of the corner is copied to the boxes as they are dragged.
  2176. *
  2177. * This method required to object to have a box property.
  2178. *
  2179. * @static
  2180. */
  2181. Helpers.boxResizeTool = function(object)
  2182. {
  2183. if(object.box === undefined)
  2184. {
  2185. console.warn("trenette.js: Helpers.boxResizeTool(), object box property missing.");
  2186. return;
  2187. }
  2188. function updateHelpers()
  2189. {
  2190. topRight.position.copy(object.box.min);
  2191. bottomLeft.position.copy(object.box.max);
  2192. topLeft.position.set(object.box.max.x, object.box.min.y);
  2193. bottomRight.position.set(object.box.min.x, object.box.max.y);
  2194. }
  2195. var topRight = new Circle();
  2196. topRight.radius = 4;
  2197. topRight.layer = object.layer + 1;
  2198. topRight.draggable = true;
  2199. topRight.onPointerDrag = function(pointer, viewport, delta)
  2200. {
  2201. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2202. object.box.min.copy(topRight.position);
  2203. updateHelpers();
  2204. };
  2205. object.add(topRight);
  2206. var topLeft = new Circle();
  2207. topLeft.radius = 4;
  2208. topLeft.layer = object.layer + 1;
  2209. topLeft.draggable = true;
  2210. topLeft.onPointerDrag = function(pointer, viewport, delta)
  2211. {
  2212. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2213. object.box.max.x = topLeft.position.x;
  2214. object.box.min.y = topLeft.position.y;
  2215. updateHelpers();
  2216. };
  2217. object.add(topLeft);
  2218. var bottomLeft = new Circle();
  2219. bottomLeft.radius = 4;
  2220. bottomLeft.layer = object.layer + 1;
  2221. bottomLeft.draggable = true;
  2222. bottomLeft.onPointerDrag = function(pointer, viewport, delta)
  2223. {
  2224. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2225. object.box.max.copy(bottomLeft.position);
  2226. updateHelpers();
  2227. };
  2228. object.add(bottomLeft);
  2229. var bottomRight = new Circle();
  2230. bottomRight.radius = 4;
  2231. bottomRight.layer = object.layer + 1;
  2232. bottomRight.draggable = true;
  2233. bottomRight.onPointerDrag = function(pointer, viewport, delta)
  2234. {
  2235. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2236. object.box.min.x = bottomRight.position.x;
  2237. object.box.max.y = bottomRight.position.y;
  2238. updateHelpers();
  2239. };
  2240. object.add(bottomRight);
  2241. updateHelpers();
  2242. };
  2243. /**
  2244. * Box object draw a box.
  2245. *
  2246. * @class
  2247. */
  2248. function Box()
  2249. {
  2250. Object2D.call(this);
  2251. /**
  2252. * Box object containing the size of the object.
  2253. */
  2254. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  2255. /**
  2256. * Color of the box border line.
  2257. */
  2258. this.strokeStyle = "#000000";
  2259. /**
  2260. * Line width.
  2261. */
  2262. this.lineWidth = 1;
  2263. /**
  2264. * Background color of the box.
  2265. */
  2266. this.fillStyle = "#FFFFFF";
  2267. }
  2268. Box.prototype = Object.create(Object2D.prototype);
  2269. Box.prototype.onPointerEnter = function(pointer, viewport)
  2270. {
  2271. this.fillStyle = "#CCCCCC";
  2272. };
  2273. Box.prototype.onPointerLeave = function(pointer, viewport)
  2274. {
  2275. this.fillStyle = "#FFFFFF";
  2276. };
  2277. Box.prototype.isInside = function(point)
  2278. {
  2279. return this.box.containsPoint(point);
  2280. };
  2281. Box.prototype.draw = function(context, viewport, canvas)
  2282. {
  2283. var width = this.box.max.x - this.box.min.x;
  2284. var height = this.box.max.y - this.box.min.y;
  2285. context.fillStyle = this.fillStyle;
  2286. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  2287. context.lineWidth = this.lineWidth;
  2288. context.strokeStyle = this.strokeStyle;
  2289. context.strokeRect(this.box.min.x, this.box.min.y, width, height);
  2290. };
  2291. /**
  2292. * Line object draw a line from one point to another.
  2293. *
  2294. * @class
  2295. */
  2296. function Line()
  2297. {
  2298. Object2D.call(this);
  2299. /**
  2300. * Initial point of the line.
  2301. *
  2302. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  2303. */
  2304. this.from = new Vector2();
  2305. /**
  2306. * Final point of the line.
  2307. *
  2308. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  2309. */
  2310. this.to = new Vector2();
  2311. /**
  2312. * Color of the line.
  2313. */
  2314. this.strokeStyle = "#000000";
  2315. /**
  2316. * Dash line pattern to be used, is empty draws a solid line.
  2317. */
  2318. this.dashPattern = [5, 5];
  2319. /**
  2320. * Line width.
  2321. */
  2322. this.lineWidth = 1;
  2323. }
  2324. Line.prototype = Object.create(Object2D.prototype);
  2325. Line.prototype.draw = function(context, viewport, canvas)
  2326. {
  2327. context.lineWidth = this.lineWidth;
  2328. context.strokeStyle = this.strokeStyle;
  2329. context.setLineDash(this.dashPattern);
  2330. context.beginPath();
  2331. context.moveTo(this.from.x, this.from.y);
  2332. context.lineTo(this.to.x, this.to.y);
  2333. context.stroke();
  2334. };
  2335. /**
  2336. * Text element, used to draw text into the canvas.
  2337. *
  2338. * @class
  2339. */
  2340. function Text()
  2341. {
  2342. Object2D.call(this);
  2343. /**
  2344. * Text value.
  2345. */
  2346. this.text = "";
  2347. /**
  2348. * Font of the text.
  2349. */
  2350. this.font = "16px Arial";
  2351. /**
  2352. * Color (style) of the text.
  2353. */
  2354. this.color = "#000000";
  2355. /**
  2356. * Text align property.
  2357. */
  2358. this.textAlign = "center";
  2359. }
  2360. Text.prototype = Object.create(Object2D.prototype);
  2361. Text.prototype.draw = function(context, viewport, canvas)
  2362. {
  2363. context.font = this.font;
  2364. context.textAlign = this.textAlign;
  2365. context.fillStyle = this.color;
  2366. context.textBaseline = "middle";
  2367. context.fillText(this.text, 0, 0);
  2368. };
  2369. /**
  2370. * Image object is used to draw an image from URL.
  2371. *
  2372. * @class
  2373. * @param {string} [src] Source URL of the image.
  2374. */
  2375. function Image(src)
  2376. {
  2377. Object2D.call(this);
  2378. /**
  2379. * Box object containing the size of the object.
  2380. */
  2381. this.box = new Box2();
  2382. /**
  2383. * Image source DOM element.
  2384. */
  2385. this.image = document.createElement("img");
  2386. if(src !== undefined)
  2387. {
  2388. this.setImage(src);
  2389. }
  2390. }
  2391. Image.prototype = Object.create(Object2D.prototype);
  2392. /**
  2393. * Set the image of the object.
  2394. *
  2395. * Automatically sets the box size to match the image.
  2396. *
  2397. * @param {string} src Source URL of the image.
  2398. */
  2399. Image.prototype.setImage = function(src)
  2400. {
  2401. var self = this;
  2402. this.image.onload = function()
  2403. {
  2404. self.box.min.set(0, 0);
  2405. self.box.max.set(this.naturalWidth, this.naturalHeight);
  2406. };
  2407. this.image.src = src;
  2408. };
  2409. Image.prototype.isInside = function(point)
  2410. {
  2411. return this.box.containsPoint(point);
  2412. };
  2413. Image.prototype.draw = function(context, viewport, canvas)
  2414. {
  2415. if(this.image.src.length > 0)
  2416. {
  2417. context.drawImage(this.image, 0, 0, this.image.naturalWidth, this.image.naturalHeight, this.box.min.x, this.box.min.y, this.box.max.x - this.box.min.x, this.box.max.y - this.box.min.y);
  2418. }
  2419. };
  2420. /**
  2421. * A DOM object transformed using CSS3D to ver included in the graph.
  2422. *
  2423. * DOM objects always stay on top of everything else, mouse events are not supported for these.
  2424. *
  2425. * Use the normal DOM events for interaction.
  2426. *
  2427. * @class
  2428. * @param parentDOM Parent DOM element that contains the drawing canvas.
  2429. * @param type Type of the DOM element (e.g. "div", "p", ...)
  2430. */
  2431. function DOM(parentDOM, type)
  2432. {
  2433. Object2D.call(this);
  2434. /**
  2435. * Parent element that contains this DOM div.
  2436. */
  2437. this.parentDOM = parentDOM;
  2438. /**
  2439. * DOM element contained by this object.
  2440. *
  2441. * Bye default it has the pointerEvents style set to none.
  2442. */
  2443. this.element = document.createElement("div");
  2444. this.element.style.transformStyle = "preserve-3d";
  2445. this.element.style.position = "absolute";
  2446. this.element.style.top = "0px";
  2447. this.element.style.bottom = "0px";
  2448. this.element.style.transformOrigin = "0px 0px";
  2449. this.element.style.overflow = "auto";
  2450. this.element.style.pointerEvents = "none";
  2451. /**
  2452. * Size of the DOM element (in world coordinates).
  2453. */
  2454. this.size = new Vector2(100, 100);
  2455. }
  2456. DOM.prototype = Object.create(Object2D.prototype);
  2457. DOM.prototype.onAdd = function()
  2458. {
  2459. this.parentDOM.appendChild(this.element);
  2460. };
  2461. DOM.prototype.onRemove = function()
  2462. {
  2463. this.parentDOM.removeChild(this.element);
  2464. };
  2465. DOM.prototype.transform = function(context, viewport, canvas)
  2466. {
  2467. // CSS trasnformation matrix
  2468. var projection = viewport.matrix.clone();
  2469. projection.multiply(this.globalMatrix);
  2470. this.element.style.transform = projection.cssTransform();
  2471. // Size of the element
  2472. this.element.style.width = this.size.x + "px";
  2473. this.element.style.height = this.size.y + "px";
  2474. // Visibility
  2475. this.element.style.display = this.visible ? "block" : "none";
  2476. };
  2477. /**
  2478. * Pattern object draw a image repeated as a pattern.
  2479. *
  2480. * Its similar to the Image class but the image can be repeat infinitly.
  2481. *
  2482. * @class
  2483. */
  2484. function Pattern(src)
  2485. {
  2486. Object2D.call(this);
  2487. /**
  2488. * Box object containing the size of the object.
  2489. */
  2490. this.box = new Box2();
  2491. /**
  2492. * Image source DOM element.
  2493. */
  2494. this.image = document.createElement("img");
  2495. /**
  2496. * A DOMString indicating how to repeat the pattern image.
  2497. */
  2498. this.repetition = "repeat";
  2499. if(src !== undefined)
  2500. {
  2501. this.setImage(src);
  2502. }
  2503. }
  2504. Pattern.prototype = Object.create(Object2D.prototype);
  2505. /**
  2506. * Set the image of the object.
  2507. *
  2508. * Automatically sets the box size to match the image.
  2509. */
  2510. Pattern.prototype.setImage = function(src)
  2511. {
  2512. var self = this;
  2513. this.image.onload = function()
  2514. {
  2515. self.box.min.set(0, 0);
  2516. self.box.max.set(this.naturalWidth, this.naturalHeight);
  2517. };
  2518. this.image.src = src;
  2519. };
  2520. Pattern.prototype.isInside = function(point)
  2521. {
  2522. return this.box.containsPoint(point);
  2523. };
  2524. Pattern.prototype.draw = function(context, viewport, canvas)
  2525. {
  2526. var width = this.box.max.x - this.box.min.x;
  2527. var height = this.box.max.y - this.box.min.y;
  2528. if(this.image.src.length > 0)
  2529. {
  2530. var pattern = context.createPattern(this.image, this.repetition);
  2531. context.fillStyle = pattern;
  2532. context.fillRect(this.box.min.x, this.box.min.y, width, height);
  2533. }
  2534. };
  2535. /**
  2536. * Graph object is used to draw simple graph data into the canvas.
  2537. *
  2538. * Graph data is composed of X, Y values.
  2539. *
  2540. * @class
  2541. */
  2542. function Graph()
  2543. {
  2544. Object2D.call(this);
  2545. /**
  2546. * Graph object containing the size of the object.
  2547. */
  2548. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  2549. /**
  2550. * Color of the box border line.
  2551. */
  2552. this.strokeStyle = "rgb(0, 153, 255)";
  2553. /**
  2554. * Line width.
  2555. */
  2556. this.lineWidth = 1;
  2557. /**
  2558. * Background color of the box.
  2559. */
  2560. this.fillStyle = "rgba(0, 153, 255, 0.3)";
  2561. /**
  2562. * Minimum value of the graph.
  2563. */
  2564. this.min = 0;
  2565. /**
  2566. * Maximum value of the graph.
  2567. */
  2568. this.max = 10;
  2569. /**
  2570. * Data to be presented in the graph.
  2571. *
  2572. * The array should store numeric values.
  2573. */
  2574. this.data = [];
  2575. }
  2576. Graph.prototype = Object.create(Object2D.prototype);
  2577. Graph.prototype.isInside = function(point)
  2578. {
  2579. return this.box.containsPoint(point);
  2580. };
  2581. Graph.prototype.draw = function(context, viewport, canvas)
  2582. {
  2583. if(this.data.length === 0)
  2584. {
  2585. return;
  2586. }
  2587. var width = this.box.max.x - this.box.min.x;
  2588. var height = this.box.max.y - this.box.min.y;
  2589. context.lineWidth = this.lineWidth;
  2590. context.strokeStyle = this.strokeStyle;
  2591. context.beginPath();
  2592. var step = width / (this.data.length - 1);
  2593. var gamma = this.max - this.min;
  2594. context.moveTo(this.box.min.x, this.box.max.y - ((this.data[0] - this.min) / gamma) * height);
  2595. for(var i = 1, s = step; i < this.data.length; s += step, i++)
  2596. {
  2597. context.lineTo(this.box.min.x + s, this.box.max.y - ((this.data[i] - this.min) / gamma) * height);
  2598. }
  2599. context.stroke();
  2600. if(this.fillStyle !== null)
  2601. {
  2602. context.fillStyle = this.fillStyle;
  2603. context.lineTo(this.box.max.x, this.box.max.y);
  2604. context.lineTo(this.box.min.x, this.box.max.y);
  2605. context.fill();
  2606. }
  2607. };
  2608. /**
  2609. * BezierCurve object draw as bezier curve between two points.
  2610. *
  2611. * @class
  2612. */
  2613. function BezierCurve()
  2614. {
  2615. Object2D.call(this);
  2616. /**
  2617. * Initial point of the curve.
  2618. *
  2619. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  2620. */
  2621. this.from = new Vector2();
  2622. /**
  2623. * Intial position control point, indicates the tangent of the bezier curve on the first point.
  2624. */
  2625. this.fromCp = new Vector2();
  2626. /**
  2627. * Final point of the curve.
  2628. *
  2629. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  2630. */
  2631. this.to = new Vector2();
  2632. /**
  2633. * Final position control point, indicates the tangent of the bezier curve on the last point.
  2634. */
  2635. this.toCp = new Vector2();
  2636. /**
  2637. * Color of the line.
  2638. */
  2639. this.strokeStyle = "#000000";
  2640. /**
  2641. * Dash line pattern to be used, is empty draws a solid line.
  2642. */
  2643. this.dashPattern = [5, 5];
  2644. /**
  2645. * BezierCurve width.
  2646. */
  2647. this.lineWidth = 1;
  2648. }
  2649. BezierCurve.prototype = Object.create(Object2D.prototype);
  2650. /**
  2651. * Create a bezier curve helper, to edit the bezier curve anchor points.
  2652. *
  2653. * @static
  2654. */
  2655. BezierCurve.curveHelper = function(object)
  2656. {
  2657. var fromCp = new Circle();
  2658. fromCp.radius = 3;
  2659. fromCp.layer = object.layer + 1;
  2660. fromCp.draggable = true;
  2661. fromCp.onPointerDrag = function(pointer, viewport, delta)
  2662. {
  2663. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2664. object.fromCp.copy(fromCp.position);
  2665. };
  2666. object.parent.add(fromCp);
  2667. var fromLine = new Line();
  2668. fromLine.from = object.from;
  2669. fromLine.to = object.fromCp;
  2670. object.parent.add(fromLine);
  2671. var toCp = new Circle();
  2672. toCp.radius = 3;
  2673. toCp.layer = object.layer + 1;
  2674. toCp.draggable = true;
  2675. toCp.onPointerDrag = function(pointer, viewport, delta)
  2676. {
  2677. Object2D.prototype.onPointerDrag.call(this, pointer, viewport, delta);
  2678. object.toCp.copy(toCp.position);
  2679. };
  2680. object.parent.add(toCp);
  2681. var toLine = new Line();
  2682. toLine.from = object.to;
  2683. toLine.to = object.toCp;
  2684. object.parent.add(toLine);
  2685. };
  2686. BezierCurve.prototype.draw = function(context, viewport, canvas)
  2687. {
  2688. context.lineWidth = this.lineWidth;
  2689. context.strokeStyle = this.strokeStyle;
  2690. context.setLineDash(this.dashPattern);
  2691. context.beginPath();
  2692. context.moveTo(this.from.x, this.from.y);
  2693. context.bezierCurveTo(this.fromCp.x, this.fromCp.y, this.toCp.x, this.toCp.y, this.to.x, this.to.y);
  2694. context.stroke();
  2695. };
  2696. exports.BezierCurve = BezierCurve;
  2697. exports.Box = Box;
  2698. exports.Box2 = Box2;
  2699. exports.BoxMask = BoxMask;
  2700. exports.Circle = Circle;
  2701. exports.DOM = DOM;
  2702. exports.EventManager = EventManager;
  2703. exports.Graph = Graph;
  2704. exports.Helpers = Helpers;
  2705. exports.Image = Image;
  2706. exports.Key = Key;
  2707. exports.Line = Line;
  2708. exports.Mask = Mask;
  2709. exports.Matrix = Matrix;
  2710. exports.Object2D = Object2D;
  2711. exports.Pattern = Pattern;
  2712. exports.Pointer = Pointer;
  2713. exports.Renderer = Renderer;
  2714. exports.Text = Text;
  2715. exports.UUID = UUID;
  2716. exports.Vector2 = Vector2;
  2717. exports.Viewport = Viewport;
  2718. exports.ViewportControls = ViewportControls;
  2719. Object.defineProperty(exports, '__esModule', { value: true });
  2720. }));