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.

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