escher.js 69 KB

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