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.

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