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.

515 lines
15 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. // draw shadow if enabled
  86. this.enableShadow(ctx);
  87. ctx.stroke();
  88. // disable shadows for other elements.
  89. this.disableShadow(ctx);
  90. }
  91. return via;
  92. }
  93. findBorderPosition(nearNode, ctx, options) {
  94. if (this.from != this.to) {
  95. return this._findBorderPosition(nearNode, ctx, options);
  96. }
  97. else {
  98. return this._findBorderPositionCircle(nearNode, ctx, options);
  99. }
  100. }
  101. findBorderPositions(ctx) {
  102. let from = {};
  103. let to = {};
  104. if (this.from != this.to) {
  105. from = this._findBorderPosition(this.from, ctx);
  106. to = this._findBorderPosition(this.to, ctx);
  107. }
  108. else {
  109. let [x,y,radius] = this._getCircleData(ctx);
  110. from = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.25, high:0.6, direction:-1});
  111. to = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.6, high:0.8, direction:1});
  112. }
  113. return {from, to};
  114. }
  115. _getCircleData(ctx) {
  116. let x, y;
  117. let node = this.from;
  118. let radius = this.options.selfReferenceSize;
  119. if (ctx !== undefined) {
  120. if (node.shape.width === undefined) {
  121. node.shape.resize(ctx);
  122. }
  123. }
  124. // get circle coordinates
  125. if (node.shape.width > node.shape.height) {
  126. x = node.x + node.shape.width * 0.5;
  127. y = node.y - radius;
  128. }
  129. else {
  130. x = node.x + radius;
  131. y = node.y - node.shape.height * 0.5;
  132. }
  133. return [x,y,radius];
  134. }
  135. /**
  136. * Get a point on a circle
  137. * @param {Number} x
  138. * @param {Number} y
  139. * @param {Number} radius
  140. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  141. * @return {Object} point
  142. * @private
  143. */
  144. _pointOnCircle(x, y, radius, percentage) {
  145. let angle = percentage * 2 * Math.PI;
  146. return {
  147. x: x + radius * Math.cos(angle),
  148. y: y - radius * Math.sin(angle)
  149. }
  150. }
  151. /**
  152. * This function uses binary search to look for the point where the circle crosses the border of the node.
  153. * @param node
  154. * @param ctx
  155. * @param options
  156. * @returns {*}
  157. * @private
  158. */
  159. _findBorderPositionCircle(node, ctx, options) {
  160. let x = options.x;
  161. let y = options.y;
  162. let low = options.low;
  163. let high = options.high;
  164. let direction = options.direction;
  165. let maxIterations = 10;
  166. let iteration = 0;
  167. let radius = this.options.selfReferenceSize;
  168. let pos, angle, distanceToBorder, distanceToPoint, difference;
  169. let threshold = 0.05;
  170. let middle = (low + high) * 0.5
  171. while (low <= high && iteration < maxIterations) {
  172. middle = (low + high) * 0.5;
  173. pos = this._pointOnCircle(x, y, radius, middle);
  174. angle = Math.atan2((node.y - pos.y), (node.x - pos.x));
  175. distanceToBorder = node.distanceToBorder(ctx, angle);
  176. distanceToPoint = Math.sqrt(Math.pow(pos.x - node.x, 2) + Math.pow(pos.y - node.y, 2));
  177. difference = distanceToBorder - distanceToPoint;
  178. if (Math.abs(difference) < threshold) {
  179. break; // found
  180. }
  181. 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.
  182. if (direction > 0) {
  183. low = middle;
  184. }
  185. else {
  186. high = middle;
  187. }
  188. }
  189. else {
  190. if (direction > 0) {
  191. high = middle;
  192. }
  193. else {
  194. low = middle;
  195. }
  196. }
  197. iteration++;
  198. }
  199. pos.t = middle;
  200. return pos;
  201. }
  202. /**
  203. * Get the line width of the edge. Depends on width and whether one of the
  204. * connected nodes is selected.
  205. * @return {Number} width
  206. * @private
  207. */
  208. getLineWidth(selected, hover) {
  209. if (selected === true) {
  210. return Math.max(Math.min(this.options.widthSelectionMultiplier * this.options.width, this.options.scaling.max), 0.3 / this.body.view.scale);
  211. }
  212. else {
  213. if (hover === true) {
  214. return Math.max(Math.min(this.options.hoverWidth, this.options.scaling.max), 0.3 / this.body.view.scale);
  215. }
  216. else {
  217. return Math.max(this.options.width, 0.3 / this.body.view.scale);
  218. }
  219. }
  220. }
  221. getColor(ctx) {
  222. let colorOptions = this.options.color;
  223. if (colorOptions.inherit !== false) {
  224. // when this is a loop edge, just use the 'from' method
  225. if (colorOptions.inherit === 'both' && this.from.id !== this.to.id) {
  226. let grd = ctx.createLinearGradient(this.from.x, this.from.y, this.to.x, this.to.y);
  227. let fromColor, toColor;
  228. fromColor = this.from.options.color.highlight.border;
  229. toColor = this.to.options.color.highlight.border;
  230. if (this.from.selected === false && this.to.selected === false) {
  231. fromColor = util.overrideOpacity(this.from.options.color.border, this.options.color.opacity);
  232. toColor = util.overrideOpacity(this.to.options.color.border, this.options.color.opacity);
  233. }
  234. else if (this.from.selected === true && this.to.selected === false) {
  235. toColor = this.to.options.color.border;
  236. }
  237. else if (this.from.selected === false && this.to.selected === true) {
  238. fromColor = this.from.options.color.border;
  239. }
  240. grd.addColorStop(0, fromColor);
  241. grd.addColorStop(1, toColor);
  242. // -------------------- this returns -------------------- //
  243. return grd;
  244. }
  245. if (this.colorDirty === true) {
  246. if (colorOptions.inherit === "to") {
  247. this.color.highlight = this.to.options.color.highlight.border;
  248. this.color.hover = this.to.options.color.hover.border;
  249. this.color.color = util.overrideOpacity(this.to.options.color.border, colorOptions.opacity);
  250. }
  251. else { // (this.options.color.inherit.source === "from") {
  252. this.color.highlight = this.from.options.color.highlight.border;
  253. this.color.hover = this.from.options.color.hover.border;
  254. this.color.color = util.overrideOpacity(this.from.options.color.border, colorOptions.opacity);
  255. }
  256. }
  257. }
  258. else if (this.colorDirty === true) {
  259. this.color.highlight = colorOptions.highlight;
  260. this.color.hover = colorOptions.hover;
  261. this.color.color = util.overrideOpacity(colorOptions.color, colorOptions.opacity);
  262. }
  263. // if color inherit is on and gradients are used, the function has already returned by now.
  264. this.colorDirty = false;
  265. if (this.selected === true) {
  266. return this.color.highlight;
  267. }
  268. else if (this.hover === true) {
  269. return this.color.hover;
  270. }
  271. else {
  272. return this.color.color;
  273. }
  274. }
  275. /**
  276. * Draw a line from a node to itself, a circle
  277. * @param {CanvasRenderingContext2D} ctx
  278. * @param {Number} x
  279. * @param {Number} y
  280. * @param {Number} radius
  281. * @private
  282. */
  283. _circle(ctx, x, y, radius) {
  284. // draw shadow if enabled
  285. this.enableShadow(ctx);
  286. // draw a circle
  287. ctx.beginPath();
  288. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  289. ctx.stroke();
  290. // disable shadows for other elements.
  291. this.disableShadow(ctx);
  292. }
  293. /**
  294. * Calculate the distance between a point (x3,y3) and a line segment from
  295. * (x1,y1) to (x2,y2).
  296. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  297. * @param {number} x1
  298. * @param {number} y1
  299. * @param {number} x2
  300. * @param {number} y2
  301. * @param {number} x3
  302. * @param {number} y3
  303. * @private
  304. */
  305. getDistanceToEdge(x1, y1, x2, y2, x3, y3, via) { // x3,y3 is the point
  306. let returnValue = 0;
  307. if (this.from != this.to) {
  308. returnValue = this._getDistanceToEdge(x1, y1, x2, y2, x3, y3, via)
  309. }
  310. else {
  311. let [x,y,radius] = this._getCircleData();
  312. let dx = x - x3;
  313. let dy = y - y3;
  314. returnValue = Math.abs(Math.sqrt(dx * dx + dy * dy) - radius);
  315. }
  316. if (this.labelModule.size.left < x3 &&
  317. this.labelModule.size.left + this.labelModule.size.width > x3 &&
  318. this.labelModule.size.top < y3 &&
  319. this.labelModule.size.top + this.labelModule.size.height > y3) {
  320. return 0;
  321. }
  322. else {
  323. return returnValue;
  324. }
  325. }
  326. _getDistanceToLine(x1, y1, x2, y2, x3, y3) {
  327. let px = x2 - x1;
  328. let py = y2 - y1;
  329. let something = px * px + py * py;
  330. let u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  331. if (u > 1) {
  332. u = 1;
  333. }
  334. else if (u < 0) {
  335. u = 0;
  336. }
  337. let x = x1 + u * px;
  338. let y = y1 + u * py;
  339. let dx = x - x3;
  340. let dy = y - y3;
  341. //# Note: If the actual distance does not matter,
  342. //# if you only want to compare what this function
  343. //# returns to other results of this function, you
  344. //# can just return the squared distance instead
  345. //# (i.e. remove the sqrt) to gain a little performance
  346. return Math.sqrt(dx * dx + dy * dy);
  347. }
  348. /**
  349. *
  350. * @param ctx
  351. * @param position
  352. * @param viaNode
  353. */
  354. drawArrowHead(ctx, position, viaNode, selected, hover) {
  355. // set style
  356. ctx.strokeStyle = this.getColor(ctx);
  357. ctx.fillStyle = ctx.strokeStyle;
  358. ctx.lineWidth = this.getLineWidth(selected, hover);
  359. // set lets
  360. let angle;
  361. let length;
  362. let arrowPos;
  363. let node1;
  364. let node2;
  365. let guideOffset;
  366. let scaleFactor;
  367. if (position === 'from') {
  368. node1 = this.from;
  369. node2 = this.to;
  370. guideOffset = 0.1;
  371. scaleFactor = this.options.arrows.from.scaleFactor;
  372. }
  373. else if (position === 'to') {
  374. node1 = this.to;
  375. node2 = this.from;
  376. guideOffset = -0.1;
  377. scaleFactor = this.options.arrows.to.scaleFactor;
  378. }
  379. else {
  380. node1 = this.to;
  381. node2 = this.from;
  382. scaleFactor = this.options.arrows.middle.scaleFactor;
  383. }
  384. // if not connected to itself
  385. if (node1 != node2) {
  386. if (position !== 'middle') {
  387. // draw arrow head
  388. if (this.options.smooth.enabled === true) {
  389. arrowPos = this.findBorderPosition(node1, ctx, {via: viaNode});
  390. let guidePos = this.getPoint(Math.max(0.0, Math.min(1.0, arrowPos.t + guideOffset)), viaNode);
  391. angle = Math.atan2((arrowPos.y - guidePos.y), (arrowPos.x - guidePos.x));
  392. }
  393. else {
  394. angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  395. arrowPos = this.findBorderPosition(node1, ctx);
  396. }
  397. }
  398. else {
  399. angle = Math.atan2((node1.y - node2.y), (node1.x - node2.x));
  400. arrowPos = this.getPoint(0.6, viaNode); // this is 0.6 to account for the size of the arrow.
  401. }
  402. // draw arrow at the end of the line
  403. length = (10 + 5 * this.options.width) * scaleFactor;
  404. ctx.arrow(arrowPos.x, arrowPos.y, angle, length);
  405. // draw shadow if enabled
  406. this.enableShadow(ctx);
  407. ctx.fill();
  408. // disable shadows for other elements.
  409. this.disableShadow(ctx);
  410. ctx.stroke();
  411. }
  412. else {
  413. // draw circle
  414. let angle, point;
  415. let [x,y,radius] = this._getCircleData(ctx);
  416. if (position === 'from') {
  417. point = this.findBorderPosition(this.from, ctx, {x, y, low:0.25, high:0.6, direction:-1});
  418. angle = point.t * -2 * Math.PI + 1.5 * Math.PI + 0.1 * Math.PI;
  419. }
  420. else if (position === 'to') {
  421. point = this.findBorderPosition(this.from, ctx, {x, y, low:0.6, high:1.0, direction:1});
  422. angle = point.t * -2 * Math.PI + 1.5 * Math.PI - 1.1 * Math.PI;
  423. }
  424. else {
  425. point = this._pointOnCircle(x, y, radius, 0.175);
  426. angle = 3.9269908169872414; // === 0.175 * -2 * Math.PI + 1.5 * Math.PI + 0.1 * Math.PI;
  427. }
  428. // draw the arrowhead
  429. let length = (10 + 5 * this.options.width) * scaleFactor;
  430. ctx.arrow(point.x, point.y, angle, length);
  431. // draw shadow if enabled
  432. this.enableShadow(ctx);
  433. ctx.fill();
  434. // disable shadows for other elements.
  435. this.disableShadow(ctx);
  436. ctx.stroke();
  437. }
  438. }
  439. enableShadow(ctx) {
  440. if (this.options.shadow.enabled === true) {
  441. ctx.shadowColor = 'rgba(0,0,0,0.5)';
  442. ctx.shadowBlur = this.options.shadow.size;
  443. ctx.shadowOffsetX = this.options.shadow.x;
  444. ctx.shadowOffsetY = this.options.shadow.y;
  445. }
  446. }
  447. disableShadow(ctx) {
  448. if (this.options.shadow.enabled === true) {
  449. ctx.shadowColor = 'rgba(0,0,0,0)';
  450. ctx.shadowBlur = 0;
  451. ctx.shadowOffsetX = 0;
  452. ctx.shadowOffsetY = 0;
  453. }
  454. }
  455. }
  456. export default EdgeBase;