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.

334 lines
12 KiB

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