sigma.canvas.edgehovers.arrow.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.arrow =
  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. size = edge[prefix + 'size'] || 1,
  21. tSize = target[prefix + 'size'],
  22. sX = source[prefix + 'x'],
  23. sY = source[prefix + 'y'],
  24. tX = target[prefix + 'x'],
  25. tY = target[prefix + 'y'];
  26. size = (edge.hover) ?
  27. settings('edgeHoverSizeRatio') * size : size;
  28. var aSize = size * 2.5,
  29. d = Math.sqrt(Math.pow(tX - sX, 2) + Math.pow(tY - sY, 2)),
  30. aX = sX + (tX - sX) * (d - aSize - tSize) / d,
  31. aY = sY + (tY - sY) * (d - aSize - tSize) / d,
  32. vX = (tX - sX) * aSize / d,
  33. vY = (tY - sY) * aSize / d;
  34. if (!color)
  35. switch (edgeColor) {
  36. case 'source':
  37. color = source.color || defaultNodeColor;
  38. break;
  39. case 'target':
  40. color = target.color || defaultNodeColor;
  41. break;
  42. default:
  43. color = defaultEdgeColor;
  44. break;
  45. }
  46. if (settings('edgeHoverColor') === 'edge') {
  47. color = edge.hover_color || color;
  48. } else {
  49. color = edge.hover_color || settings('defaultEdgeHoverColor') || color;
  50. }
  51. context.strokeStyle = color;
  52. context.lineWidth = size;
  53. context.beginPath();
  54. context.moveTo(sX, sY);
  55. context.lineTo(
  56. aX,
  57. aY
  58. );
  59. context.stroke();
  60. context.fillStyle = color;
  61. context.beginPath();
  62. context.moveTo(aX + vX, aY + vY);
  63. context.lineTo(aX + vY * 0.6, aY - vX * 0.6);
  64. context.lineTo(aX - vY * 0.6, aY + vX * 0.6);
  65. context.lineTo(aX + vX, aY + vY);
  66. context.closePath();
  67. context.fill();
  68. };
  69. })();