sigma.canvas.edges.tapered.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edges');
  4. /**
  5. * This method renders the edge as a tapered line.
  6. * Danny Holten, Petra Isenberg, Jean-Daniel Fekete, and J. Van Wijk (2010)
  7. * Performance Evaluation of Tapered, Curved, and Animated Directed-Edge
  8. * Representations in Node-Link Graphs. Research Report, Sep 2010.
  9. *
  10. * @param {object} edge The edge object.
  11. * @param {object} source node The edge source node.
  12. * @param {object} target node The edge target node.
  13. * @param {CanvasRenderingContext2D} context The canvas context.
  14. * @param {configurable} settings The settings function.
  15. */
  16. sigma.canvas.edges.tapered = function(edge, source, target, context, settings) {
  17. // The goal is to draw a triangle where the target node is a point of
  18. // the triangle, and the two other points are the intersection of the
  19. // source circle and the circle (target, distance(source, target)).
  20. var color = edge.active ?
  21. edge.active_color || settings('defaultEdgeActiveColor') :
  22. edge.color,
  23. prefix = settings('prefix') || '',
  24. size = edge[prefix + 'size'] || 1,
  25. edgeColor = settings('edgeColor'),
  26. prefix = settings('prefix') || '',
  27. defaultNodeColor = settings('defaultNodeColor'),
  28. defaultEdgeColor = settings('defaultEdgeColor'),
  29. sX = source[prefix + 'x'],
  30. sY = source[prefix + 'y'],
  31. tX = target[prefix + 'x'],
  32. tY = target[prefix + 'y'],
  33. dist = sigma.utils.getDistance(sX, sY, tX, tY);
  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. // Intersection points:
  47. var c = sigma.utils.getCircleIntersection(sX, sY, size, tX, tY, dist);
  48. context.save();
  49. if (edge.active) {
  50. context.fillStyle = settings('edgeActiveColor') === 'edge' ?
  51. (color || defaultEdgeColor) :
  52. settings('defaultEdgeActiveColor');
  53. }
  54. else {
  55. context.fillStyle = color;
  56. }
  57. // Turn transparency on:
  58. context.globalAlpha = 0.65;
  59. // Draw the triangle:
  60. context.beginPath();
  61. context.moveTo(tX, tY);
  62. context.lineTo(c.xi, c.yi);
  63. context.lineTo(c.xi_prime, c.yi_prime);
  64. context.closePath();
  65. context.fill();
  66. context.restore();
  67. };
  68. })();