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.

526 lines
15 KiB

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