sigma.canvas.edges.curvedArrow.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. tSize = target[prefix + 'size'],
  23. sX = source[prefix + 'x'],
  24. sY = source[prefix + 'y'],
  25. tX = target[prefix + 'x'],
  26. tY = target[prefix + 'y'],
  27. aSize = Math.max(size * 2.5, settings('minArrowSize')),
  28. d,
  29. aX,
  30. aY,
  31. vX,
  32. vY;
  33. cp = (source.id === target.id) ?
  34. sigma.utils.getSelfLoopControlPoints(sX, sY, tSize) :
  35. sigma.utils.getQuadraticControlPoint(sX, sY, tX, tY);
  36. if (source.id === target.id) {
  37. d = Math.sqrt(Math.pow(tX - cp.x1, 2) + Math.pow(tY - cp.y1, 2));
  38. aX = cp.x1 + (tX - cp.x1) * (d - aSize - tSize) / d;
  39. aY = cp.y1 + (tY - cp.y1) * (d - aSize - tSize) / d;
  40. vX = (tX - cp.x1) * aSize / d;
  41. vY = (tY - cp.y1) * aSize / d;
  42. }
  43. else {
  44. d = Math.sqrt(Math.pow(tX - cp.x, 2) + Math.pow(tY - cp.y, 2));
  45. aX = cp.x + (tX - cp.x) * (d - aSize - tSize) / d;
  46. aY = cp.y + (tY - cp.y) * (d - aSize - tSize) / d;
  47. vX = (tX - cp.x) * aSize / d;
  48. vY = (tY - cp.y) * aSize / d;
  49. }
  50. if (!color)
  51. switch (edgeColor) {
  52. case 'source':
  53. color = source.color || defaultNodeColor;
  54. break;
  55. case 'target':
  56. color = target.color || defaultNodeColor;
  57. break;
  58. default:
  59. color = defaultEdgeColor;
  60. break;
  61. }
  62. context.strokeStyle = color;
  63. context.lineWidth = size;
  64. context.beginPath();
  65. context.moveTo(sX, sY);
  66. if (source.id === target.id) {
  67. context.bezierCurveTo(cp.x2, cp.y2, cp.x1, cp.y1, aX, aY);
  68. } else {
  69. context.quadraticCurveTo(cp.x, cp.y, aX, aY);
  70. }
  71. context.stroke();
  72. context.fillStyle = color;
  73. context.beginPath();
  74. context.moveTo(aX + vX, aY + vY);
  75. context.lineTo(aX + vY * 0.6, aY - vX * 0.6);
  76. context.lineTo(aX - vY * 0.6, aY + vX * 0.6);
  77. context.lineTo(aX + vX, aY + vY);
  78. context.closePath();
  79. context.fill();
  80. };
  81. })();