sigma.canvas.edges.arrow.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edges');
  4. /**
  5. * This edge renderer will display edges as arrows going from the source node
  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.arrow = function(edge, source, target, context, settings) {
  14. var color = edge.color,
  15. prefix = settings('prefix') || '',
  16. edgeColor = settings('edgeColor'),
  17. defaultNodeColor = settings('defaultNodeColor'),
  18. defaultEdgeColor = settings('defaultEdgeColor'),
  19. size = edge[prefix + 'size'] || 1,
  20. tSize = target[prefix + 'size'],
  21. sX = source[prefix + 'x'],
  22. sY = source[prefix + 'y'],
  23. tX = target[prefix + 'x'],
  24. tY = target[prefix + 'y'],
  25. aSize = Math.max(size * 2.5, settings('minArrowSize')),
  26. d = Math.sqrt(Math.pow(tX - sX, 2) + Math.pow(tY - sY, 2)),
  27. aX = sX + (tX - sX) * (d - aSize - tSize) / d,
  28. aY = sY + (tY - sY) * (d - aSize - tSize) / d,
  29. vX = (tX - sX) * aSize / d,
  30. vY = (tY - sY) * aSize / d;
  31. if (!color)
  32. switch (edgeColor) {
  33. case 'source':
  34. color = source.color || defaultNodeColor;
  35. break;
  36. case 'target':
  37. color = target.color || defaultNodeColor;
  38. break;
  39. default:
  40. color = defaultEdgeColor;
  41. break;
  42. }
  43. context.strokeStyle = color;
  44. context.lineWidth = size;
  45. context.beginPath();
  46. context.moveTo(sX, sY);
  47. context.lineTo(
  48. aX,
  49. aY
  50. );
  51. context.stroke();
  52. context.fillStyle = color;
  53. context.beginPath();
  54. context.moveTo(aX + vX, aY + vY);
  55. context.lineTo(aX + vY * 0.6, aY - vX * 0.6);
  56. context.lineTo(aX - vY * 0.6, aY + vX * 0.6);
  57. context.lineTo(aX + vX, aY + vY);
  58. context.closePath();
  59. context.fill();
  60. };
  61. })();