sigma.canvas.edgehovers.curvedArrow.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edgehovers');
  4. /**
  5. * This hover renderer will display the edge with a different color or size.
  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.edgehovers.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 = settings('edgeHoverSizeRatio') * (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. d,
  29. aSize,
  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. aSize = size * 2.5;
  40. aX = cp.x1 + (tX - cp.x1) * (d - aSize - tSize) / d;
  41. aY = cp.y1 + (tY - cp.y1) * (d - aSize - tSize) / d;
  42. vX = (tX - cp.x1) * aSize / d;
  43. vY = (tY - cp.y1) * aSize / d;
  44. }
  45. else {
  46. d = Math.sqrt(Math.pow(tX - cp.x, 2) + Math.pow(tY - cp.y, 2));
  47. aSize = size * 2.5;
  48. aX = cp.x + (tX - cp.x) * (d - aSize - tSize) / d;
  49. aY = cp.y + (tY - cp.y) * (d - aSize - tSize) / d;
  50. vX = (tX - cp.x) * aSize / d;
  51. vY = (tY - cp.y) * aSize / d;
  52. }
  53. if (!color)
  54. switch (edgeColor) {
  55. case 'source':
  56. color = source.color || defaultNodeColor;
  57. break;
  58. case 'target':
  59. color = target.color || defaultNodeColor;
  60. break;
  61. default:
  62. color = defaultEdgeColor;
  63. break;
  64. }
  65. if (settings('edgeHoverColor') === 'edge') {
  66. color = edge.hover_color || color;
  67. } else {
  68. color = edge.hover_color || settings('defaultEdgeHoverColor') || color;
  69. }
  70. context.strokeStyle = color;
  71. context.lineWidth = size;
  72. context.beginPath();
  73. context.moveTo(sX, sY);
  74. if (source.id === target.id) {
  75. context.bezierCurveTo(cp.x2, cp.y2, cp.x1, cp.y1, aX, aY);
  76. } else {
  77. context.quadraticCurveTo(cp.x, cp.y, aX, aY);
  78. }
  79. context.stroke();
  80. context.fillStyle = color;
  81. context.beginPath();
  82. context.moveTo(aX + vX, aY + vY);
  83. context.lineTo(aX + vY * 0.6, aY - vX * 0.6);
  84. context.lineTo(aX - vY * 0.6, aY + vX * 0.6);
  85. context.lineTo(aX + vX, aY + vY);
  86. context.closePath();
  87. context.fill();
  88. };
  89. })();