sigma.canvas.edges.dotCurve.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.dotCurve = function(edge, source, target, context, settings) {
  14. var color = edge.color,
  15. prefix = settings('prefix') || '',
  16. size = edge[prefix + 'size'] || 1,
  17. edgeColor = settings('edgeColor'),
  18. defaultNodeColor = settings('defaultNodeColor'),
  19. defaultEdgeColor = settings('defaultEdgeColor'),
  20. cp = {},
  21. sSize = source[prefix + 'size'],
  22. sX = source[prefix + 'x'],
  23. sY = source[prefix + 'y'],
  24. tX = target[prefix + 'x'],
  25. tY = target[prefix + 'y'];
  26. cp = (source.id === target.id) ?
  27. sigma.utils.getSelfLoopControlPoints(sX, sY, sSize) :
  28. sigma.utils.getQuadraticControlPoint(sX, sY, tX, tY);
  29. if (!color)
  30. switch (edgeColor) {
  31. case 'source':
  32. color = source.color || defaultNodeColor;
  33. break;
  34. case 'target':
  35. color = target.color || defaultNodeColor;
  36. break;
  37. default:
  38. color = defaultEdgeColor;
  39. break;
  40. }
  41. context.strokeStyle = color;
  42. context.lineWidth = size;
  43. context.beginPath();
  44. context.moveTo(sX, sY);
  45. if (source.id === target.id) {
  46. context.bezierCurveTo(cp.x1, cp.y1, cp.x2, cp.y2, tX, tY);
  47. } else {
  48. context.quadraticCurveTo(cp.x, cp.y, tX, tY);
  49. }
  50. context.stroke();
  51. if(edge.sourceDotColor != undefined || edge.targetDotColor != undefined) {
  52. var dotOffset = edge.dotOffset || 3;
  53. var dotSize = edge.dotSize || 1;
  54. dotSize = size*dotSize;
  55. dotOffset = dotOffset*sSize;
  56. if(edge.sourceDotColor != undefined) {
  57. createDot(context, sX, sY, cp, tX, tY, dotOffset, dotSize, edge.sourceDotColor);
  58. }
  59. if (edge.targetDotColor != undefined){
  60. createDot(context, tX, tY, cp, sX, sY, dotOffset, dotSize, edge.targetDotColor);
  61. }
  62. }
  63. };
  64. function createDot(context, sX, sY, cp, tX, tY, offset, size, color) {
  65. context.beginPath();
  66. context.fillStyle = color;
  67. var dot = getPointOnBezier(sX, sY, cp.x, cp.y, tX, tY,
  68. offset);
  69. context.arc(dot.x, dot.y, size * 3, 0, 2 * Math.PI,
  70. false);
  71. context.fill();
  72. }
  73. function getQBezierValue(t, p1, p2, p3) {
  74. var iT = 1 - t;
  75. return iT * iT * p1 + 2 * iT * t * p2 + t * t * p3;
  76. }
  77. function getQuadraticCurvePoint(startX, startY, cpX, cpY, endX, endY, position) {
  78. return {
  79. x:getQBezierValue(position, startX, cpX, endX),
  80. y:getQBezierValue(position, startY, cpY, endY)
  81. };
  82. }
  83. function getDistanceBetweenPoints(x1, y1, x2, y2){
  84. return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
  85. }
  86. /* Function to get a point on a bezier curve a certain distance away from
  87. its source. Needed since the position on a beziercurve is given to the
  88. formula as a percentage (t).*/
  89. function getPointOnBezier(startX, startY, cpX, cpY, endX, endY, distance){
  90. var bestT = 0;
  91. var bestAccuracy = 1000;
  92. var stepSize = 0.001;
  93. for(var t = 0; t<1; t+=stepSize){
  94. var currentPoint = getQuadraticCurvePoint(startX, startY, cpX, cpY,
  95. endX, endY, t);
  96. var currentDistance = getDistanceBetweenPoints(startX, startY,
  97. currentPoint.x, currentPoint.y);
  98. if(Math.abs(currentDistance-distance) < bestAccuracy){
  99. bestAccuracy = Math.abs(currentDistance-distance);
  100. bestT = t;
  101. }
  102. }
  103. return getQuadraticCurvePoint(startX, startY, cpX, cpY, endX, endY, bestT);
  104. }
  105. })();