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.

1187 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.fillStyle = ctx.strokeStyle;
  657. ctx.lineWidth = this._getLineWidth();
  658. if (this.from != this.to) {
  659. // draw line
  660. var via = this._line(ctx);
  661. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  662. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  663. // draw an arrow halfway the line
  664. if (this.options.smoothCurves.enabled == true && via != null) {
  665. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  666. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  667. point = {x:midpointX, y:midpointY};
  668. }
  669. else {
  670. point = this._pointOnLine(0.5);
  671. }
  672. ctx.arrow(point.x, point.y, angle, length);
  673. ctx.fill();
  674. ctx.stroke();
  675. // draw label
  676. if (this.label) {
  677. this._label(ctx, this.label, point.x, point.y);
  678. }
  679. }
  680. else {
  681. // draw circle
  682. var x, y;
  683. var radius = 0.25 * Math.max(100,this.physics.springLength);
  684. var node = this.from;
  685. if (!node.width) {
  686. node.resize(ctx);
  687. }
  688. if (node.width > node.height) {
  689. x = node.x + node.width * 0.5;
  690. y = node.y - radius;
  691. }
  692. else {
  693. x = node.x + radius;
  694. y = node.y - node.height * 0.5;
  695. }
  696. this._circle(ctx, x, y, radius);
  697. // draw all arrows
  698. var angle = 0.2 * Math.PI;
  699. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  700. point = this._pointOnCircle(x, y, radius, 0.5);
  701. ctx.arrow(point.x, point.y, angle, length);
  702. ctx.fill();
  703. ctx.stroke();
  704. // draw label
  705. if (this.label) {
  706. point = this._pointOnCircle(x, y, radius, 0.5);
  707. this._label(ctx, this.label, point.x, point.y);
  708. }
  709. }
  710. };
  711. /**
  712. * Redraw a edge as a line with an arrow
  713. * Draw this edge in the given canvas
  714. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  715. * @param {CanvasRenderingContext2D} ctx
  716. * @private
  717. */
  718. Edge.prototype._drawArrow = function(ctx) {
  719. // set style
  720. ctx.strokeStyle = this._getColor();
  721. ctx.fillStyle = ctx.strokeStyle;
  722. ctx.lineWidth = this._getLineWidth();
  723. var angle, length;
  724. //draw a line
  725. if (this.from != this.to) {
  726. angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  727. var dx = (this.to.x - this.from.x);
  728. var dy = (this.to.y - this.from.y);
  729. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  730. var fromBorderDist = this.from.distanceToBorder(ctx, angle + Math.PI);
  731. var fromBorderPoint = (edgeSegmentLength - fromBorderDist) / edgeSegmentLength;
  732. var xFrom = (fromBorderPoint) * this.from.x + (1 - fromBorderPoint) * this.to.x;
  733. var yFrom = (fromBorderPoint) * this.from.y + (1 - fromBorderPoint) * this.to.y;
  734. var via;
  735. if (this.options.smoothCurves.dynamic == true && this.options.smoothCurves.enabled == true ) {
  736. via = this.via;
  737. }
  738. else if (this.options.smoothCurves.enabled == true) {
  739. via = this._getViaCoordinates();
  740. }
  741. if (this.options.smoothCurves.enabled == true && via.x != null) {
  742. angle = Math.atan2((this.to.y - via.y), (this.to.x - via.x));
  743. dx = (this.to.x - via.x);
  744. dy = (this.to.y - via.y);
  745. edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  746. }
  747. var toBorderDist = this.to.distanceToBorder(ctx, angle);
  748. var toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  749. var xTo,yTo;
  750. if (this.options.smoothCurves.enabled == true && via.x != null) {
  751. xTo = (1 - toBorderPoint) * via.x + toBorderPoint * this.to.x;
  752. yTo = (1 - toBorderPoint) * via.y + toBorderPoint * this.to.y;
  753. }
  754. else {
  755. xTo = (1 - toBorderPoint) * this.from.x + toBorderPoint * this.to.x;
  756. yTo = (1 - toBorderPoint) * this.from.y + toBorderPoint * this.to.y;
  757. }
  758. ctx.beginPath();
  759. ctx.moveTo(xFrom,yFrom);
  760. if (this.options.smoothCurves.enabled == true && via.x != null) {
  761. ctx.quadraticCurveTo(via.x,via.y,xTo, yTo);
  762. }
  763. else {
  764. ctx.lineTo(xTo, yTo);
  765. }
  766. ctx.stroke();
  767. // draw arrow at the end of the line
  768. length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  769. ctx.arrow(xTo, yTo, angle, length);
  770. ctx.fill();
  771. ctx.stroke();
  772. // draw label
  773. if (this.label) {
  774. var point;
  775. if (this.options.smoothCurves.enabled == true && via != null) {
  776. var midpointX = 0.5*(0.5*(this.from.x + via.x) + 0.5*(this.to.x + via.x));
  777. var midpointY = 0.5*(0.5*(this.from.y + via.y) + 0.5*(this.to.y + via.y));
  778. point = {x:midpointX, y:midpointY};
  779. }
  780. else {
  781. point = this._pointOnLine(0.5);
  782. }
  783. this._label(ctx, this.label, point.x, point.y);
  784. }
  785. }
  786. else {
  787. // draw circle
  788. var node = this.from;
  789. var x, y, arrow;
  790. var radius = 0.25 * Math.max(100,this.physics.springLength);
  791. if (!node.width) {
  792. node.resize(ctx);
  793. }
  794. if (node.width > node.height) {
  795. x = node.x + node.width * 0.5;
  796. y = node.y - radius;
  797. arrow = {
  798. x: x,
  799. y: node.y,
  800. angle: 0.9 * Math.PI
  801. };
  802. }
  803. else {
  804. x = node.x + radius;
  805. y = node.y - node.height * 0.5;
  806. arrow = {
  807. x: node.x,
  808. y: y,
  809. angle: 0.6 * Math.PI
  810. };
  811. }
  812. ctx.beginPath();
  813. // TODO: similarly, for a line without arrows, draw to the border of the nodes instead of the center
  814. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  815. ctx.stroke();
  816. // draw all arrows
  817. var length = (10 + 5 * this.options.width) * this.options.arrowScaleFactor;
  818. ctx.arrow(arrow.x, arrow.y, arrow.angle, length);
  819. ctx.fill();
  820. ctx.stroke();
  821. // draw label
  822. if (this.label) {
  823. point = this._pointOnCircle(x, y, radius, 0.5);
  824. this._label(ctx, this.label, point.x, point.y);
  825. }
  826. }
  827. };
  828. /**
  829. * Calculate the distance between a point (x3,y3) and a line segment from
  830. * (x1,y1) to (x2,y2).
  831. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  832. * @param {number} x1
  833. * @param {number} y1
  834. * @param {number} x2
  835. * @param {number} y2
  836. * @param {number} x3
  837. * @param {number} y3
  838. * @private
  839. */
  840. Edge.prototype._getDistanceToEdge = function (x1,y1, x2,y2, x3,y3) { // x3,y3 is the point
  841. var returnValue = 0;
  842. if (this.from != this.to) {
  843. if (this.options.smoothCurves.enabled == true) {
  844. var xVia, yVia;
  845. if (this.options.smoothCurves.enabled == true && this.options.smoothCurves.dynamic == true) {
  846. xVia = this.via.x;
  847. yVia = this.via.y;
  848. }
  849. else {
  850. var via = this._getViaCoordinates();
  851. xVia = via.x;
  852. yVia = via.y;
  853. }
  854. var minDistance = 1e9;
  855. var distance;
  856. var i,t,x,y, lastX, lastY;
  857. for (i = 0; i < 10; i++) {
  858. t = 0.1*i;
  859. x = Math.pow(1-t,2)*x1 + (2*t*(1 - t))*xVia + Math.pow(t,2)*x2;
  860. y = Math.pow(1-t,2)*y1 + (2*t*(1 - t))*yVia + Math.pow(t,2)*y2;
  861. if (i > 0) {
  862. distance = this._getDistanceToLine(lastX,lastY,x,y, x3,y3);
  863. minDistance = distance < minDistance ? distance : minDistance;
  864. }
  865. lastX = x; lastY = y;
  866. }
  867. returnValue = minDistance;
  868. }
  869. else {
  870. returnValue = this._getDistanceToLine(x1,y1,x2,y2,x3,y3);
  871. }
  872. }
  873. else {
  874. var x, y, dx, dy;
  875. var radius = 0.25 * this.physics.springLength;
  876. var node = this.from;
  877. if (node.width > node.height) {
  878. x = node.x + 0.5 * node.width;
  879. y = node.y - radius;
  880. }
  881. else {
  882. x = node.x + radius;
  883. y = node.y - 0.5 * node.height;
  884. }
  885. dx = x - x3;
  886. dy = y - y3;
  887. returnValue = Math.abs(Math.sqrt(dx*dx + dy*dy) - radius);
  888. }
  889. if (this.labelDimensions.left < x3 &&
  890. this.labelDimensions.left + this.labelDimensions.width > x3 &&
  891. this.labelDimensions.top < y3 &&
  892. this.labelDimensions.top + this.labelDimensions.height > y3) {
  893. return 0;
  894. }
  895. else {
  896. return returnValue;
  897. }
  898. };
  899. Edge.prototype._getDistanceToLine = function(x1,y1,x2,y2,x3,y3) {
  900. var px = x2-x1,
  901. py = y2-y1,
  902. something = px*px + py*py,
  903. u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  904. if (u > 1) {
  905. u = 1;
  906. }
  907. else if (u < 0) {
  908. u = 0;
  909. }
  910. var x = x1 + u * px,
  911. y = y1 + u * py,
  912. dx = x - x3,
  913. dy = y - y3;
  914. //# Note: If the actual distance does not matter,
  915. //# if you only want to compare what this function
  916. //# returns to other results of this function, you
  917. //# can just return the squared distance instead
  918. //# (i.e. remove the sqrt) to gain a little performance
  919. return Math.sqrt(dx*dx + dy*dy);
  920. };
  921. /**
  922. * This allows the zoom level of the network to influence the rendering
  923. *
  924. * @param scale
  925. */
  926. Edge.prototype.setScale = function(scale) {
  927. this.networkScaleInv = 1.0/scale;
  928. };
  929. Edge.prototype.select = function() {
  930. this.selected = true;
  931. };
  932. Edge.prototype.unselect = function() {
  933. this.selected = false;
  934. };
  935. Edge.prototype.positionBezierNode = function() {
  936. if (this.via !== null && this.from !== null && this.to !== null) {
  937. this.via.x = 0.5 * (this.from.x + this.to.x);
  938. this.via.y = 0.5 * (this.from.y + this.to.y);
  939. }
  940. };
  941. /**
  942. * This function draws the control nodes for the manipulator. In order to enable this, only set the this.controlNodesEnabled to true.
  943. * @param ctx
  944. */
  945. Edge.prototype._drawControlNodes = function(ctx) {
  946. if (this.controlNodesEnabled == true) {
  947. if (this.controlNodes.from === null && this.controlNodes.to === null) {
  948. var nodeIdFrom = "edgeIdFrom:".concat(this.id);
  949. var nodeIdTo = "edgeIdTo:".concat(this.id);
  950. var constants = {
  951. nodes:{group:'', radius:8},
  952. physics:{damping:0},
  953. clustering: {maxNodeSizeIncrements: 0 ,nodeScaling: {width:0, height: 0, radius:0}}
  954. };
  955. this.controlNodes.from = new Node(
  956. {id:nodeIdFrom,
  957. shape:'dot',
  958. color:{background:'#ff4e00', border:'#3c3c3c', highlight: {background:'#07f968'}}
  959. },{},{},constants);
  960. this.controlNodes.to = new Node(
  961. {id:nodeIdTo,
  962. shape:'dot',
  963. color:{background:'#ff4e00', border:'#3c3c3c', highlight: {background:'#07f968'}}
  964. },{},{},constants);
  965. }
  966. if (this.controlNodes.from.selected == false && this.controlNodes.to.selected == false) {
  967. this.controlNodes.positions = this.getControlNodePositions(ctx);
  968. this.controlNodes.from.x = this.controlNodes.positions.from.x;
  969. this.controlNodes.from.y = this.controlNodes.positions.from.y;
  970. this.controlNodes.to.x = this.controlNodes.positions.to.x;
  971. this.controlNodes.to.y = this.controlNodes.positions.to.y;
  972. }
  973. this.controlNodes.from.draw(ctx);
  974. this.controlNodes.to.draw(ctx);
  975. }
  976. else {
  977. this.controlNodes = {from:null, to:null, positions:{}};
  978. }
  979. };
  980. /**
  981. * Enable control nodes.
  982. * @private
  983. */
  984. Edge.prototype._enableControlNodes = function() {
  985. this.controlNodesEnabled = true;
  986. };
  987. /**
  988. * disable control nodes
  989. * @private
  990. */
  991. Edge.prototype._disableControlNodes = function() {
  992. this.controlNodesEnabled = false;
  993. };
  994. /**
  995. * This checks if one of the control nodes is selected and if so, returns the control node object. Else it returns null.
  996. * @param x
  997. * @param y
  998. * @returns {null}
  999. * @private
  1000. */
  1001. Edge.prototype._getSelectedControlNode = function(x,y) {
  1002. var positions = this.controlNodes.positions;
  1003. var fromDistance = Math.sqrt(Math.pow(x - positions.from.x,2) + Math.pow(y - positions.from.y,2));
  1004. var toDistance = Math.sqrt(Math.pow(x - positions.to.x ,2) + Math.pow(y - positions.to.y ,2));
  1005. if (fromDistance < 15) {
  1006. this.connectedNode = this.from;
  1007. this.from = this.controlNodes.from;
  1008. return this.controlNodes.from;
  1009. }
  1010. else if (toDistance < 15) {
  1011. this.connectedNode = this.to;
  1012. this.to = this.controlNodes.to;
  1013. return this.controlNodes.to;
  1014. }
  1015. else {
  1016. return null;
  1017. }
  1018. };
  1019. /**
  1020. * this resets the control nodes to their original position.
  1021. * @private
  1022. */
  1023. Edge.prototype._restoreControlNodes = function() {
  1024. if (this.controlNodes.from.selected == true) {
  1025. this.from = this.connectedNode;
  1026. this.connectedNode = null;
  1027. this.controlNodes.from.unselect();
  1028. }
  1029. if (this.controlNodes.to.selected == true) {
  1030. this.to = this.connectedNode;
  1031. this.connectedNode = null;
  1032. this.controlNodes.to.unselect();
  1033. }
  1034. };
  1035. /**
  1036. * this calculates the position of the control nodes on the edges of the parent nodes.
  1037. *
  1038. * @param ctx
  1039. * @returns {{from: {x: number, y: number}, to: {x: *, y: *}}}
  1040. */
  1041. Edge.prototype.getControlNodePositions = function(ctx) {
  1042. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  1043. var dx = (this.to.x - this.from.x);
  1044. var dy = (this.to.y - this.from.y);
  1045. var edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  1046. var fromBorderDist = this.from.distanceToBorder(ctx, angle + Math.PI);
  1047. var fromBorderPoint = (edgeSegmentLength - fromBorderDist) / edgeSegmentLength;
  1048. var xFrom = (fromBorderPoint) * this.from.x + (1 - fromBorderPoint) * this.to.x;
  1049. var yFrom = (fromBorderPoint) * this.from.y + (1 - fromBorderPoint) * this.to.y;
  1050. var via;
  1051. if (this.options.smoothCurves.dynamic == true && this.options.smoothCurves.enabled == true) {
  1052. via = this.via;
  1053. }
  1054. else if (this.options.smoothCurves.enabled == true) {
  1055. via = this._getViaCoordinates();
  1056. }
  1057. if (this.options.smoothCurves.enabled == true && via.x != null) {
  1058. angle = Math.atan2((this.to.y - via.y), (this.to.x - via.x));
  1059. dx = (this.to.x - via.x);
  1060. dy = (this.to.y - via.y);
  1061. edgeSegmentLength = Math.sqrt(dx * dx + dy * dy);
  1062. }
  1063. var toBorderDist = this.to.distanceToBorder(ctx, angle);
  1064. var toBorderPoint = (edgeSegmentLength - toBorderDist) / edgeSegmentLength;
  1065. var xTo,yTo;
  1066. if (this.options.smoothCurves.enabled == true && via.x != null) {
  1067. xTo = (1 - toBorderPoint) * via.x + toBorderPoint * this.to.x;
  1068. yTo = (1 - toBorderPoint) * via.y + toBorderPoint * this.to.y;
  1069. }
  1070. else {
  1071. xTo = (1 - toBorderPoint) * this.from.x + toBorderPoint * this.to.x;
  1072. yTo = (1 - toBorderPoint) * this.from.y + toBorderPoint * this.to.y;
  1073. }
  1074. return {from:{x:xFrom,y:yFrom},to:{x:xTo,y:yTo}};
  1075. };
  1076. module.exports = Edge;