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.

1143 lines
40 KiB

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