sigma.canvas.edges.curvedArrow.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edges');
  4. /**
  5. * This edge renderer will display edges as curves with arrow heading.
  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.curvedArrow =
  14. function(edge, source, target, context, settings) {
  15. var color = edge.color,
  16. prefix = settings('prefix') || '',
  17. edgeColor = settings('edgeColor'),
  18. defaultNodeColor = settings('defaultNodeColor'),
  19. defaultEdgeColor = settings('defaultEdgeColor'),
  20. cp = {},
  21. size = edge[prefix + 'size'] || 1,
  22. count = edge.count || 0,
  23. tSize = target[prefix + 'size'],
  24. sX = source[prefix + 'x'],
  25. sY = source[prefix + 'y'],
  26. tX = target[prefix + 'x'],
  27. tY = target[prefix + 'y'],
  28. aSize = Math.max(size * 2.5, settings('minArrowSize')),
  29. d,
  30. aX,
  31. aY,
  32. vX,
  33. vY;
  34. cp = (source.id === target.id) ?
  35. sigma.utils.getSelfLoopControlPoints(sX, sY, tSize, count) :
  36. sigma.utils.getQuadraticControlPoint(sX, sY, tX, tY, count);
  37. if (source.id === target.id) {
  38. d = Math.sqrt(Math.pow(tX - cp.x1, 2) + Math.pow(tY - cp.y1, 2));
  39. aX = cp.x1 + (tX - cp.x1) * (d - aSize - tSize) / d;
  40. aY = cp.y1 + (tY - cp.y1) * (d - aSize - tSize) / d;
  41. vX = (tX - cp.x1) * aSize / d;
  42. vY = (tY - cp.y1) * aSize / d;
  43. }
  44. else {
  45. d = Math.sqrt(Math.pow(tX - cp.x, 2) + Math.pow(tY - cp.y, 2));
  46. aX = cp.x + (tX - cp.x) * (d - aSize - tSize) / d;
  47. aY = cp.y + (tY - cp.y) * (d - aSize - tSize) / d;
  48. vX = (tX - cp.x) * aSize / d;
  49. vY = (tY - cp.y) * aSize / d;
  50. }
  51. if (!color)
  52. switch (edgeColor) {
  53. case 'source':
  54. color = source.color || defaultNodeColor;
  55. break;
  56. case 'target':
  57. color = target.color || defaultNodeColor;
  58. break;
  59. default:
  60. color = defaultEdgeColor;
  61. break;
  62. }
  63. context.strokeStyle = color;
  64. context.lineWidth = size;
  65. context.beginPath();
  66. context.moveTo(sX, sY);
  67. if (source.id === target.id) {
  68. context.bezierCurveTo(cp.x2, cp.y2, cp.x1, cp.y1, aX, aY);
  69. } else {
  70. context.quadraticCurveTo(cp.x, cp.y, aX, aY);
  71. }
  72. context.stroke();
  73. context.fillStyle = color;
  74. context.beginPath();
  75. context.moveTo(aX + vX, aY + vY);
  76. context.lineTo(aX + vY * 0.6, aY - vX * 0.6);
  77. context.lineTo(aX - vY * 0.6, aY + vX * 0.6);
  78. context.lineTo(aX + vX, aY + vY);
  79. context.closePath();
  80. context.fill();
  81. };
  82. })();