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.

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