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.

472 lines
14 KiB

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