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.

1185 lines
35 KiB

10 years ago
10 years ago
  1. var util = require('../util');
  2. var Node = require('./Node');
  3. /**
  4. * @class Edge
  5. *
  6. * A edge connects two nodes
  7. * @param {Object} properties Object with properties. Must contain
  8. * At least properties from and to.
  9. * Available properties: from (number),
  10. * to (number), label (string, color (string),
  11. * width (number), style (string),
  12. * length (number), title (string)
  13. * @param {Network} network A Network object, used to find and edge to
  14. * nodes.
  15. * @param {Object} constants An object with default values for
  16. * example for the color
  17. */
  18. function Edge (properties, network, networkConstants) {
  19. if (!network) {
  20. throw "No network provided";
  21. }
  22. var fields = ['edges','physics'];
  23. var constants = util.selectiveBridgeObject(fields,networkConstants);
  24. this.options = constants.edges;
  25. this.physics = constants.physics;
  26. this.options['smoothCurves'] = networkConstants['smoothCurves'];
  27. this.network = network;
  28. // initialize variables
  29. this.id = undefined;
  30. this.fromId = undefined;
  31. this.toId = undefined;
  32. this.title = undefined;
  33. this.widthSelected = this.options.width * this.options.widthSelectionMultiplier;
  34. this.value = undefined;
  35. this.selected = false;
  36. this.hover = false;
  37. this.labelDimensions = {top:0,left:0,width:0,height:0,yLine:0}; // could be cached
  38. this.dirtyLabel = true;
  39. this.from = null; // a node
  40. this.to = null; // a node
  41. this.via = null; // a temp node
  42. // we use this to be able to reconnect the edge to a cluster if its node is put into a cluster
  43. // by storing the original information we can revert to the original connection when the cluser is opened.
  44. this.originalFromId = [];
  45. this.originalToId = [];
  46. this.connected = false;
  47. this.widthFixed = false;
  48. this.lengthFixed = false;
  49. this.setProperties(properties);
  50. this.controlNodesEnabled = false;
  51. this.controlNodes = {from:null, to:null, positions:{}};
  52. this.connectedNode = null;
  53. }
  54. /**
  55. * Set or overwrite properties for the edge
  56. * @param {Object} properties an object with properties
  57. * @param {Object} constants and object with default, global properties
  58. */
  59. Edge.prototype.setProperties = function(properties) {
  60. if (!properties) {
  61. return;
  62. }
  63. var fields = ['style','fontSize','fontFace','fontColor','fontFill','width',
  64. 'widthSelectionMultiplier','hoverWidth','arrowScaleFactor','dash','inheritColor'
  65. ];
  66. util.selectiveDeepExtend(fields, this.options, properties);
  67. if (properties.from !== undefined) {this.fromId = properties.from;}
  68. if (properties.to !== undefined) {this.toId = properties.to;}
  69. if (properties.id !== undefined) {this.id = properties.id;}
  70. if (properties.label !== undefined) {this.label = properties.label; this.dirtyLabel = true;}
  71. if (properties.title !== undefined) {this.title = properties.title;}
  72. if (properties.value !== undefined) {this.value = properties.value;}
  73. if (properties.length !== undefined) {this.physics.springLength = properties.length;}
  74. if (properties.color !== undefined) {
  75. this.options.inheritColor = false;
  76. if (util.isString(properties.color)) {
  77. this.options.color.color = properties.color;
  78. this.options.color.highlight = properties.color;
  79. }
  80. else {
  81. if (properties.color.color !== undefined) {this.options.color.color = properties.color.color;}
  82. if (properties.color.highlight !== undefined) {this.options.color.highlight = properties.color.highlight;}
  83. if (properties.color.hover !== undefined) {this.options.color.hover = properties.color.hover;}
  84. }
  85. }
  86. // A node is connected when it has a from and to node.
  87. this.connect();
  88. this.widthFixed = this.widthFixed || (properties.width !== undefined);
  89. this.lengthFixed = this.lengthFixed || (properties.length !== undefined);
  90. this.widthSelected = this.options.width* this.options.widthSelectionMultiplier;
  91. // set draw method based on style
  92. switch (this.options.style) {
  93. case 'line': this.draw = this._drawLine; break;
  94. case 'arrow': this.draw = this._drawArrow; break;
  95. case 'arrow-center': this.draw = this._drawArrowCenter; break;
  96. case 'dash-line': this.draw = this._drawDashLine; break;
  97. default: this.draw = this._drawLine; break;
  98. }
  99. };
  100. /**
  101. * Connect an edge to its nodes
  102. */
  103. Edge.prototype.connect = function () {
  104. this.disconnect();
  105. this.from = this.network.nodes[this.fromId] || null;
  106. this.to = this.network.nodes[this.toId] || null;
  107. this.connected = (this.from && this.to);
  108. if (this.connected) {
  109. this.from.attachEdge(this);
  110. this.to.attachEdge(this);
  111. }
  112. else {
  113. if (this.from) {
  114. this.from.detachEdge(this);
  115. }
  116. if (this.to) {
  117. this.to.detachEdge(this);
  118. }
  119. }
  120. };
  121. /**
  122. * Disconnect an edge from its nodes
  123. */
  124. Edge.prototype.disconnect = function () {
  125. if (this.from) {
  126. this.from.detachEdge(this);
  127. this.from = null;
  128. }
  129. if (this.to) {
  130. this.to.detachEdge(this);
  131. this.to = null;
  132. }
  133. this.connected = false;
  134. };
  135. /**
  136. * get the title of this edge.
  137. * @return {string} title The title of the edge, or undefined when no title
  138. * has been set.
  139. */
  140. Edge.prototype.getTitle = function() {
  141. return typeof this.title === "function" ? this.title() : this.title;
  142. };
  143. /**
  144. * Retrieve the value of the edge. Can be undefined
  145. * @return {Number} value
  146. */
  147. Edge.prototype.getValue = function() {
  148. return this.value;
  149. };
  150. /**
  151. * Adjust the value range of the edge. The edge will adjust it's width
  152. * based on its value.
  153. * @param {Number} min
  154. * @param {Number} max
  155. */
  156. Edge.prototype.setValueRange = function(min, max) {
  157. if (!this.widthFixed && this.value !== undefined) {
  158. var scale = (this.options.widthMax - this.options.widthMin) / (max - min);
  159. this.options.width= (this.value - min) * scale + this.options.widthMin;
  160. this.widthSelected = this.options.width* this.options.widthSelectionMultiplier;
  161. }
  162. };
  163. /**
  164. * Redraw a edge
  165. * Draw this edge in the given canvas
  166. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  167. * @param {CanvasRenderingContext2D} ctx
  168. */
  169. Edge.prototype.draw = function(ctx) {
  170. throw "Method draw not initialized in edge";
  171. };
  172. /**
  173. * Check if this object is overlapping with the provided object
  174. * @param {Object} obj an object with parameters left, top
  175. * @return {boolean} True if location is located on the edge
  176. */
  177. Edge.prototype.isOverlappingWith = function(obj) {
  178. if (this.connected) {
  179. var distMax = 10;
  180. var xFrom = this.from.x;
  181. var yFrom = this.from.y;
  182. var xTo = this.to.x;
  183. var yTo = this.to.y;
  184. var xObj = obj.left;
  185. var yObj = obj.top;
  186. var dist = this._getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  187. return (dist < distMax);
  188. }
  189. else {
  190. return false
  191. }
  192. };
  193. Edge.prototype._getColor = function() {
  194. var colorObj = this.options.color;
  195. if (this.options.inheritColor == "to") {
  196. colorObj = {
  197. highlight: this.to.options.color.highlight.border,
  198. hover: this.to.options.color.hover.border,
  199. color: this.to.options.color.border
  200. };
  201. }
  202. else if (this.options.inheritColor == "from" || this.options.inheritColor == true) {
  203. colorObj = {
  204. highlight: this.from.options.color.highlight.border,
  205. hover: this.from.options.color.hover.border,
  206. color: this.from.options.color.border
  207. };
  208. }
  209. if (this.selected == true) {return colorObj.highlight;}
  210. else if (this.hover == true) {return colorObj.hover;}
  211. else {return colorObj.color;}
  212. };
  213. /**
  214. * Redraw a edge as a line
  215. * Draw this edge in the given canvas
  216. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  217. * @param {CanvasRenderingContext2D} ctx
  218. * @private
  219. */
  220. Edge.prototype._drawLine = function(ctx) {
  221. // set style
  222. ctx.strokeStyle = this._getColor();
  223. ctx.lineWidth = this._getLineWidth();
  224. if (this.from != this.to) {
  225. // draw line
  226. var via = this._line(ctx);
  227. // draw label
  228. var point;
  229. if (this.label) {
  230. if (this.options.smoothCurves.enabled == true && via != null) {
  231. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  232. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  233. point = {x:midpointX, y:midpointY};
  234. }
  235. else {
  236. point = this._pointOnLine(0.5);
  237. }
  238. this._label(ctx, this.label, point.x, point.y);
  239. }
  240. }
  241. else {
  242. var x, y;
  243. var radius = this.physics.springLength / 4;
  244. var node = this.from;
  245. if (!node.width) {
  246. node.resize(ctx);
  247. }
  248. if (node.width > node.height) {
  249. x = node.x + node.width / 2;
  250. y = node.y - radius;
  251. }
  252. else {
  253. x = node.x + radius;
  254. y = node.y - node.height / 2;
  255. }
  256. this._circle(ctx, x, y, radius);
  257. point = this._pointOnCircle(x, y, radius, 0.5);
  258. this._label(ctx, this.label, point.x, point.y);
  259. }
  260. };
  261. /**
  262. * Get the line width of the edge. Depends on width and whether one of the
  263. * connected nodes is selected.
  264. * @return {Number} width
  265. * @private
  266. */
  267. Edge.prototype._getLineWidth = function() {
  268. if (this.selected == true) {
  269. return Math.max(Math.min(this.widthSelected, this.options.widthMax), 0.3*this.networkScaleInv);
  270. }
  271. else {
  272. if (this.hover == true) {
  273. return Math.max(Math.min(this.options.hoverWidth, this.options.widthMax), 0.3*this.networkScaleInv);
  274. }
  275. else {
  276. return Math.max(this.options.width, 0.3*this.networkScaleInv);
  277. }
  278. }
  279. };
  280. Edge.prototype._getViaCoordinates = function () {
  281. var xVia = null;
  282. var yVia = null;
  283. var factor = this.options.smoothCurves.roundness;
  284. var type = this.options.smoothCurves.type;
  285. var dx = Math.abs(this.from.x - this.to.x);
  286. var dy = Math.abs(this.from.y - this.to.y);
  287. if (type == 'discrete' || type == 'diagonalCross') {
  288. if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) {
  289. if (this.from.y > this.to.y) {
  290. if (this.from.x < this.to.x) {
  291. xVia = this.from.x + factor * dy;
  292. yVia = this.from.y - factor * dy;
  293. }
  294. else if (this.from.x > this.to.x) {
  295. xVia = this.from.x - factor * dy;
  296. yVia = this.from.y - factor * dy;
  297. }
  298. }
  299. else if (this.from.y < this.to.y) {
  300. if (this.from.x < this.to.x) {
  301. xVia = this.from.x + factor * dy;
  302. yVia = this.from.y + factor * dy;
  303. }
  304. else if (this.from.x > this.to.x) {
  305. xVia = this.from.x - factor * dy;
  306. yVia = this.from.y + factor * dy;
  307. }
  308. }
  309. if (type == "discrete") {
  310. xVia = dx < factor * dy ? this.from.x : xVia;
  311. }
  312. }
  313. else if (Math.abs(this.from.x - this.to.x) > Math.abs(this.from.y - this.to.y)) {
  314. if (this.from.y > this.to.y) {
  315. if (this.from.x < this.to.x) {
  316. xVia = this.from.x + factor * dx;
  317. yVia = this.from.y - factor * dx;
  318. }
  319. else if (this.from.x > this.to.x) {
  320. xVia = this.from.x - factor * dx;
  321. yVia = this.from.y - factor * dx;
  322. }
  323. }
  324. else if (this.from.y < this.to.y) {
  325. if (this.from.x < this.to.x) {
  326. xVia = this.from.x + factor * dx;
  327. yVia = this.from.y + factor * dx;
  328. }
  329. else if (this.from.x > this.to.x) {
  330. xVia = this.from.x - factor * dx;
  331. yVia = this.from.y + factor * dx;
  332. }
  333. }
  334. if (type == "discrete") {
  335. yVia = dy < factor * dx ? this.from.y : yVia;
  336. }
  337. }
  338. }
  339. else if (type == "straightCross") {
  340. if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) { // up - down
  341. xVia = this.from.x;
  342. if (this.from.y < this.to.y) {
  343. yVia = this.to.y - (1-factor) * dy;
  344. }
  345. else {
  346. yVia = this.to.y + (1-factor) * dy;
  347. }
  348. }
  349. else if (Math.abs(this.from.x - this.to.x) > Math.abs(this.from.y - this.to.y)) { // left - right
  350. if (this.from.x < this.to.x) {
  351. xVia = this.to.x - (1-factor) * dx;
  352. }
  353. else {
  354. xVia = this.to.x + (1-factor) * dx;
  355. }
  356. yVia = this.from.y;
  357. }
  358. }
  359. else if (type == 'horizontal') {
  360. if (this.from.x < this.to.x) {
  361. xVia = this.to.x - (1-factor) * dx;
  362. }
  363. else {
  364. xVia = this.to.x + (1-factor) * dx;
  365. }
  366. yVia = this.from.y;
  367. }
  368. else if (type == 'vertical') {
  369. xVia = this.from.x;
  370. if (this.from.y < this.to.y) {
  371. yVia = this.to.y - (1-factor) * dy;
  372. }
  373. else {
  374. yVia = this.to.y + (1-factor) * dy;
  375. }
  376. }
  377. else { // continuous
  378. if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) {
  379. if (this.from.y > this.to.y) {
  380. if (this.from.x < this.to.x) {
  381. // console.log(1)
  382. xVia = this.from.x + factor * dy;
  383. yVia = this.from.y - factor * dy;
  384. xVia = this.to.x < xVia ? this.to.x : xVia;
  385. }
  386. else if (this.from.x > this.to.x) {
  387. // console.log(2)
  388. xVia = this.from.x - factor * dy;
  389. yVia = this.from.y - factor * dy;
  390. xVia = this.to.x > xVia ? this.to.x :xVia;
  391. }
  392. }
  393. else if (this.from.y < this.to.y) {
  394. if (this.from.x < this.to.x) {
  395. // console.log(3)
  396. xVia = this.from.x + factor * dy;
  397. yVia = this.from.y + factor * dy;
  398. xVia = this.to.x < xVia ? this.to.x : xVia;
  399. }
  400. else if (this.from.x > this.to.x) {
  401. // console.log(4, this.from.x, this.to.x)
  402. xVia = this.from.x - factor * dy;
  403. yVia = this.from.y + factor * dy;
  404. xVia = this.to.x > xVia ? this.to.x : xVia;
  405. }
  406. }
  407. }
  408. else if (Math.abs(this.from.x - this.to.x) > Math.abs(this.from.y - this.to.y)) {
  409. if (this.from.y > this.to.y) {
  410. if (this.from.x < this.to.x) {
  411. // console.log(5)
  412. xVia = this.from.x + factor * dx;
  413. yVia = this.from.y - factor * dx;
  414. yVia = this.to.y > yVia ? this.to.y : yVia;
  415. }
  416. else if (this.from.x > this.to.x) {
  417. // console.log(6)
  418. xVia = this.from.x - factor * dx;
  419. yVia = this.from.y - factor * dx;
  420. yVia = this.to.y > yVia ? this.to.y : yVia;
  421. }
  422. }
  423. else if (this.from.y < this.to.y) {
  424. if (this.from.x < this.to.x) {
  425. // console.log(7)
  426. xVia = this.from.x + factor * dx;
  427. yVia = this.from.y + factor * dx;
  428. yVia = this.to.y < yVia ? this.to.y : yVia;
  429. }
  430. else if (this.from.x > this.to.x) {
  431. // console.log(8)
  432. xVia = this.from.x - factor * dx;
  433. yVia = this.from.y + factor * dx;
  434. yVia = this.to.y < yVia ? this.to.y : yVia;
  435. }
  436. }
  437. }
  438. }
  439. return {x:xVia, y:yVia};
  440. };
  441. /**
  442. * Draw a line between two nodes
  443. * @param {CanvasRenderingContext2D} ctx
  444. * @private
  445. */
  446. Edge.prototype._line = function (ctx) {
  447. // draw a straight line
  448. ctx.beginPath();
  449. ctx.moveTo(this.from.x, this.from.y);
  450. if (this.options.smoothCurves.enabled == true) {
  451. if (this.options.smoothCurves.dynamic == false) {
  452. var via = this._getViaCoordinates();
  453. if (via.x == null) {
  454. ctx.lineTo(this.to.x, this.to.y);
  455. ctx.stroke();
  456. return null;
  457. }
  458. else {
  459. // this.via.x = via.x;
  460. // this.via.y = via.y;
  461. ctx.quadraticCurveTo(via.x,via.y,this.to.x, this.to.y);
  462. ctx.stroke();
  463. return via;
  464. }
  465. }
  466. else {
  467. ctx.quadraticCurveTo(this.via.x,this.via.y,this.to.x, this.to.y);
  468. ctx.stroke();
  469. return this.via;
  470. }
  471. }
  472. else {
  473. ctx.lineTo(this.to.x, this.to.y);
  474. ctx.stroke();
  475. return null;
  476. }
  477. };
  478. /**
  479. * Draw a line from a node to itself, a circle
  480. * @param {CanvasRenderingContext2D} ctx
  481. * @param {Number} x
  482. * @param {Number} y
  483. * @param {Number} radius
  484. * @private
  485. */
  486. Edge.prototype._circle = function (ctx, x, y, radius) {
  487. // draw a circle
  488. ctx.beginPath();
  489. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  490. ctx.stroke();
  491. };
  492. /**
  493. * Draw label with white background and with the middle at (x, y)
  494. * @param {CanvasRenderingContext2D} ctx
  495. * @param {String} text
  496. * @param {Number} x
  497. * @param {Number} y
  498. * @private
  499. */
  500. Edge.prototype._label = function (ctx, text, x, y) {
  501. if (text) {
  502. ctx.font = ((this.from.selected || this.to.selected) ? "bold " : "") +
  503. this.options.fontSize + "px " + this.options.fontFace;
  504. var yLine;
  505. if (this.dirtyLabel == true) {
  506. var lines = String(text).split('\n');
  507. var lineCount = lines.length;
  508. var fontSize = (Number(this.options.fontSize) + 4);
  509. yLine = y + (1 - lineCount) / 2 * fontSize;
  510. var width = ctx.measureText(lines[0]).width;
  511. for (var i = 1; i < lineCount; i++) {
  512. var lineWidth = ctx.measureText(lines[i]).width;
  513. width = lineWidth > width ? lineWidth : width;
  514. }
  515. var height = this.options.fontSize * lineCount;
  516. var left = x - width / 2;
  517. var top = y - height / 2;
  518. // cache
  519. this.labelDimensions = {top:top,left:left,width:width,height:height,yLine:yLine};
  520. }
  521. if (this.options.fontFill !== undefined && this.options.fontFill !== null && this.options.fontFill !== "none") {
  522. ctx.fillStyle = this.options.fontFill;
  523. ctx.fillRect(this.labelDimensions.left,
  524. this.labelDimensions.top,
  525. this.labelDimensions.width,
  526. this.labelDimensions.height);
  527. }
  528. // draw text
  529. ctx.fillStyle = this.options.fontColor || "black";
  530. ctx.textAlign = "center";
  531. ctx.textBaseline = "middle";
  532. yLine = this.labelDimensions.yLine;
  533. for (var i = 0; i < lineCount; i++) {
  534. ctx.fillText(lines[i], x, yLine);
  535. yLine += fontSize;
  536. }
  537. }
  538. };
  539. /**
  540. * Redraw a edge as a dashed line
  541. * Draw this edge in the given canvas
  542. * @author David Jordan
  543. * @date 2012-08-08
  544. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  545. * @param {CanvasRenderingContext2D} ctx
  546. * @private
  547. */
  548. Edge.prototype._drawDashLine = function(ctx) {
  549. // set style
  550. ctx.strokeStyle = this._getColor();
  551. ctx.lineWidth = this._getLineWidth();
  552. var via = null;
  553. // only firefox and chrome support this method, else we use the legacy one.
  554. if (ctx.mozDash !== undefined || ctx.setLineDash !== undefined) {
  555. // configure the dash pattern
  556. var pattern = [0];
  557. if (this.options.dash.length !== undefined && this.options.dash.gap !== undefined) {
  558. pattern = [this.options.dash.length,this.options.dash.gap];
  559. }
  560. else {
  561. pattern = [5,5];
  562. }
  563. // set dash settings for chrome or firefox
  564. if (typeof ctx.setLineDash !== 'undefined') { //Chrome
  565. ctx.setLineDash(pattern);
  566. ctx.lineDashOffset = 0;
  567. } else { //Firefox
  568. ctx.mozDash = pattern;
  569. ctx.mozDashOffset = 0;
  570. }
  571. // draw the line
  572. via = this._line(ctx);
  573. // restore the dash settings.
  574. if (typeof ctx.setLineDash !== 'undefined') { //Chrome
  575. ctx.setLineDash([0]);
  576. ctx.lineDashOffset = 0;
  577. } else { //Firefox
  578. ctx.mozDash = [0];
  579. ctx.mozDashOffset = 0;
  580. }
  581. }
  582. else { // unsupporting smooth lines
  583. // draw dashed line
  584. ctx.beginPath();
  585. ctx.lineCap = 'round';
  586. if (this.options.dash.altLength !== undefined) //If an alt dash value has been set add to the array this value
  587. {
  588. ctx.dashedLine(this.from.x,this.from.y,this.to.x,this.to.y,
  589. [this.options.dash.length,this.options.dash.gap,this.options.dash.altLength,this.options.dash.gap]);
  590. }
  591. else if (this.options.dash.length !== undefined && this.options.dash.gap !== undefined) //If a dash and gap value has been set add to the array this value
  592. {
  593. ctx.dashedLine(this.from.x,this.from.y,this.to.x,this.to.y,
  594. [this.options.dash.length,this.options.dash.gap]);
  595. }
  596. else //If all else fails draw a line
  597. {
  598. ctx.moveTo(this.from.x, this.from.y);
  599. ctx.lineTo(this.to.x, this.to.y);
  600. }
  601. ctx.stroke();
  602. }
  603. // draw label
  604. if (this.label) {
  605. var point;
  606. if (this.options.smoothCurves.enabled == true && via != null) {
  607. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  608. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  609. point = {x:midpointX, y:midpointY};
  610. }
  611. else {
  612. point = this._pointOnLine(0.5);
  613. }
  614. this._label(ctx, this.label, point.x, point.y);
  615. }
  616. };
  617. /**
  618. * Get a point on a line
  619. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  620. * @return {Object} point
  621. * @private
  622. */
  623. Edge.prototype._pointOnLine = function (percentage) {
  624. return {
  625. x: (1 - percentage) * this.from.x + percentage * this.to.x,
  626. y: (1 - percentage) * this.from.y + percentage * this.to.y
  627. }
  628. };
  629. /**
  630. * Get a point on a circle
  631. * @param {Number} x
  632. * @param {Number} y
  633. * @param {Number} radius
  634. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  635. * @return {Object} point
  636. * @private
  637. */
  638. Edge.prototype._pointOnCircle = function (x, y, radius, percentage) {
  639. var angle = (percentage - 3/8) * 2 * Math.PI;
  640. return {
  641. x: x + radius * Math.cos(angle),
  642. y: y - radius * Math.sin(angle)
  643. }
  644. };
  645. /**
  646. * Redraw a edge as a line with an arrow halfway the line
  647. * Draw this edge in the given canvas
  648. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  649. * @param {CanvasRenderingContext2D} ctx
  650. * @private
  651. */
  652. Edge.prototype._drawArrowCenter = function(ctx) {
  653. var point;
  654. // set style
  655. ctx.strokeStyle = this._getColor();
  656. ctx.lineWidth = this._getLineWidth();
  657. if (this.from != this.to) {
  658. // draw line
  659. var via = this._line(ctx);
  660. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  661. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  662. // draw an arrow halfway the line
  663. if (this.options.smoothCurves.enabled == true && via != null) {
  664. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  665. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  666. point = {x:midpointX, y:midpointY};
  667. }
  668. else {
  669. point = this._pointOnLine(0.5);
  670. }
  671. ctx.arrow(point.x, point.y, angle, length);
  672. ctx.fill();
  673. ctx.stroke();
  674. // draw label
  675. if (this.label) {
  676. this._label(ctx, this.label, point.x, point.y);
  677. }
  678. }
  679. else {
  680. // draw circle
  681. var x, y;
  682. var radius = 0.25 * Math.max(100,this.physics.springLength);
  683. var node = this.from;
  684. if (!node.width) {
  685. node.resize(ctx);
  686. }
  687. if (node.width > node.height) {
  688. x = node.x + node.width * 0.5;
  689. y = node.y - radius;
  690. }
  691. else {
  692. x = node.x + radius;
  693. y = node.y - node.height * 0.5;
  694. }
  695. this._circle(ctx, x, y, radius);
  696. // draw all arrows
  697. var angle = 0.2 * Math.PI;
  698. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  699. point = this._pointOnCircle(x, y, radius, 0.5);
  700. ctx.arrow(point.x, point.y, angle, length);
  701. ctx.fill();
  702. ctx.stroke();
  703. // draw label
  704. if (this.label) {
  705. point = this._pointOnCircle(x, y, radius, 0.5);
  706. this._label(ctx, this.label, point.x, point.y);
  707. }
  708. }
  709. };
  710. /**
  711. * Redraw a edge as a line with an arrow
  712. * Draw this edge in the given canvas
  713. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  714. * @param {CanvasRenderingContext2D} ctx
  715. * @private
  716. */
  717. Edge.prototype._drawArrow = function(ctx) {
  718. // set style
  719. ctx.strokeStyle = this._getColor();
  720. ctx.lineWidth = this._getLineWidth();
  721. var angle, length;
  722. //draw a line
  723. if (this.from != this.to) {
  724. angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  725. var dx = (this.to.x - this.from.x);
  726. var dy = (this.to.y - this.from.y);
  727. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  728. var fromBorderDist = this.from.distanceToBorder(ctx, angle + Math.PI);
  729. var fromBorderPoint = (edgeSegmentLength - fromBorderDist) / edgeSegmentLength;
  730. var xFrom = (fromBorderPoint) * this.from.x + (1 - fromBorderPoint) * this.to.x;
  731. var yFrom = (fromBorderPoint) * this.from.y + (1 - fromBorderPoint) * this.to.y;
  732. var via;
  733. if (this.options.smoothCurves.dynamic == true && this.options.smoothCurves.enabled == true ) {
  734. via = this.via;
  735. }
  736. else if (this.options.smoothCurves.enabled == true) {
  737. via = this._getViaCoordinates();
  738. }
  739. if (this.options.smoothCurves.enabled == true && via.x != null) {
  740. angle = Math.atan2((this.to.y - via.y), (this.to.x - via.x));
  741. dx = (this.to.x - via.x);
  742. dy = (this.to.y - via.y);
  743. edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  744. }
  745. var toBorderDist = this.to.distanceToBorder(ctx, angle);
  746. var toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  747. var xTo,yTo;
  748. if (this.options.smoothCurves.enabled == true && via.x != null) {
  749. xTo = (1 - toBorderPoint) * via.x + toBorderPoint * this.to.x;
  750. yTo = (1 - toBorderPoint) * via.y + toBorderPoint * this.to.y;
  751. }
  752. else {
  753. xTo = (1 - toBorderPoint) * this.from.x + toBorderPoint * this.to.x;
  754. yTo = (1 - toBorderPoint) * this.from.y + toBorderPoint * this.to.y;
  755. }
  756. ctx.beginPath();
  757. ctx.moveTo(xFrom,yFrom);
  758. if (this.options.smoothCurves.enabled == true && via.x != null) {
  759. ctx.quadraticCurveTo(via.x,via.y,xTo, yTo);
  760. }
  761. else {
  762. ctx.lineTo(xTo, yTo);
  763. }
  764. ctx.stroke();
  765. // draw arrow at the end of the line
  766. length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  767. ctx.arrow(xTo, yTo, angle, length);
  768. ctx.fill();
  769. ctx.stroke();
  770. // draw label
  771. if (this.label) {
  772. var point;
  773. if (this.options.smoothCurves.enabled == true && via != null) {
  774. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  775. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  776. point = {x:midpointX, y:midpointY};
  777. }
  778. else {
  779. point = this._pointOnLine(0.5);
  780. }
  781. this._label(ctx, this.label, point.x, point.y);
  782. }
  783. }
  784. else {
  785. // draw circle
  786. var node = this.from;
  787. var x, y, arrow;
  788. var radius = 0.25 * Math.max(100,this.physics.springLength);
  789. if (!node.width) {
  790. node.resize(ctx);
  791. }
  792. if (node.width > node.height) {
  793. x = node.x + node.width * 0.5;
  794. y = node.y - radius;
  795. arrow = {
  796. x: x,
  797. y: node.y,
  798. angle: 0.9 * Math.PI
  799. };
  800. }
  801. else {
  802. x = node.x + radius;
  803. y = node.y - node.height * 0.5;
  804. arrow = {
  805. x: node.x,
  806. y: y,
  807. angle: 0.6 * Math.PI
  808. };
  809. }
  810. ctx.beginPath();
  811. // TODO: similarly, for a line without arrows, draw to the border of the nodes instead of the center
  812. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  813. ctx.stroke();
  814. // draw all arrows
  815. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  816. ctx.arrow(arrow.x, arrow.y, arrow.angle, length);
  817. ctx.fill();
  818. ctx.stroke();
  819. // draw label
  820. if (this.label) {
  821. point = this._pointOnCircle(x, y, radius, 0.5);
  822. this._label(ctx, this.label, point.x, point.y);
  823. }
  824. }
  825. };
  826. /**
  827. * Calculate the distance between a point (x3,y3) and a line segment from
  828. * (x1,y1) to (x2,y2).
  829. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  830. * @param {number} x1
  831. * @param {number} y1
  832. * @param {number} x2
  833. * @param {number} y2
  834. * @param {number} x3
  835. * @param {number} y3
  836. * @private
  837. */
  838. Edge.prototype._getDistanceToEdge = function (x1,y1, x2,y2, x3,y3) { // x3,y3 is the point
  839. var returnValue = 0;
  840. if (this.from != this.to) {
  841. if (this.options.smoothCurves.enabled == true) {
  842. var xVia, yVia;
  843. if (this.options.smoothCurves.enabled == true && this.options.smoothCurves.dynamic == true) {
  844. xVia = this.via.x;
  845. yVia = this.via.y;
  846. }
  847. else {
  848. var via = this._getViaCoordinates();
  849. xVia = via.x;
  850. yVia = via.y;
  851. }
  852. var minDistance = 1e9;
  853. var distance;
  854. var i,t,x,y, lastX, lastY;
  855. for (i = 0; i < 10; i++) {
  856. t = 0.1*i;
  857. x = Math.pow(1-t,2)*x1 + (2*t*(1 - t))*xVia + Math.pow(t,2)*x2;
  858. y = Math.pow(1-t,2)*y1 + (2*t*(1 - t))*yVia + Math.pow(t,2)*y2;
  859. if (i > 0) {
  860. distance = this._getDistanceToLine(lastX,lastY,x,y, x3,y3);
  861. minDistance = distance < minDistance ? distance : minDistance;
  862. }
  863. lastX = x; lastY = y;
  864. }
  865. returnValue = minDistance;
  866. }
  867. else {
  868. returnValue = this._getDistanceToLine(x1,y1,x2,y2,x3,y3);
  869. }
  870. }
  871. else {
  872. var x, y, dx, dy;
  873. var radius = 0.25 * this.physics.springLength;
  874. var node = this.from;
  875. if (node.width > node.height) {
  876. x = node.x + 0.5 * node.width;
  877. y = node.y - radius;
  878. }
  879. else {
  880. x = node.x + radius;
  881. y = node.y - 0.5 * node.height;
  882. }
  883. dx = x - x3;
  884. dy = y - y3;
  885. returnValue = Math.abs(Math.sqrt(dx*dx + dy*dy) - radius);
  886. }
  887. if (this.labelDimensions.left < x3 &&
  888. this.labelDimensions.left + this.labelDimensions.width > x3 &&
  889. this.labelDimensions.top < y3 &&
  890. this.labelDimensions.top + this.labelDimensions.height > y3) {
  891. return 0;
  892. }
  893. else {
  894. return returnValue;
  895. }
  896. };
  897. Edge.prototype._getDistanceToLine = function(x1,y1,x2,y2,x3,y3) {
  898. var px = x2-x1,
  899. py = y2-y1,
  900. something = px*px + py*py,
  901. u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  902. if (u > 1) {
  903. u = 1;
  904. }
  905. else if (u < 0) {
  906. u = 0;
  907. }
  908. var x = x1 + u * px,
  909. y = y1 + u * py,
  910. dx = x - x3,
  911. dy = y - y3;
  912. //# Note: If the actual distance does not matter,
  913. //# if you only want to compare what this function
  914. //# returns to other results of this function, you
  915. //# can just return the squared distance instead
  916. //# (i.e. remove the sqrt) to gain a little performance
  917. return Math.sqrt(dx*dx + dy*dy);
  918. };
  919. /**
  920. * This allows the zoom level of the network to influence the rendering
  921. *
  922. * @param scale
  923. */
  924. Edge.prototype.setScale = function(scale) {
  925. this.networkScaleInv = 1.0/scale;
  926. };
  927. Edge.prototype.select = function() {
  928. this.selected = true;
  929. };
  930. Edge.prototype.unselect = function() {
  931. this.selected = false;
  932. };
  933. Edge.prototype.positionBezierNode = function() {
  934. if (this.via !== null && this.from !== null && this.to !== null) {
  935. this.via.x = 0.5 * (this.from.x + this.to.x);
  936. this.via.y = 0.5 * (this.from.y + this.to.y);
  937. }
  938. };
  939. /**
  940. * This function draws the control nodes for the manipulator. In order to enable this, only set the this.controlNodesEnabled to true.
  941. * @param ctx
  942. */
  943. Edge.prototype._drawControlNodes = function(ctx) {
  944. if (this.controlNodesEnabled == true) {
  945. if (this.controlNodes.from === null && this.controlNodes.to === null) {
  946. var nodeIdFrom = "edgeIdFrom:".concat(this.id);
  947. var nodeIdTo = "edgeIdTo:".concat(this.id);
  948. var constants = {
  949. nodes:{group:'', radius:8},
  950. physics:{damping:0},
  951. clustering: {maxNodeSizeIncrements: 0 ,nodeScaling: {width:0, height: 0, radius:0}}
  952. };
  953. this.controlNodes.from = new Node(
  954. {id:nodeIdFrom,
  955. shape:'dot',
  956. color:{background:'#ff4e00', border:'#3c3c3c', highlight: {background:'#07f968'}}
  957. },{},{},constants);
  958. this.controlNodes.to = new Node(
  959. {id:nodeIdTo,
  960. shape:'dot',
  961. color:{background:'#ff4e00', border:'#3c3c3c', highlight: {background:'#07f968'}}
  962. },{},{},constants);
  963. }
  964. if (this.controlNodes.from.selected == false && this.controlNodes.to.selected == false) {
  965. this.controlNodes.positions = this.getControlNodePositions(ctx);
  966. this.controlNodes.from.x = this.controlNodes.positions.from.x;
  967. this.controlNodes.from.y = this.controlNodes.positions.from.y;
  968. this.controlNodes.to.x = this.controlNodes.positions.to.x;
  969. this.controlNodes.to.y = this.controlNodes.positions.to.y;
  970. }
  971. this.controlNodes.from.draw(ctx);
  972. this.controlNodes.to.draw(ctx);
  973. }
  974. else {
  975. this.controlNodes = {from:null, to:null, positions:{}};
  976. }
  977. };
  978. /**
  979. * Enable control nodes.
  980. * @private
  981. */
  982. Edge.prototype._enableControlNodes = function() {
  983. this.controlNodesEnabled = true;
  984. };
  985. /**
  986. * disable control nodes
  987. * @private
  988. */
  989. Edge.prototype._disableControlNodes = function() {
  990. this.controlNodesEnabled = false;
  991. };
  992. /**
  993. * This checks if one of the control nodes is selected and if so, returns the control node object. Else it returns null.
  994. * @param x
  995. * @param y
  996. * @returns {null}
  997. * @private
  998. */
  999. Edge.prototype._getSelectedControlNode = function(x,y) {
  1000. var positions = this.controlNodes.positions;
  1001. var fromDistance = Math.sqrt(Math.pow(x - positions.from.x,2) + Math.pow(y - positions.from.y,2));
  1002. var toDistance = Math.sqrt(Math.pow(x - positions.to.x ,2) + Math.pow(y - positions.to.y ,2));
  1003. if (fromDistance < 15) {
  1004. this.connectedNode = this.from;
  1005. this.from = this.controlNodes.from;
  1006. return this.controlNodes.from;
  1007. }
  1008. else if (toDistance < 15) {
  1009. this.connectedNode = this.to;
  1010. this.to = this.controlNodes.to;
  1011. return this.controlNodes.to;
  1012. }
  1013. else {
  1014. return null;
  1015. }
  1016. };
  1017. /**
  1018. * this resets the control nodes to their original position.
  1019. * @private
  1020. */
  1021. Edge.prototype._restoreControlNodes = function() {
  1022. if (this.controlNodes.from.selected == true) {
  1023. this.from = this.connectedNode;
  1024. this.connectedNode = null;
  1025. this.controlNodes.from.unselect();
  1026. }
  1027. if (this.controlNodes.to.selected == true) {
  1028. this.to = this.connectedNode;
  1029. this.connectedNode = null;
  1030. this.controlNodes.to.unselect();
  1031. }
  1032. };
  1033. /**
  1034. * this calculates the position of the control nodes on the edges of the parent nodes.
  1035. *
  1036. * @param ctx
  1037. * @returns {{from: {x: number, y: number}, to: {x: *, y: *}}}
  1038. */
  1039. Edge.prototype.getControlNodePositions = function(ctx) {
  1040. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  1041. var dx = (this.to.x - this.from.x);
  1042. var dy = (this.to.y - this.from.y);
  1043. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  1044. var fromBorderDist = this.from.distanceToBorder(ctx, angle + Math.PI);
  1045. var fromBorderPoint = (edgeSegmentLength - fromBorderDist) / edgeSegmentLength;
  1046. var xFrom = (fromBorderPoint) * this.from.x + (1 - fromBorderPoint) * this.to.x;
  1047. var yFrom = (fromBorderPoint) * this.from.y + (1 - fromBorderPoint) * this.to.y;
  1048. var via;
  1049. if (this.options.smoothCurves.dynamic == true && this.options.smoothCurves.enabled == true) {
  1050. via = this.via;
  1051. }
  1052. else if (this.options.smoothCurves.enabled == true) {
  1053. via = this._getViaCoordinates();
  1054. }
  1055. if (this.options.smoothCurves.enabled == true && via.x != null) {
  1056. angle = Math.atan2((this.to.y - via.y), (this.to.x - via.x));
  1057. dx = (this.to.x - via.x);
  1058. dy = (this.to.y - via.y);
  1059. edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  1060. }
  1061. var toBorderDist = this.to.distanceToBorder(ctx, angle);
  1062. var toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  1063. var xTo,yTo;
  1064. if (this.options.smoothCurves.enabled == true && via.x != null) {
  1065. xTo = (1 - toBorderPoint) * via.x + toBorderPoint * this.to.x;
  1066. yTo = (1 - toBorderPoint) * via.y + toBorderPoint * this.to.y;
  1067. }
  1068. else {
  1069. xTo = (1 - toBorderPoint) * this.from.x + toBorderPoint * this.to.x;
  1070. yTo = (1 - toBorderPoint) * this.from.y + toBorderPoint * this.to.y;
  1071. }
  1072. return {from:{x:xFrom,y:yFrom},to:{x:xTo,y:yTo}};
  1073. };
  1074. module.exports = Edge;