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.

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