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.

960 lines
32 KiB

10 years ago
10 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} options An object containing options for the node. All
  6. * options 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", "icon"
  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 options
  23. * @param {Object} constants An object with default values for
  24. * example for the color
  25. *
  26. */
  27. function Node(options, imagelist, grouplist, globalOptions) {
  28. this.options = util.bridgeObject(globalOptions);
  29. this.selected = false;
  30. this.hover = false;
  31. this.edges = []; // all edges connected to this node
  32. // set defaults for the options
  33. this.id = undefined;
  34. this.allowedToMoveX = false;
  35. this.allowedToMoveY = false;
  36. this.xFixed = false;
  37. this.yFixed = false;
  38. this.horizontalAlignLeft = true; // these are for the navigation controls
  39. this.verticalAlignTop = true; // these are for the navigation controls
  40. this.baseRadiusValue = this.options.radius;
  41. this.radiusFixed = false;
  42. this.labelDimensions = {top:0, left:0, width:0, height:0, yLine:0}; // could be cached
  43. this.boundingBox = {top:0, left:0, right:0, bottom:0};
  44. this.imagelist = imagelist;
  45. this.grouplist = grouplist;
  46. // physics options
  47. this.x = null;
  48. this.y = null;
  49. this.predefinedPosition = false; // used to check if initial zoomExtent should just take the range or approximate
  50. this.fixedData = {x:null,y:null};
  51. // TODO: global options should not be needed here.
  52. this.setOptions(options, globalOptions);
  53. // variables to tell the node about the network.
  54. this.networkScaleInv = 1;
  55. this.networkScale = 1;
  56. this.canvasTopLeft = {x: -300, y: -300};
  57. this.canvasBottomRight = {x: 300, y: 300};
  58. this.parentEdgeId = null;
  59. }
  60. /**
  61. * Attach a edge to the node
  62. * @param {Edge} edge
  63. */
  64. Node.prototype.attachEdge = function(edge) {
  65. if (this.edges.indexOf(edge) == -1) {
  66. this.edges.push(edge);
  67. }
  68. };
  69. /**
  70. * Detach a edge from the node
  71. * @param {Edge} edge
  72. */
  73. Node.prototype.detachEdge = function(edge) {
  74. var index = this.edges.indexOf(edge);
  75. if (index != -1) {
  76. this.edges.splice(index, 1);
  77. }
  78. };
  79. /**
  80. * Set or overwrite options for the node
  81. * @param {Object} options an object with options
  82. * @param {Object} constants and object with default, global options
  83. */
  84. Node.prototype.setOptions = function(options, constants) {
  85. if (!options) {
  86. return;
  87. }
  88. var fields = ['borderWidth', 'borderWidthSelected', 'shape', 'image', 'brokenImage', 'radius', 'label',
  89. 'customScalingFunction', 'icon', 'value','hidden','physics'
  90. ];
  91. util.selectiveDeepExtend(fields, this.options, options);
  92. // basic options
  93. if (options.id !== undefined) {this.id = options.id;}
  94. if (options.label !== undefined) {this.label = options.label; this.originalLabel = options.label;}
  95. if (options.title !== undefined) {this.title = options.title;}
  96. if (options.x !== undefined) {this.x = options.x; this.predefinedPosition = true;}
  97. if (options.y !== undefined) {this.y = options.y; this.predefinedPosition = true;}
  98. if (options.value !== undefined) {this.value = options.value;}
  99. if (options.level !== undefined) {this.level = options.level; this.preassignedLevel = true;}
  100. // navigation controls options
  101. if (options.horizontalAlignLeft !== undefined) {this.horizontalAlignLeft = options.horizontalAlignLeft;}
  102. if (options.verticalAlignTop !== undefined) {this.verticalAlignTop = options.verticalAlignTop;}
  103. if (options.triggerFunction !== undefined) {this.triggerFunction = options.triggerFunction;}
  104. if (this.id === undefined) {
  105. throw "Node must have an id";
  106. }
  107. // copy group options
  108. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  109. var groupObj = this.grouplist.get(options.group);
  110. util.deepExtend(this.options, groupObj);
  111. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  112. this.options.color = util.parseColor(this.options.color);
  113. }
  114. // individual shape options
  115. if (options.radius !== undefined) {this.baseRadiusValue = this.options.radius;}
  116. if (options.color !== undefined) {this.options.color = util.parseColor(options.color);}
  117. if (this.options.image !== undefined && this.options.image!= "") {
  118. if (this.imagelist) {
  119. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  120. }
  121. else {
  122. throw "No imagelist provided";
  123. }
  124. }
  125. if (options.allowedToMoveX !== undefined) {
  126. this.xFixed = !options.allowedToMoveX;
  127. this.allowedToMoveX = options.allowedToMoveX;
  128. }
  129. else if (options.x !== undefined && this.allowedToMoveX == false) {
  130. this.xFixed = true;
  131. }
  132. if (options.allowedToMoveY !== undefined) {
  133. this.yFixed = !options.allowedToMoveY;
  134. this.allowedToMoveY = options.allowedToMoveY;
  135. }
  136. else if (options.y !== undefined && this.allowedToMoveY == false) {
  137. this.yFixed = true;
  138. }
  139. this.radiusFixed = this.radiusFixed || (options.radius !== undefined);
  140. if (this.options.shape === 'image' || this.options.shape === 'circularImage') {
  141. this.options.radiusMin = constants.nodes.widthMin;
  142. this.options.radiusMax = constants.nodes.widthMax;
  143. }
  144. // choose draw method depending on the shape
  145. switch (this.options.shape) {
  146. case 'database': this.draw = this._drawDatabase; this.resize = this._resizeDatabase; break;
  147. case 'box': this.draw = this._drawBox; this.resize = this._resizeBox; break;
  148. case 'circle': this.draw = this._drawCircle; this.resize = this._resizeCircle; break;
  149. case 'ellipse': this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  150. // TODO: add diamond shape
  151. case 'image': this.draw = this._drawImage; this.resize = this._resizeImage; break;
  152. case 'circularImage': this.draw = this._drawCircularImage; this.resize = this._resizeCircularImage; break;
  153. case 'text': this.draw = this._drawText; this.resize = this._resizeText; break;
  154. case 'dot': this.draw = this._drawDot; this.resize = this._resizeShape; break;
  155. case 'square': this.draw = this._drawSquare; this.resize = this._resizeShape; break;
  156. case 'triangle': this.draw = this._drawTriangle; this.resize = this._resizeShape; break;
  157. case 'triangleDown': this.draw = this._drawTriangleDown; this.resize = this._resizeShape; break;
  158. case 'star': this.draw = this._drawStar; this.resize = this._resizeShape; break;
  159. case 'icon': this.draw = this._drawIcon; this.resize = this._resizeIcon; break;
  160. default: this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  161. }
  162. // reset the size of the node, this can be changed
  163. this._reset();
  164. };
  165. /**
  166. * select this node
  167. */
  168. Node.prototype.select = function() {
  169. this.selected = true;
  170. this._reset();
  171. };
  172. /**
  173. * unselect this node
  174. */
  175. Node.prototype.unselect = function() {
  176. this.selected = false;
  177. this._reset();
  178. };
  179. /**
  180. * Reset the calculated size of the node, forces it to recalculate its size
  181. * @private
  182. */
  183. Node.prototype._reset = function() {
  184. this.width = undefined;
  185. this.height = undefined;
  186. };
  187. /**
  188. * get the title of this node.
  189. * @return {string} title The title of the node, or undefined when no title
  190. * has been set.
  191. */
  192. Node.prototype.getTitle = function() {
  193. return typeof this.title === "function" ? this.title() : this.title;
  194. };
  195. /**
  196. * Calculate the distance to the border of the Node
  197. * @param {CanvasRenderingContext2D} ctx
  198. * @param {Number} angle Angle in radians
  199. * @returns {number} distance Distance to the border in pixels
  200. */
  201. Node.prototype.distanceToBorder = function (ctx, angle) {
  202. var borderWidth = 1;
  203. if (!this.width) {
  204. this.resize(ctx);
  205. }
  206. switch (this.options.shape) {
  207. case 'circle':
  208. case 'dot':
  209. return this.options.radius+ borderWidth;
  210. case 'ellipse':
  211. var a = this.width / 2;
  212. var b = this.height / 2;
  213. var w = (Math.sin(angle) * a);
  214. var h = (Math.cos(angle) * b);
  215. return a * b / Math.sqrt(w * w + h * h);
  216. // TODO: implement distanceToBorder for database
  217. // TODO: implement distanceToBorder for triangle
  218. // TODO: implement distanceToBorder for triangleDown
  219. case 'box':
  220. case 'image':
  221. case 'text':
  222. default:
  223. if (this.width) {
  224. return Math.min(
  225. Math.abs(this.width / 2 / Math.cos(angle)),
  226. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  227. // TODO: reckon with border radius too in case of box
  228. }
  229. else {
  230. return 0;
  231. }
  232. }
  233. // TODO: implement calculation of distance to border for all shapes
  234. };
  235. /**
  236. * Check if this node has a fixed x and y position
  237. * @return {boolean} true if fixed, false if not
  238. */
  239. Node.prototype.isFixed = function() {
  240. return (this.xFixed && this.yFixed);
  241. };
  242. /**
  243. * check if this node is selecte
  244. * @return {boolean} selected True if node is selected, else false
  245. */
  246. Node.prototype.isSelected = function() {
  247. return this.selected;
  248. };
  249. /**
  250. * Retrieve the value of the node. Can be undefined
  251. * @return {Number} value
  252. */
  253. Node.prototype.getValue = function() {
  254. return this.value;
  255. };
  256. /**
  257. * Calculate the distance from the nodes location to the given location (x,y)
  258. * @param {Number} x
  259. * @param {Number} y
  260. * @return {Number} value
  261. */
  262. Node.prototype.getDistance = function(x, y) {
  263. var dx = this.x - x,
  264. dy = this.y - y;
  265. return Math.sqrt(dx * dx + dy * dy);
  266. };
  267. /**
  268. * Adjust the value range of the node. The node will adjust it's radius
  269. * based on its value.
  270. * @param {Number} min
  271. * @param {Number} max
  272. */
  273. Node.prototype.setValueRange = function(min, max, total) {
  274. if (!this.radiusFixed && this.value !== undefined) {
  275. var scale = this.options.customScalingFunction(min, max, total, this.value);
  276. var radiusDiff = this.options.radiusMax - this.options.radiusMin;
  277. if (this.options.labelStyle.scaleFontWithValue == true) {
  278. var fontDiff = this.options.labelStyle.fontSizeMax - this.options.labelStyle.fontSizeMin;
  279. this.options.labelStyle.fontSize = this.options.labelStyle.fontSizeMin + scale * fontDiff;
  280. }
  281. this.options.radius = this.options.radiusMin + scale * radiusDiff;
  282. }
  283. this.baseRadiusValue = this.options.radius;
  284. };
  285. /**
  286. * Draw this node in the given canvas
  287. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  288. * @param {CanvasRenderingContext2D} ctx
  289. */
  290. Node.prototype.draw = function(ctx) {
  291. throw "Draw method not initialized for node";
  292. };
  293. /**
  294. * Recalculate the size of this node in the given canvas
  295. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  296. * @param {CanvasRenderingContext2D} ctx
  297. */
  298. Node.prototype.resize = function(ctx) {
  299. throw "Resize method not initialized for node";
  300. };
  301. /**
  302. * Check if this object is overlapping with the provided object
  303. * @param {Object} obj an object with parameters left, top, right, bottom
  304. * @return {boolean} True if location is located on node
  305. */
  306. Node.prototype.isOverlappingWith = function(obj) {
  307. return (this.left < obj.right &&
  308. this.left + this.width > obj.left &&
  309. this.top < obj.bottom &&
  310. this.top + this.height > obj.top);
  311. };
  312. Node.prototype._resizeImage = function (ctx) {
  313. // TODO: pre calculate the image size
  314. if (!this.width || !this.height) { // undefined or 0
  315. var width, height;
  316. if (this.value) {
  317. this.options.radius= this.baseRadiusValue;
  318. var scale = this.imageObj.height / this.imageObj.width;
  319. if (scale !== undefined) {
  320. width = this.options.radius|| this.imageObj.width;
  321. height = this.options.radius* scale || this.imageObj.height;
  322. }
  323. else {
  324. width = 0;
  325. height = 0;
  326. }
  327. }
  328. else {
  329. width = this.imageObj.width;
  330. height = this.imageObj.height;
  331. }
  332. this.width = width;
  333. this.height = height;
  334. }
  335. };
  336. Node.prototype._drawImageAtPosition = function (ctx) {
  337. if (this.imageObj.width != 0 ) {
  338. // draw the image
  339. ctx.globalAlpha = 1.0;
  340. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  341. }
  342. };
  343. Node.prototype._drawImageLabel = function (ctx) {
  344. var yLabel;
  345. var offset = 0;
  346. if (this.height){
  347. offset = this.height / 2;
  348. var labelDimensions = this.getTextSize(ctx);
  349. if (labelDimensions.lineCount >= 1){
  350. offset += labelDimensions.height / 2;
  351. offset += 3;
  352. }
  353. }
  354. yLabel = this.y + offset;
  355. this._label(ctx, this.label, this.x, yLabel, undefined);
  356. };
  357. Node.prototype._drawImage = function (ctx) {
  358. this._resizeImage(ctx);
  359. this.left = this.x - this.width / 2;
  360. this.top = this.y - this.height / 2;
  361. this._drawImageAtPosition(ctx);
  362. this.boundingBox.top = this.top;
  363. this.boundingBox.left = this.left;
  364. this.boundingBox.right = this.left + this.width;
  365. this.boundingBox.bottom = this.top + this.height;
  366. this._drawImageLabel(ctx);
  367. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  368. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  369. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  370. };
  371. Node.prototype._resizeCircularImage = function (ctx) {
  372. if(!this.imageObj.src || !this.imageObj.width || !this.imageObj.height){
  373. if (!this.width) {
  374. var diameter = this.options.radius * 2;
  375. this.width = diameter;
  376. this.height = diameter;
  377. this._swapToImageResizeWhenImageLoaded = true;
  378. }
  379. }
  380. else {
  381. if (this._swapToImageResizeWhenImageLoaded) {
  382. this.width = 0;
  383. this.height = 0;
  384. delete this._swapToImageResizeWhenImageLoaded;
  385. }
  386. this._resizeImage(ctx);
  387. }
  388. };
  389. Node.prototype._drawCircularImage = function (ctx) {
  390. this._resizeCircularImage(ctx);
  391. this.left = this.x - this.width / 2;
  392. this.top = this.y - this.height / 2;
  393. var centerX = this.left + (this.width / 2);
  394. var centerY = this.top + (this.height / 2);
  395. var radius = Math.abs(this.height / 2);
  396. this._drawRawCircle(ctx, centerX, centerY, radius);
  397. ctx.save();
  398. ctx.circle(this.x, this.y, radius);
  399. ctx.stroke();
  400. ctx.clip();
  401. this._drawImageAtPosition(ctx);
  402. ctx.restore();
  403. this.boundingBox.top = this.y - this.options.radius;
  404. this.boundingBox.left = this.x - this.options.radius;
  405. this.boundingBox.right = this.x + this.options.radius;
  406. this.boundingBox.bottom = this.y + this.options.radius;
  407. this._drawImageLabel(ctx);
  408. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  409. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  410. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  411. };
  412. Node.prototype._resizeBox = function (ctx) {
  413. if (!this.width) {
  414. var margin = 5;
  415. var textSize = this.getTextSize(ctx);
  416. this.width = textSize.width + 2 * margin;
  417. this.height = textSize.height + 2 * margin;
  418. }
  419. };
  420. Node.prototype._drawBox = function (ctx) {
  421. this._resizeBox(ctx);
  422. this.left = this.x - this.width / 2;
  423. this.top = this.y - this.height / 2;
  424. var borderWidth = this.options.borderWidth;
  425. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  426. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  427. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth);
  428. ctx.lineWidth *= this.networkScaleInv;
  429. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  430. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  431. ctx.roundRect(this.left, this.top, this.width, this.height, this.options.radius);
  432. ctx.fill();
  433. ctx.stroke();
  434. this.boundingBox.top = this.top;
  435. this.boundingBox.left = this.left;
  436. this.boundingBox.right = this.left + this.width;
  437. this.boundingBox.bottom = this.top + this.height;
  438. this._label(ctx, this.label, this.x, this.y);
  439. };
  440. Node.prototype._resizeDatabase = function (ctx) {
  441. if (!this.width) {
  442. var margin = 5;
  443. var textSize = this.getTextSize(ctx);
  444. var size = textSize.width + 2 * margin;
  445. this.width = size;
  446. this.height = size;
  447. }
  448. };
  449. Node.prototype._drawDatabase = function (ctx) {
  450. this._resizeDatabase(ctx);
  451. this.left = this.x - this.width / 2;
  452. this.top = this.y - this.height / 2;
  453. var borderWidth = this.options.borderWidth;
  454. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  455. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  456. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth);
  457. ctx.lineWidth *= this.networkScaleInv;
  458. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  459. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  460. ctx.database(this.x - this.width/2, this.y - this.height*0.5, this.width, this.height);
  461. ctx.fill();
  462. ctx.stroke();
  463. this.boundingBox.top = this.top;
  464. this.boundingBox.left = this.left;
  465. this.boundingBox.right = this.left + this.width;
  466. this.boundingBox.bottom = this.top + this.height;
  467. this._label(ctx, this.label, this.x, this.y);
  468. };
  469. Node.prototype._resizeCircle = function (ctx) {
  470. if (!this.width) {
  471. var margin = 5;
  472. var textSize = this.getTextSize(ctx);
  473. var diameter = Math.max(textSize.width, textSize.height) + 2 * margin;
  474. this.options.radius = diameter / 2;
  475. this.width = diameter;
  476. this.height = diameter;
  477. }
  478. };
  479. Node.prototype._drawRawCircle = function (ctx, x, y, radius) {
  480. var borderWidth = this.options.borderWidth;
  481. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  482. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  483. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth);
  484. ctx.lineWidth *= this.networkScaleInv;
  485. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  486. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  487. ctx.circle(this.x, this.y, radius);
  488. ctx.fill();
  489. ctx.stroke();
  490. };
  491. Node.prototype._drawCircle = function (ctx) {
  492. this._resizeCircle(ctx);
  493. this.left = this.x - this.width / 2;
  494. this.top = this.y - this.height / 2;
  495. this._drawRawCircle(ctx, this.x, this.y, this.options.radius);
  496. this.boundingBox.top = this.y - this.options.radius;
  497. this.boundingBox.left = this.x - this.options.radius;
  498. this.boundingBox.right = this.x + this.options.radius;
  499. this.boundingBox.bottom = this.y + this.options.radius;
  500. this._label(ctx, this.label, this.x, this.y);
  501. };
  502. Node.prototype._resizeEllipse = function (ctx) {
  503. if (this.width === undefined) {
  504. var textSize = this.getTextSize(ctx);
  505. this.width = textSize.width * 1.5;
  506. this.height = textSize.height * 2;
  507. if (this.width < this.height) {
  508. this.width = this.height;
  509. }
  510. var defaultSize = this.width;
  511. }
  512. };
  513. Node.prototype._drawEllipse = function (ctx) {
  514. this._resizeEllipse(ctx);
  515. this.left = this.x - this.width / 2;
  516. this.top = this.y - this.height / 2;
  517. var borderWidth = this.options.borderWidth;
  518. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  519. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  520. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth);
  521. ctx.lineWidth *= this.networkScaleInv;
  522. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  523. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  524. ctx.ellipse(this.left, this.top, this.width, this.height);
  525. ctx.fill();
  526. ctx.stroke();
  527. this.boundingBox.top = this.top;
  528. this.boundingBox.left = this.left;
  529. this.boundingBox.right = this.left + this.width;
  530. this.boundingBox.bottom = this.top + this.height;
  531. this._label(ctx, this.label, this.x, this.y);
  532. };
  533. Node.prototype._drawDot = function (ctx) {
  534. this._drawShape(ctx, 'circle');
  535. };
  536. Node.prototype._drawTriangle = function (ctx) {
  537. this._drawShape(ctx, 'triangle');
  538. };
  539. Node.prototype._drawTriangleDown = function (ctx) {
  540. this._drawShape(ctx, 'triangleDown');
  541. };
  542. Node.prototype._drawSquare = function (ctx) {
  543. this._drawShape(ctx, 'square');
  544. };
  545. Node.prototype._drawStar = function (ctx) {
  546. this._drawShape(ctx, 'star');
  547. };
  548. Node.prototype._resizeShape = function (ctx) {
  549. if (!this.width) {
  550. this.options.radius= this.baseRadiusValue;
  551. var size = 2 * this.options.radius;
  552. this.width = size;
  553. this.height = size;
  554. }
  555. };
  556. Node.prototype._drawShape = function (ctx, shape) {
  557. this._resizeShape(ctx);
  558. this.left = this.x - this.width / 2;
  559. this.top = this.y - this.height / 2;
  560. var borderWidth = this.options.borderWidth;
  561. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  562. var radiusMultiplier = 2;
  563. // choose draw method depending on the shape
  564. switch (shape) {
  565. case 'dot': radiusMultiplier = 2; break;
  566. case 'square': radiusMultiplier = 2; break;
  567. case 'triangle': radiusMultiplier = 3; break;
  568. case 'triangleDown': radiusMultiplier = 3; break;
  569. case 'star': radiusMultiplier = 4; break;
  570. }
  571. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  572. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth);
  573. ctx.lineWidth *= this.networkScaleInv;
  574. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  575. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  576. ctx[shape](this.x, this.y, this.options.radius);
  577. ctx.fill();
  578. ctx.stroke();
  579. this.boundingBox.top = this.y - this.options.radius;
  580. this.boundingBox.left = this.x - this.options.radius;
  581. this.boundingBox.right = this.x + this.options.radius;
  582. this.boundingBox.bottom = this.y + this.options.radius;
  583. if (this.label) {
  584. this._label(ctx, this.label, this.x, this.y + this.height / 2, undefined, 'hanging',true);
  585. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  586. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  587. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  588. }
  589. };
  590. Node.prototype._resizeText = function (ctx) {
  591. if (!this.width) {
  592. var margin = 5;
  593. var textSize = this.getTextSize(ctx);
  594. this.width = textSize.width + 2 * margin;
  595. this.height = textSize.height + 2 * margin;
  596. }
  597. };
  598. Node.prototype._drawText = function (ctx) {
  599. this._resizeText(ctx);
  600. this.left = this.x - this.width / 2;
  601. this.top = this.y - this.height / 2;
  602. this._label(ctx, this.label, this.x, this.y);
  603. this.boundingBox.top = this.top;
  604. this.boundingBox.left = this.left;
  605. this.boundingBox.right = this.left + this.width;
  606. this.boundingBox.bottom = this.top + this.height;
  607. };
  608. Node.prototype._resizeIcon = function (ctx) {
  609. if (!this.width) {
  610. var margin = 5;
  611. var iconSize =
  612. {
  613. width: Number(this.options.icon.iconSize),
  614. height: Number(this.options.icon.iconSize)
  615. };
  616. this.width = iconSize.width + 2 * margin;
  617. this.height = iconSize.height + 2 * margin;
  618. }
  619. };
  620. Node.prototype._drawIcon = function (ctx) {
  621. this._resizeIcon(ctx);
  622. this.options.icon.iconSize = this.options.icon.iconSize || 50;
  623. this.left = this.x - this.width / 2;
  624. this.top = this.y - this.height / 2;
  625. this._icon(ctx);
  626. this.boundingBox.top = this.y - this.options.icon.iconSize/2;
  627. this.boundingBox.left = this.x - this.options.icon.iconSize/2;
  628. this.boundingBox.right = this.x + this.options.icon.iconSize/2;
  629. this.boundingBox.bottom = this.y + this.options.icon.iconSize/2;
  630. if (this.label) {
  631. var iconTextSpacing = 5;
  632. this._label(ctx, this.label, this.x, this.y + this.height / 2 + iconTextSpacing, 'top', true);
  633. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  634. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  635. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  636. }
  637. };
  638. Node.prototype._icon = function (ctx) {
  639. var relativeIconSize = Number(this.options.icon.iconSize) * this.networkScale;
  640. if (this.options.icon.code && relativeIconSize > this.options.labelStyle.fontDrawThreshold - 1) {
  641. var iconSize = Number(this.options.icon.iconSize);
  642. ctx.font = (this.selected ? "bold " : "") + iconSize + "px " + this.options.icon.iconFontFace;
  643. // draw icon
  644. ctx.fillStyle = this.options.icon.iconColor || "black";
  645. ctx.textAlign = "center";
  646. ctx.textBaseline = "middle";
  647. ctx.fillText(this.options.icon.code, this.x, this.y);
  648. }
  649. };
  650. Node.prototype._label = function (ctx, text, x, y, align, baseline, labelUnderNode) {
  651. var relativeFontSize = Number(this.options.labelStyle.fontSize) * this.networkScale;
  652. if (text && relativeFontSize >= this.options.labelStyle.fontDrawThreshold - 1) {
  653. var fontSize = Number(this.options.labelStyle.fontSize);
  654. // this ensures that there will not be HUGE letters on screen by setting an upper limit on the visible text size (regardless of zoomLevel)
  655. if (relativeFontSize >= this.options.labelStyle.fontSizeMaxVisible) {
  656. fontSize = Number(this.options.labelStyle.fontSizeMaxVisible) * this.networkScaleInv;
  657. }
  658. // fade in when relative scale is between threshold and threshold - 1
  659. var fontColor = this.options.labelStyle.fontColor || "#000000";
  660. var strokecolor = this.options.labelStyle.fontStrokeColor;
  661. if (relativeFontSize <= this.options.labelStyle.fontDrawThreshold) {
  662. var opacity = Math.max(0,Math.min(1,1 - (this.options.labelStyle.fontDrawThreshold - relativeFontSize)));
  663. fontColor = util.overrideOpacity(fontColor, opacity);
  664. strokecolor = util.overrideOpacity(strokecolor, opacity);
  665. }
  666. ctx.font = (this.selected ? "bold " : "") + fontSize + "px " + this.options.labelStyle.fontFace;
  667. var lines = text.split('\n');
  668. var lineCount = lines.length;
  669. var yLine = y + (1 - lineCount) / 2 * fontSize;
  670. if (labelUnderNode == true) {
  671. yLine = y + (1 - lineCount) / (2 * fontSize);
  672. }
  673. // font fill from edges now for nodes!
  674. var width = ctx.measureText(lines[0]).width;
  675. for (var i = 1; i < lineCount; i++) {
  676. var lineWidth = ctx.measureText(lines[i]).width;
  677. width = lineWidth > width ? lineWidth : width;
  678. }
  679. var height = fontSize * lineCount;
  680. var left = x - width / 2;
  681. var top = y - height / 2;
  682. if (baseline == "hanging") {
  683. top += 0.5 * fontSize;
  684. top += 4; // distance from node, required because we use hanging. Hanging has less difference between browsers
  685. yLine += 4; // distance from node
  686. }
  687. this.labelDimensions = {top:top,left:left,width:width,height:height,yLine:yLine};
  688. // create the fontfill background
  689. if (this.options.labelStyle.fontFill !== undefined && this.options.labelStyle.fontFill !== null && this.options.labelStyle.fontFill !== "none") {
  690. ctx.fillStyle = this.options.labelStyle.fontFill;
  691. ctx.fillRect(left, top, width, height);
  692. }
  693. // draw text
  694. ctx.fillStyle = fontColor;
  695. ctx.textAlign = align || "center";
  696. ctx.textBaseline = baseline || "middle";
  697. if (this.options.labelStyle.fontStrokeWidth > 0){
  698. ctx.lineWidth = this.options.labelStyle.fontStrokeWidth;
  699. ctx.strokeStyle = strokecolor;
  700. ctx.lineJoin = 'round';
  701. }
  702. for (var i = 0; i < lineCount; i++) {
  703. if(this.options.labelStyle.fontStrokeWidth){
  704. ctx.strokeText(lines[i], x, yLine);
  705. }
  706. ctx.fillText(lines[i], x, yLine);
  707. yLine += fontSize;
  708. }
  709. }
  710. };
  711. Node.prototype.getTextSize = function(ctx) {
  712. if (this.label !== undefined) {
  713. var fontSize = Number(this.options.labelStyle.fontSize);
  714. if (fontSize * this.networkScale > this.options.labelStyle.fontSizeMaxVisible) {
  715. fontSize = Number(this.options.labelStyle.fontSizeMaxVisible) * this.networkScaleInv;
  716. }
  717. ctx.font = (this.selected ? "bold " : "") + fontSize + "px " + this.options.labelStyle.fontFace;
  718. var lines = this.label.split('\n'),
  719. height = (fontSize + 4) * lines.length,
  720. width = 0;
  721. for (var i = 0, iMax = lines.length; i < iMax; i++) {
  722. width = Math.max(width, ctx.measureText(lines[i]).width);
  723. }
  724. return {width: width, height: height, lineCount: lines.length};
  725. }
  726. else {
  727. return {width: 0, height: 0, lineCount: 0};
  728. }
  729. };
  730. /**
  731. * this is used to determine if a node is visible at all. this is used to determine when it needs to be drawn.
  732. * there is a safety margin of 0.3 * width;
  733. *
  734. * @returns {boolean}
  735. */
  736. Node.prototype.inArea = function() {
  737. if (this.width !== undefined) {
  738. return (this.x + this.width *this.networkScaleInv >= this.canvasTopLeft.x &&
  739. this.x - this.width *this.networkScaleInv < this.canvasBottomRight.x &&
  740. this.y + this.height*this.networkScaleInv >= this.canvasTopLeft.y &&
  741. this.y - this.height*this.networkScaleInv < this.canvasBottomRight.y);
  742. }
  743. else {
  744. return true;
  745. }
  746. };
  747. /**
  748. * This allows the zoom level of the network to influence the rendering
  749. * We store the inverted scale and the coordinates of the top left, and bottom right points of the canvas
  750. *
  751. * @param scale
  752. * @param canvasTopLeft
  753. * @param canvasBottomRight
  754. */
  755. Node.prototype.setScaleAndPos = function(scale,canvasTopLeft,canvasBottomRight) {
  756. this.networkScaleInv = 1.0/scale;
  757. this.networkScale = scale;
  758. this.canvasTopLeft = canvasTopLeft;
  759. this.canvasBottomRight = canvasBottomRight;
  760. };
  761. /**
  762. * This allows the zoom level of the network to influence the rendering
  763. *
  764. * @param scale
  765. */
  766. Node.prototype.setScale = function(scale) {
  767. this.networkScaleInv = 1.0/scale;
  768. this.networkScale = scale;
  769. };
  770. /**
  771. * set the velocity at 0. Is called when this node is contained in another during clustering
  772. */
  773. Node.prototype.clearVelocity = function() {
  774. this.vx = 0;
  775. this.vy = 0;
  776. };
  777. /**
  778. * Basic preservation of (kinectic) energy
  779. *
  780. * @param massBeforeClustering
  781. */
  782. Node.prototype.updateVelocity = function(massBeforeClustering) {
  783. var energyBefore = this.vx * this.vx * massBeforeClustering;
  784. //this.vx = (this.vx < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  785. this.vx = Math.sqrt(energyBefore/this.options.mass);
  786. energyBefore = this.vy * this.vy * massBeforeClustering;
  787. //this.vy = (this.vy < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  788. this.vy = Math.sqrt(energyBefore/this.options.mass);
  789. };
  790. module.exports = Node;