sigma.canvas.edgehovers.curve.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.curve =
  14. function(edge, source, target, context, settings) {
  15. var color = edge.color,
  16. prefix = settings('prefix') || '',
  17. size = settings('edgeHoverSizeRatio') * (edge[prefix + 'size'] || 1),
  18. count = edge.count || 0,
  19. edgeColor = settings('edgeColor'),
  20. defaultNodeColor = settings('defaultNodeColor'),
  21. defaultEdgeColor = settings('defaultEdgeColor'),
  22. cp = {},
  23. sSize = source[prefix + 'size'],
  24. sX = source[prefix + 'x'],
  25. sY = source[prefix + 'y'],
  26. tX = target[prefix + 'x'],
  27. tY = target[prefix + 'y'];
  28. cp = (source.id === target.id) ?
  29. sigma.utils.getSelfLoopControlPoints(sX, sY, sSize, count) :
  30. sigma.utils.getQuadraticControlPoint(sX, sY, tX, tY, count);
  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. if (settings('edgeHoverColor') === 'edge') {
  44. color = edge.hover_color || color;
  45. } else {
  46. color = edge.hover_color || settings('defaultEdgeHoverColor') || color;
  47. }
  48. context.strokeStyle = color;
  49. context.lineWidth = size;
  50. context.beginPath();
  51. context.moveTo(sX, sY);
  52. if (source.id === target.id) {
  53. context.bezierCurveTo(cp.x1, cp.y1, cp.x2, cp.y2, tX, tY);
  54. } else {
  55. context.quadraticCurveTo(cp.x, cp.y, tX, tY);
  56. }
  57. context.stroke();
  58. };
  59. })();