trenette.module.js 58 KB

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