ConcernementMapItem.vue 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. <script>
  2. // https://brm.io/matter-js/docs/classes/Engine.html
  3. // import {
  4. // // Engine,
  5. // // Render,
  6. // // World,
  7. // Bodies,
  8. // Body,
  9. // Events,
  10. // Composite,
  11. // // Composites,
  12. // // Constraint,
  13. // // Vertices,
  14. // // Mouse,
  15. // // MouseConstraint,
  16. // // Query,
  17. // // Common
  18. // } from "matter-js";
  19. import Matter from "matter-js";
  20. import MatterAttractors from "matter-attractors";
  21. // Matter.use(MatterAttractors);
  22. // import polydecomp from "poly-decomp";
  23. import paper from 'paper';
  24. // import { easeInOutQuad, easeInOutQuart } from 'easing-utils';
  25. import Tween from "@tweenjs/tween.js";
  26. import { mapState, mapActions } from 'pinia'
  27. import { ConcernementsStore } from '@/stores/concernements'
  28. import { CommonStore } from '@/stores/common'
  29. export default {
  30. inject: ['canvasMap', 'matterEngine'],
  31. data() {
  32. return {
  33. id: null,
  34. entities: null,
  35. canvas: null,
  36. ctx: null,
  37. pos : {
  38. x: 0,
  39. y: 0
  40. },
  41. ray: 100,
  42. time: 0,
  43. salientPoints: [],
  44. scale: 1,
  45. prev_scale: 1,
  46. opacity: 0,
  47. tween: null,
  48. is_opening: false,
  49. is_closing: false,
  50. is_hover: false,
  51. //
  52. body: null,
  53. body_parts: [],
  54. constraint: null,
  55. //
  56. paper_main_object: {},
  57. paper_groups: {}
  58. }
  59. },
  60. props: ['concernement', 'is_opened', 'active_revision'],
  61. computed: {
  62. ...mapState(ConcernementsStore,['map_mode',
  63. 'concernementsByID',
  64. 'opened_concernement',
  65. 'opened_entite_id']),
  66. ...mapState(CommonStore,['hover_elmt',
  67. 'map_item_ray',
  68. 'cartouch_width',
  69. 'paper_symbol_definitions'])
  70. },
  71. created () {
  72. // console.log(`ConcernementsMapItem ${this.concernement.id} created`, this.canvasMap, this.matterEngine);
  73. this.id = this.concernement.id
  74. // this.entites = this.concernement.entites
  75. this.entites = this.concernement.revisions_byid[this.concernement.revision_id].entites;
  76. // this.entites_byid = this.concernement.entites_byid
  77. // disable concernement if less than 3 entite
  78. if(this.entites.length < 3){
  79. this.hideShowConcernement(this.concernement.id, false);
  80. } else{
  81. // record canvas and ctx for rendering (drawing)
  82. this.canvas = this.canvasMap.canvas
  83. this.ctx = this.canvasMap.ctx
  84. // define the ray regarding the width and height of screen
  85. this.ray = this.map_item_ray;
  86. // console.log(`this.ray: ${this.ray}`);
  87. //
  88. this.parseEntityPointsValues()
  89. // this.getSalientPoints()
  90. this.getJarvisEnvelopeConvexe()
  91. // if (this.canvasMap) {
  92. // define init position of the item
  93. this.pos = this.getRandomPos();
  94. //
  95. this.initMatterBody()
  96. //
  97. this.initPaperObjects()
  98. // }
  99. }
  100. },
  101. // mounted() {
  102. // console.log(`ConcernementsMapItem ${this.concernement.id} mounted`, this.canvasMap.canvas);
  103. // },
  104. watch: {
  105. // canvasMap (n, o) {
  106. // console.log("concernementItem watch canvasMap", o, n);
  107. // }
  108. canvasMap: {
  109. handler (n, o){
  110. // console.log("concernementItem watch canvasMap.ctx", typeof this.canvas, o, n);
  111. if (!this.canvas) {
  112. this.initCanvasMap()
  113. }
  114. },
  115. deep: true
  116. },
  117. is_opened: {
  118. handler (n, o) {
  119. // console.log('mapItem watch is_opened', n, this.is_opened);
  120. if(n){ // opened
  121. this.openClose(true);
  122. }else{ // closed
  123. this.openClose(false);
  124. }
  125. },
  126. deep: true
  127. },
  128. opened_concernement: {
  129. handler (n, o) {
  130. if (!n) {
  131. this.applyShuffleForces();
  132. }
  133. },
  134. deep: true
  135. },
  136. map_mode: {
  137. handler (n, o) {
  138. console.log('concernementMapItem watch map_mode', o, n);
  139. if (n === 'terraindevie' && !this.opened_concernement) {
  140. this.applyShuffleForces(); // apply a little force to check the map when returning to terrain de vie
  141. }
  142. if (this.is_opened & n !== o) {
  143. this.setPaperContents();
  144. }
  145. if (n !== o) {
  146. this.handlePaperVisibilityOnMapMode()
  147. }
  148. },
  149. deep: true
  150. },
  151. hover_elmt: {
  152. handler (n, o) {
  153. // console.log(`watch hover_elmt ${this.id}`, o, n);
  154. if (n && n.type === 'concernement' && n.id === this.id) {
  155. this.is_hover = true;
  156. } else {
  157. this.is_hover = false;
  158. }
  159. },
  160. deep: true
  161. },
  162. active_revision: {
  163. handler (n, o) {
  164. console.log(`concernementMapItem watch active_revision o:${o}, n:${n}`);
  165. this.resetPaperActiveRevision();
  166. },
  167. deep: true
  168. }
  169. },
  170. methods: {
  171. ...mapActions(CommonStore,['setHoverElmt']),
  172. ...mapActions(ConcernementsStore,['openCloseConcernements',
  173. 'hideShowConcernement',
  174. 'setBesoinPaperId',
  175. 'setOpenedDoleanceField',
  176. 'setOpenedRecit']),
  177. // getResponsiveRay(){
  178. // return Math.min(this.canvas.width, this.canvas.height) * 0.08;
  179. // },
  180. parseEntityPointsValues (){
  181. // converts data (menace/maintien, actuel/future, prise) into atcual position x,y
  182. for (let r = 0; r < this.concernement.revisions.length; r++) { // loop through all revisions
  183. for (let i = 0; i < this.concernement.revisions[r].entites.length; i++) { // loop through all entite for each revision
  184. let entite = this.concernement.revisions[r].entites[i]
  185. // console.log('entite', entite);
  186. entite.display = {
  187. alpha: null,
  188. ray: null
  189. }
  190. // RAYON
  191. // https://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another
  192. // slope = (output_end - output_start) / (input_end - input_start)
  193. // output = output_start + slope * (input - input_start)
  194. // from range 0 -> 100 to range 0 -> this.ray
  195. let init_max = 100
  196. let slope = this.ray / init_max
  197. entite.display.ray = slope * (init_max - entite.prise);
  198. // if (this.concernement.id === 28) {
  199. // console.log(`entity prise: ${entite.prise} | ray: ${entite.display.ray}`);
  200. // }
  201. // ANGLE
  202. // -90 <= mm <= 90
  203. if (entite.actuelfuture) {
  204. // future en haut : 180 <= a <= 360
  205. // from -90 -> 90 to range 180 -> 360
  206. entite.display.alpha = entite.menacemaintien + 270
  207. } else {
  208. // actuel: en bas : O <= a <= 180
  209. // from -90 -> 90 to range 180 -> 0
  210. entite.display.alpha = -1 * entite.menacemaintien + 90
  211. }
  212. // POSITION X Y (par rapport au centre du concernement)
  213. entite.display.pos = {
  214. x: entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180)),
  215. y: entite.display.ray * Math.sin(entite.display.alpha * (Math.PI/180))
  216. }
  217. // if (entite.entite) {
  218. // this.entites_byid[entite.entite.id].display = entite.display;
  219. // }
  220. }
  221. }
  222. },
  223. getSalientPoints_OLD() {
  224. // debugger
  225. // console.log(this.entites);
  226. let arc = 360/30;
  227. // loop through arcs
  228. // for (let i = 360/arc; i >= 0 ; i--) {
  229. for (let i = 0; i <= 360/arc ; i++) {
  230. // loop through entities to find the farest on the arc
  231. let max_r = 0;
  232. let farest = null;
  233. for (let j = 0; j < this.entites.length; j++) {
  234. let entite = this.entites[j];
  235. if(arc*i <= entite.display.alpha && entite.display.alpha <= arc*i+arc) { // if entity is in arc
  236. if (entite.display.ray > max_r) { // && entite.display.ray > this.ray/2 // and farest from minimu
  237. // if entity is farest from precedent one
  238. max_r = entite.display.ray;
  239. // recalcul x & y to get a little padding between entite and contour by increasing ray
  240. farest = {
  241. alpha: entite.display.alpha,
  242. ray: entite.display.ray,
  243. pos: {
  244. x: (entite.display.ray + 3) * Math.cos(entite.display.alpha * (Math.PI/180)),
  245. y: (entite.display.ray + 3) * Math.sin(entite.display.alpha * (Math.PI/180))
  246. }
  247. };
  248. }
  249. }
  250. }
  251. if (farest) {
  252. this.salientPoints.push(farest)
  253. }
  254. }
  255. // console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints);
  256. },
  257. getJarvisEnvelopeConvexe(){
  258. this.salientPoints = [];
  259. let entites = this.concernement.revisions_byid[this.concernement.active_revision].entites;
  260. // console.log(`getJarvisEnvelopeConvexe ${this.id}`, this.entites.length);
  261. // https://www.geeksforgeeks.org/convex-hull-using-jarvis-algorithm-or-wrapping/
  262. // find the most left point
  263. let l, min_x = null;
  264. for (let i = 0; i < entites.length; i++) {
  265. let entite = entites[i];
  266. let x = entite.display.ray * Math.cos(entite.display.alpha * (Math.PI/180));
  267. if(!min_x || min_x > x){
  268. l = i;
  269. min_x = x;
  270. }
  271. }
  272. // Start from leftmost point, keep moving clockwise
  273. // until reach the start point again. This loop runs O(h)
  274. // times where h is number of points in result or output.
  275. let p = l, q;
  276. do {
  277. // console.log(`do while ${this.id}`, p);
  278. // Add current point to result
  279. let entite = entites[p];
  280. let farest = {
  281. alpha: entite.display.alpha,
  282. ray: entite.display.ray,
  283. pos: {
  284. x: (entite.display.ray) * Math.cos(entite.display.alpha * (Math.PI/180)),
  285. y: (entite.display.ray) * Math.sin(entite.display.alpha * (Math.PI/180))
  286. }
  287. };
  288. this.salientPoints.push(farest);
  289. // Search for a point 'q' such that
  290. // orientation(p, q, x) is clockwise
  291. // for all points 'x'. The idea is to keep
  292. // track of last visited most clockwise
  293. // point in q. If any point 'i' is more
  294. // clockwise than q, then update q.
  295. q = (p + 1) % entites.length;
  296. for (let i = 0; i < entites.length; i++) {
  297. let p_x = (entites[p].display.ray) * Math.cos(entites[p].display.alpha * (Math.PI/180));
  298. let p_y = (entites[p].display.ray) * Math.sin(entites[p].display.alpha * (Math.PI/180));
  299. let i_x = (entites[i].display.ray) * Math.cos(entites[i].display.alpha * (Math.PI/180));
  300. let i_y = (entites[i].display.ray) * Math.sin(entites[i].display.alpha * (Math.PI/180));
  301. let q_x = (entites[q].display.ray) * Math.cos(entites[q].display.alpha * (Math.PI/180));
  302. let q_y = (entites[q].display.ray) * Math.sin(entites[q].display.alpha * (Math.PI/180));
  303. let val = (i_y - p_y) * (q_x - i_x) - (i_x - p_x) * (q_y - i_y);
  304. // If i is more clockwise than current q, then update q
  305. if (val > 0){
  306. q = i;
  307. }
  308. }
  309. // Now q is the most clockwise with
  310. // respect to p. Set p as q for next iteration,
  311. // so that q is added to result 'hull'
  312. p = q;
  313. } while (p != l);
  314. },
  315. getRandomPos(){
  316. let pad = 200;
  317. // if (this.concernement.id === 56) {
  318. // return {
  319. // x: Math.random() > 0.5 ? pad : this.canvas.width - pad,
  320. // y: pad + this.ray/2 + Math.random()*(this.canvas.height - this.ray - pad)
  321. // };
  322. // } else {
  323. return {
  324. x: pad + this.getGaussianRandom()*(this.canvas.width - pad*2),
  325. y: pad + this.getGaussianRandom()*(this.canvas.height - pad*2)
  326. };
  327. // }
  328. },
  329. getGaussianRandom(){
  330. // INFO https://stackoverflow.com/a/49434653
  331. let u = 0, v = 0;
  332. while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
  333. while(v === 0) v = Math.random();
  334. let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
  335. num = num / 10.0 + 0.5; // Translate to 0 -> 1
  336. if (num > 1 || num < 0) return this.getGaussianRandom() // resample between 0 and 1
  337. return num
  338. },
  339. // MATTER BODY
  340. initMatterBody (){
  341. // MATTER
  342. // create the matter body and add it to the engine
  343. if (!this.body) {
  344. // console.log('concernementItem creating body');
  345. // INFO https://github.com/liabru/matter-attractors/issues/8
  346. // INFO https://github.com/liabru/matter-attractors/blob/master/index.js
  347. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L180
  348. MatterAttractors.Attractors.gravityConstant = -20;
  349. // Create parts of the body : main big circle & entities
  350. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  351. let entite_range = [3, 100];
  352. let ray_range = [this.ray*0.8,this.ray*1.2];
  353. let ray = ray_range[0] + (this.entites.length - entite_range[0]) * (ray_range[1] - ray_range[0]) / (entite_range[1] - entite_range[0]);
  354. this.body_parts = [
  355. Matter.Bodies.circle(0, 0, ray, {
  356. item_type: 'concernement',
  357. id: this.concernement.id,
  358. })
  359. ];
  360. // INFO map a range of numbers to another range of numbers https://stackoverflow.com/a/46462321
  361. let mass_range = [5,15];
  362. let mass = mass_range[0] + (this.entites.length - entite_range[0]) * (mass_range[1] - mass_range[0]) / (entite_range[1] - entite_range[0]);
  363. // create the body
  364. this.body = Matter.Body.create({
  365. parts: this.body_parts,
  366. item_type: 'concernement',
  367. id: this.concernement.id,
  368. frictionAir: 0,
  369. // mass: Math.pow(3, this.entites.length),
  370. // mass: 10,
  371. mass: mass,
  372. restitution: 0.06,
  373. collisionFilter: {
  374. group: -1
  375. },
  376. plugin: {
  377. attractors: [
  378. // // there is a built in helper function for Newtonian gravity!
  379. // // you can find out how it works in index.js
  380. MatterAttractors.Attractors.gravity
  381. // function(bodyA, bodyB) {
  382. // var force = {
  383. // x: (bodyA.position.x - bodyB.position.x) * 1e-6,
  384. // y: (bodyA.position.y - bodyB.position.y) * 1e-6
  385. // }
  386. // // apply force to both bodies
  387. // Matter.Body.applyForce(bodyA, bodyA.position, force);
  388. // Matter.Body.applyForce(bodyB, bodyB.position, Matter.Vector.neg(force));
  389. // }
  390. // INFO https://github.com/liabru/matter-attractors/blob/master/build/matter-attractors.js#L192
  391. // function (bodyA, bodyB){
  392. // // use Newton's law of gravitation
  393. // var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
  394. // distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
  395. // normal = Matter.Vector.normalise(bToA),
  396. // magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
  397. // force = Matter.Vector.mult(normal, magnitude);
  398. // // to apply forces to both bodies
  399. // Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
  400. // Matter.Body.applyForce(bodyB, bodyB.position, force);
  401. // }
  402. ]
  403. }
  404. });
  405. Matter.Body.setPosition(this.body, this.pos);
  406. // add init velocity
  407. this.setInitBodyVelocity()
  408. // console.log('concernementItem mass', this.body.mass);
  409. Matter.Composite.add(this.matterEngine.world, this.body);
  410. // console.log('concernement body', this.body);
  411. // listen for afterUpdate event from Matter.Engine object
  412. Matter.Events.on(this.matterEngine, "beforeUpdate", this.onBeforeEngineUpdate);
  413. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  414. }
  415. },
  416. setInitBodyVelocity(){
  417. let delta = 50;
  418. Matter.Body.setVelocity(this.body, {
  419. x: -delta + Math.random()*delta*2,
  420. y: -delta + Math.random()*delta*2
  421. });
  422. },
  423. // PAPER OBJECTS
  424. initPaperObjects(){
  425. // the main paper group containing all paper graphical items for one concernement
  426. this.paper_main_object = new paper.Group({
  427. pivot: new paper.Point(this.pos),
  428. cid: this.id
  429. });
  430. // the sub items for one concernement
  431. this.paper_main_object.addChild(this.setPaperContour());
  432. if (this.concernement.has_puissancedagir) {
  433. this.addNewPaperSymbolInstance('puissanceagir_icon', false, 0.7);
  434. }
  435. if (this.concernement.has_agissantes) {
  436. this.paper_main_object.addChild(this.setPaperAgissantesIcons());
  437. }
  438. if (this.concernement.has_doleance) {
  439. this.addNewPaperSymbolInstance('doleance_icon', false, 0.7);
  440. }
  441. this.initPaperEvents()
  442. },
  443. setPaperContents(){
  444. // trigered once opening tween is complete
  445. this.clearPaperContents();
  446. switch(this.map_mode){
  447. case 'terraindevie':
  448. this.addNewPaperSymbolInstance('boussole_bg', true);
  449. this.paper_main_object.addChild(this.setPaperEntites());
  450. break;
  451. case 'puissancedagir':
  452. this.addNewPaperSymbolInstance('puissanceagir_bg', true);
  453. this.paper_main_object.addChild(this.setPaperPuissanceagirBesoins());
  454. break;
  455. case 'action':
  456. this.addNewPaperSymbolInstance('boussole_bg', true);
  457. this.paper_main_object.addChild(this.setPaperAgissantes());
  458. break;
  459. case 'doleancer':
  460. this.addNewPaperSymbolInstance('doleance_bg');
  461. this.paper_main_object.addChild(this.setPaperDoleances());
  462. break;
  463. }
  464. },
  465. resetPaperActiveRevision(){
  466. this.getJarvisEnvelopeConvexe();
  467. // remove contours if already exists
  468. if (this.paper_main_object.children.contours) {
  469. this.paper_main_object.children.contours.remove();
  470. }
  471. this.paper_main_object.addChild(this.setPaperContour());
  472. // remove entites if already exists
  473. if (this.paper_main_object.children.entites) {
  474. this.paper_main_object.children.entites.remove();
  475. }
  476. this.paper_main_object.addChild(this.setPaperEntites());
  477. },
  478. clearPaperContents(){
  479. let clearable_children = ['boussole_bg', 'entites',
  480. 'puissanceagir_bg','puissanceagir_besoins',
  481. 'agissantes',
  482. 'doleance_bg', 'doleances'];
  483. clearable_children.forEach(child_name => {
  484. if (this.paper_main_object.children[child_name]) {
  485. this.paper_main_object.children[child_name].remove();
  486. }
  487. });
  488. },
  489. addNewPaperSymbolInstance(name, back, scale){
  490. let instance = new paper.SymbolItem(this.paper_symbol_definitions[name]); // , {x:0,y:0}
  491. instance.name = name;
  492. // instance.pivot = new paper.Point({x:0,y:0});
  493. instance.position = this.pos;
  494. let s = scale ? this.scale * scale : this.scale;
  495. instance.scale(s);
  496. // instance.locked = true;
  497. this.paper_main_object.addChild(instance);
  498. if (back) {
  499. this.paper_main_object.children[name].sendToBack();
  500. } else {
  501. this.paper_main_object.children[name].bringToFront();
  502. }
  503. },
  504. setPaperContour(){
  505. // console.log(`setPaperContour ${this.concernement.id}`);
  506. let getPaddedRoundedSegments = (b,a,c,d) => {
  507. const ac = { x: c.x - a.x, y: c.y - a.y } // get ac vecteur
  508. const lac = Math.sqrt(Math.pow(ac.x, 2) + Math.pow(ac.y, 2)); // get ac longueur ac
  509. const ab = { x: b.x - a.x, y: b.y - a.y } // get ab vecteur
  510. const lab = Math.sqrt(Math.pow(ab.x, 2) + Math.pow(ab.y, 2)); // get ab longeur
  511. const vab = { x: ab.x/lab, y: ab.y/lab } // get unit vecteur ab
  512. const an = { x: vab.x*lac, y: vab.y*lac } // get an vecteur
  513. const n = { x: a.x + an.x, y: a.y+an.y } // get n point
  514. const m = { x: (c.x + n.x)/2, y: (c.y + n.y)/2 } // get nc midle point
  515. const ma = { x:a.x - m.x, y: a.y - m.y } // get ma vecteur
  516. const lma = Math.sqrt(Math.pow(ma.x, 2)+Math.pow(ma.y, 2)) // get longeur m->a
  517. const vma = { x: ma.x/lma, y: ma.y/lma } // get ma vecteur unitaire
  518. // console.log(`vma x:${vma.x}, y:${vma.y}`);
  519. const pad = 4; // exterior padding
  520. // the final padded point
  521. const pa = [
  522. this.pos.x+(a.x+vma.x*pad)*this.scale,
  523. this.pos.y+(a.y+vma.y*pad)*this.scale
  524. ]
  525. // handles
  526. const delta = 0.05;
  527. // handle IN
  528. const hli = Math.abs(lab)*delta; // handle longeur
  529. const vnai = { x: -vma.y, y: vma.x } // get the ma normal unit vector IN
  530. const hai = [ vnai.x*hli, vnai.y*hli ]; // get the handleIn point
  531. // handle OUT
  532. const hlo = Math.abs(lac)*delta; // handle longeur
  533. const vnao = { x: vma.y, y: -vma.x } // get the ma normal vector Out
  534. const hao = [ vnao.x*hlo, vnao.y*hlo ]; // get the handleOut point
  535. return new paper.Segment({
  536. point: pa,
  537. handleIn: hai,
  538. handleOut: hao
  539. })
  540. }
  541. const first_point = getPaddedRoundedSegments(
  542. this.salientPoints[this.salientPoints.length-1].pos,
  543. this.salientPoints[0].pos,
  544. this.salientPoints[1].pos
  545. );
  546. let segments = [first_point];
  547. for (let j = 1; j < this.salientPoints.length-1; j++) {
  548. // segments.push([this.pos.x+this.salientPoints[j].pos.x*this.scale*gap, this.pos.y+this.salientPoints[j].pos.y*this.scale*gap])
  549. segments.push(getPaddedRoundedSegments(
  550. this.salientPoints[j-1].pos,
  551. this.salientPoints[j].pos,
  552. this.salientPoints[j+1].pos
  553. ))
  554. }
  555. const last_point = getPaddedRoundedSegments(
  556. this.salientPoints[this.salientPoints.length-2].pos,
  557. this.salientPoints[this.salientPoints.length-1].pos,
  558. this.salientPoints[0].pos
  559. );
  560. segments.push(last_point)
  561. segments.push(first_point)
  562. const contrs = new paper.Path({
  563. name: 'contours',
  564. segments: segments,
  565. fillColor: 'rgba(255,255,255,0.4)',
  566. // selected: true,
  567. strokeColor: '#fff',
  568. strokeWidth: 1,
  569. pivot: new paper.Point(this.pos),
  570. cid: this.id
  571. });
  572. return contrs;
  573. },
  574. setPaperEntites(){
  575. let g = new paper.Group({
  576. pivot: new paper.Point(this.pos),
  577. name: 'entites'
  578. });
  579. // for (let i = 0; i < this.entites.length; i++) {
  580. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.active_revision].entites.length; i++) {
  581. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  582. // use paper symbol
  583. let symbol_name = entite.entite ? entite.entite.agissante ? 'entite_action' : 'entite' : 'entite_hidden';
  584. let instance = new paper.SymbolItem(this.paper_symbol_definitions[symbol_name]);
  585. instance.name = 'entite';
  586. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  587. instance.scale(this.scale);
  588. instance.fillColor = '#000';
  589. instance.item_id = entite.entite ? entite.entite.id : null;
  590. instance.item_type = symbol_name;
  591. instance.is_symbol_instance = true;
  592. g.addChild(instance)
  593. }
  594. return g;
  595. },
  596. setPaperAgissantes(){
  597. console.log('setPaperAgissantes');
  598. let g = new paper.Group({
  599. pivot: new paper.Point(this.pos),
  600. name: 'agissantes'
  601. });
  602. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  603. let entite = this.concernement.revisions_byid[this.concernement.revision_id].entites[i];
  604. console.log('agissantes', entite);
  605. if (entite.entite && entite.entite.agissante) {
  606. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action']);
  607. instance.name = 'entite_action';
  608. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  609. instance.fillColor = '#000';
  610. // instance.scale(0.2);
  611. instance.scale(this.scale);
  612. instance.item_id = entite.entite.id;
  613. instance.item_type = 'entite_action';
  614. instance.is_symbol_instance = true;
  615. g.addChild(instance)
  616. }
  617. }
  618. return g;
  619. },
  620. setPaperAgissantesIcons(){
  621. let g = new paper.Group({
  622. pivot: new paper.Point(this.pos),
  623. name: 'agissantes_icons'
  624. });
  625. for (let i = 0; i < this.concernement.revisions_byid[this.concernement.revision_id].entites.length; i++) {
  626. let entite = this.concernement.revisions_byid[this.concernement.active_revision].entites[i];
  627. if (entite.entite && entite.entite.agissante) {
  628. let instance = new paper.SymbolItem(this.paper_symbol_definitions['entite_action_icon']);
  629. instance.name = 'entite_action';
  630. instance.position = new paper.Point([this.pos.x + entite.display.pos.x * this.scale, this.pos.y + entite.display.pos.y * this.scale]);
  631. instance.fillColor = '#000';
  632. // instance.scale(0.2);
  633. instance.scale(this.scale);
  634. instance.item_id = entite.entite.id;
  635. instance.item_type = 'entite_action';
  636. instance.is_symbol_instance = true;
  637. g.addChild(instance)
  638. }
  639. }
  640. return g;
  641. },
  642. setPaperPuissanceagirBesoins(){
  643. let g = new paper.Group({
  644. pivot: new paper.Point(this.pos),
  645. name: 'puissanceagir_besoins'
  646. });
  647. let res_fields = ['qui','quoi','ou','avec'];
  648. let arc = (360 / 16); // unit arc
  649. let r = (this.ray * this.scale)/5; // unit ray
  650. let br = r - r/3; // besoin ray
  651. for (let i = 0; i < this.concernement.besoins.length; i++) {
  652. let start_a = arc * i; // angle depart (for reponses)
  653. let center_a = start_a + arc/2; // angle central
  654. let x = Math.cos(center_a*(Math.PI/180)) * br;
  655. let y = Math.sin(center_a*(Math.PI/180)) * br;
  656. // use paper symbol
  657. let besoin = new paper.SymbolItem(this.paper_symbol_definitions['besoin']);
  658. besoin.position = new paper.Point([this.pos.x + x, this.pos.y + y]);
  659. besoin.scale(this.scale);
  660. besoin.item_id = this.concernement.besoins[i].id;
  661. besoin.item_cid = this.concernement.id;
  662. besoin.item_type = 'besoin';
  663. besoin.is_symbol_instance = true;
  664. console.log(`paper besoin id:${besoin.id}`);
  665. this.setBesoinPaperId(besoin.id, this.id, this.concernement.besoins[i].id);
  666. g.addChild(besoin)
  667. let res_arc = arc / (1 + this.concernement.besoins[i].reponses.length); // unit arc for responses depending responses number
  668. for (let j = 0; j < this.concernement.besoins[i].reponses.length; j++) {
  669. let res_a = start_a + res_arc * (j+1); // angle for response line
  670. for (let f = 0; f < res_fields.length; f++) { // loop through all 4 fields, keep only the last one filled
  671. if( this.concernement.besoins[i].reponses[j][res_fields[f]] // if field filled
  672. && (f === res_fields.length -1 || !this.concernement.besoins[i].reponses[j][res_fields[f+1]]) // and is last field or last field filled
  673. ){
  674. let rr = this.ray * this.scale - r*f - r/2; // reponse field ray
  675. let rx = Math.cos(res_a*(Math.PI/180)) * rr;
  676. let ry = Math.sin(res_a*(Math.PI/180)) * rr;
  677. // use paper symbol
  678. let reponse = new paper.SymbolItem(this.paper_symbol_definitions['reponse']);
  679. reponse.position = new paper.Point([this.pos.x + rx, this.pos.y + ry]);
  680. reponse.scale(this.scale);
  681. reponse.item_id = this.concernement.besoins[i].reponses[j].id;
  682. reponse.item_bid = this.concernement.besoins[i].id;
  683. reponse.item_cid = this.concernement.id;
  684. reponse.item_type = 'reponse';
  685. reponse.is_symbol_instance = true;
  686. this.setBesoinPaperId(reponse.id, this.id, this.concernement.besoins[i].id, this.concernement.besoins[i].reponses[j].id);
  687. g.addChild(reponse)
  688. }
  689. }
  690. }
  691. }
  692. return g;
  693. },
  694. setPaperDoleances(){
  695. let g = new paper.Group({
  696. pivot: new paper.Point({x:0,y:0}),
  697. name: `doleances`
  698. });
  699. this.concernement.doleances.forEach((d) => {
  700. g.addChild(this.setPaperDoleanceSteps(d))
  701. });
  702. return g;
  703. },
  704. setPaperDoleanceSteps(doleance){
  705. let g = new paper.Group({
  706. pivot: new paper.Point({x:0,y:0}),
  707. name: `doleance_${doleance.id}`,
  708. doleance_id: doleance.id
  709. });
  710. // let doleance = this.concernement.doleances[0];
  711. let all_fields = [
  712. [
  713. 'leprobleme',
  714. 'lenquete',
  715. {
  716. fieldname: 'groupesinterets',
  717. fields: [
  718. 'groupe_interets',
  719. 'accorder_interets',
  720. 'formuler',
  721. ]
  722. }
  723. ],
  724. [
  725. 'entite_addresse_doleance',
  726. 'comment_ennonce_doleance',
  727. 'aqui_addresse_doleance',
  728. {
  729. fieldname: 'reception_traitement',
  730. fields: [
  731. 'entite_adressee',
  732. 'doleance_formulee',
  733. 'traite_doleance',
  734. 'entite_recoit_doleance',
  735. ]
  736. }
  737. ],
  738. [
  739. 'entites_decisionnaires',
  740. 'decision_formule',
  741. {
  742. fieldname: 'mise_en_oeuvre_decision',
  743. fields: [
  744. 'entite_adresse_decision',
  745. 'formule_decision',
  746. 'entite_metenoeuvre_decisio',
  747. ]
  748. }
  749. ],
  750. [
  751. 'entite_adresse_application',
  752. 'aqui_adresse_decision',
  753. 'comment_formule_decision',
  754. {
  755. fieldname: 'receptions_et_applications',
  756. fields: [
  757. 'applique_decision',
  758. 'formule_decision_applic',
  759. 'entite_recoit_decision',
  760. ]
  761. },
  762. 'probleme_initial_resolu',
  763. 'oui_nouvelle_situation',
  764. 'non_adresse_doleance',
  765. ]
  766. ];
  767. var r = this.ray * this.scale * 0.8; // ray
  768. var dr = r/2; // demi ray
  769. // var pcr = 2*this.scale; // petits cercle rayon
  770. // https://fr.wikipedia.org/wiki/Trigonom%C3%A9trie#/media/Fichier:Unit_circle_angles_color.svg
  771. // https://fr.wikipedia.org/wiki/Identit%C3%A9_trigonom%C3%A9trique_pythagoricienne#Preuve_utilisant_le_cercle_unit%C3%A9
  772. // radians = degrees * (pi/180)
  773. // degrees = radians * (180/pi)
  774. // Points for 45° axes
  775. let m = Math.sin(45*(Math.PI/180)) * r; // x = y for rayon
  776. let n = Math.sin(45*(Math.PI/180)) * r/2; // x = y for demi rayon
  777. let o = Math.cos(22.5*(Math.PI/180)) * r; // x @ 22.5° for rayon
  778. let p = Math.sin(22.5*(Math.PI/180)) * r; // y @ 22.5° for rayon
  779. let o_d = Math.cos(22.5*(Math.PI/180)) * r/2; // x @ 22.5° for demi rayon
  780. let p_d = Math.sin(22.5*(Math.PI/180)) * r/2; // y @ 22.5° for demi rayon
  781. // let fontsize = 4;
  782. //
  783. // CAMENBERT STATIQUES
  784. //
  785. // l'enquete
  786. //
  787. // camenbert
  788. let cam = new paper.Path({
  789. strokeColor: '#fff',
  790. strokeWidth: 2,
  791. fillColor: "rgba(255, 255, 255, 0.4)",
  792. item_type: 'doleance_step',
  793. item_id: `lenquete`,
  794. did: doleance.id,
  795. field: 'lenquete',
  796. field_index: null
  797. });
  798. cam.add({x: this.pos.x , y: this.pos.y + dr});
  799. cam.lineTo({x: this.pos.x, y: this.pos.y + r});
  800. cam.arcTo({x: this.pos.x - p, y: this.pos.y + o}, {x: this.pos.x - m, y: this.pos.y + m});
  801. cam.lineTo({x: this.pos.x - n, y: this.pos.y + n});
  802. cam.arcTo({x: this.pos.x - p_d, y: this.pos.y + o_d}, {x: this.pos.x , y: this.pos.y + dr});
  803. // texte
  804. // enquete.addChild(new paper.PointText({
  805. // point: {
  806. // x:this.pos.x + Math.sin(22.5*(Math.PI/180)) * r*0.75,
  807. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  808. // },
  809. // content: doleance['lenquete'],
  810. // fontSize: fontsize,
  811. // fillColor: '#fff',
  812. // justification: 'center',
  813. // locked: true
  814. // }));
  815. g.addChild(cam);
  816. //
  817. // probleme_initial_resolu
  818. //
  819. // camenbert
  820. let rescam = new paper.Path({
  821. strokeColor: '#fff',
  822. strokeWidth: 2,
  823. fillColor: "rgba(255, 255, 255, 0.4)",
  824. closed: true,
  825. item_type: 'doleance_step',
  826. item_id: `probleme_initial_resolu`,
  827. did: doleance.id,
  828. field: 'probleme_initial_resolu',
  829. field_index: null
  830. });
  831. rescam.add({x: this.pos.x + n, y: this.pos.y + n});
  832. rescam.lineTo({x: this.pos.x + m , y: this.pos.y + m});
  833. rescam.arcTo({x: this.pos.x + p, y: this.pos.y + o}, {x: this.pos.x, y: this.pos.y + r});
  834. rescam.lineTo({x: this.pos.x, y: this.pos.y + dr});
  835. rescam.arcTo({x: this.pos.x + p_d, y: this.pos.y + o_d}, {x: this.pos.x + n, y: this.pos.y + n});
  836. // texte
  837. // resolution.addChild(new paper.PointText({
  838. // point: {
  839. // x:this.pos.x - Math.sin(22.5*(Math.PI/180)) * r*0.75,
  840. // y:this.pos.y + Math.cos(22.5*(Math.PI/180)) * r*0.75
  841. // },
  842. // content: doleance['probleme_initial_resolu'] ? doleance['oui_nouvelle_situation'] : doleance['non_adresse_doleance'],
  843. // fontSize: fontsize,
  844. // fillColor: '#fff',
  845. // justification: 'center'
  846. // }));
  847. g.addChild(rescam)
  848. //
  849. // MULTIPLE FIELDS
  850. //
  851. let multiple_fields = [
  852. { field_name: 'groupesinterets', arc: 45, decalage: -45 },
  853. { field_name: 'reception_traitement', arc: 90, decalage: -90 },
  854. { field_name: 'mise_en_oeuvre_decision', arc: 90, decalage: -180 },
  855. { field_name: 'receptions_et_applications', arc: 45, decalage: 90 }
  856. ]
  857. multiple_fields.forEach((mf, j) => {
  858. for (let i = 0, l = doleance[mf.field_name].length, a = mf.arc/l; i < l; i++) {
  859. // let gi = new paper.Group({
  860. // item_type: 'doleance_step',
  861. // item_id: 'lenquete',
  862. // item_field: mf.field_name,
  863. // item_index: i
  864. // });
  865. //camenbert
  866. let x1= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * dr,
  867. y1= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * dr;
  868. let x2= this.pos.x + Math.sin((mf.decalage- a*i)*(Math.PI/180)) * r,
  869. y2= this.pos.y + Math.cos((mf.decalage- a*i)*(Math.PI/180)) * r;
  870. let x3= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * r,
  871. y3= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * r;
  872. let x3t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r,
  873. y3t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r;
  874. let x4= this.pos.x + Math.sin((mf.decalage- a*(i+1))*(Math.PI/180)) * dr,
  875. y4= this.pos.y + Math.cos((mf.decalage- a*(i+1))*(Math.PI/180)) * dr;
  876. let x4t= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr,
  877. y4t= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * dr;
  878. let x5= this.pos.x + Math.sin((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75,
  879. y5= this.pos.y + Math.cos((mf.decalage- a*(i+0.5))*(Math.PI/180)) * r*0.75;
  880. let p = new paper.Path({
  881. strokeColor: '#fff',
  882. strokeWidth: 2,
  883. fillColor: "rgba(255, 255, 255, 0.4)",
  884. item_type: 'doleance_step',
  885. item_id: `${mf.field_name}-${i}`,
  886. did: doleance.id,
  887. field: mf.field_name,
  888. field_index: i
  889. });
  890. p.add([x1,y1]);
  891. p.lineTo([x2,y2]);
  892. p.arcTo([x3t,y3t], [x3,y3]);
  893. p.lineTo([x4,y4]);
  894. p.arcTo([x4t,y4t], [x1,y1]);
  895. g.addChild(p);
  896. // gi.addChild(p);
  897. // // text
  898. // gi.addChild(new paper.PointText({
  899. // point: {x:x5,y:y5},
  900. // content: `${j}-${i}`,
  901. // fontSize: fontsize,
  902. // fillColor: '#fff',
  903. // justification: 'center'
  904. // }))
  905. // g.addChild(gi)
  906. }
  907. });
  908. // POINTS CARDINAUX
  909. //
  910. // // leprobleme
  911. // g.addChild(new paper.Path.Circle({
  912. // center: [this.pos.x, this.pos.y + r],
  913. // radius: 3,
  914. // style: {
  915. // strokeColor: '#fff',
  916. // strokeWidth: 2,
  917. // fillColor: "rgba(255, 255, 255, 0.4)",
  918. // },
  919. // item_type: 'doleance_step',
  920. // item_id: `leprobleme`,
  921. // did: doleance.id,
  922. // field: 'leprobleme'
  923. // }));
  924. // adresse de la doleance
  925. g.addChild(new paper.Path.Circle({
  926. center: [this.pos.x - r, this.pos.y],
  927. radius: 3 * this.scale,
  928. style: {
  929. strokeColor: '#fff',
  930. strokeWidth: 2,
  931. fillColor: "rgba(255, 255, 255, 0.9)",
  932. },
  933. item_type: 'doleance_step',
  934. item_id: `adresse_de_la_doleance`,
  935. did: doleance.id,
  936. field: 'adresse_de_la_doleance'
  937. }));
  938. // decision
  939. g.addChild(new paper.Path.Circle({
  940. center: [this.pos.x, this.pos.y - r],
  941. radius: 3 * this.scale,
  942. style: {
  943. strokeColor: '#fff',
  944. strokeWidth: 2,
  945. fillColor: "rgba(255, 255, 255, 0.9)",
  946. },
  947. item_type: 'doleance_step',
  948. item_id: `decision`,
  949. did: doleance.id,
  950. field: 'decision'
  951. }));
  952. // adresse_de_la_decision
  953. g.addChild(new paper.Path.Circle({
  954. center: [this.pos.x + r, this.pos.y],
  955. radius: 3 * this.scale,
  956. style: {
  957. strokeColor: '#fff',
  958. strokeWidth: 2,
  959. fillColor: "rgba(255, 255, 255, 0.9)",
  960. },
  961. item_type: 'doleance_step',
  962. item_id: `adresse_de_la_decision`,
  963. did: doleance.id,
  964. field: 'adresse_de_la_decision'
  965. }));
  966. return g;
  967. },
  968. // PAPER VISIBILITY
  969. handlePaperVisibilityOnBeforeOpen(){
  970. // agissantes
  971. if (this.concernement.has_agissantes && this.map_mode === "action") {
  972. if (!this.is_opened) {
  973. this.paper_main_object.children.agissantes_icons.visible = true;
  974. } else {
  975. this.paper_main_object.children.agissantes_icons.visible = false;
  976. }
  977. }
  978. },
  979. handlePaperVisibilityOnOpened(){
  980. },
  981. handlePaperVisibilityOnBeforeClose(){
  982. },
  983. handlePaperVisibilityOnClosed(){
  984. // agissantes
  985. if (this.concernement.has_agissantes && this.map_mode === "action") {
  986. if (!this.is_opened) {
  987. this.paper_main_object.children.agissantes_icons.visible = true;
  988. } else {
  989. this.paper_main_object.children.agissantes_icons.visible = false;
  990. }
  991. }
  992. },
  993. handlePaperVisibilityOnMapMode(){
  994. },
  995. handlePaperVisibilityOnAfterEnginUpdate(){
  996. // contours focused
  997. if (!this.isFocused()){
  998. this.paper_main_object.children['contours'].fillColor = "rgba(255,255,255,0.1)";
  999. }else{
  1000. this.paper_main_object.children['contours'].fillColor = "rgba(255,255,255,0.4)";
  1001. if (this.is_hover) {
  1002. this.paper_main_object.children['contours'].strokeColor = "#01ffe2";
  1003. this.paper_main_object.children['contours'].strokeWidth = 2;
  1004. }else{
  1005. this.paper_main_object.children['contours'].strokeColor = "#fff";
  1006. this.paper_main_object.children['contours'].strokeWidth = 1;
  1007. }
  1008. }
  1009. // contours visibility
  1010. if (!this.is_opened
  1011. || (this.is_opened && this.map_mode !== "puissancedagir" && this.map_mode !== "doleancer")) {
  1012. this.paper_main_object.children['contours'].visible = true;
  1013. } else {
  1014. this.paper_main_object.children['contours'].visible = false;
  1015. }
  1016. // puissance d'agir
  1017. if (this.concernement.has_puissancedagir) {
  1018. if (this.map_mode === "puissancedagir") {
  1019. if (!this.is_opened) {
  1020. this.paper_main_object.children.puissanceagir_icon.visible = true; // if not opened and has_puissancedagir draw the puissance d'agir icone
  1021. } else {
  1022. this.paper_main_object.children.puissanceagir_icon.visible = false;
  1023. }
  1024. } else {
  1025. this.paper_main_object.children.puissanceagir_icon.visible = false;
  1026. }
  1027. }
  1028. // agissantes
  1029. if (this.concernement.has_agissantes) {
  1030. if (this.map_mode !== "action") {
  1031. this.paper_main_object.children.agissantes_icons.visible = false;
  1032. } else if(!this.is_opened && !this.is_closing){
  1033. this.paper_main_object.children.agissantes_icons.visible = true;
  1034. }
  1035. }
  1036. // doleance
  1037. if (this.concernement.has_doleance) {
  1038. if (this.map_mode === "doleancer") {
  1039. if (!this.is_opened) {
  1040. this.paper_main_object.children.doleance_icon.visible = true;
  1041. } else {
  1042. this.paper_main_object.children.doleance_icon.visible = false;
  1043. // display the right (opened) doleance
  1044. this.concernement.doleances.forEach((d) => {
  1045. this.paper_main_object.children.doleances.children[`doleance_${d.id}`].visible = d.id === this.concernement.opened_doleance.id;
  1046. })
  1047. }
  1048. } else {
  1049. this.paper_main_object.children.doleance_icon.visible = false;
  1050. }
  1051. }
  1052. },
  1053. // PAPER EVENTS
  1054. initPaperEvents(){
  1055. // this.paper_main_object.onMouseEnter = function(event){
  1056. // if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened and is this focused
  1057. // this.setHoverElmt({
  1058. // type: 'concernement',
  1059. // id: this.id
  1060. // });
  1061. // document.body.style.cursor = "pointer";
  1062. // }
  1063. // }.bind(this);
  1064. this.paper_main_object.onMouseLeave = function(event){
  1065. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened
  1066. this.resetHoverElmt();
  1067. document.body.style.cursor = "auto";
  1068. }
  1069. }.bind(this);
  1070. this.paper_main_object.onMouseMove = function(event){
  1071. // console.log(`onmousemove ${this.id}`);
  1072. if (!this.is_opened) {
  1073. if (!this.opened_concernement && this.isFocused()) { // only if no concernement is opened and is this focused
  1074. this.setHoverElmt({
  1075. type: 'concernement',
  1076. id: this.id
  1077. });
  1078. document.body.style.cursor = "pointer";
  1079. }
  1080. } else {
  1081. // lets define some options regarding the map_mode
  1082. let paper_group_tohit;
  1083. switch (this.map_mode) {
  1084. case "terraindevie":
  1085. paper_group_tohit = this.paper_main_object.children['entites'];
  1086. break;
  1087. case "action":
  1088. paper_group_tohit = this.paper_main_object.children['agissantes'];
  1089. break;
  1090. case "puissancedagir":
  1091. paper_group_tohit = this.paper_main_object.children['puissanceagir_besoins'];
  1092. break;
  1093. case "doleancer":
  1094. if(this.paper_main_object.children['doleances']){
  1095. paper_group_tohit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1096. }
  1097. break;
  1098. }
  1099. let result = paper_group_tohit ? paper_group_tohit.hitTest(event.point) : null;
  1100. // console.log('move result', result);
  1101. if (result && result.item.item_id) {
  1102. // console.log('move has result', result);
  1103. let new_hover_elmt = {
  1104. paper_id: result.item.id,
  1105. type: result.item.item_type,
  1106. id: result.item.item_id
  1107. };
  1108. switch (result.item.item_type) {
  1109. case "besoin":
  1110. new_hover_elmt.no_popup = true;
  1111. break;
  1112. case "reponse":
  1113. new_hover_elmt.bid = result.item.item_bid;
  1114. new_hover_elmt.cid = result.item.item_cid;
  1115. new_hover_elmt.no_popup = true;
  1116. break;
  1117. }
  1118. if (!this.hover_elmt || new_hover_elmt.paper_id !== this.hover_elmt.paper_id) {
  1119. // console.log(`before setHoverElmt ${this.id}`);
  1120. this.setHoverElmt(new_hover_elmt);
  1121. }
  1122. document.body.style.cursor = "pointer";
  1123. } else {
  1124. // console.log('move no result');
  1125. this.resetHoverElmt();
  1126. document.body.style.cursor = "auto";
  1127. }
  1128. }
  1129. }.bind(this);
  1130. this.paper_main_object.onClick = function(event){
  1131. console.log('paper concernement onClick');
  1132. if (!this.is_opened) { // si ce concernement n'est pas ouvet
  1133. if (!this.opened_concernement) { // si aucun concernement n'est ouvert
  1134. console.log(`Open me ${this.id}`);
  1135. // open/close all concernements
  1136. this.openCloseConcernements(this.id)
  1137. // push route (keep the hash for map_mode)
  1138. this.$router.push({
  1139. name: 'concernement',
  1140. hash: `#${this.map_mode}`,
  1141. params: {id: this.id}
  1142. });
  1143. // reset the mousehover
  1144. this.resetHoverElmt();
  1145. }
  1146. } else { // si ce concernement est ouvert
  1147. // lets define some options regarding the map_mode
  1148. let group_to_hit = null;
  1149. switch (this.map_mode) {
  1150. case "terraindevie":
  1151. group_to_hit = this.paper_main_object.children['entites'];
  1152. break;
  1153. case "action":
  1154. group_to_hit = this.paper_main_object.children['agissantes'];
  1155. break;
  1156. case "doleancer":
  1157. group_to_hit = this.paper_main_object.children['doleances'].children[`doleance_${this.concernement.opened_doleance.id}`];
  1158. break;
  1159. }
  1160. if (group_to_hit) {
  1161. let result = group_to_hit.hitTest(event.point);
  1162. console.log('click result', result);
  1163. if (result) {
  1164. switch (this.map_mode) {
  1165. case "terraindevie":
  1166. case "action":
  1167. // we have clicked on an entite
  1168. this.$router.push({
  1169. name: 'concernement',
  1170. hash: `#${this.map_mode}`,
  1171. params: {id: this.id, eid: result.item.item_id}
  1172. });
  1173. break;
  1174. case "doleancer":
  1175. this.setOpenedDoleanceField(this.id, result.item.did, result.item.field, result.item.field_index);
  1176. break;
  1177. }
  1178. } else {
  1179. // otherwise we close the entite and come back to the concernement
  1180. this.$router.push({
  1181. name: 'concernement',
  1182. hash: `#${this.map_mode}`,
  1183. params: {id: this.id}
  1184. });
  1185. // reset the mousehover
  1186. this.resetHoverElmt();
  1187. }
  1188. }
  1189. }
  1190. }.bind(this);
  1191. },
  1192. resetHoverElmt(){
  1193. // console.log('resetHoverElmt');
  1194. setTimeout(()=>{
  1195. this.setHoverElmt(null);
  1196. }, 100);
  1197. },
  1198. // OPEN / CLOSE (with tween)
  1199. openClose(open) {
  1200. // console.log(`ConcernementsMapItem ${this.concernement.id} openClose: ${open}`);
  1201. if (this.tween) {
  1202. this.tween.stop();
  1203. }
  1204. if (open) {
  1205. this.is_opening = true;
  1206. // paper bring to front
  1207. this.paper_main_object.bringToFront();
  1208. // create the paper objects to display (like entite, besoin, etc)
  1209. this.setPaperContents();
  1210. this.handlePaperVisibilityOnBeforeOpen();
  1211. // calcul opened size regarding window size
  1212. // let ch = this.canvas.height;
  1213. // let s = this.canvas.height / (this.ray*2.8)
  1214. let s = Math.min(this.canvas.height, this.canvas.width - this.cartouch_width) / (this.ray*2.8)
  1215. // opening tweening
  1216. this.tween = new Tween.Tween({s: this.scale, x: this.pos.x, y: this.pos.y, o: 0})
  1217. .to({
  1218. s: s,
  1219. x: (this.canvas.width - this.cartouch_width) / 2,
  1220. y: this.canvas.height / 2,
  1221. o: 0.8
  1222. }, 800)
  1223. .onUpdate((obj) => {
  1224. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  1225. // revert to the original size (by reverting the previous scale)
  1226. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  1227. this.paper_main_object.scale(1 / this.scale);
  1228. // then scale again to new scale
  1229. Matter.Body.scale(this.body, obj.s, obj.s)
  1230. this.paper_main_object.scale(obj.s);
  1231. // record new scale
  1232. this.prev_scale = this.scale;
  1233. this.scale = obj.s;
  1234. this.opacity = obj.o;
  1235. this.pos = {x:obj.x, y:obj.y};
  1236. Matter.Body.setPosition(this.body, this.pos);
  1237. })
  1238. .onComplete((obj) => {
  1239. // record tween one last time
  1240. this.prev_scale = this.scale = obj.s;
  1241. this.opacity = obj.o;
  1242. this.pos = {x:obj.x, y:obj.y};
  1243. Matter.Body.setPosition(this.body, this.pos);
  1244. this.handlePaperVisibilityOnOpened();
  1245. // fix the concernement position with a constraint
  1246. this.constraint = Matter.Constraint.create({
  1247. pointA: this.pos,
  1248. bodyB: this.body,
  1249. stiffness: 1,
  1250. damping: 0,
  1251. length: 0
  1252. });
  1253. Matter.Composite.add(this.matterEngine.world, [this.body, this.constraint]);
  1254. this.is_opening = false;
  1255. });
  1256. // recreate the matter engine event to get it a the end of the events stack
  1257. Matter.Events.off(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  1258. Matter.Events.on(this.matterEngine, "afterUpdate", this.onAfterEngineUpdate);
  1259. } else {
  1260. // closing
  1261. this.is_closing = true;
  1262. if(this.constraint){
  1263. Matter.Composite.remove(this.matterEngine.world, this.constraint);
  1264. }
  1265. this.concernement.active_revision = this.concernement.revision_id;
  1266. this.handlePaperVisibilityOnBeforeClose();
  1267. // this.setOpenedRecit(null);
  1268. this.tween = new Tween.Tween({s: this.scale, o: 1})
  1269. .to({s: 1, o: 0}, 500)
  1270. .onUpdate((obj) => {
  1271. // https://github.com/liabru/matter-js/issues/986#issuecomment-812488873
  1272. // revert to the original size (by reverting the previous scale)
  1273. Matter.Body.scale(this.body, 1 / this.scale, 1 / this.scale)
  1274. this.paper_main_object.scale(1 / this.scale);
  1275. // then scale again to new scale
  1276. Matter.Body.scale(this.body, obj.s, obj.s)
  1277. this.paper_main_object.scale(obj.s);
  1278. // record new scale
  1279. this.prev_scale = this.scale;
  1280. this.scale = obj.s;
  1281. this.opacity = obj.o;
  1282. })
  1283. .onComplete((obj) => {
  1284. this.prev_scale = this.scale = 1;
  1285. this.handlePaperVisibilityOnClosed();
  1286. this.clearPaperContents();
  1287. this.is_closing = false;
  1288. });
  1289. }
  1290. this.tween.easing(Tween.Easing.Quadratic.InOut).start();
  1291. },
  1292. // ENGINE UPDATE
  1293. onBeforeEngineUpdate (event) {
  1294. // TODO stop item on mouse hover
  1295. if (this.hover_elmt && this.hover_elmt.id === this.id) {
  1296. // Matter.Body.setStatic(this.body, true);
  1297. } else {
  1298. // Matter.Body.setStatic(this.body, false);
  1299. }
  1300. // update the opening/closing tweening
  1301. if (this.tween) {
  1302. this.tween.update();
  1303. }
  1304. if (this.opened_concernement) {
  1305. if (this.opened_concernement.id !== this.id) {
  1306. this.pushAside()
  1307. }
  1308. } else if (this.map_mode === 'puissancedagir'
  1309. || this.map_mode === 'action'
  1310. || this.map_mode === 'doleancer'){ // apply focus forces : move unfocused on the sides and focused on the center
  1311. this.applyFocusForces(); //
  1312. } else {
  1313. this.applyNormalForces(); // if terrain de vie apply force to gently reject items from the sides and so create a continuasly mvmt
  1314. }
  1315. // reset all matter rotation forces otherwise items will spin when colide
  1316. // Matter.Body.setAngle(this.body, 0);
  1317. // Matter.Body.setAngularSpeed(this.body, 0);
  1318. },
  1319. applyFocusForces(){
  1320. if(!this.isFocused()) {
  1321. this.pushAside()
  1322. }else{
  1323. this.bringToCenter()
  1324. }
  1325. },
  1326. isFocused(){
  1327. return this.map_mode === 'terraindevie'
  1328. || (this.map_mode === 'action' && this.concernement.has_agissantes)
  1329. || (this.map_mode === 'puissancedagir' && this.concernement.has_puissancedagir)
  1330. || (this.map_mode === 'doleancer' && this.concernement.has_doleance);
  1331. },
  1332. pushAside(){
  1333. // console.log('pushAside', this.opened_concernement);
  1334. // apply a force in direction of one side or an other depending of the start position
  1335. // the force is exponentialy proportional to the distance from the side
  1336. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1337. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1338. let pseudo_center_x = this.opened_concernement
  1339. ? (this.canvas.width - this.cartouch_width) / 2
  1340. : this.canvas.width / 2;
  1341. // get the direction to the closest side
  1342. let dir = this.pos.x > pseudo_center_x
  1343. ? 1 // to the right
  1344. : -1; // to the left
  1345. // max and min item position
  1346. let minp = 0;
  1347. let maxp = dir < 0
  1348. ? pseudo_center_x
  1349. : this.canvas.width - pseudo_center_x;
  1350. // max and min force
  1351. let minf = 0;
  1352. let maxf = 6;
  1353. // scale factor
  1354. let scale = (maxf-minf) / (maxp-minp);
  1355. // get the inversed distance
  1356. let dist = dir < 0
  1357. ? this.pos.x
  1358. : this.canvas.width - this.pos.x; // get the distance from the side
  1359. // // calculate the force
  1360. // let x_force = Math.pow(dist/700,100) * dir;
  1361. // calculate the logarithmic force
  1362. let x_force = Math.exp(minf + scale*(dist-minp)) * dir;
  1363. let ori_pos = {x:pseudo_center_x, y:this.body.position.y};
  1364. Matter.Body.applyForce(
  1365. this.body,
  1366. ori_pos,
  1367. {
  1368. x: x_force,
  1369. y: 0
  1370. }
  1371. );
  1372. this.body.frictionAir = 0.01;
  1373. },
  1374. bringToCenter(){
  1375. // bring to the centre
  1376. // apply a force in direction of one side or an other depending of the start position
  1377. // the force is exponentialy inversed proportional to the distance from the side
  1378. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1379. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1380. let pseudo_center_x = this.opened_concernement
  1381. ? (this.canvas.width - this.cartouch_width) / 2
  1382. : this.canvas.width / 2;
  1383. let dir = this.pos.x > pseudo_center_x
  1384. ? -1 // to left
  1385. : 1; // to right
  1386. // get the inversed distance from the side
  1387. let dist = dir < 0
  1388. ? this.pos.x - pseudo_center_x
  1389. : pseudo_center_x - this.pos.x;
  1390. if (dist > this.canvas.width/6) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1391. // max & min item position
  1392. let minp = 0;
  1393. let maxp = dir < 0
  1394. ? this.canvas.width - pseudo_center_x
  1395. : pseudo_center_x;
  1396. // X force decrsinsing near the center
  1397. // max and min force
  1398. let xminf = 0;
  1399. let xmaxf = 1 + Math.random()*4;
  1400. // scale factor
  1401. let xscale = (xmaxf-xminf) / (maxp-minp);
  1402. let x_force = Math.exp(xminf + xscale*(dist-minp)) * dir;
  1403. let ori_pos = dir < 0
  1404. ? {x:this.canvas.width, y:this.body.position.y}
  1405. : {x:0, y:this.body.position.y};
  1406. Matter.Body.applyForce(
  1407. this.body,
  1408. ori_pos,
  1409. {
  1410. x: x_force,
  1411. y: 0
  1412. }
  1413. );
  1414. // Friction air fa (increasing near the center)
  1415. let fa_minf = 0;
  1416. let fa_maxf = 6;
  1417. // scale factor
  1418. let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  1419. let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  1420. this.body.frictionAir = fa;
  1421. // console.log(`bringToCenter dist:${dist}, x_force:${x_force}, friction air:${fa}`);
  1422. } else { // in the ceneter zone let items floats freely
  1423. let fa = 0.01;
  1424. let velocity = Matter.Body.getVelocity(this.body);
  1425. if (velocity.x) {
  1426. let velocityx = Math.abs(velocity.x);
  1427. // map a range of numbers to another range of numbers
  1428. // INFO https://stackoverflow.com/a/46462321
  1429. let velocity_range = [50, 1000];
  1430. let fa_range = [0,3];
  1431. fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  1432. // console.log(`bringToCenter velocityx:${velocityx}, fa:${fa}, velocity`, velocity);
  1433. } else {
  1434. console.warn(`bringToCenter no velocity`, velocity);
  1435. }
  1436. this.body.frictionAir = fa;
  1437. }
  1438. },
  1439. applyShuffleForces() {
  1440. // console.log('applyShuffleForces');
  1441. // var dist, dir, x_velocity;
  1442. // dir = this.pos.x > this.canvas.width/2 ? -1 : 1; // get the direction to the centre
  1443. // dist = (dir < 0 ? this.pos.x - this.canvas.width/2 : this.canvas.width/2 - this.pos.x); // get the distance from the side
  1444. // x_velocity = Math.pow(dist/650,10) * dir;
  1445. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1446. let pseudo_center_x = this.opened_concernement
  1447. ? (this.canvas.width - this.cartouch_width) / 2
  1448. : this.canvas.width / 2;
  1449. // get the direction to the centre
  1450. let dir = this.pos.x > pseudo_center_x
  1451. ? -1 // to left
  1452. : 1; // to right
  1453. // get the inversed distance from the side
  1454. let dist = dir < 0
  1455. ? this.pos.x - pseudo_center_x
  1456. : pseudo_center_x - this.pos.x;
  1457. dist = dist > 1000 ? 1000 : dist;
  1458. if (dist > this.canvas.width/6) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1459. // max & min item position
  1460. let minp = 0;
  1461. let maxp = dir < 0
  1462. ? this.canvas.width - pseudo_center_x
  1463. : pseudo_center_x;
  1464. // X max and min force
  1465. let xminf = 0;
  1466. let xmaxf = 4 + Math.random()*3;
  1467. // scale factor
  1468. let xscale = (xmaxf-xminf) / (maxp-minp);
  1469. let x_velocity = Math.exp(xminf + xscale*(dist-minp))*dir;
  1470. // let y_velocity = 30 + Math.random() * 50;
  1471. // y_velocity = Math.random() > 0.5 ? -y_velocity : y_velocity;
  1472. // X max and min force
  1473. let yminf = 1;
  1474. let ymaxf = 2 + Math.random()*4;
  1475. // scale factor
  1476. let yscale = (ymaxf-yminf) / (maxp-minp);
  1477. let y_velocity = Math.exp(yminf + yscale*(dist-minp)) * (Math.random() > 0.5 ? 1 : -1); // let ori_pos = dir < 0
  1478. // ? {x:this.canvas.width, y:this.body.position.y}
  1479. // : {x:0, y:this.body.position.y};
  1480. // let x_force = Math.pow(dist/800,10) * dir;
  1481. console.log(`applyShuffleForces dir:${dir}, maxp:${maxp}, dist:${dist}, x_velocity:${x_velocity}, y_velocity:${y_velocity}`);
  1482. Matter.Body.setVelocity(this.body, {x: x_velocity, y: y_velocity});
  1483. } else { // if in center zone real shuffle velocity
  1484. // x_velocity = -50 + Math.random()*100;
  1485. // y_velocity = -50 + Math.random()*100;
  1486. // this.setInitBodyVelocity();
  1487. }
  1488. this.body.frictionAir = 0.01;
  1489. },
  1490. applyNormalForces () {
  1491. // bring gently to the centre
  1492. // apply a force in direction of one side or an other depending of the start position
  1493. // the force is exponentialy inversed proportional to the distance from the side
  1494. // INFO logarithmic force : https://stackoverflow.com/questions/846221/logarithmic-slider/846249#846249
  1495. // TODO cartouch width should change regarding actual cartouch is opened or not (and how many cartouch are opened)
  1496. // X
  1497. let pseudo_center_x = this.opened_concernement
  1498. ? (this.canvas.width - this.cartouch_width) / 2
  1499. : this.canvas.width / 2;
  1500. let xdir = this.pos.x > pseudo_center_x
  1501. ? -1 // to left
  1502. : 1; // to right
  1503. // get the inversed distance from the side
  1504. let xdist = xdir < 0
  1505. ? this.pos.x - pseudo_center_x
  1506. : pseudo_center_x - this.pos.x;
  1507. // Y
  1508. let pseudo_center_y = this.canvas.height / 2;
  1509. let ydir = this.pos.y > pseudo_center_y
  1510. ? -1 // to top
  1511. : 1; // to bottom
  1512. // get the inversed distance from the side
  1513. let ydist = ydir < 0
  1514. ? this.pos.y - pseudo_center_y
  1515. : pseudo_center_y - this.pos.y;
  1516. let k = 4;
  1517. if (xdist > this.canvas.width/k || ydist > this.canvas.height/k) { // apply decreasing forces and increasing friction air only out of center to let the items float in the center zone
  1518. // max and min force
  1519. let minf = 0;
  1520. let maxf = 0.1;
  1521. // X
  1522. // max & min item position
  1523. let xminp = 0;
  1524. let xmaxp = xdir < 0
  1525. ? this.canvas.width - pseudo_center_x
  1526. : pseudo_center_x;
  1527. // X force decrsinsing near the center
  1528. // scale factor
  1529. let xscale = (maxf-minf) / (xmaxp-xminp);
  1530. let x_force = Math.exp(minf + xscale*(xdist-xminp)) * xdir;
  1531. let xori_pos = xdir < 0
  1532. ? {x:this.canvas.width, y:this.body.position.y}
  1533. : {x:0, y:this.body.position.y};
  1534. Matter.Body.applyForce(
  1535. this.body,
  1536. xori_pos,
  1537. {
  1538. x: x_force,
  1539. y: 0
  1540. }
  1541. );
  1542. // Y
  1543. // max & min item position
  1544. let yminp = 0;
  1545. let ymaxp = ydir < 0
  1546. ? this.canvas.height - pseudo_center_y
  1547. : pseudo_center_y;
  1548. // X force decrsinsing near the center
  1549. // scale factor
  1550. let yscale = (maxf-minf) / (ymaxp-yminp);
  1551. let y_force = Math.exp(minf + yscale*(ydist-xminp)) * ydir;
  1552. let yori_pos = ydir < 0
  1553. ? {x:this.body.position.x, y:this.canvas.height}
  1554. : {x:this.body.position.x, y:0};
  1555. Matter.Body.applyForce(
  1556. this.body,
  1557. yori_pos,
  1558. {
  1559. x: 0,
  1560. y: y_force
  1561. }
  1562. );
  1563. // // Friction air fa (increasing near the center)
  1564. // let fa_minf = 0;
  1565. // let fa_maxf = 6;
  1566. // // scale factor
  1567. // let fa_scale = (fa_maxf-fa_minf) / (maxp-minp);
  1568. // let fa = Math.exp(fa_minf + fa_scale*(dist-minp)*-1);
  1569. // this.body.frictionAir = fa;
  1570. // console.log(`bringToCenter dist:${xdist}, x_force:${x_force}`);
  1571. } else { // in the ceneter zone let items floats freely
  1572. // let velocity = Matter.Body.getVelocity(this.body);
  1573. // let velocityx = Math.abs(velocity.x);
  1574. // // map a range of numbers to another range of numbers
  1575. // // INFO https://stackoverflow.com/a/46462321
  1576. // let velocity_range = [50, 1000];
  1577. // let fa_range = [0,3];
  1578. // let fa = fa_range[0] + (velocityx - velocity_range[0]) * (fa_range[1] - fa_range[0]) / (velocity_range[1] - velocity_range[0]);
  1579. // console.log(`bringToCenter velocity.x:${velocity.x}, fa:${fa}`);
  1580. // this.body.frictionAir = fa;
  1581. this.body.frictionAir = 0.1;
  1582. }
  1583. },
  1584. respawn() {
  1585. // respawn element if outside screen
  1586. if(this.pos.x <= 0
  1587. || this.pos.x >= this.canvas.width
  1588. || this.pos.y <= 0
  1589. || this.pos.y >= this.canvas.height){
  1590. this.pos = this.getRandomPos()
  1591. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y});
  1592. this.setInitBodyVelocity();
  1593. }
  1594. },
  1595. onAfterEngineUpdate (event) {
  1596. this.respawn();
  1597. if (this.is_hover) { // if mouse hover reset the matter body position to prev position
  1598. Matter.Body.setPosition(this.body, {x:this.pos.x, y:this.pos.y}, true);
  1599. }
  1600. this.paper_main_object.position = this.pos = this.body.position;
  1601. this.handlePaperVisibilityOnAfterEnginUpdate()
  1602. },
  1603. },
  1604. render() {
  1605. // console.log('render()', this.ctx);
  1606. },
  1607. }
  1608. </script>