escher.module.js 66 KB

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