Skip to content
Discussion options

You must be logged in to vote

without memoization (not recommended), this is one way to do it

using node.style.labelText

const _canvas = document.createElement("canvas");
const measureTextWidth = (text, font = {}) => {
        const { fontSize, fontFamily = "sans-serif", fontWeight, fontStyle, fontVariant } = font;
        const ctx = _canvas.getContext("2d");
        ctx.font = [fontStyle, fontWeight, fontVariant, `${fontSize}px`, fontFamily].join(" ");
        return ctx.measureText(text).width;
};

const graph = new G6.Graph({
  node: {
    style: {
      size: (node) => [measureTextWidth(node.style.labelText, {}), 30],
    },
  },
});

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by curious-broccoli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant