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.

350 lines
10 KiB

  1. /**
  2. * Created by Alex on 3/20/2015.
  3. */
  4. var util = require("../../../../../util")
  5. class BaseEdge {
  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();
  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;
  41. let radius = this.options.selfReferenceSize;
  42. let node = this.from;
  43. node.resize(ctx);
  44. if (node.shape.width > node.shape.height) {
  45. x = node.x + node.shape.width * 0.5;
  46. y = node.y - radius;
  47. }
  48. else {
  49. x = node.x + radius;
  50. y = node.y - node.shape.height * 0.5;
  51. }
  52. this._circle(ctx, x, y, radius);
  53. }
  54. return via;
  55. }
  56. _drawDashedLine(ctx) {
  57. let via = undefined;
  58. // only firefox and chrome support this method, else we use the legacy one.
  59. if (ctx.setLineDash !== undefined) {
  60. ctx.save();
  61. // configure the dash pattern
  62. var pattern = [0];
  63. if (this.options.dashes.length !== undefined && this.options.dashes.gap !== undefined) {
  64. pattern = [this.options.dashes.length, this.options.dashes.gap];
  65. }
  66. else {
  67. pattern = [5, 5];
  68. }
  69. // set dash settings for chrome or firefox
  70. ctx.setLineDash(pattern);
  71. ctx.lineDashOffset = 0;
  72. // draw the line
  73. via = this._line(ctx);
  74. // restore the dash settings.
  75. ctx.setLineDash([0]);
  76. ctx.lineDashOffset = 0;
  77. ctx.restore();
  78. }
  79. else { // unsupporting smooth lines
  80. // draw dashes line
  81. ctx.beginPath();
  82. ctx.lineCap = 'round';
  83. if (this.options.dashes.altLength !== undefined) //If an alt dash value has been set add to the array this value
  84. {
  85. ctx.dashesLine(this.from.x, this.from.y, this.to.x, this.to.y,
  86. [this.options.dashes.length, this.options.dashes.gap, this.options.dashes.altLength, this.options.dashes.gap]);
  87. }
  88. 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
  89. {
  90. ctx.dashesLine(this.from.x, this.from.y, this.to.x, this.to.y,
  91. [this.options.dashes.length, this.options.dashes.gap]);
  92. }
  93. else //If all else fails draw a line
  94. {
  95. ctx.moveTo(this.from.x, this.from.y);
  96. ctx.lineTo(this.to.x, this.to.y);
  97. }
  98. ctx.stroke();
  99. }
  100. return via;
  101. }
  102. findBorderPosition(nearNode, ctx, options) {
  103. if (this.from != this.to) {
  104. console.log(1)
  105. return this._findBorderPosition(nearNode, ctx, options);
  106. }
  107. else {
  108. return this._findBorderPositionCircle(nearNode, ctx, options);
  109. }
  110. }
  111. /**
  112. * This function uses binary search to look for the point where the circle crosses the border of the node.
  113. * @param x
  114. * @param y
  115. * @param radius
  116. * @param node
  117. * @param low
  118. * @param high
  119. * @param direction
  120. * @param ctx
  121. * @returns {*}
  122. * @private
  123. */
  124. _findBorderPositionCircle(node, ctx, options) {
  125. let x = options.x;
  126. let y = options.y;
  127. let low = options.low;
  128. let high = options.high;
  129. let direction = options.direction;
  130. let maxIterations = 10;
  131. let iteration = 0;
  132. let radius = this.options.selfReferenceSize;
  133. let pos, angle, distanceToBorder, distanceToPoint, difference;
  134. let threshold = 0.05;
  135. while (low <= high && iteration < maxIterations) {
  136. let middle = (low + high) * 0.5;
  137. pos = this._pointOnCircle(x,y,radius,middle);
  138. angle = Math.atan2((node.y - pos.y), (node.x - pos.x));
  139. distanceToBorder = node.distanceToBorder(ctx, angle);
  140. distanceToPoint = Math.sqrt(Math.pow(pos.x - node.x, 2) + Math.pow(pos.y - node.y, 2));
  141. difference = distanceToBorder - distanceToPoint;
  142. if (Math.abs(difference) < threshold) {
  143. break; // found
  144. }
  145. 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.
  146. if (direction > 0) {
  147. low = middle;
  148. }
  149. else {
  150. high = middle;
  151. }
  152. }
  153. else {
  154. if (direction > 0) {
  155. high = middle;
  156. }
  157. else {
  158. low = middle;
  159. }
  160. }
  161. iteration++;
  162. }
  163. pos.t = middle;
  164. return pos;
  165. }
  166. /**
  167. * Get the line width of the edge. Depends on width and whether one of the
  168. * connected nodes is selected.
  169. * @return {Number} width
  170. * @private
  171. */
  172. getLineWidth(selected, hover) {
  173. if (selected == true) {
  174. return Math.max(Math.min(this.options.widthSelectionMultiplier * this.options.width, this.options.scaling.max), 0.3 / this.body.view.scale);
  175. }
  176. else {
  177. if (hover == true) {
  178. return Math.max(Math.min(this.options.hoverWidth, this.options.scaling.max), 0.3 / this.body.view.scale);
  179. }
  180. else {
  181. return Math.max(this.options.width, 0.3 / this.body.view.scale);
  182. }
  183. }
  184. }
  185. getColor(ctx) {
  186. var colorObj = this.options.color;
  187. if (colorObj.inherit.enabled === true) {
  188. if (colorObj.inherit.useGradients == true) {
  189. var grd = ctx.createLinearGradient(this.from.x, this.from.y, this.to.x, this.to.y);
  190. var fromColor, toColor;
  191. fromColor = this.from.options.color.highlight.border;
  192. toColor = this.to.options.color.highlight.border;
  193. if (this.from.selected == false && this.to.selected == false) {
  194. fromColor = util.overrideOpacity(this.from.options.color.border, this.options.color.opacity);
  195. toColor = util.overrideOpacity(this.to.options.color.border, this.options.color.opacity);
  196. }
  197. else if (this.from.selected == true && this.to.selected == false) {
  198. toColor = this.to.options.color.border;
  199. }
  200. else if (this.from.selected == false && this.to.selected == true) {
  201. fromColor = this.from.options.color.border;
  202. }
  203. grd.addColorStop(0, fromColor);
  204. grd.addColorStop(1, toColor);
  205. // -------------------- this returns -------------------- //
  206. return grd;
  207. }
  208. if (this.colorDirty === true) {
  209. if (colorObj.inherit.source == "to") {
  210. colorObj.highlight = this.to.options.color.highlight.border;
  211. colorObj.hover = this.to.options.color.hover.border;
  212. colorObj.color = util.overrideOpacity(this.to.options.color.border, this.options.color.opacity);
  213. }
  214. else { // (this.options.color.inherit.source == "from") {
  215. colorObj.highlight = this.from.options.color.highlight.border;
  216. colorObj.hover = this.from.options.color.hover.border;
  217. colorObj.color = util.overrideOpacity(this.from.options.color.border, this.options.color.opacity);
  218. }
  219. }
  220. }
  221. // if color inherit is on and gradients are used, the function has already returned by now.
  222. this.colorDirty = false;
  223. if (this.selected == true) {
  224. return colorObj.highlight;
  225. }
  226. else if (this.hover == true) {
  227. return colorObj.hover;
  228. }
  229. else {
  230. return colorObj.color;
  231. }
  232. }
  233. /**
  234. * Draw a line from a node to itself, a circle
  235. * @param {CanvasRenderingContext2D} ctx
  236. * @param {Number} x
  237. * @param {Number} y
  238. * @param {Number} radius
  239. * @private
  240. */
  241. _circle(ctx, x, y, radius) {
  242. // draw a circle
  243. ctx.beginPath();
  244. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  245. ctx.stroke();
  246. }
  247. /**
  248. * Calculate the distance between a point (x3,y3) and a line segment from
  249. * (x1,y1) to (x2,y2).
  250. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  251. * @param {number} x1
  252. * @param {number} y1
  253. * @param {number} x2
  254. * @param {number} y2
  255. * @param {number} x3
  256. * @param {number} y3
  257. * @private
  258. */
  259. getDistanceToEdge(x1, y1, x2, y2, x3, y3, via) { // x3,y3 is the point
  260. var returnValue = 0;
  261. if (this.from != this.to) {
  262. returnValue = this._getDistanceToEdge(x1, y1, x2, y2, x3, y3, via)
  263. }
  264. else {
  265. var x, y, dx, dy;
  266. var radius = this.options.selfReferenceSize;
  267. var node = this.from;
  268. if (node.width > node.height) {
  269. x = node.x + 0.5 * node.width;
  270. y = node.y - radius;
  271. }
  272. else {
  273. x = node.x + radius;
  274. y = node.y - 0.5 * node.height;
  275. }
  276. dx = x - x3;
  277. dy = y - y3;
  278. returnValue = Math.abs(Math.sqrt(dx * dx + dy * dy) - radius);
  279. }
  280. if (this.labelModule.size.left < x3 &&
  281. this.labelModule.size.left + this.labelModule.size.width > x3 &&
  282. this.labelModule.size.top < y3 &&
  283. this.labelModule.size.top + this.labelModule.size.height > y3) {
  284. return 0;
  285. }
  286. else {
  287. return returnValue;
  288. }
  289. }
  290. _getDistanceToLine(x1, y1, x2, y2, x3, y3) {
  291. var px = x2 - x1;
  292. var py = y2 - y1;
  293. var something = px * px + py * py;
  294. var u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  295. if (u > 1) {
  296. u = 1;
  297. }
  298. else if (u < 0) {
  299. u = 0;
  300. }
  301. var x = x1 + u * px;
  302. var y = y1 + u * py;
  303. var dx = x - x3;
  304. var dy = y - y3;
  305. //# Note: If the actual distance does not matter,
  306. //# if you only want to compare what this function
  307. //# returns to other results of this function, you
  308. //# can just return the squared distance instead
  309. //# (i.e. remove the sqrt) to gain a little performance
  310. return Math.sqrt(dx * dx + dy * dy);
  311. }
  312. }
  313. export default BaseEdge;