sigma.canvas.edges.parallel.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edges');
  4. /**
  5. * This method renders the edge as two parallel lines.
  6. *
  7. * @param {object} edge The edge object.
  8. * @param {object} source node The edge source node.
  9. * @param {object} target node The edge target node.
  10. * @param {CanvasRenderingContext2D} context The canvas context.
  11. * @param {configurable} settings The settings function.
  12. */
  13. sigma.canvas.edges.parallel = function(edge, source, target, context, settings) {
  14. var color = edge.active ?
  15. edge.active_color || settings('defaultEdgeActiveColor') :
  16. edge.color,
  17. prefix = settings('prefix') || '',
  18. size = edge[prefix + 'size'] || 1,
  19. edgeColor = settings('edgeColor'),
  20. defaultNodeColor = settings('defaultNodeColor'),
  21. defaultEdgeColor = settings('defaultEdgeColor'),
  22. sX = source[prefix + 'x'],
  23. sY = source[prefix + 'y'],
  24. tX = target[prefix + 'x'],
  25. tY = target[prefix + 'y'],
  26. c,
  27. d,
  28. dist = sigma.utils.getDistance(sX, sY, tX, tY);
  29. if (!color)
  30. switch (edgeColor) {
  31. case 'source':
  32. color = source.color || defaultNodeColor;
  33. break;
  34. case 'target':
  35. color = target.color || defaultNodeColor;
  36. break;
  37. default:
  38. color = defaultEdgeColor;
  39. break;
  40. }
  41. // Intersection points of the source node circle:
  42. c = sigma.utils.getCircleIntersection(sX, sY, size, tX, tY, dist);
  43. // Intersection points of the target node circle:
  44. d = sigma.utils.getCircleIntersection(tX, tY, size, sX, sY, dist);
  45. context.save();
  46. if (edge.active) {
  47. context.strokeStyle = settings('edgeActiveColor') === 'edge' ?
  48. (color || defaultEdgeColor) :
  49. settings('defaultEdgeActiveColor');
  50. }
  51. else {
  52. context.strokeStyle = color;
  53. }
  54. context.lineWidth = size;
  55. context.beginPath();
  56. context.moveTo(c.xi, c.yi);
  57. context.lineTo(d.xi_prime, d.yi_prime);
  58. context.closePath();
  59. context.stroke();
  60. context.beginPath();
  61. context.moveTo(c.xi_prime, c.yi_prime);
  62. context.lineTo(d.xi, d.yi);
  63. context.closePath();
  64. context.stroke();
  65. context.restore();
  66. };
  67. })();