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.

1367 lines
41 KiB

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