escher.module.js 70 KB

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