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.

279 lines
9.2 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="../exampleUtil.js"></script>
  33. <script type="text/javascript" src="../../../dist/vis.js"></script>
  34. <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />
  35. <script type="text/javascript">
  36. var nodes = null;
  37. var edges = null;
  38. var network = null;
  39. var offsetx, offsety, scale, positionx, positiony, duration, easingFunction, doButton, focusButton, showButton;
  40. var statusUpdateSpan;
  41. var finishMessage = '';
  42. var showInterval = false;
  43. var showPhase = 1;
  44. var amountOfNodes = 25;
  45. function destroy() {
  46. if (network !== null) {
  47. network.destroy();
  48. network = null;
  49. }
  50. }
  51. function updateValues() {
  52. offsetx = parseInt(document.getElementById('offsetx').value);
  53. offsety = parseInt(document.getElementById('offsety').value);
  54. duration = parseInt(document.getElementById('duration').value);
  55. scale = parseFloat(document.getElementById('scale').value);
  56. positionx = parseInt(document.getElementById('positionx').value);
  57. positiony = parseInt(document.getElementById('positiony').value);
  58. easingFunction = document.getElementById('easingFunction').value;
  59. }
  60. function draw() {
  61. destroy();
  62. statusUpdateSpan = document.getElementById('statusUpdate');
  63. doButton = document.getElementById('btnDo');
  64. focusButton = document.getElementById('btnFocus');
  65. showButton = document.getElementById('btnShow');
  66. // randomly create some nodes and edges
  67. var data = getScaleFreeNetwork(amountOfNodes);
  68. // create a network
  69. var container = document.getElementById('mynetwork');
  70. var options = {
  71. physics: {
  72. stabilization: {
  73. iterations: 1200
  74. }
  75. }
  76. };
  77. network = new vis.Network(container, data, options);
  78. // add event listeners
  79. network.on('select', function(params) {
  80. document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
  81. });
  82. network.on('stabilized', function (params) {
  83. document.getElementById('stabilization').innerHTML = 'Stabilization took ' + params.iterations + ' iterations.';
  84. });
  85. network.on('animationFinished', function() {
  86. statusUpdateSpan.innerHTML = finishMessage;
  87. })
  88. }
  89. function fitAnimated() {
  90. updateValues();
  91. var options = {offset: {x:offsetx,y:offsety},
  92. duration: duration,
  93. easingFunction: easingFunction
  94. };
  95. statusUpdateSpan.innerHTML = 'Doing fit() Animation.';
  96. finishMessage = 'Animation finished.';
  97. network.fit({animation:options});
  98. }
  99. function doDefaultAnimation() {
  100. updateValues();
  101. var options = {
  102. position: {x:positionx,y:positiony},
  103. scale: scale,
  104. offset: {x:offsetx,y:offsety},
  105. animation: true // default duration is 1000ms and default easingFunction is easeInOutQuad.
  106. };
  107. statusUpdateSpan.innerHTML = 'Doing Animation.';
  108. finishMessage = 'Animation finished.';
  109. network.moveTo(options);
  110. }
  111. function doAnimation() {
  112. updateValues();
  113. var options = {
  114. position: {x:positionx,y:positiony},
  115. scale: scale,
  116. offset: {x:offsetx,y:offsety},
  117. animation: {
  118. duration: duration,
  119. easingFunction: easingFunction
  120. }
  121. };
  122. statusUpdateSpan.innerHTML = 'Doing Animation.';
  123. finishMessage = 'Animation finished.';
  124. network.moveTo(options);
  125. }
  126. function focusRandom() {
  127. updateValues();
  128. var nodeId = Math.floor(Math.random() * amountOfNodes);
  129. var options = {
  130. // position: {x:positionx,y:positiony}, // this is not relevant when focusing on nodes
  131. scale: scale,
  132. offset: {x:offsetx,y:offsety},
  133. animation: {
  134. duration: duration,
  135. easingFunction: easingFunction
  136. }
  137. };
  138. statusUpdateSpan.innerHTML = 'Focusing on node: ' + nodeId;
  139. finishMessage = 'Node: ' + nodeId + ' in focus.';
  140. network.focus(nodeId, options);
  141. }
  142. function startShow() {
  143. updateValues();
  144. if (showInterval !== false) {
  145. showInterval = false;
  146. showButton.value = 'Start a show!';
  147. network.fit();
  148. }
  149. else {
  150. showButton.value = 'Stop the show!';
  151. focusRandom();
  152. setTimeout(doTheShow, duration);
  153. showInterval = true;
  154. }
  155. }
  156. function doTheShow() {
  157. updateValues();
  158. if (showInterval == true) {
  159. if (showPhase == 0) {
  160. focusRandom();
  161. showPhase = 1;
  162. }
  163. else {
  164. fitAnimated();
  165. showPhase = 0;
  166. }
  167. setTimeout(doTheShow, duration);
  168. }
  169. }
  170. </script>
  171. </head>
  172. <body onload="draw();">
  173. <h2>Camera animations</h2>
  174. <div style="width:700px; font-size:14px;">
  175. You can move the view around programmatically using the .moveTo(options) function. The options supplied to this function can
  176. also be (partially) supplied to the .fit() and .focusOnNode() methods. These are explained in the docs.
  177. <br /><br/>
  178. The buttons below take the fields from the table when they can. For instance, the "Animate with default settings." takes the position, scale and offset while using
  179. the default animation values for duration and easing function. The focusOnNode takes everything except the position and the fit takes only the duration and easing function.
  180. <br/><br/>
  181. Here you can see a full description of the options you can supply to moveTo:
  182. </div>
  183. <pre>
  184. var moveToOptions = {
  185. position: {x:x, y:x}, // position to animate to (Numbers)
  186. scale: 1.0, // scale to animate to (Number)
  187. offset: {x:x, y:y}, // offset from the center in DOM pixels (Numbers)
  188. animation: { // animation object, can also be Boolean
  189. duration: 1000, // animation duration in milliseconds (Number)
  190. easingFunction: "easeInOutQuad" // Animation easing function, available are:
  191. } // linear, easeInQuad, easeOutQuad, easeInOutQuad,
  192. } // easeInCubic, easeOutCubic, easeInOutCubic,
  193. // easeInQuart, easeOutQuart, easeInOutQuart,
  194. // easeInQuint, easeOutQuint, easeInOutQuint
  195. </pre>
  196. <div class="left">
  197. <table>
  198. <tr>
  199. <td>position x</td><td><input type="text" value="300" id="positionx" style="width:170px;"></td>
  200. </tr>
  201. <tr>
  202. <td>position y</td><td><input type="text" value="300" id="positiony" style="width:170px;"></td>
  203. </tr>
  204. <tr>
  205. <td>scale</td><td><input type="text" value="1.0" id="scale" style="width:170px;"></td>
  206. </tr>
  207. <tr>
  208. <td>offset x</td><td><input type="text" value="0" id="offsetx" style="width:170px;"> px</td>
  209. </tr>
  210. <tr>
  211. <td>offset y</td><td><input type="text" value="0" id="offsety" style="width:170px;"> px</td>
  212. </tr>
  213. <tr>
  214. <td>duration</td><td><input type="text" value="1000" id="duration" style="width:170px;"> ms</td>
  215. </tr>
  216. <tr>
  217. <td>easingFunction</td><td>
  218. <select id="easingFunction" style="width:174px;">
  219. <option value="linear">linear</option>
  220. <option value="easeInQuad">easeInQuad</option>
  221. <option value="easeOutQuad">easeOutQuad</option>
  222. <option value="easeInOutQuad" selected="selected">easeInOutQuad</option>
  223. <option value="easeInCubic">easeInCubic</option>
  224. <option value="easeOutCubic">easeOutCubic</option>
  225. <option value="easeInOutCubic">easeInOutCubic</option>
  226. <option value="easeInQuart">easeInQuart</option>
  227. <option value="easeOutQuart">easeOutQuart</option>
  228. <option value="easeInOutQuart">easeInOutQuart</option>
  229. <option value="easeInQuint">easeInQuint</option>
  230. <option value="easeOutQuint">easeOutQuint</option>
  231. <option value="easeInOutQuint">easeInOutQuint</option>
  232. </select>
  233. </td>
  234. </tr>
  235. </table>
  236. <div class="bottom">
  237. <span id="statusUpdate"></span><br />
  238. Examples:
  239. <input type="button" onclick="doAnimation();" value="Animate with above settings." style="width:300px;" id="btnDo"> <br/>
  240. <input type="button" onclick="doDefaultAnimation();" value="Animate with default settings." style="width:300px;" id="btnDoDefault"> <br/>
  241. <input type="button" onclick="fitAnimated();" value="Animated fit()." style="width:300px;" id="btnZoom"> <br/>
  242. <input type="button" onclick="focusRandom();" value="Focus on random node." style="width:300px;" id="btnFocus"><br/>
  243. <input type="button" onclick="startShow();" value="Start a show!" style="width:300px;" id="btnShow"><br/>
  244. </div>
  245. </div>
  246. <div class="right">
  247. <div id="mynetwork"></div>
  248. <p id="selection"></p>
  249. <p id="stabilization"></p>
  250. </div>
  251. </body>
  252. </html>