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.

965 lines
33 KiB

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