Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge labels with border and spacing #1290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/network/modules/EdgesHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class EdgesHandler {
size: 14, // px
face: "arial",
background: "none",
borderColor: "none",
borderWidth: 0, // px
padding: 0, // px
strokeWidth: 2, // px
strokeColor: "#ffffff",
align: "horizontal",
Expand Down
3 changes: 3 additions & 0 deletions lib/network/modules/NodesHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class NodesHandler {
size: 14, // px
face: "arial",
background: "none",
borderColor: "none",
borderWidth: 0, // px
padding: 0, // px
strokeWidth: 0, // px
strokeColor: "#ffffff",
align: "center",
Expand Down
47 changes: 46 additions & 1 deletion lib/network/modules/components/shared/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class Label {
// update the size cache if required
this.calculateLabelSize(ctx, selected, hover, x, y, baseline);
this._drawBackground(ctx);
this._drawBorder(ctx);
this._drawText(ctx, x, this.size.yLine, baseline, viewFontSize);
}

Expand All @@ -447,11 +448,55 @@ class Label {
this.fontOptions.background !== "none"
) {
ctx.fillStyle = this.fontOptions.background;
const size = this.getSize();
const size = this._getSizeWithSpacing();
ctx.fillRect(size.left, size.top, size.width, size.height);
}
}

/**
* Draws the label border
*
* @param {CanvasRenderingContext2D} ctx
* @private
*/
_drawBorder(ctx) {
if (
this.fontOptions.borderColor !== undefined &&
this.fontOptions.borderColor !== "none"
) {
ctx.lineWidth = 1;
if (
this.fontOptions.borderWidth !== undefined &&
this.fontOptions.borderWidth !== "none"
) {
ctx.lineWidth = this.fontOptions.borderWidth;
}
ctx.strokeStyle = this.fontOptions.borderColor;
const size = this._getSizeWithSpacing();
ctx.strokeRect(size.left, size.top, size.width, size.height);
}
}

/**
* Gets label's size with specified padding.
*
* @returns {number} Label's size.
* @private
*/
_getSizeWithSpacing() {
const size = this.getSize();
if (
this.fontOptions.padding !== undefined &&
this.fontOptions.padding !== "none"
) {
size.left -= this.fontOptions.padding;
size.top -= this.fontOptions.padding;
size.width += 2 * this.fontOptions.padding;
size.height += 2 * this.fontOptions.padding;
}
return size;
}

/**
*
* @param {CanvasRenderingContext2D} ctx
Expand Down
12 changes: 12 additions & 0 deletions lib/network/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ const nodeOptions: OptionsConfig = {
size: { number }, // px
face: { string },
background: { string },
borderColor: { string },
borderWidth: { number }, // px
padding: { number }, // px
strokeWidth: { number }, // px
strokeColor: { string },
vadjust: { number },
Expand Down Expand Up @@ -292,6 +295,9 @@ const allOptions: OptionsConfig = {
size: { number }, // px
face: { string },
background: { string },
borderColor: { string },
borderWidth: { number }, // px
padding: { number }, // px
strokeWidth: { number }, // px
strokeColor: { string },
align: { string: ["horizontal", "top", "middle", "bottom"] },
Expand Down Expand Up @@ -598,6 +604,9 @@ const configureOptions: ConfiguratorConfig = {
size: [14, 0, 100, 1], // px
face: ["arial", "verdana", "tahoma"],
background: ["color", "none"],
borderColor: ["color", "none"],
borderWidth: [0, 0, 10, 1], // px
padding: [0, 0, 10, 1], // px
strokeWidth: [0, 0, 50, 1], // px
strokeColor: ["color", "#ffffff"],
},
Expand Down Expand Up @@ -676,6 +685,9 @@ const configureOptions: ConfiguratorConfig = {
size: [14, 0, 100, 1], // px
face: ["arial", "verdana", "tahoma"],
background: ["color", "none"],
borderColor: ["color", "none"],
borderWidth: [0, 0, 10, 1], // px
padding: [0, 0, 10, 1], // px
strokeWidth: [2, 0, 50, 1], // px
strokeColor: ["color", "#ffffff"],
align: ["horizontal", "top", "middle", "bottom"],
Expand Down
3 changes: 3 additions & 0 deletions test/Network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ function checkFontProperties(fontItem, checkStrict = true) {
"size",
"face",
"background",
"borderColor",
"borderWidth",
"padding",
"strokeWidth",
"strokeColor",
"align",
Expand Down