vis.js is a dynamic, browser-based visualization library
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.

314 lines
11 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Animation</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  10. width: 600px;
  11. height: 600px;
  12. border: 1px solid lightgray;
  13. }
  14. div.left {
  15. position:relative;
  16. float:left;
  17. width:300px;
  18. border: 1px #c7c7c7 solid;
  19. height:590px;
  20. padding:5px;
  21. }
  22. div.right {
  23. padding-left:10px;
  24. float:left;
  25. width:600px;
  26. }
  27. div.bottom {
  28. position:absolute;
  29. bottom:5px;
  30. }
  31. </style>
  32. <script type="text/javascript" src="../../dist/vis.js"></script>
  33. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  34. <script type="text/javascript">
  35. var nodes = null;
  36. var edges = null;
  37. var network = null;
  38. var doButton, focusButton, showButton;
  39. var statusUpdateSpan;
  40. var finishMessage = "";
  41. function draw() {
  42. statusUpdateSpan = document.getElementById("statusUpdate");
  43. doButton = document.getElementById("btnDo");
  44. focusButton = document.getElementById("btnFocus");
  45. showButton = document.getElementById("btnShow");
  46. nodes = [];
  47. edges = [];
  48. var connectionCount = [];
  49. // randomly create some nodes and edges
  50. var nodeCount = 25;
  51. for (var i = 0; i < nodeCount; i++) {
  52. nodes.push({
  53. id: i,
  54. label: String(i)
  55. });
  56. connectionCount[i] = 0;
  57. // create edges in a scale-free-network way
  58. if (i == 1) {
  59. var from = i;
  60. var to = 0;
  61. edges.push({
  62. from: from,
  63. to: to
  64. });
  65. connectionCount[from]++;
  66. connectionCount[to]++;
  67. }
  68. else if (i > 1) {
  69. var conn = edges.length * 2;
  70. var rand = Math.floor(Math.random() * conn);
  71. var cum = 0;
  72. var j = 0;
  73. while (j < connectionCount.length && cum < rand) {
  74. cum += connectionCount[j];
  75. j++;
  76. }
  77. var from = i;
  78. var to = j;
  79. edges.push({
  80. from: from,
  81. to: to
  82. });
  83. connectionCount[from]++;
  84. connectionCount[to]++;
  85. }
  86. }
  87. // create a network
  88. var container = document.getElementById('mynetwork');
  89. var data = {
  90. nodes: nodes,
  91. edges: edges
  92. };
  93. var options = {};
  94. network = new vis.Network(container, data, options);
  95. // add event listeners
  96. network.on('select', function(params) {
  97. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  98. });
  99. network.on('stabilized', function (params) {
  100. document.getElementById('stabilization').innerHTML = 'Stabilization took ' + params.iterations + ' iterations.';
  101. });
  102. network.on("animationFinished", function() {
  103. statusUpdateSpan.innerHTML = finishMessage;
  104. })
  105. }
  106. function zoomExtentAnimated() {
  107. var offsetx = parseInt(document.getElementById("offsetx").value);
  108. var offsety = parseInt(document.getElementById("offsety").value);
  109. var duration = parseInt(document.getElementById("duration").value);
  110. var easingFunction = document.getElementById("easingFunction").value;
  111. var options = {offset: {x:offsetx,y:offsety},
  112. duration: duration,
  113. easingFunction: easingFunction
  114. }
  115. statusUpdateSpan.innerHTML = "Doing ZoomExtent() Animation.";
  116. finishMessage = "Animation finished."
  117. network.zoomExtent(options);
  118. }
  119. function doDefaultAnimation() {
  120. var offsetx = parseInt(document.getElementById("offsetx").value);
  121. var offsety = parseInt(document.getElementById("offsety").value);
  122. var scale = parseFloat(document.getElementById("scale").value);
  123. var positionx = parseInt(document.getElementById("positionx").value);
  124. var positiony = parseInt(document.getElementById("positiony").value);
  125. var easingFunction = document.getElementById("easingFunction").value;
  126. var options = {
  127. position: {x:positionx,y:positiony},
  128. scale: scale,
  129. offset: {x:offsetx,y:offsety},
  130. animation: true // default duration is 1000ms and default easingFunction is easeInOutQuad.
  131. }
  132. statusUpdateSpan.innerHTML = "Doing Animation.";
  133. finishMessage = "Animation finished."
  134. network.moveTo(options);
  135. }
  136. function doAnimation() {
  137. var offsetx = parseInt(document.getElementById("offsetx").value);
  138. var offsety = parseInt(document.getElementById("offsety").value);
  139. var duration = parseInt(document.getElementById("duration").value);
  140. var scale = parseFloat(document.getElementById("scale").value);
  141. var positionx = parseInt(document.getElementById("positionx").value);
  142. var positiony = parseInt(document.getElementById("positiony").value);
  143. var easingFunction = document.getElementById("easingFunction").value;
  144. var options = {
  145. position: {x:positionx,y:positiony},
  146. scale: scale,
  147. offset: {x:offsetx,y:offsety},
  148. animation: {
  149. duration: duration,
  150. easingFunction: easingFunction
  151. }
  152. }
  153. statusUpdateSpan.innerHTML = "Doing Animation.";
  154. finishMessage = "Animation finished."
  155. network.moveTo(options);
  156. }
  157. function focusRandom() {
  158. var nodeId = Math.floor(Math.random() * 25);
  159. var offsetx = parseInt(document.getElementById("offsetx").value);
  160. var offsety = parseInt(document.getElementById("offsety").value);
  161. var duration = parseInt(document.getElementById("duration").value);
  162. var scale = parseFloat(document.getElementById("scale").value);
  163. var easingFunction = document.getElementById("easingFunction").value;
  164. var options = {
  165. // position: {x:positionx,y:positiony}, // this is not relevant when focusing on nodes
  166. scale: scale,
  167. offset: {x:offsetx,y:offsety},
  168. animation: {
  169. duration: duration,
  170. easingFunction: easingFunction
  171. }
  172. }
  173. statusUpdateSpan.innerHTML = "Focusing on node: " + nodeId;
  174. finishMessage = "Node: " + nodeId + " in focus.";
  175. network.focusOnNode(nodeId, options);
  176. }
  177. var showInterval = false;
  178. var showPhase = 1;
  179. function startShow() {
  180. if (showInterval !== false) {
  181. showInterval = false;
  182. showButton.value = "Start a show!";
  183. network.zoomExtent();
  184. }
  185. else {
  186. showButton.value = "Stop the show!";
  187. var duration = parseInt(document.getElementById("duration").value);
  188. focusRandom();
  189. window.setTimeout(doTheShow, duration);
  190. showInterval = true;
  191. }
  192. }
  193. function doTheShow() {
  194. if (showInterval == true) {
  195. var duration = parseInt(document.getElementById("duration").value);
  196. if (showPhase == 0) {
  197. focusRandom();
  198. showPhase = 1;
  199. }
  200. else {
  201. zoomExtentAnimated();
  202. showPhase = 0;
  203. }
  204. window.setTimeout(doTheShow, duration);
  205. }
  206. }
  207. </script>
  208. </head>
  209. <body onload="draw();">
  210. <h2>Camera animations</h2>
  211. <div style="width:700px; font-size:14px;">
  212. You can move the view around programmatically using the .moveTo(options) function. The options supplied to this function can
  213. also be (partially) supplied to the zoomExtent() function
  214. </div>
  215. <pre>
  216. var moveToOptions = {
  217. position: {x:x, y:x}, // position to animate to (Numbers)
  218. scale: 1.0, // scale to animate to (Number)
  219. offset: {x:x, y:y}, // offset from the center in DOM pixels (Numbers)
  220. animation: { // animation object, can also be Boolean
  221. duration: 1000, // animation duration in milliseconds (Number)
  222. easingFunction: "easeInOutQuad" // Animation easing function, available are:
  223. } // linear, easeInQuad, easeOutQuad, easeInOutQuad,
  224. } // easeInCubic, easeOutCubic, easeInOutCubic,
  225. // easeInQuart, easeOutQuart, easeInOutQuart,
  226. // easeInQuint, easeOutQuint, easeInOutQuint
  227. </pre>
  228. <div class="left">
  229. <table>
  230. <tr>
  231. <td>position x</td><td><input type="text" value="300" id="positionx" style="width:170px;"></td>
  232. </tr>
  233. <tr>
  234. <td>position y</td><td><input type="text" value="300" id="positiony" style="width:170px;"></td>
  235. </tr>
  236. <tr>
  237. <td>scale</td><td><input type="text" value="1.0" id="scale" style="width:170px;"></td>
  238. </tr>
  239. <tr>
  240. <td>offset x</td><td><input type="text" value="0" id="offsetx" style="width:170px;"> px</td>
  241. </tr>
  242. <tr>
  243. <td>offset y</td><td><input type="text" value="0" id="offsety" style="width:170px;"> px</td>
  244. </tr>
  245. <tr>
  246. <td>duration</td><td><input type="text" value="2500" id="duration" style="width:170px;"> ms</td>
  247. </tr>
  248. <tr>
  249. <td>easingFunction</td><td>
  250. <select id="easingFunction" style="width:174px;">
  251. <option value="linear">linear</option>
  252. <option value="easeInQuad">easeInQuad</option>
  253. <option value="easeOutQuad">easeOutQuad</option>
  254. <option value="easeInOutQuad" selected="selected">easeInOutQuad</option>
  255. <option value="easeInCubic">easeInCubic</option>
  256. <option value="easeOutCubic">easeOutCubic</option>
  257. <option value="easeInOutCubic">easeInOutCubic</option>
  258. <option value="easeInQuart">easeInQuart</option>
  259. <option value="easeOutQuart">easeOutQuart</option>
  260. <option value="easeInOutQuart">easeInOutQuart</option>
  261. <option value="easeInQuint">easeInQuint</option>
  262. <option value="easeOutQuint">easeOutQuint</option>
  263. <option value="easeInOutQuint">easeInOutQuint</option>
  264. </select>
  265. </td>
  266. </tr>
  267. </table>
  268. <div class="bottom">
  269. <span id="statusUpdate"></span><br />
  270. Examples:
  271. <input type="button" onclick="doAnimation();" value="Animate with above settings." style="width:300px;" id="btnDo"> <br/>
  272. <input type="button" onclick="doDefaultAnimation();" value="Animate with default settings." style="width:300px;" id="btnDoDefault"> <br/>
  273. <input type="button" onclick="zoomExtentAnimated();" value="Animated ZoomExtent()." style="width:300px;" id="btnZoom"> <br/>
  274. <input type="button" onclick="focusRandom();" value="Focus on random node." style="width:300px;" id="btnFocus"><br/>
  275. <input type="button" onclick="startShow();" value="Start a show!" style="width:300px;" id="btnShow"><br/>
  276. </div>
  277. </div>
  278. <div class="right">
  279. <div id="mynetwork"></div>
  280. <p id="selection"></p>
  281. <p id="stabilization"></p>
  282. </div>
  283. </body>
  284. </html>