Browse Source

Refactored line drawing for Bezier edges (#3122)

Code in the `_line()` of the sibling Bezier edge types was either extremely similar or identical.
The Bezier drawing code has been consolidated in a single method in the parent class.
gemini
wimrijnders 7 years ago
committed by yotamberk
parent
commit
418b42bb64
4 changed files with 46 additions and 47 deletions
  1. +1
    -14
      lib/network/modules/components/edges/BezierEdgeDynamic.js
  2. +1
    -15
      lib/network/modules/components/edges/BezierEdgeStatic.js
  3. +2
    -17
      lib/network/modules/components/edges/CubicBezierEdge.js
  4. +42
    -1
      lib/network/modules/components/edges/util/BezierEdgeBase.js

+ 1
- 14
lib/network/modules/components/edges/BezierEdgeDynamic.js View File

@ -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() {

+ 1
- 15
lib/network/modules/components/edges/BezierEdgeStatic.js View File

@ -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() {

+ 2
- 17
lib/network/modules/components/edges/CubicBezierEdge.js View File

@ -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;
export default CubicBezierEdge;

+ 42
- 1
lib/network/modules/components/edges/util/BezierEdgeBase.js View File

@ -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;
export default BezierEdgeBase;

Loading…
Cancel
Save