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.

462 lines
13 KiB

  1. /**
  2. * Created by Alex on 3/20/2015.
  3. */
  4. var util = require("../../../../../util")
  5. class EdgeBase {
  6. constructor(options, body, labelModule) {
  7. this.body = body;
  8. this.labelModule = labelModule;
  9. this.setOptions(options);
  10. this.colorDirty = true;
  11. }
  12. setOptions(options) {
  13. this.options = options;
  14. this.from = this.body.nodes[this.options.from];
  15. this.to = this.body.nodes[this.options.to];
  16. this.id = this.options.id;
  17. }
  18. /**
  19. * Redraw a edge as a line
  20. * Draw this edge in the given canvas
  21. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  22. * @param {CanvasRenderingContext2D} ctx
  23. * @private
  24. */
  25. drawLine(ctx, selected, hover) {
  26. // set style
  27. ctx.strokeStyle = this.getColor(ctx);
  28. ctx.lineWidth = this.getLineWidth(selected, hover);
  29. let via = undefined;
  30. if (this.from != this.to) {
  31. // draw line
  32. if (this.options.dashes.enabled == true) {
  33. via = this._drawDashedLine(ctx);
  34. }
  35. else {
  36. via = this._line(ctx);
  37. }
  38. }
  39. else {
  40. let [x,y,radius] = this._getCircleData();
  41. this._circle(ctx, x, y, radius);
  42. }
  43. return via;
  44. }
  45. _drawDashedLine(ctx) {
  46. let via = undefined;
  47. // only firefox and chrome support this method, else we use the legacy one.
  48. if (ctx.setLineDash !== undefined && this.options.dashes.altLength === undefined) {
  49. ctx.save();
  50. // configure the dash pattern
  51. var pattern = [0];
  52. if (this.options.dashes.length !== undefined && this.options.dashes.gap !== undefined) {
  53. pattern = [this.options.dashes.length, this.options.dashes.gap];
  54. }
  55. else {
  56. pattern = [5, 5];
  57. }
  58. // set dash settings for chrome or firefox
  59. ctx.setLineDash(pattern);
  60. ctx.lineDashOffset = 0;
  61. // draw the line
  62. via = this._line(ctx);
  63. // restore the dash settings.
  64. ctx.setLineDash([0]);
  65. ctx.lineDashOffset = 0;
  66. ctx.restore();
  67. }
  68. else { // unsupporting smooth lines
  69. // draw dashes line
  70. ctx.beginPath();
  71. ctx.lineCap = 'round';
  72. if (this.options.dashes.altLength !== undefined) //If an alt dash value has been set add to the array this value
  73. {
  74. ctx.dashesLine(this.from.x, this.from.y, this.to.x, this.to.y,
  75. [this.options.dashes.length, this.options.dashes.gap, this.options.dashes.altLength, this.options.dashes.gap]);
  76. }
  77. else if (this.options.dashes.length !== undefined && this.options.dashes.gap !== undefined) //If a dash and gap value has been set add to the array this value
  78. {
  79. ctx.dashesLine(this.from.x, this.from.y, this.to.x, this.to.y,
  80. [this.options.dashes.length, this.options.dashes.gap]);
  81. }
  82. else //If all else fails draw a line
  83. {
  84. ctx.moveTo(this.from.x, this.from.y);
  85. ctx.lineTo(this.to.x, this.to.y);
  86. }
  87. ctx.stroke();
  88. }
  89. return via;
  90. }
  91. findBorderPosition(nearNode, ctx, options) {
  92. if (this.from != this.to) {
  93. return this._findBorderPosition(nearNode, ctx, options);
  94. }
  95. else {
  96. return this._findBorderPositionCircle(nearNode, ctx, options);
  97. }
  98. }
  99. findBorderPositions(ctx) {
  100. let from = {};
  101. let to = {};
  102. if (this.from != this.to) {
  103. from = this._findBorderPosition(this.from, ctx);
  104. to = this._findBorderPosition(this.to, ctx);
  105. }
  106. else {
  107. let [x,y,radius] = this._getCircleData();
  108. from = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.25, high:0.6, direction:-1});
  109. to = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.6, high:0.8, direction:1});
  110. }
  111. return {from, to};
  112. }
  113. _getCircleData() {
  114. let x, y;
  115. let node = this.from;
  116. let radius = this.options.selfReferenceSize;
  117. // get circle coordinates
  118. if (node.shape.width > node.shape.height) {
  119. x = node.x + node.shape.width * 0.5;
  120. y = node.y - radius;
  121. }
  122. else {
  123. x = node.x + radius;
  124. y = node.y - node.shape.height * 0.5;
  125. }
  126. return [x,y,radius];
  127. }
  128. /**
  129. * Get a point on a circle
  130. * @param {Number} x
  131. * @param {Number} y
  132. * @param {Number} radius
  133. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  134. * @return {Object} point
  135. * @private
  136. */
  137. _pointOnCircle(x, y, radius, percentage) {
  138. var angle = percentage * 2 * Math.PI;
  139. return {
  140. x: x + radius * Math.cos(angle),
  141. y: y - radius * Math.sin(angle)
  142. }
  143. }
  144. /**
  145. * This function uses binary search to look for the point where the circle crosses the border of the node.
  146. * @param node
  147. * @param ctx
  148. * @param options
  149. * @returns {*}
  150. * @private
  151. */
  152. _findBorderPositionCircle(node, ctx, options) {
  153. let x = options.x;
  154. let y = options.y;
  155. let low = options.low;
  156. let high = options.high;
  157. let direction = options.direction;
  158. let maxIterations = 10;
  159. let iteration = 0;
  160. let radius = this.options.selfReferenceSize;
  161. let pos, angle, distanceToBorder, distanceToPoint, difference;
  162. let threshold = 0.05;
  163. let middle = (low + high) * 0.5
  164. while (low <= high && iteration < maxIterations) {
  165. middle = (low + high) * 0.5;
  166. pos = this._pointOnCircle(x, y, radius, middle);
  167. angle = Math.atan2((node.y - pos.y), (node.x - pos.x));
  168. distanceToBorder = node.distanceToBorder(ctx, angle);
  169. distanceToPoint = Math.sqrt(Math.pow(pos.x - node.x, 2) + Math.pow(pos.y - node.y, 2));
  170. difference = distanceToBorder - distanceToPoint;
  171. if (Math.abs(difference) < threshold) {
  172. break; // found
  173. }
  174. 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.
  175. if (direction > 0) {
  176. low = middle;
  177. }
  178. else {
  179. high = middle;
  180. }
  181. }
  182. else {
  183. if (direction > 0) {
  184. high = middle;
  185. }
  186. else {
  187. low = middle;
  188. }
  189. }
  190. iteration++;
  191. }
  192. pos.t = middle;
  193. return pos;
  194. }
  195. /**
  196. * Get the line width of the edge. Depends on width and whether one of the
  197. * connected nodes is selected.
  198. * @return {Number} width
  199. * @private
  200. */
  201. getLineWidth(selected, hover) {
  202. if (selected == true) {
  203. return Math.max(Math.min(this.options.widthSelectionMultiplier * this.options.width, this.options.scaling.max), 0.3 / this.body.view.scale);
  204. }
  205. else {
  206. if (hover == true) {
  207. return Math.max(Math.min(this.options.hoverWidth, this.options.scaling.max), 0.3 / this.body.view.scale);
  208. }
  209. else {
  210. return Math.max(this.options.width, 0.3 / this.body.view.scale);
  211. }
  212. }
  213. }
  214. getColor(ctx) {
  215. var colorObj = this.options.color;
  216. if (colorObj.inherit.enabled === true) {
  217. if (colorObj.inherit.useGradients == true) {
  218. var grd = ctx.createLinearGradient(this.from.x, this.from.y, this.to.x, this.to.y);
  219. var fromColor, toColor;
  220. fromColor = this.from.options.color.highlight.border;
  221. toColor = this.to.options.color.highlight.border;
  222. if (this.from.selected == false && this.to.selected == false) {
  223. fromColor = util.overrideOpacity(this.from.options.color.border, this.options.color.opacity);
  224. toColor = util.overrideOpacity(this.to.options.color.border, this.options.color.opacity);
  225. }
  226. else if (this.from.selected == true && this.to.selected == false) {
  227. toColor = this.to.options.color.border;
  228. }
  229. else if (this.from.selected == false && this.to.selected == true) {
  230. fromColor = this.from.options.color.border;
  231. }
  232. grd.addColorStop(0, fromColor);
  233. grd.addColorStop(1, toColor);
  234. // -------------------- this returns -------------------- //
  235. return grd;
  236. }
  237. if (this.colorDirty === true) {
  238. if (colorObj.inherit.source == "to") {
  239. colorObj.highlight = this.to.options.color.highlight.border;
  240. colorObj.hover = this.to.options.color.hover.border;
  241. colorObj.color = util.overrideOpacity(this.to.options.color.border, this.options.color.opacity);
  242. }
  243. else { // (this.options.color.inherit.source == "from") {
  244. colorObj.highlight = this.from.options.color.highlight.border;
  245. colorObj.hover = this.from.options.color.hover.border;
  246. colorObj.color = util.overrideOpacity(this.from.options.color.border, this.options.color.opacity);
  247. }
  248. }
  249. }
  250. // if color inherit is on and gradients are used, the function has already returned by now.
  251. this.colorDirty = false;
  252. if (this.selected == true) {
  253. return colorObj.highlight;
  254. }
  255. else if (this.hover == true) {
  256. return colorObj.hover;
  257. }
  258. else {
  259. return colorObj.color;
  260. }
  261. }
  262. /**
  263. * Draw a line from a node to itself, a circle
  264. * @param {CanvasRenderingContext2D} ctx
  265. * @param {Number} x
  266. * @param {Number} y
  267. * @param {Number} radius
  268. * @private
  269. */
  270. _circle(ctx, x, y, radius) {
  271. // draw a circle
  272. ctx.beginPath();
  273. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  274. ctx.stroke();
  275. }
  276. /**
  277. * Calculate the distance between a point (x3,y3) and a line segment from
  278. * (x1,y1) to (x2,y2).
  279. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  280. * @param {number} x1
  281. * @param {number} y1
  282. * @param {number} x2
  283. * @param {number} y2
  284. * @param {number} x3
  285. * @param {number} y3
  286. * @private
  287. */
  288. getDistanceToEdge(x1, y1, x2, y2, x3, y3, via) { // x3,y3 is the point
  289. var returnValue = 0;
  290. if (this.from != this.to) {
  291. returnValue = this._getDistanceToEdge(x1, y1, x2, y2, x3, y3, via)
  292. }
  293. else {
  294. let [x,y,radius] = this._getCircleData();
  295. let dx = x - x3;
  296. let dy = y - y3;
  297. returnValue = Math.abs(Math.sqrt(dx * dx + dy * dy) - radius);
  298. }
  299. if (this.labelModule.size.left < x3 &&
  300. this.labelModule.size.left + this.labelModule.size.width > x3 &&
  301. this.labelModule.size.top < y3 &&
  302. this.labelModule.size.top + this.labelModule.size.height > y3) {
  303. return 0;
  304. }
  305. else {
  306. return returnValue;
  307. }
  308. }
  309. _getDistanceToLine(x1, y1, x2, y2, x3, y3) {
  310. var px = x2 - x1;
  311. var py = y2 - y1;
  312. var something = px * px + py * py;
  313. var u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  314. if (u > 1) {
  315. u = 1;
  316. }
  317. else if (u < 0) {
  318. u = 0;
  319. }
  320. var x = x1 + u * px;
  321. var y = y1 + u * py;
  322. var dx = x - x3;
  323. var dy = y - y3;
  324. //# Note: If the actual distance does not matter,
  325. //# if you only want to compare what this function
  326. //# returns to other results of this function, you
  327. //# can just return the squared distance instead
  328. //# (i.e. remove the sqrt) to gain a little performance
  329. return Math.sqrt(dx * dx + dy * dy);
  330. }
  331. /**
  332. *
  333. * @param ctx
  334. * @param position
  335. * @param viaNode
  336. */
  337. drawArrowHead(ctx, position, viaNode, selected, hover) {
  338. // set style
  339. ctx.strokeStyle = this.getColor(ctx);
  340. ctx.fillStyle = ctx.strokeStyle;
  341. ctx.lineWidth = this.getLineWidth(selected, hover);
  342. // set lets
  343. let angle;
  344. let length;
  345. let arrowPos;
  346. let node1;
  347. let node2;
  348. let guideOffset;
  349. let scaleFactor;
  350. if (position == 'from') {
  351. node1 = this.from;
  352. node2 = this.to;
  353. guideOffset = 0.1;
  354. scaleFactor = this.options.arrows.from.scaleFactor;
  355. }
  356. else if (position == 'to') {
  357. node1 = this.to;
  358. node2 = this.from;
  359. guideOffset = -0.1;
  360. scaleFactor = this.options.arrows.to.scaleFactor;
  361. }
  362. else {
  363. node1 = this.to;
  364. node2 = this.from;
  365. scaleFactor = this.options.arrows.middle.scaleFactor;
  366. }
  367. // if not connected to itself
  368. if (node1 != node2) {
  369. if (position !== 'middle') {
  370. // draw arrow head
  371. if (this.options.smooth.enabled == true) {
  372. arrowPos = this.findBorderPosition(node1, ctx, {via: viaNode});
  373. let guidePos = this.getPoint(Math.max(0.0, Math.min(1.0, arrowPos.t + guideOffset)), viaNode);
  374. angle = Math.atan2((arrowPos.y - guidePos.y), (arrowPos.x - guidePos.x));
  375. }
  376. else {
  377. angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  378. arrowPos = this.findBorderPosition(node1, ctx);
  379. }
  380. }
  381. else {
  382. angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  383. arrowPos = this.getPoint(0.6, viaNode); // this is 0.6 to account for the size of the arrow.
  384. }
  385. // draw arrow at the end of the line
  386. length = (10 + 5 * this.options.width) * scaleFactor;
  387. ctx.arrow(arrowPos.x, arrowPos.y, angle, length);
  388. ctx.fill();
  389. ctx.stroke();
  390. }
  391. else {
  392. // draw circle
  393. let angle, point;
  394. let [x,y,radius] = this._getCircleData();
  395. if (position == 'from') {
  396. point = this.findBorderPosition(this.from, ctx, {x, y, low:0.25, high:0.6, direction:-1});
  397. angle = point.t * -2 * Math.PI + 1.5 * Math.PI + 0.1 * Math.PI;
  398. }
  399. else if (position == 'to') {
  400. point = this.findBorderPosition(this.from, ctx, {x, y, low:0.6, high:1.0, direction:1});
  401. angle = point.t * -2 * Math.PI + 1.5 * Math.PI - 1.1 * Math.PI;
  402. }
  403. else {
  404. point = this._pointOnCircle(x, y, radius, 0.175);
  405. angle = 3.9269908169872414; // == 0.175 * -2 * Math.PI + 1.5 * Math.PI + 0.1 * Math.PI;
  406. }
  407. // draw the arrowhead
  408. let length = (10 + 5 * this.options.width) * scaleFactor;
  409. ctx.arrow(point.x, point.y, angle, length);
  410. ctx.fill();
  411. ctx.stroke();
  412. }
  413. }
  414. }
  415. export default EdgeBase;