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. /**
  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. 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;