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.

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