diff --git a/lib/network/modules/components/edges/BezierEdgeDynamic.js b/lib/network/modules/components/edges/BezierEdgeDynamic.js index a57081c4..203478ca 100644 --- a/lib/network/modules/components/edges/BezierEdgeDynamic.js +++ b/lib/network/modules/components/edges/BezierEdgeDynamic.js @@ -103,20 +103,7 @@ class BezierEdgeDynamic extends BezierEdgeBase { * @private */ _line(ctx, values, viaNode) { - // draw a straight line - ctx.beginPath(); - ctx.moveTo(this.fromPoint.x, this.fromPoint.y); - // fallback to normal straight edges - if (viaNode.x === undefined) { - ctx.lineTo(this.toPoint.x, this.toPoint.y); - } - else { - ctx.quadraticCurveTo(viaNode.x, viaNode.y, this.toPoint.x, this.toPoint.y); - } - // draw shadow if enabled - this.enableShadow(ctx, values); - ctx.stroke(); - this.disableShadow(ctx, values); + this._bezierCurve(ctx, values, viaNode); } getViaNode() { diff --git a/lib/network/modules/components/edges/BezierEdgeStatic.js b/lib/network/modules/components/edges/BezierEdgeStatic.js index 03bb9fbf..d2bfe629 100644 --- a/lib/network/modules/components/edges/BezierEdgeStatic.js +++ b/lib/network/modules/components/edges/BezierEdgeStatic.js @@ -11,21 +11,7 @@ class BezierEdgeStatic extends BezierEdgeBase { * @private */ _line(ctx, values, viaNode) { - // draw a straight line - ctx.beginPath(); - ctx.moveTo(this.fromPoint.x, this.fromPoint.y); - - // fallback to normal straight edges - if (viaNode.x === undefined) { - ctx.lineTo(this.toPoint.x, this.toPoint.y); - } - else { - ctx.quadraticCurveTo(viaNode.x, viaNode.y, this.toPoint.x, this.toPoint.y); - } - // draw shadow if enabled - this.enableShadow(ctx, values); - ctx.stroke(); - this.disableShadow(ctx, values); + this._bezierCurve(ctx, values, viaNode); } getViaNode() { diff --git a/lib/network/modules/components/edges/CubicBezierEdge.js b/lib/network/modules/components/edges/CubicBezierEdge.js index 4c9579fb..fcfde588 100644 --- a/lib/network/modules/components/edges/CubicBezierEdge.js +++ b/lib/network/modules/components/edges/CubicBezierEdge.js @@ -14,22 +14,7 @@ class CubicBezierEdge extends CubicBezierEdgeBase { // get the coordinates of the support points. let via1 = viaNodes[0]; let via2 = viaNodes[1]; - - // start drawing the line. - ctx.beginPath(); - ctx.moveTo(this.fromPoint.x, this.fromPoint.y); - - // fallback to normal straight edges - if (viaNodes === undefined || via1.x === undefined) { - ctx.lineTo(this.toPoint.x, this.toPoint.y); - } - else { - ctx.bezierCurveTo(via1.x, via1.y, via2.x, via2.y, this.toPoint.x, this.toPoint.y); - } - // draw shadow if enabled - this.enableShadow(ctx, values); - ctx.stroke(); - this.disableShadow(ctx, values); + this._bezierCurve(ctx, values, via1, via2); } _getViaCoordinates() { @@ -90,4 +75,4 @@ class CubicBezierEdge extends CubicBezierEdgeBase { } -export default CubicBezierEdge; \ No newline at end of file +export default CubicBezierEdge; diff --git a/lib/network/modules/components/edges/util/BezierEdgeBase.js b/lib/network/modules/components/edges/util/BezierEdgeBase.js index 48b663bf..6bc82c88 100644 --- a/lib/network/modules/components/edges/util/BezierEdgeBase.js +++ b/lib/network/modules/components/edges/util/BezierEdgeBase.js @@ -101,6 +101,47 @@ class BezierEdgeBase extends EdgeBase { return minDistance; } + + + /** + * Draw a bezier curve between two nodes + * + * The method accepts zero, one or two control points. + * Passing zero control points just draws a straight line + * + * @param {CanvasRenderingContext2D} ctx + * @param {Object} values | options for shadow drawing + * @param {Object|undefined} viaNode1 | first control point for curve drawing + * @param {Object|undefined} viaNode2 | second control point for curve drawing + * + * @protected + */ + _bezierCurve(ctx, values, viaNode1, viaNode2) { + var hasNode1 = (viaNode1 !== undefined && viaNode1.x !== undefined); + var hasNode2 = (viaNode2 !== undefined && viaNode2.x !== undefined); + + ctx.beginPath(); + ctx.moveTo(this.fromPoint.x, this.fromPoint.y); + + if (hasNode1 && hasNode2) { + ctx.bezierCurveTo(viaNode1.x, viaNode1.y, viaNode2.x, viaNode2.y, this.toPoint.x, this.toPoint.y); + } else if (hasNode1) { + ctx.quadraticCurveTo(viaNode1.x, viaNode1.y, this.toPoint.x, this.toPoint.y); + } else { + // fallback to normal straight edge + ctx.lineTo(this.toPoint.x, this.toPoint.y); + } + + // draw shadow if enabled + this.enableShadow(ctx, values); + ctx.stroke(); + this.disableShadow(ctx, values); + } + + + getViaNode() { + return this._getViaCoordinates(); + } } -export default BezierEdgeBase; \ No newline at end of file +export default BezierEdgeBase;