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.

1181 lines
35 KiB

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