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.

301 lines
11 KiB

  1. let util = require('../../util');
  2. var NetworkUtil = require('../NetworkUtil').default;
  3. /**
  4. * The view
  5. */
  6. class View {
  7. /**
  8. * @param {Object} body
  9. * @param {Canvas} canvas
  10. */
  11. constructor(body, canvas) {
  12. this.body = body;
  13. this.canvas = canvas;
  14. this.animationSpeed = 1/this.renderRefreshRate;
  15. this.animationEasingFunction = "easeInOutQuint";
  16. this.easingTime = 0;
  17. this.sourceScale = 0;
  18. this.targetScale = 0;
  19. this.sourceTranslation = 0;
  20. this.targetTranslation = 0;
  21. this.lockedOnNodeId = undefined;
  22. this.lockedOnNodeOffset = undefined;
  23. this.touchTime = 0;
  24. this.viewFunction = undefined;
  25. this.body.emitter.on("fit", this.fit.bind(this));
  26. this.body.emitter.on("animationFinished", () => {this.body.emitter.emit("_stopRendering");});
  27. this.body.emitter.on("unlockNode", this.releaseNode.bind(this));
  28. }
  29. /**
  30. *
  31. * @param {Object} [options={}]
  32. */
  33. setOptions(options = {}) {
  34. this.options = options;
  35. }
  36. /**
  37. * This function zooms out to fit all data on screen based on amount of nodes
  38. * @param {Object} [options={{nodes=Array}}]
  39. * @param {boolean} [initialZoom=false] | zoom based on fitted formula or range, true = fitted, default = false;
  40. */
  41. fit(options = {nodes:[]}, initialZoom = false) {
  42. let range;
  43. let zoomLevel;
  44. if (options.nodes === undefined || options.nodes.length === 0) {
  45. options.nodes = this.body.nodeIndices;
  46. }
  47. if (initialZoom === true) {
  48. // check if more than half of the nodes have a predefined position. If so, we use the range, not the approximation.
  49. let positionDefined = 0;
  50. for (let nodeId in this.body.nodes) {
  51. if (this.body.nodes.hasOwnProperty(nodeId)) {
  52. let node = this.body.nodes[nodeId];
  53. if (node.predefinedPosition === true) {
  54. positionDefined += 1;
  55. }
  56. }
  57. }
  58. if (positionDefined > 0.5 * this.body.nodeIndices.length) {
  59. this.fit(options,false);
  60. return;
  61. }
  62. range = NetworkUtil.getRange(this.body.nodes, options.nodes);
  63. let numberOfNodes = this.body.nodeIndices.length;
  64. zoomLevel = 12.662 / (numberOfNodes + 7.4147) + 0.0964822; // this is obtained from fitting a dataset from 5 points with scale levels that looked good.
  65. // correct for larger canvasses.
  66. let factor = Math.min(this.canvas.frame.canvas.clientWidth / 600, this.canvas.frame.canvas.clientHeight / 600);
  67. zoomLevel *= factor;
  68. }
  69. else {
  70. this.body.emitter.emit("_resizeNodes");
  71. range = NetworkUtil.getRange(this.body.nodes, options.nodes);
  72. let xDistance = Math.abs(range.maxX - range.minX) * 1.1;
  73. let yDistance = Math.abs(range.maxY - range.minY) * 1.1;
  74. let xZoomLevel = this.canvas.frame.canvas.clientWidth / xDistance;
  75. let yZoomLevel = this.canvas.frame.canvas.clientHeight / yDistance;
  76. zoomLevel = (xZoomLevel <= yZoomLevel) ? xZoomLevel : yZoomLevel;
  77. }
  78. if (zoomLevel > 1.0) {
  79. zoomLevel = 1.0;
  80. }
  81. else if (zoomLevel === 0) {
  82. zoomLevel = 1.0;
  83. }
  84. let center = NetworkUtil.findCenter(range);
  85. let animationOptions = {position: center, scale: zoomLevel, animation: options.animation};
  86. this.moveTo(animationOptions);
  87. }
  88. // animation
  89. /**
  90. * Center a node in view.
  91. *
  92. * @param {number} nodeId
  93. * @param {number} [options]
  94. */
  95. focus(nodeId, options = {}) {
  96. if (this.body.nodes[nodeId] !== undefined) {
  97. let nodePosition = {x: this.body.nodes[nodeId].x, y: this.body.nodes[nodeId].y};
  98. options.position = nodePosition;
  99. options.lockedOnNode = nodeId;
  100. this.moveTo(options)
  101. }
  102. else {
  103. console.log("Node: " + nodeId + " cannot be found.");
  104. }
  105. }
  106. /**
  107. *
  108. * @param {Object} options | options.offset = {x:number, y:number} // offset from the center in DOM pixels
  109. * | options.scale = number // scale to move to
  110. * | options.position = {x:number, y:number} // position to move to
  111. * | options.animation = {duration:number, easingFunction:String} || Boolean // position to move to
  112. */
  113. moveTo(options) {
  114. if (options === undefined) {
  115. options = {};
  116. return;
  117. }
  118. if (options.offset === undefined) {options.offset = {x: 0, y: 0}; }
  119. if (options.offset.x === undefined) {options.offset.x = 0; }
  120. if (options.offset.y === undefined) {options.offset.y = 0; }
  121. if (options.scale === undefined) {options.scale = this.body.view.scale; }
  122. if (options.position === undefined) {options.position = this.getViewPosition();}
  123. if (options.animation === undefined) {options.animation = {duration:0}; }
  124. if (options.animation === false ) {options.animation = {duration:0}; }
  125. if (options.animation === true ) {options.animation = {}; }
  126. if (options.animation.duration === undefined) {options.animation.duration = 1000; } // default duration
  127. if (options.animation.easingFunction === undefined) {options.animation.easingFunction = "easeInOutQuad"; } // default easing function
  128. this.animateView(options);
  129. }
  130. /**
  131. *
  132. * @param {Object} options | options.offset = {x:number, y:number} // offset from the center in DOM pixels
  133. * | options.time = number // animation time in milliseconds
  134. * | options.scale = number // scale to animate to
  135. * | options.position = {x:number, y:number} // position to animate to
  136. * | options.easingFunction = String // linear, easeInQuad, easeOutQuad, easeInOutQuad,
  137. * // easeInCubic, easeOutCubic, easeInOutCubic,
  138. * // easeInQuart, easeOutQuart, easeInOutQuart,
  139. * // easeInQuint, easeOutQuint, easeInOutQuint
  140. */
  141. animateView(options) {
  142. if (options === undefined) {
  143. return;
  144. }
  145. this.animationEasingFunction = options.animation.easingFunction;
  146. // release if something focussed on the node
  147. this.releaseNode();
  148. if (options.locked === true) {
  149. this.lockedOnNodeId = options.lockedOnNode;
  150. this.lockedOnNodeOffset = options.offset;
  151. }
  152. // forcefully complete the old animation if it was still running
  153. if (this.easingTime != 0) {
  154. this._transitionRedraw(true); // by setting easingtime to 1, we finish the animation.
  155. }
  156. this.sourceScale = this.body.view.scale;
  157. this.sourceTranslation = this.body.view.translation;
  158. this.targetScale = options.scale;
  159. // set the scale so the viewCenter is based on the correct zoom level. This is overridden in the transitionRedraw
  160. // but at least then we'll have the target transition
  161. this.body.view.scale = this.targetScale;
  162. let viewCenter = this.canvas.DOMtoCanvas({x: 0.5 * this.canvas.frame.canvas.clientWidth, y: 0.5 * this.canvas.frame.canvas.clientHeight});
  163. let distanceFromCenter = { // offset from view, distance view has to change by these x and y to center the node
  164. x: viewCenter.x - options.position.x,
  165. y: viewCenter.y - options.position.y
  166. };
  167. this.targetTranslation = {
  168. x: this.sourceTranslation.x + distanceFromCenter.x * this.targetScale + options.offset.x,
  169. y: this.sourceTranslation.y + distanceFromCenter.y * this.targetScale + options.offset.y
  170. };
  171. // if the time is set to 0, don't do an animation
  172. if (options.animation.duration === 0) {
  173. if (this.lockedOnNodeId != undefined) {
  174. this.viewFunction = this._lockedRedraw.bind(this);
  175. this.body.emitter.on("initRedraw", this.viewFunction);
  176. }
  177. else {
  178. this.body.view.scale = this.targetScale;
  179. this.body.view.translation = this.targetTranslation;
  180. this.body.emitter.emit("_requestRedraw");
  181. }
  182. }
  183. else {
  184. this.animationSpeed = 1 / (60 * options.animation.duration * 0.001) || 1 / 60; // 60 for 60 seconds, 0.001 for milli's
  185. this.animationEasingFunction = options.animation.easingFunction;
  186. this.viewFunction = this._transitionRedraw.bind(this);
  187. this.body.emitter.on("initRedraw", this.viewFunction);
  188. this.body.emitter.emit("_startRendering");
  189. }
  190. }
  191. /**
  192. * used to animate smoothly by hijacking the redraw function.
  193. * @private
  194. */
  195. _lockedRedraw() {
  196. let nodePosition = {x: this.body.nodes[this.lockedOnNodeId].x, y: this.body.nodes[this.lockedOnNodeId].y};
  197. let viewCenter = this.canvas.DOMtoCanvas({x: 0.5 * this.canvas.frame.canvas.clientWidth, y: 0.5 * this.canvas.frame.canvas.clientHeight});
  198. let distanceFromCenter = { // offset from view, distance view has to change by these x and y to center the node
  199. x: viewCenter.x - nodePosition.x,
  200. y: viewCenter.y - nodePosition.y
  201. };
  202. let sourceTranslation = this.body.view.translation;
  203. let targetTranslation = {
  204. x: sourceTranslation.x + distanceFromCenter.x * this.body.view.scale + this.lockedOnNodeOffset.x,
  205. y: sourceTranslation.y + distanceFromCenter.y * this.body.view.scale + this.lockedOnNodeOffset.y
  206. };
  207. this.body.view.translation = targetTranslation;
  208. }
  209. /**
  210. * Resets state of a locked on Node
  211. */
  212. releaseNode() {
  213. if (this.lockedOnNodeId !== undefined && this.viewFunction !== undefined) {
  214. this.body.emitter.off("initRedraw", this.viewFunction);
  215. this.lockedOnNodeId = undefined;
  216. this.lockedOnNodeOffset = undefined;
  217. }
  218. }
  219. /**
  220. * @param {boolean} [finished=false]
  221. * @private
  222. */
  223. _transitionRedraw(finished = false) {
  224. this.easingTime += this.animationSpeed;
  225. this.easingTime = finished === true ? 1.0 : this.easingTime;
  226. let progress = util.easingFunctions[this.animationEasingFunction](this.easingTime);
  227. this.body.view.scale = this.sourceScale + (this.targetScale - this.sourceScale) * progress;
  228. this.body.view.translation = {
  229. x: this.sourceTranslation.x + (this.targetTranslation.x - this.sourceTranslation.x) * progress,
  230. y: this.sourceTranslation.y + (this.targetTranslation.y - this.sourceTranslation.y) * progress
  231. };
  232. // cleanup
  233. if (this.easingTime >= 1.0) {
  234. this.body.emitter.off("initRedraw", this.viewFunction);
  235. this.easingTime = 0;
  236. if (this.lockedOnNodeId != undefined) {
  237. this.viewFunction = this._lockedRedraw.bind(this);
  238. this.body.emitter.on("initRedraw", this.viewFunction);
  239. }
  240. this.body.emitter.emit("animationFinished");
  241. }
  242. }
  243. /**
  244. *
  245. * @returns {number}
  246. */
  247. getScale() {
  248. return this.body.view.scale;
  249. }
  250. /**
  251. *
  252. * @returns {{x: number, y: number}}
  253. */
  254. getViewPosition() {
  255. return this.canvas.DOMtoCanvas({x: 0.5 * this.canvas.frame.canvas.clientWidth, y: 0.5 * this.canvas.frame.canvas.clientHeight});
  256. }
  257. }
  258. export default View;