sigma.canvas.edges.curve.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ;(function() {
  2. 'use strict';
  3. sigma.utils.pkg('sigma.canvas.edges');
  4. /**
  5. * This edge renderer will display edges as curves.
  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.curve = function(edge, source, target, context, settings) {
  14. var color = edge.color,
  15. prefix = settings('prefix') || '',
  16. size = edge[prefix + 'size'] || 1,
  17. count = edge.count || 0,
  18. edgeColor = settings('edgeColor'),
  19. defaultNodeColor = settings('defaultNodeColor'),
  20. defaultEdgeColor = settings('defaultEdgeColor'),
  21. cp = {},
  22. sSize = source[prefix + 'size'],
  23. sX = source[prefix + 'x'],
  24. sY = source[prefix + 'y'],
  25. tX = target[prefix + 'x'],
  26. tY = target[prefix + 'y'];
  27. cp = (source.id === target.id) ?
  28. sigma.utils.getSelfLoopControlPoints(sX, sY, sSize, count) :
  29. sigma.utils.getQuadraticControlPoint(sX, sY, tX, tY, count);
  30. if (!color)
  31. switch (edgeColor) {
  32. case 'source':
  33. color = source.color || defaultNodeColor;
  34. break;
  35. case 'target':
  36. color = target.color || defaultNodeColor;
  37. break;
  38. default:
  39. color = defaultEdgeColor;
  40. break;
  41. }
  42. context.strokeStyle = color;
  43. context.lineWidth = size;
  44. context.beginPath();
  45. context.moveTo(sX, sY);
  46. if (source.id === target.id) {
  47. context.bezierCurveTo(cp.x1, cp.y1, cp.x2, cp.y2, tX, tY);
  48. } else {
  49. context.quadraticCurveTo(cp.x, cp.y, tX, tY);
  50. }
  51. context.stroke();
  52. };
  53. })();