svgdom icon indicating copy to clipboard operation
svgdom copied to clipboard

2d Canvas Polyfill to Measure Text?

Open swamidass opened this issue 4 years ago • 1 comments

A library I am using has this code to measure text.

   const element = document.createElement('canvas');
    const ctx = element.getContext("2d");
    ctx.font = `${this.opts.fontSizeLarge}pt Helvetica, Arial, sans-serif`
    
    let textMetrics = ctx.measureText(text)

Is there any way to get this to work with svgdom? If not, what is replacement code that would accomplish the same?

swamidass avatar Apr 08 '22 21:04 swamidass

Just get the bounding box of the text by creating a text element and calling getBBox on it

Fuzzyma avatar Apr 08 '22 23:04 Fuzzyma