server actix for cannabinieri website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.1 KiB

3 years ago
  1. let graph = {
  2. nodes : [
  3. {id : "hemp", index: 0, x: 719.1790913580512, y: 326.11409230900296, vy: 0.0009665554475078223, vx: 0.00044710537175425926},
  4. {id : "permapp", index: 1, x: 473.51321690437135, y: 317.1181430586339, vy: 0.0005811239352358496, vx: 0.0005105059544352408 },
  5. {id : "spider", index: 2, x: 495.66982580572676, y: 18.53, vy: 0.0007713356454593206, vx: 0.0007887999665437037 },
  6. {id : "cyberpreneur", index: 3, x: 457.9995206323447, y: 277.13445200140694, vy: 0.0004637678594351416, vx: 0.0007441570379881761 },
  7. {id : "kaoscube", index: 4, x: 428.05791236956037, y: 237.65121337678465, vy: 0.00037483669542696317, vx: 0.00039028860355878376 },
  8. {id : "offgrid", index: 5, x: 461.65464520161396, y: 189.18158017082138, vy: -0.00023265081878199873, vx: 0.00006278089396472572 }
  9. ],
  10. links : [
  11. {source: 0, target: 1, index: 0 },
  12. {source: 0, target: 2, index: 1 },
  13. {source: 0, target: 3, index: 2 },
  14. {source: 0, target: 4, index: 3 },
  15. {source: 1, target: 2, index: 4 },
  16. {source: 1, target: 4, index: 5 },
  17. {source: 2, target: 3 ,index: 6 },
  18. {source: 2, target: 4 ,index: 7 },
  19. {source: 2, target: 5 ,index: 8 },
  20. {source: 4, target: 5 ,index: 9 }
  21. ],
  22. }
  23. let width = 100;
  24. let height = 200;
  25. const svg = d3.select("#mobile_network").append("svg").attr("viewBox", "0, 0, 100, 200").attr("preserveAspectRatio", "xMidYMid meet"),
  26. link = svg
  27. .selectAll(".link")
  28. .data(graph.links)
  29. .join("line")
  30. .classed("link", true),
  31. node = svg
  32. .selectAll(".node")
  33. .data(graph.nodes)
  34. .join("circle")
  35. .attr("id", d => d.id)
  36. .attr("r", 15)
  37. .classed("node", true)
  38. .classed("fixed", d => d.fx !== undefined);
  39. const node1 = svg.select("#hemp")
  40. .attr("fill", "url(#imgHemp)")
  41. .append("a")
  42. .attr("xlink:href", "/hemp");
  43. const node2 = svg.select("#permapp")
  44. .attr("fill", "url(#imgPermapp)");
  45. const node3 = svg.select("#spider")
  46. .attr("fill", "url(#imgSpider)");
  47. const node4 = svg.select("#cyberpreneur")
  48. .attr("fill", "url(#imgCyber)");
  49. const node5 = svg.select("#kaoscube")
  50. .attr("fill", "url(#imgKaos)");
  51. const node6 = svg.select("#offgrid")
  52. .attr("fill", "url(#imgOff)");
  53. const defs = svg.append("svg:defs");
  54. defs.append("svg:pattern")
  55. .attr("width", "100%")
  56. .attr("height", "100%")
  57. .attr("id", "imgHemp")
  58. .append("svg:image")
  59. .attr("xlink:href", "/assets/img/hemp.webp")
  60. .attr("width", 35)
  61. .attr("height", 35)
  62. .attr("x", -5 )
  63. .attr("y", 0);
  64. defs.append("svg:pattern")
  65. .attr("width", "100%")
  66. .attr("height", "100%")
  67. .attr("id", "imgPermapp")
  68. .append("svg:image")
  69. .attr("xlink:href", "/assets/img/v1-permapp.webp")
  70. .attr("width", 35)
  71. .attr("height", 35)
  72. .attr("x", 0 )
  73. .attr("y", 0);
  74. defs.append("svg:pattern")
  75. .attr("width", "100%")
  76. .attr("height", "100%")
  77. .attr("id", "imgSpider")
  78. .append("svg:image")
  79. .attr("xlink:href", "/assets/img/spider_out.webp")
  80. .attr("width", 34)
  81. .attr("height", 34)
  82. .attr("x", -4 )
  83. .attr("y", -4);
  84. defs.append("svg:pattern")
  85. .attr("width", "100%")
  86. .attr("height", "100%")
  87. .attr("id", "imgCyber")
  88. .append("svg:image")
  89. .attr("xlink:href", "/assets/img/3d_greenhouse.webp")
  90. .attr("width", 40)
  91. .attr("height", 40)
  92. .attr("x", -5 )
  93. .attr("y", -8);
  94. defs.append("svg:pattern")
  95. .attr("width", "100%")
  96. .attr("height", "100%")
  97. .attr("id", "imgKaos")
  98. .append("svg:image")
  99. .attr("xlink:href", "/assets/img/cube_space.webp")
  100. .attr("width", 35)
  101. .attr("height", 35)
  102. .attr("x", -6 )
  103. .attr("y", -6);
  104. defs.append("svg:pattern")
  105. .attr("width", "100%")
  106. .attr("height", "100%")
  107. .attr("id", "imgOff")
  108. .append("svg:image")
  109. .attr("xlink:href", "/assets/img/boat_construction.webp")
  110. .attr("width", 35)
  111. .attr("height", 35)
  112. .attr("x", -6 )
  113. .attr("y", -6);
  114. const simulation = d3
  115. .forceSimulation()
  116. .nodes(graph.nodes)
  117. .force("charge", d3.forceManyBody().strength(-60))
  118. .force("center", d3.forceCenter(width / 2.1, height / 2.1))
  119. .force("link", d3.forceLink(graph.links).distance(50))
  120. .force("collide", d3.forceCollide(22))
  121. .force("x", d3.forceX(-40))
  122. .on("tick", tick);
  123. const drag = d3
  124. .drag()
  125. .on("start", dragstart)
  126. .on("drag", dragged);
  127. // invokes regiserd callback
  128. node.call(drag).on("click", click);
  129. function tick() {
  130. link
  131. .attr("x1", d => d.source.x)
  132. .attr("y1", d => d.source.y)
  133. .attr("x2", d => d.target.x)
  134. .attr("y2", d => d.target.y);
  135. node
  136. .attr("cx", d => d.x)
  137. .attr("cy", d => d.y);
  138. }
  139. // make this work on touch as well
  140. function click(event, d) {
  141. delete d.fx;
  142. delete d.fy;
  143. d3.select(this).classed("fixed", false);
  144. simulation.alpha(1).restart();
  145. // on this click display html text box
  146. }
  147. function dragstart() {
  148. d3.select(this).classed("fixed", true);
  149. }
  150. function dragged(event, d) {
  151. d.fx = clamp(event.x, 0, width);
  152. d.fy = clamp(event.y, 0, height);
  153. simulation.alpha(1).restart();
  154. }
  155. function clamp(x, lo, hi) {
  156. return x < lo ? lo : x > hi ? hi : x;
  157. }
  158. const Nodes = graph.nodes;
  159. const nodeIx = d => d.img
  160. const N = d3.map(Nodes, nodeIx);
  161. const Images = d3.map( Nodes, ( _,i ) => ({ img: N[i] }) );