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.

1061 lines
37 KiB

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