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.

972 lines
32 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../util');
  2. /**
  3. * @class Node
  4. * A node. A node can be connected to other nodes via one or multiple edges.
  5. * @param {object} properties An object containing properties for the node. All
  6. * properties are optional, except for the id.
  7. * {number} id Id of the node. Required
  8. * {string} label Text label for the node
  9. * {number} x Horizontal position of the node
  10. * {number} y Vertical position of the node
  11. * {string} shape Node shape, available:
  12. * "database", "circle", "ellipse",
  13. * "box", "image", "text", "dot",
  14. * "star", "triangle", "triangleDown",
  15. * "square"
  16. * {string} image An image url
  17. * {string} title An title text, can be HTML
  18. * {anytype} group A group name or number
  19. * @param {Network.Images} imagelist A list with images. Only needed
  20. * when the node has an image
  21. * @param {Network.Groups} grouplist A list with groups. Needed for
  22. * retrieving group properties
  23. * @param {Object} constants An object with default values for
  24. * example for the color
  25. *
  26. */
  27. function Node(properties, imagelist, grouplist, constants) {
  28. this.selected = false;
  29. this.hover = false;
  30. this.edges = []; // all edges connected to this node
  31. this.dynamicEdges = [];
  32. this.reroutedEdges = {};
  33. this.group = constants.nodes.group;
  34. this.fontSize = Number(constants.nodes.fontSize);
  35. this.fontFace = constants.nodes.fontFace;
  36. this.fontColor = constants.nodes.fontColor;
  37. this.fontDrawThreshold = 3;
  38. this.color = constants.nodes.color;
  39. // set defaults for the properties
  40. this.id = undefined;
  41. this.shape = constants.nodes.shape;
  42. this.image = constants.nodes.image;
  43. this.x = null;
  44. this.y = null;
  45. this.xFixed = false;
  46. this.yFixed = false;
  47. this.horizontalAlignLeft = true; // these are for the navigation controls
  48. this.verticalAlignTop = true; // these are for the navigation controls
  49. this.radius = constants.nodes.radius;
  50. this.baseRadiusValue = constants.nodes.radius;
  51. this.radiusFixed = false;
  52. this.radiusMin = constants.nodes.radiusMin;
  53. this.radiusMax = constants.nodes.radiusMax;
  54. this.level = -1;
  55. this.preassignedLevel = false;
  56. this.imagelist = imagelist;
  57. this.grouplist = grouplist;
  58. // physics properties
  59. this.fx = 0.0; // external force x
  60. this.fy = 0.0; // external force y
  61. this.vx = 0.0; // velocity x
  62. this.vy = 0.0; // velocity y
  63. this.minForce = constants.minForce;
  64. this.damping = constants.physics.damping;
  65. this.mass = 1; // kg
  66. this.fixedData = {x:null,y:null};
  67. this.setProperties(properties, constants);
  68. // creating the variables for clustering
  69. this.resetCluster();
  70. this.dynamicEdgesLength = 0;
  71. this.clusterSession = 0;
  72. this.clusterSizeWidthFactor = constants.clustering.nodeScaling.width;
  73. this.clusterSizeHeightFactor = constants.clustering.nodeScaling.height;
  74. this.clusterSizeRadiusFactor = constants.clustering.nodeScaling.radius;
  75. this.maxNodeSizeIncrements = constants.clustering.maxNodeSizeIncrements;
  76. this.growthIndicator = 0;
  77. // variables to tell the node about the network.
  78. this.networkScaleInv = 1;
  79. this.networkScale = 1;
  80. this.canvasTopLeft = {"x": -300, "y": -300};
  81. this.canvasBottomRight = {"x": 300, "y": 300};
  82. this.parentEdgeId = null;
  83. }
  84. /**
  85. * (re)setting the clustering variables and objects
  86. */
  87. Node.prototype.resetCluster = function() {
  88. // clustering variables
  89. this.formationScale = undefined; // this is used to determine when to open the cluster
  90. this.clusterSize = 1; // this signifies the total amount of nodes in this cluster
  91. this.containedNodes = {};
  92. this.containedEdges = {};
  93. this.clusterSessions = [];
  94. };
  95. /**
  96. * Attach a edge to the node
  97. * @param {Edge} edge
  98. */
  99. Node.prototype.attachEdge = function(edge) {
  100. if (this.edges.indexOf(edge) == -1) {
  101. this.edges.push(edge);
  102. }
  103. if (this.dynamicEdges.indexOf(edge) == -1) {
  104. this.dynamicEdges.push(edge);
  105. }
  106. this.dynamicEdgesLength = this.dynamicEdges.length;
  107. };
  108. /**
  109. * Detach a edge from the node
  110. * @param {Edge} edge
  111. */
  112. Node.prototype.detachEdge = function(edge) {
  113. var index = this.edges.indexOf(edge);
  114. if (index != -1) {
  115. this.edges.splice(index, 1);
  116. this.dynamicEdges.splice(index, 1);
  117. }
  118. this.dynamicEdgesLength = this.dynamicEdges.length;
  119. };
  120. /**
  121. * Set or overwrite properties for the node
  122. * @param {Object} properties an object with properties
  123. * @param {Object} constants and object with default, global properties
  124. */
  125. Node.prototype.setProperties = function(properties, constants) {
  126. if (!properties) {
  127. return;
  128. }
  129. this.originalLabel = undefined;
  130. // basic properties
  131. if (properties.id !== undefined) {this.id = properties.id;}
  132. if (properties.label !== undefined) {this.label = properties.label; this.originalLabel = properties.label;}
  133. if (properties.title !== undefined) {this.title = properties.title;}
  134. if (properties.group !== undefined) {this.group = properties.group;}
  135. if (properties.x !== undefined) {this.x = properties.x;}
  136. if (properties.y !== undefined) {this.y = properties.y;}
  137. if (properties.value !== undefined) {this.value = properties.value;}
  138. if (properties.level !== undefined) {this.level = properties.level; this.preassignedLevel = true;}
  139. // physics
  140. if (properties.mass !== undefined) {this.mass = properties.mass;}
  141. // navigation controls properties
  142. if (properties.horizontalAlignLeft !== undefined) {this.horizontalAlignLeft = properties.horizontalAlignLeft;}
  143. if (properties.verticalAlignTop !== undefined) {this.verticalAlignTop = properties.verticalAlignTop;}
  144. if (properties.triggerFunction !== undefined) {this.triggerFunction = properties.triggerFunction;}
  145. if (this.id === undefined) {
  146. throw "Node must have an id";
  147. }
  148. // copy group properties
  149. if (this.group) {
  150. var groupObj = this.grouplist.get(this.group);
  151. for (var prop in groupObj) {
  152. if (groupObj.hasOwnProperty(prop)) {
  153. this[prop] = groupObj[prop];
  154. }
  155. }
  156. }
  157. // individual shape properties
  158. if (properties.shape !== undefined) {this.shape = properties.shape;}
  159. if (properties.image !== undefined) {this.image = properties.image;}
  160. if (properties.radius !== undefined) {this.radius = properties.radius; this.baseRadiusValue = this.radius;}
  161. if (properties.color !== undefined) {this.color = util.parseColor(properties.color);}
  162. if (properties.fontColor !== undefined) {this.fontColor = properties.fontColor;}
  163. if (properties.fontSize !== undefined) {this.fontSize = properties.fontSize;}
  164. if (properties.fontFace !== undefined) {this.fontFace = properties.fontFace;}
  165. if (this.image !== undefined && this.image != "") {
  166. if (this.imagelist) {
  167. this.imageObj = this.imagelist.load(this.image);
  168. }
  169. else {
  170. throw "No imagelist provided";
  171. }
  172. }
  173. this.xFixed = this.xFixed || (properties.x !== undefined && !properties.allowedToMoveX);
  174. this.yFixed = this.yFixed || (properties.y !== undefined && !properties.allowedToMoveY);
  175. this.radiusFixed = this.radiusFixed || (properties.radius !== undefined);
  176. if (this.shape == 'image') {
  177. this.radiusMin = constants.nodes.widthMin;
  178. this.radiusMax = constants.nodes.widthMax;
  179. }
  180. // choose draw method depending on the shape
  181. switch (this.shape) {
  182. case 'database': this.draw = this._drawDatabase; this.resize = this._resizeDatabase; break;
  183. case 'box': this.draw = this._drawBox; this.resize = this._resizeBox; break;
  184. case 'circle': this.draw = this._drawCircle; this.resize = this._resizeCircle; break;
  185. case 'ellipse': this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  186. // TODO: add diamond shape
  187. case 'image': this.draw = this._drawImage; this.resize = this._resizeImage; break;
  188. case 'text': this.draw = this._drawText; this.resize = this._resizeText; break;
  189. case 'dot': this.draw = this._drawDot; this.resize = this._resizeShape; break;
  190. case 'square': this.draw = this._drawSquare; this.resize = this._resizeShape; break;
  191. case 'triangle': this.draw = this._drawTriangle; this.resize = this._resizeShape; break;
  192. case 'triangleDown': this.draw = this._drawTriangleDown; this.resize = this._resizeShape; break;
  193. case 'star': this.draw = this._drawStar; this.resize = this._resizeShape; break;
  194. default: this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  195. }
  196. // reset the size of the node, this can be changed
  197. this._reset();
  198. };
  199. /**
  200. * select this node
  201. */
  202. Node.prototype.select = function() {
  203. this.selected = true;
  204. this._reset();
  205. };
  206. /**
  207. * unselect this node
  208. */
  209. Node.prototype.unselect = function() {
  210. this.selected = false;
  211. this._reset();
  212. };
  213. /**
  214. * Reset the calculated size of the node, forces it to recalculate its size
  215. */
  216. Node.prototype.clearSizeCache = function() {
  217. this._reset();
  218. };
  219. /**
  220. * Reset the calculated size of the node, forces it to recalculate its size
  221. * @private
  222. */
  223. Node.prototype._reset = function() {
  224. this.width = undefined;
  225. this.height = undefined;
  226. };
  227. /**
  228. * get the title of this node.
  229. * @return {string} title The title of the node, or undefined when no title
  230. * has been set.
  231. */
  232. Node.prototype.getTitle = function() {
  233. return typeof this.title === "function" ? this.title() : this.title;
  234. };
  235. /**
  236. * Calculate the distance to the border of the Node
  237. * @param {CanvasRenderingContext2D} ctx
  238. * @param {Number} angle Angle in radians
  239. * @returns {number} distance Distance to the border in pixels
  240. */
  241. Node.prototype.distanceToBorder = function (ctx, angle) {
  242. var borderWidth = 1;
  243. if (!this.width) {
  244. this.resize(ctx);
  245. }
  246. switch (this.shape) {
  247. case 'circle':
  248. case 'dot':
  249. return this.radius + borderWidth;
  250. case 'ellipse':
  251. var a = this.width / 2;
  252. var b = this.height / 2;
  253. var w = (Math.sin(angle) * a);
  254. var h = (Math.cos(angle) * b);
  255. return a * b / Math.sqrt(w * w + h * h);
  256. // TODO: implement distanceToBorder for database
  257. // TODO: implement distanceToBorder for triangle
  258. // TODO: implement distanceToBorder for triangleDown
  259. case 'box':
  260. case 'image':
  261. case 'text':
  262. default:
  263. if (this.width) {
  264. return Math.min(
  265. Math.abs(this.width / 2 / Math.cos(angle)),
  266. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  267. // TODO: reckon with border radius too in case of box
  268. }
  269. else {
  270. return 0;
  271. }
  272. }
  273. // TODO: implement calculation of distance to border for all shapes
  274. };
  275. /**
  276. * Set forces acting on the node
  277. * @param {number} fx Force in horizontal direction
  278. * @param {number} fy Force in vertical direction
  279. */
  280. Node.prototype._setForce = function(fx, fy) {
  281. this.fx = fx;
  282. this.fy = fy;
  283. };
  284. /**
  285. * Add forces acting on the node
  286. * @param {number} fx Force in horizontal direction
  287. * @param {number} fy Force in vertical direction
  288. * @private
  289. */
  290. Node.prototype._addForce = function(fx, fy) {
  291. this.fx += fx;
  292. this.fy += fy;
  293. };
  294. /**
  295. * Perform one discrete step for the node
  296. * @param {number} interval Time interval in seconds
  297. */
  298. Node.prototype.discreteStep = function(interval) {
  299. if (!this.xFixed) {
  300. var dx = this.damping * this.vx; // damping force
  301. var ax = (this.fx - dx) / this.mass; // acceleration
  302. this.vx += ax * interval; // velocity
  303. this.x += this.vx * interval; // position
  304. }
  305. if (!this.yFixed) {
  306. var dy = this.damping * this.vy; // damping force
  307. var ay = (this.fy - dy) / this.mass; // acceleration
  308. this.vy += ay * interval; // velocity
  309. this.y += this.vy * interval; // position
  310. }
  311. };
  312. /**
  313. * Perform one discrete step for the node
  314. * @param {number} interval Time interval in seconds
  315. * @param {number} maxVelocity The speed limit imposed on the velocity
  316. */
  317. Node.prototype.discreteStepLimited = function(interval, maxVelocity) {
  318. if (!this.xFixed) {
  319. var dx = this.damping * this.vx; // damping force
  320. var ax = (this.fx - dx) / this.mass; // acceleration
  321. this.vx += ax * interval; // velocity
  322. this.vx = (Math.abs(this.vx) > maxVelocity) ? ((this.vx > 0) ? maxVelocity : -maxVelocity) : this.vx;
  323. this.x += this.vx * interval; // position
  324. }
  325. else {
  326. this.fx = 0;
  327. }
  328. if (!this.yFixed) {
  329. var dy = this.damping * this.vy; // damping force
  330. var ay = (this.fy - dy) / this.mass; // acceleration
  331. this.vy += ay * interval; // velocity
  332. this.vy = (Math.abs(this.vy) > maxVelocity) ? ((this.vy > 0) ? maxVelocity : -maxVelocity) : this.vy;
  333. this.y += this.vy * interval; // position
  334. }
  335. else {
  336. this.fy = 0;
  337. }
  338. };
  339. /**
  340. * Check if this node has a fixed x and y position
  341. * @return {boolean} true if fixed, false if not
  342. */
  343. Node.prototype.isFixed = function() {
  344. return (this.xFixed && this.yFixed);
  345. };
  346. /**
  347. * Check if this node is moving
  348. * @param {number} vmin the minimum velocity considered as "moving"
  349. * @return {boolean} true if moving, false if it has no velocity
  350. */
  351. // TODO: replace this method with calculating the kinetic energy
  352. Node.prototype.isMoving = function(vmin) {
  353. return (Math.abs(this.vx) > vmin || Math.abs(this.vy) > vmin);
  354. };
  355. /**
  356. * check if this node is selecte
  357. * @return {boolean} selected True if node is selected, else false
  358. */
  359. Node.prototype.isSelected = function() {
  360. return this.selected;
  361. };
  362. /**
  363. * Retrieve the value of the node. Can be undefined
  364. * @return {Number} value
  365. */
  366. Node.prototype.getValue = function() {
  367. return this.value;
  368. };
  369. /**
  370. * Calculate the distance from the nodes location to the given location (x,y)
  371. * @param {Number} x
  372. * @param {Number} y
  373. * @return {Number} value
  374. */
  375. Node.prototype.getDistance = function(x, y) {
  376. var dx = this.x - x,
  377. dy = this.y - y;
  378. return Math.sqrt(dx * dx + dy * dy);
  379. };
  380. /**
  381. * Adjust the value range of the node. The node will adjust it's radius
  382. * based on its value.
  383. * @param {Number} min
  384. * @param {Number} max
  385. */
  386. Node.prototype.setValueRange = function(min, max) {
  387. if (!this.radiusFixed && this.value !== undefined) {
  388. if (max == min) {
  389. this.radius = (this.radiusMin + this.radiusMax) / 2;
  390. }
  391. else {
  392. var scale = (this.radiusMax - this.radiusMin) / (max - min);
  393. this.radius = (this.value - min) * scale + this.radiusMin;
  394. }
  395. }
  396. this.baseRadiusValue = this.radius;
  397. };
  398. /**
  399. * Draw this node in the given canvas
  400. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  401. * @param {CanvasRenderingContext2D} ctx
  402. */
  403. Node.prototype.draw = function(ctx) {
  404. throw "Draw method not initialized for node";
  405. };
  406. /**
  407. * Recalculate the size of this node in the given canvas
  408. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  409. * @param {CanvasRenderingContext2D} ctx
  410. */
  411. Node.prototype.resize = function(ctx) {
  412. throw "Resize method not initialized for node";
  413. };
  414. /**
  415. * Check if this object is overlapping with the provided object
  416. * @param {Object} obj an object with parameters left, top, right, bottom
  417. * @return {boolean} True if location is located on node
  418. */
  419. Node.prototype.isOverlappingWith = function(obj) {
  420. return (this.left < obj.right &&
  421. this.left + this.width > obj.left &&
  422. this.top < obj.bottom &&
  423. this.top + this.height > obj.top);
  424. };
  425. Node.prototype._resizeImage = function (ctx) {
  426. // TODO: pre calculate the image size
  427. if (!this.width || !this.height) { // undefined or 0
  428. var width, height;
  429. if (this.value) {
  430. this.radius = this.baseRadiusValue;
  431. var scale = this.imageObj.height / this.imageObj.width;
  432. if (scale !== undefined) {
  433. width = this.radius || this.imageObj.width;
  434. height = this.radius * scale || this.imageObj.height;
  435. }
  436. else {
  437. width = 0;
  438. height = 0;
  439. }
  440. }
  441. else {
  442. width = this.imageObj.width;
  443. height = this.imageObj.height;
  444. }
  445. this.width = width;
  446. this.height = height;
  447. this.growthIndicator = 0;
  448. if (this.width > 0 && this.height > 0) {
  449. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  450. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  451. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  452. this.growthIndicator = this.width - width;
  453. }
  454. }
  455. };
  456. Node.prototype._drawImage = function (ctx) {
  457. this._resizeImage(ctx);
  458. this.left = this.x - this.width / 2;
  459. this.top = this.y - this.height / 2;
  460. var yLabel;
  461. if (this.imageObj.width != 0 ) {
  462. // draw the shade
  463. if (this.clusterSize > 1) {
  464. var lineWidth = ((this.clusterSize > 1) ? 10 : 0.0);
  465. lineWidth *= this.networkScaleInv;
  466. lineWidth = Math.min(0.2 * this.width,lineWidth);
  467. ctx.globalAlpha = 0.5;
  468. ctx.drawImage(this.imageObj, this.left - lineWidth, this.top - lineWidth, this.width + 2*lineWidth, this.height + 2*lineWidth);
  469. }
  470. // draw the image
  471. ctx.globalAlpha = 1.0;
  472. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  473. yLabel = this.y + this.height / 2;
  474. }
  475. else {
  476. // image still loading... just draw the label for now
  477. yLabel = this.y;
  478. }
  479. this._label(ctx, this.label, this.x, yLabel, undefined, "top");
  480. };
  481. Node.prototype._resizeBox = function (ctx) {
  482. if (!this.width) {
  483. var margin = 5;
  484. var textSize = this.getTextSize(ctx);
  485. this.width = textSize.width + 2 * margin;
  486. this.height = textSize.height + 2 * margin;
  487. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  488. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  489. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  490. // this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  491. }
  492. };
  493. Node.prototype._drawBox = function (ctx) {
  494. this._resizeBox(ctx);
  495. this.left = this.x - this.width / 2;
  496. this.top = this.y - this.height / 2;
  497. var clusterLineWidth = 2.5;
  498. var selectionLineWidth = 2;
  499. ctx.strokeStyle = this.selected ? this.color.highlight.border : this.hover ? this.color.hover.border : this.color.border;
  500. // draw the outer border
  501. if (this.clusterSize > 1) {
  502. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  503. ctx.lineWidth *= this.networkScaleInv;
  504. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  505. ctx.roundRect(this.left-2*ctx.lineWidth, this.top-2*ctx.lineWidth, this.width+4*ctx.lineWidth, this.height+4*ctx.lineWidth, this.radius);
  506. ctx.stroke();
  507. }
  508. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  509. ctx.lineWidth *= this.networkScaleInv;
  510. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  511. ctx.fillStyle = this.selected ? this.color.highlight.background : this.color.background;
  512. ctx.roundRect(this.left, this.top, this.width, this.height, this.radius);
  513. ctx.fill();
  514. ctx.stroke();
  515. this._label(ctx, this.label, this.x, this.y);
  516. };
  517. Node.prototype._resizeDatabase = function (ctx) {
  518. if (!this.width) {
  519. var margin = 5;
  520. var textSize = this.getTextSize(ctx);
  521. var size = textSize.width + 2 * margin;
  522. this.width = size;
  523. this.height = size;
  524. // scaling used for clustering
  525. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  526. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  527. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  528. this.growthIndicator = this.width - size;
  529. }
  530. };
  531. Node.prototype._drawDatabase = function (ctx) {
  532. this._resizeDatabase(ctx);
  533. this.left = this.x - this.width / 2;
  534. this.top = this.y - this.height / 2;
  535. var clusterLineWidth = 2.5;
  536. var selectionLineWidth = 2;
  537. ctx.strokeStyle = this.selected ? this.color.highlight.border : this.hover ? this.color.hover.border : this.color.border;
  538. // draw the outer border
  539. if (this.clusterSize > 1) {
  540. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  541. ctx.lineWidth *= this.networkScaleInv;
  542. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  543. ctx.database(this.x - this.width/2 - 2*ctx.lineWidth, this.y - this.height*0.5 - 2*ctx.lineWidth, this.width + 4*ctx.lineWidth, this.height + 4*ctx.lineWidth);
  544. ctx.stroke();
  545. }
  546. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  547. ctx.lineWidth *= this.networkScaleInv;
  548. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  549. ctx.fillStyle = this.selected ? this.color.highlight.background : this.hover ? this.color.hover.background : this.color.background;
  550. ctx.database(this.x - this.width/2, this.y - this.height*0.5, this.width, this.height);
  551. ctx.fill();
  552. ctx.stroke();
  553. this._label(ctx, this.label, this.x, this.y);
  554. };
  555. Node.prototype._resizeCircle = function (ctx) {
  556. if (!this.width) {
  557. var margin = 5;
  558. var textSize = this.getTextSize(ctx);
  559. var diameter = Math.max(textSize.width, textSize.height) + 2 * margin;
  560. this.radius = diameter / 2;
  561. this.width = diameter;
  562. this.height = diameter;
  563. // scaling used for clustering
  564. // this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  565. // this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  566. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  567. this.growthIndicator = this.radius - 0.5*diameter;
  568. }
  569. };
  570. Node.prototype._drawCircle = function (ctx) {
  571. this._resizeCircle(ctx);
  572. this.left = this.x - this.width / 2;
  573. this.top = this.y - this.height / 2;
  574. var clusterLineWidth = 2.5;
  575. var selectionLineWidth = 2;
  576. ctx.strokeStyle = this.selected ? this.color.highlight.border : this.hover ? this.color.hover.border : this.color.border;
  577. // draw the outer border
  578. if (this.clusterSize > 1) {
  579. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  580. ctx.lineWidth *= this.networkScaleInv;
  581. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  582. ctx.circle(this.x, this.y, this.radius+2*ctx.lineWidth);
  583. ctx.stroke();
  584. }
  585. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  586. ctx.lineWidth *= this.networkScaleInv;
  587. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  588. ctx.fillStyle = this.selected ? this.color.highlight.background : this.hover ? this.color.hover.background : this.color.background;
  589. ctx.circle(this.x, this.y, this.radius);
  590. ctx.fill();
  591. ctx.stroke();
  592. this._label(ctx, this.label, this.x, this.y);
  593. };
  594. Node.prototype._resizeEllipse = function (ctx) {
  595. if (!this.width) {
  596. var textSize = this.getTextSize(ctx);
  597. this.width = textSize.width * 1.5;
  598. this.height = textSize.height * 2;
  599. if (this.width < this.height) {
  600. this.width = this.height;
  601. }
  602. var defaultSize = this.width;
  603. // scaling used for clustering
  604. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  605. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  606. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  607. this.growthIndicator = this.width - defaultSize;
  608. }
  609. };
  610. Node.prototype._drawEllipse = function (ctx) {
  611. this._resizeEllipse(ctx);
  612. this.left = this.x - this.width / 2;
  613. this.top = this.y - this.height / 2;
  614. var clusterLineWidth = 2.5;
  615. var selectionLineWidth = 2;
  616. ctx.strokeStyle = this.selected ? this.color.highlight.border : this.hover ? this.color.hover.border : this.color.border;
  617. // draw the outer border
  618. if (this.clusterSize > 1) {
  619. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  620. ctx.lineWidth *= this.networkScaleInv;
  621. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  622. ctx.ellipse(this.left-2*ctx.lineWidth, this.top-2*ctx.lineWidth, this.width+4*ctx.lineWidth, this.height+4*ctx.lineWidth);
  623. ctx.stroke();
  624. }
  625. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  626. ctx.lineWidth *= this.networkScaleInv;
  627. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  628. ctx.fillStyle = this.selected ? this.color.highlight.background : this.hover ? this.color.hover.background : this.color.background;
  629. ctx.ellipse(this.left, this.top, this.width, this.height);
  630. ctx.fill();
  631. ctx.stroke();
  632. this._label(ctx, this.label, this.x, this.y);
  633. };
  634. Node.prototype._drawDot = function (ctx) {
  635. this._drawShape(ctx, 'circle');
  636. };
  637. Node.prototype._drawTriangle = function (ctx) {
  638. this._drawShape(ctx, 'triangle');
  639. };
  640. Node.prototype._drawTriangleDown = function (ctx) {
  641. this._drawShape(ctx, 'triangleDown');
  642. };
  643. Node.prototype._drawSquare = function (ctx) {
  644. this._drawShape(ctx, 'square');
  645. };
  646. Node.prototype._drawStar = function (ctx) {
  647. this._drawShape(ctx, 'star');
  648. };
  649. Node.prototype._resizeShape = function (ctx) {
  650. if (!this.width) {
  651. this.radius = this.baseRadiusValue;
  652. var size = 2 * this.radius;
  653. this.width = size;
  654. this.height = size;
  655. // scaling used for clustering
  656. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  657. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  658. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  659. this.growthIndicator = this.width - size;
  660. }
  661. };
  662. Node.prototype._drawShape = function (ctx, shape) {
  663. this._resizeShape(ctx);
  664. this.left = this.x - this.width / 2;
  665. this.top = this.y - this.height / 2;
  666. var clusterLineWidth = 2.5;
  667. var selectionLineWidth = 2;
  668. var radiusMultiplier = 2;
  669. // choose draw method depending on the shape
  670. switch (shape) {
  671. case 'dot': radiusMultiplier = 2; break;
  672. case 'square': radiusMultiplier = 2; break;
  673. case 'triangle': radiusMultiplier = 3; break;
  674. case 'triangleDown': radiusMultiplier = 3; break;
  675. case 'star': radiusMultiplier = 4; break;
  676. }
  677. ctx.strokeStyle = this.selected ? this.color.highlight.border : this.hover ? this.color.hover.border : this.color.border;
  678. // draw the outer border
  679. if (this.clusterSize > 1) {
  680. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  681. ctx.lineWidth *= this.networkScaleInv;
  682. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  683. ctx[shape](this.x, this.y, this.radius + radiusMultiplier * ctx.lineWidth);
  684. ctx.stroke();
  685. }
  686. ctx.lineWidth = (this.selected ? selectionLineWidth : 1.0) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  687. ctx.lineWidth *= this.networkScaleInv;
  688. ctx.lineWidth = Math.min(0.1 * this.width,ctx.lineWidth);
  689. ctx.fillStyle = this.selected ? this.color.highlight.background : this.hover ? this.color.hover.background : this.color.background;
  690. ctx[shape](this.x, this.y, this.radius);
  691. ctx.fill();
  692. ctx.stroke();
  693. if (this.label) {
  694. this._label(ctx, this.label, this.x, this.y + this.height / 2, undefined, 'top',true);
  695. }
  696. };
  697. Node.prototype._resizeText = function (ctx) {
  698. if (!this.width) {
  699. var margin = 5;
  700. var textSize = this.getTextSize(ctx);
  701. this.width = textSize.width + 2 * margin;
  702. this.height = textSize.height + 2 * margin;
  703. // scaling used for clustering
  704. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  705. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  706. this.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  707. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  708. }
  709. };
  710. Node.prototype._drawText = function (ctx) {
  711. this._resizeText(ctx);
  712. this.left = this.x - this.width / 2;
  713. this.top = this.y - this.height / 2;
  714. this._label(ctx, this.label, this.x, this.y);
  715. };
  716. Node.prototype._label = function (ctx, text, x, y, align, baseline, labelUnderNode) {
  717. if (text && this.fontSize * this.networkScale > this.fontDrawThreshold) {
  718. ctx.font = (this.selected ? "bold " : "") + this.fontSize + "px " + this.fontFace;
  719. ctx.fillStyle = this.fontColor || "black";
  720. ctx.textAlign = align || "center";
  721. ctx.textBaseline = baseline || "middle";
  722. var lines = text.split('\n');
  723. var lineCount = lines.length;
  724. var fontSize = (this.fontSize + 4);
  725. var yLine = y + (1 - lineCount) / 2 * fontSize;
  726. if (labelUnderNode == true) {
  727. yLine = y + (1 - lineCount) / (2 * fontSize);
  728. }
  729. for (var i = 0; i < lineCount; i++) {
  730. ctx.fillText(lines[i], x, yLine);
  731. yLine += fontSize;
  732. }
  733. }
  734. };
  735. Node.prototype.getTextSize = function(ctx) {
  736. if (this.label !== undefined) {
  737. ctx.font = (this.selected ? "bold " : "") + this.fontSize + "px " + this.fontFace;
  738. var lines = this.label.split('\n'),
  739. height = (this.fontSize + 4) * lines.length,
  740. width = 0;
  741. for (var i = 0, iMax = lines.length; i < iMax; i++) {
  742. width = Math.max(width, ctx.measureText(lines[i]).width);
  743. }
  744. return {"width": width, "height": height};
  745. }
  746. else {
  747. return {"width": 0, "height": 0};
  748. }
  749. };
  750. /**
  751. * this is used to determine if a node is visible at all. this is used to determine when it needs to be drawn.
  752. * there is a safety margin of 0.3 * width;
  753. *
  754. * @returns {boolean}
  755. */
  756. Node.prototype.inArea = function() {
  757. if (this.width !== undefined) {
  758. return (this.x + this.width *this.networkScaleInv >= this.canvasTopLeft.x &&
  759. this.x - this.width *this.networkScaleInv < this.canvasBottomRight.x &&
  760. this.y + this.height*this.networkScaleInv >= this.canvasTopLeft.y &&
  761. this.y - this.height*this.networkScaleInv < this.canvasBottomRight.y);
  762. }
  763. else {
  764. return true;
  765. }
  766. };
  767. /**
  768. * checks if the core of the node is in the display area, this is used for opening clusters around zoom
  769. * @returns {boolean}
  770. */
  771. Node.prototype.inView = function() {
  772. return (this.x >= this.canvasTopLeft.x &&
  773. this.x < this.canvasBottomRight.x &&
  774. this.y >= this.canvasTopLeft.y &&
  775. this.y < this.canvasBottomRight.y);
  776. };
  777. /**
  778. * This allows the zoom level of the network to influence the rendering
  779. * We store the inverted scale and the coordinates of the top left, and bottom right points of the canvas
  780. *
  781. * @param scale
  782. * @param canvasTopLeft
  783. * @param canvasBottomRight
  784. */
  785. Node.prototype.setScaleAndPos = function(scale,canvasTopLeft,canvasBottomRight) {
  786. this.networkScaleInv = 1.0/scale;
  787. this.networkScale = scale;
  788. this.canvasTopLeft = canvasTopLeft;
  789. this.canvasBottomRight = canvasBottomRight;
  790. };
  791. /**
  792. * This allows the zoom level of the network to influence the rendering
  793. *
  794. * @param scale
  795. */
  796. Node.prototype.setScale = function(scale) {
  797. this.networkScaleInv = 1.0/scale;
  798. this.networkScale = scale;
  799. };
  800. /**
  801. * set the velocity at 0. Is called when this node is contained in another during clustering
  802. */
  803. Node.prototype.clearVelocity = function() {
  804. this.vx = 0;
  805. this.vy = 0;
  806. };
  807. /**
  808. * Basic preservation of (kinectic) energy
  809. *
  810. * @param massBeforeClustering
  811. */
  812. Node.prototype.updateVelocity = function(massBeforeClustering) {
  813. var energyBefore = this.vx * this.vx * massBeforeClustering;
  814. //this.vx = (this.vx < 0) ? -Math.sqrt(energyBefore/this.mass) : Math.sqrt(energyBefore/this.mass);
  815. this.vx = Math.sqrt(energyBefore/this.mass);
  816. energyBefore = this.vy * this.vy * massBeforeClustering;
  817. //this.vy = (this.vy < 0) ? -Math.sqrt(energyBefore/this.mass) : Math.sqrt(energyBefore/this.mass);
  818. this.vy = Math.sqrt(energyBefore/this.mass);
  819. };
  820. module.exports = Node;