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.

1052 lines
36 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 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._label(ctx, this.label, this.x, yLabel, undefined, "top");
  496. };
  497. Node.prototype._resizeBox = function (ctx) {
  498. if (!this.width) {
  499. var margin = 5;
  500. var textSize = this.getTextSize(ctx);
  501. this.width = textSize.width + 2 * margin;
  502. this.height = textSize.height + 2 * margin;
  503. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  504. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  505. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  506. // this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  507. }
  508. };
  509. Node.prototype._drawBox = function (ctx) {
  510. this._resizeBox(ctx);
  511. this.left = this.x - this.width / 2;
  512. this.top = this.y - this.height / 2;
  513. var clusterLineWidth = 2.5;
  514. var borderWidth = this.options.borderWidth;
  515. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  516. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  517. // draw the outer border
  518. if (this.clusterSize > 1) {
  519. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  520. ctx.lineWidth *= this.networkScaleInv;
  521. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  522. 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);
  523. ctx.stroke();
  524. }
  525. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  526. ctx.lineWidth *= this.networkScaleInv;
  527. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  528. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.options.color.background;
  529. ctx.roundRect(this.left, this.top, this.width, this.height, this.options.radius);
  530. ctx.fill();
  531. ctx.stroke();
  532. this.boundingBox.top = this.top;
  533. this.boundingBox.left = this.left;
  534. this.boundingBox.right = this.left + this.width;
  535. this.boundingBox.bottom = this.top + this.height;
  536. this._label(ctx, this.label, this.x, this.y);
  537. };
  538. Node.prototype._resizeDatabase = function (ctx) {
  539. if (!this.width) {
  540. var margin = 5;
  541. var textSize = this.getTextSize(ctx);
  542. var size = textSize.width + 2 * margin;
  543. this.width = size;
  544. this.height = size;
  545. // scaling used for clustering
  546. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  547. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  548. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  549. this.growthIndicator = this.width - size;
  550. }
  551. };
  552. Node.prototype._drawDatabase = function (ctx) {
  553. this._resizeDatabase(ctx);
  554. this.left = this.x - this.width / 2;
  555. this.top = this.y - this.height / 2;
  556. var clusterLineWidth = 2.5;
  557. var borderWidth = this.options.borderWidth;
  558. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  559. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  560. // draw the outer border
  561. if (this.clusterSize > 1) {
  562. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  563. ctx.lineWidth *= this.networkScaleInv;
  564. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  565. 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);
  566. ctx.stroke();
  567. }
  568. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  569. ctx.lineWidth *= this.networkScaleInv;
  570. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  571. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  572. ctx.database(this.x - this.width/2, this.y - this.height*0.5, this.width, this.height);
  573. ctx.fill();
  574. ctx.stroke();
  575. this.boundingBox.top = this.top;
  576. this.boundingBox.left = this.left;
  577. this.boundingBox.right = this.left + this.width;
  578. this.boundingBox.bottom = this.top + this.height;
  579. this._label(ctx, this.label, this.x, this.y);
  580. };
  581. Node.prototype._resizeCircle = function (ctx) {
  582. if (!this.width) {
  583. var margin = 5;
  584. var textSize = this.getTextSize(ctx);
  585. var diameter = Math.max(textSize.width, textSize.height) + 2 * margin;
  586. this.options.radius = diameter / 2;
  587. this.width = diameter;
  588. this.height = diameter;
  589. // scaling used for clustering
  590. // this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  591. // this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  592. this.options.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  593. this.growthIndicator = this.options.radius- 0.5*diameter;
  594. }
  595. };
  596. Node.prototype._drawCircle = function (ctx) {
  597. this._resizeCircle(ctx);
  598. this.left = this.x - this.width / 2;
  599. this.top = this.y - this.height / 2;
  600. var clusterLineWidth = 2.5;
  601. var borderWidth = this.options.borderWidth;
  602. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  603. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  604. // draw the outer border
  605. if (this.clusterSize > 1) {
  606. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  607. ctx.lineWidth *= this.networkScaleInv;
  608. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  609. ctx.circle(this.x, this.y, this.options.radius+2*ctx.lineWidth);
  610. ctx.stroke();
  611. }
  612. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  613. ctx.lineWidth *= this.networkScaleInv;
  614. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  615. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  616. ctx.circle(this.x, this.y, this.options.radius);
  617. ctx.fill();
  618. ctx.stroke();
  619. this.boundingBox.top = this.y - this.options.radius;
  620. this.boundingBox.left = this.x - this.options.radius;
  621. this.boundingBox.right = this.x + this.options.radius;
  622. this.boundingBox.bottom = this.y + this.options.radius;
  623. this._label(ctx, this.label, this.x, this.y);
  624. };
  625. Node.prototype._resizeEllipse = function (ctx) {
  626. if (!this.width) {
  627. var textSize = this.getTextSize(ctx);
  628. this.width = textSize.width * 1.5;
  629. this.height = textSize.height * 2;
  630. if (this.width < this.height) {
  631. this.width = this.height;
  632. }
  633. var defaultSize = this.width;
  634. // scaling used for clustering
  635. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  636. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  637. this.options.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  638. this.growthIndicator = this.width - defaultSize;
  639. }
  640. };
  641. Node.prototype._drawEllipse = function (ctx) {
  642. this._resizeEllipse(ctx);
  643. this.left = this.x - this.width / 2;
  644. this.top = this.y - this.height / 2;
  645. var clusterLineWidth = 2.5;
  646. var borderWidth = this.options.borderWidth;
  647. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  648. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  649. // draw the outer border
  650. if (this.clusterSize > 1) {
  651. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  652. ctx.lineWidth *= this.networkScaleInv;
  653. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  654. ctx.ellipse(this.left-2*ctx.lineWidth, this.top-2*ctx.lineWidth, this.width+4*ctx.lineWidth, this.height+4*ctx.lineWidth);
  655. ctx.stroke();
  656. }
  657. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  658. ctx.lineWidth *= this.networkScaleInv;
  659. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  660. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  661. ctx.ellipse(this.left, this.top, this.width, this.height);
  662. ctx.fill();
  663. ctx.stroke();
  664. this.boundingBox.top = this.top;
  665. this.boundingBox.left = this.left;
  666. this.boundingBox.right = this.left + this.width;
  667. this.boundingBox.bottom = this.top + this.height;
  668. this._label(ctx, this.label, this.x, this.y);
  669. };
  670. Node.prototype._drawDot = function (ctx) {
  671. this._drawShape(ctx, 'circle');
  672. };
  673. Node.prototype._drawTriangle = function (ctx) {
  674. this._drawShape(ctx, 'triangle');
  675. };
  676. Node.prototype._drawTriangleDown = function (ctx) {
  677. this._drawShape(ctx, 'triangleDown');
  678. };
  679. Node.prototype._drawSquare = function (ctx) {
  680. this._drawShape(ctx, 'square');
  681. };
  682. Node.prototype._drawStar = function (ctx) {
  683. this._drawShape(ctx, 'star');
  684. };
  685. Node.prototype._resizeShape = function (ctx) {
  686. if (!this.width) {
  687. this.options.radius= this.baseRadiusValue;
  688. var size = 2 * this.options.radius;
  689. this.width = size;
  690. this.height = size;
  691. // scaling used for clustering
  692. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  693. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  694. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  695. this.growthIndicator = this.width - size;
  696. }
  697. };
  698. Node.prototype._drawShape = function (ctx, shape) {
  699. this._resizeShape(ctx);
  700. this.left = this.x - this.width / 2;
  701. this.top = this.y - this.height / 2;
  702. var clusterLineWidth = 2.5;
  703. var borderWidth = this.options.borderWidth;
  704. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  705. var radiusMultiplier = 2;
  706. // choose draw method depending on the shape
  707. switch (shape) {
  708. case 'dot': radiusMultiplier = 2; break;
  709. case 'square': radiusMultiplier = 2; break;
  710. case 'triangle': radiusMultiplier = 3; break;
  711. case 'triangleDown': radiusMultiplier = 3; break;
  712. case 'star': radiusMultiplier = 4; break;
  713. }
  714. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  715. // draw the outer border
  716. if (this.clusterSize > 1) {
  717. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  718. ctx.lineWidth *= this.networkScaleInv;
  719. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  720. ctx[shape](this.x, this.y, this.options.radius+ radiusMultiplier * ctx.lineWidth);
  721. ctx.stroke();
  722. }
  723. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  724. ctx.lineWidth *= this.networkScaleInv;
  725. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  726. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  727. ctx[shape](this.x, this.y, this.options.radius);
  728. ctx.fill();
  729. ctx.stroke();
  730. this.boundingBox.top = this.y - this.options.radius;
  731. this.boundingBox.left = this.x - this.options.radius;
  732. this.boundingBox.right = this.x + this.options.radius;
  733. this.boundingBox.bottom = this.y + this.options.radius;
  734. if (this.label) {
  735. this._label(ctx, this.label, this.x, this.y + this.height / 2, undefined, 'top',true);
  736. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left)
  737. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width)
  738. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height)
  739. }
  740. };
  741. Node.prototype._resizeText = function (ctx) {
  742. if (!this.width) {
  743. var margin = 5;
  744. var textSize = this.getTextSize(ctx);
  745. this.width = textSize.width + 2 * margin;
  746. this.height = textSize.height + 2 * margin;
  747. // scaling used for clustering
  748. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  749. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  750. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  751. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  752. }
  753. };
  754. Node.prototype._drawText = function (ctx) {
  755. this._resizeText(ctx);
  756. this.left = this.x - this.width / 2;
  757. this.top = this.y - this.height / 2;
  758. this._label(ctx, this.label, this.x, this.y);
  759. this.boundingBox.top = this.top;
  760. this.boundingBox.left = this.left;
  761. this.boundingBox.right = this.left + this.width;
  762. this.boundingBox.bottom = this.top + this.height;
  763. };
  764. Node.prototype._label = function (ctx, text, x, y, align, baseline, labelUnderNode) {
  765. if (text && Number(this.options.fontSize) * this.networkScale > this.fontDrawThreshold) {
  766. ctx.font = (this.selected ? "bold " : "") + this.options.fontSize + "px " + this.options.fontFace;
  767. var lines = text.split('\n');
  768. var lineCount = lines.length;
  769. var fontSize = (Number(this.options.fontSize) + 4); // TODO: why is this +4 ?
  770. var yLine = y + (1 - lineCount) / 2 * fontSize;
  771. if (labelUnderNode == true) {
  772. yLine = y + (1 - lineCount) / (2 * fontSize);
  773. }
  774. // font fill from edges now for nodes!
  775. var width = ctx.measureText(lines[0]).width;
  776. for (var i = 1; i < lineCount; i++) {
  777. var lineWidth = ctx.measureText(lines[i]).width;
  778. width = lineWidth > width ? lineWidth : width;
  779. }
  780. var height = this.options.fontSize * lineCount;
  781. var left = x - width / 2;
  782. var top = y - height / 2;
  783. if (baseline == "top") {
  784. top += 0.5 * fontSize;
  785. }
  786. this.labelDimensions = {top:top,left:left,width:width,height:height,yLine:yLine};
  787. // create the fontfill background
  788. if (this.options.fontFill !== undefined && this.options.fontFill !== null && this.options.fontFill !== "none") {
  789. ctx.fillStyle = this.options.fontFill;
  790. ctx.fillRect(left, top, width, height);
  791. }
  792. // draw text
  793. ctx.fillStyle = this.options.fontColor || "black";
  794. ctx.textAlign = align || "center";
  795. ctx.textBaseline = baseline || "middle";
  796. for (var i = 0; i < lineCount; i++) {
  797. ctx.fillText(lines[i], x, yLine);
  798. yLine += fontSize;
  799. }
  800. }
  801. };
  802. Node.prototype.getTextSize = function(ctx) {
  803. if (this.label !== undefined) {
  804. ctx.font = (this.selected ? "bold " : "") + this.options.fontSize + "px " + this.options.fontFace;
  805. var lines = this.label.split('\n'),
  806. height = (Number(this.options.fontSize) + 4) * lines.length,
  807. width = 0;
  808. for (var i = 0, iMax = lines.length; i < iMax; i++) {
  809. width = Math.max(width, ctx.measureText(lines[i]).width);
  810. }
  811. return {"width": width, "height": height};
  812. }
  813. else {
  814. return {"width": 0, "height": 0};
  815. }
  816. };
  817. /**
  818. * this is used to determine if a node is visible at all. this is used to determine when it needs to be drawn.
  819. * there is a safety margin of 0.3 * width;
  820. *
  821. * @returns {boolean}
  822. */
  823. Node.prototype.inArea = function() {
  824. if (this.width !== undefined) {
  825. return (this.x + this.width *this.networkScaleInv >= this.canvasTopLeft.x &&
  826. this.x - this.width *this.networkScaleInv < this.canvasBottomRight.x &&
  827. this.y + this.height*this.networkScaleInv >= this.canvasTopLeft.y &&
  828. this.y - this.height*this.networkScaleInv < this.canvasBottomRight.y);
  829. }
  830. else {
  831. return true;
  832. }
  833. };
  834. /**
  835. * checks if the core of the node is in the display area, this is used for opening clusters around zoom
  836. * @returns {boolean}
  837. */
  838. Node.prototype.inView = function() {
  839. return (this.x >= this.canvasTopLeft.x &&
  840. this.x < this.canvasBottomRight.x &&
  841. this.y >= this.canvasTopLeft.y &&
  842. this.y < this.canvasBottomRight.y);
  843. };
  844. /**
  845. * This allows the zoom level of the network to influence the rendering
  846. * We store the inverted scale and the coordinates of the top left, and bottom right points of the canvas
  847. *
  848. * @param scale
  849. * @param canvasTopLeft
  850. * @param canvasBottomRight
  851. */
  852. Node.prototype.setScaleAndPos = function(scale,canvasTopLeft,canvasBottomRight) {
  853. this.networkScaleInv = 1.0/scale;
  854. this.networkScale = scale;
  855. this.canvasTopLeft = canvasTopLeft;
  856. this.canvasBottomRight = canvasBottomRight;
  857. };
  858. /**
  859. * This allows the zoom level of the network to influence the rendering
  860. *
  861. * @param scale
  862. */
  863. Node.prototype.setScale = function(scale) {
  864. this.networkScaleInv = 1.0/scale;
  865. this.networkScale = scale;
  866. };
  867. /**
  868. * set the velocity at 0. Is called when this node is contained in another during clustering
  869. */
  870. Node.prototype.clearVelocity = function() {
  871. this.vx = 0;
  872. this.vy = 0;
  873. };
  874. /**
  875. * Basic preservation of (kinectic) energy
  876. *
  877. * @param massBeforeClustering
  878. */
  879. Node.prototype.updateVelocity = function(massBeforeClustering) {
  880. var energyBefore = this.vx * this.vx * massBeforeClustering;
  881. //this.vx = (this.vx < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  882. this.vx = Math.sqrt(energyBefore/this.options.mass);
  883. energyBefore = this.vy * this.vy * massBeforeClustering;
  884. //this.vy = (this.vy < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  885. this.vy = Math.sqrt(energyBefore/this.options.mass);
  886. };
  887. module.exports = Node;