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.

331 lines
12 KiB

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