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.

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