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.3 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.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. <script src="../../../googleAnalytics.js"></script>
  172. </head>
  173. <body onload="draw();">
  174. <h2>Camera animations</h2>
  175. <div style="width:700px; font-size:14px;">
  176. You can move the view around programmatically using the .moveTo(options) function. The options supplied to this function can
  177. also be (partially) supplied to the .fit() and .focusOnNode() methods. These are explained in the docs.
  178. <br /><br/>
  179. 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
  180. 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.
  181. <br/><br/>
  182. Here you can see a full description of the options you can supply to moveTo:
  183. </div>
  184. <pre>
  185. var moveToOptions = {
  186. position: {x:x, y:x}, // position to animate to (Numbers)
  187. scale: 1.0, // scale to animate to (Number)
  188. offset: {x:x, y:y}, // offset from the center in DOM pixels (Numbers)
  189. animation: { // animation object, can also be Boolean
  190. duration: 1000, // animation duration in milliseconds (Number)
  191. easingFunction: "easeInOutQuad" // Animation easing function, available are:
  192. } // linear, easeInQuad, easeOutQuad, easeInOutQuad,
  193. } // easeInCubic, easeOutCubic, easeInOutCubic,
  194. // easeInQuart, easeOutQuart, easeInOutQuart,
  195. // easeInQuint, easeOutQuint, easeInOutQuint
  196. </pre>
  197. <div class="left">
  198. <table>
  199. <tr>
  200. <td>position x</td><td><input type="text" value="300" id="positionx" style="width:170px;"></td>
  201. </tr>
  202. <tr>
  203. <td>position y</td><td><input type="text" value="300" id="positiony" style="width:170px;"></td>
  204. </tr>
  205. <tr>
  206. <td>scale</td><td><input type="text" value="1.0" id="scale" style="width:170px;"></td>
  207. </tr>
  208. <tr>
  209. <td>offset x</td><td><input type="text" value="0" id="offsetx" style="width:170px;"> px</td>
  210. </tr>
  211. <tr>
  212. <td>offset y</td><td><input type="text" value="0" id="offsety" style="width:170px;"> px</td>
  213. </tr>
  214. <tr>
  215. <td>duration</td><td><input type="text" value="1000" id="duration" style="width:170px;"> ms</td>
  216. </tr>
  217. <tr>
  218. <td>easingFunction</td><td>
  219. <select id="easingFunction" style="width:174px;">
  220. <option value="linear">linear</option>
  221. <option value="easeInQuad">easeInQuad</option>
  222. <option value="easeOutQuad">easeOutQuad</option>
  223. <option value="easeInOutQuad" selected="selected">easeInOutQuad</option>
  224. <option value="easeInCubic">easeInCubic</option>
  225. <option value="easeOutCubic">easeOutCubic</option>
  226. <option value="easeInOutCubic">easeInOutCubic</option>
  227. <option value="easeInQuart">easeInQuart</option>
  228. <option value="easeOutQuart">easeOutQuart</option>
  229. <option value="easeInOutQuart">easeInOutQuart</option>
  230. <option value="easeInQuint">easeInQuint</option>
  231. <option value="easeOutQuint">easeOutQuint</option>
  232. <option value="easeInOutQuint">easeInOutQuint</option>
  233. </select>
  234. </td>
  235. </tr>
  236. </table>
  237. <div class="bottom">
  238. <span id="statusUpdate"></span><br />
  239. Examples:
  240. <input type="button" onclick="doAnimation();" value="Animate with above settings." style="width:300px;" id="btnDo"> <br/>
  241. <input type="button" onclick="doDefaultAnimation();" value="Animate with default settings." style="width:300px;" id="btnDoDefault"> <br/>
  242. <input type="button" onclick="fitAnimated();" value="Animated fit()." style="width:300px;" id="btnZoom"> <br/>
  243. <input type="button" onclick="focusRandom();" value="Focus on random node." style="width:300px;" id="btnFocus"><br/>
  244. <input type="button" onclick="startShow();" value="Start a show!" style="width:300px;" id="btnShow"><br/>
  245. </div>
  246. </div>
  247. <div class="right">
  248. <div id="mynetwork"></div>
  249. <p id="selection"></p>
  250. <p id="stabilization"></p>
  251. </div>
  252. </body>
  253. </html>