svgdotjs / svgdom

Straightforward DOM implementation to make SVG.js run headless on Node.js
MIT License
269 stars 53 forks source link

2d Canvas Polyfill to Measure Text? #88

Open swamidass opened 2 years ago

swamidass commented 2 years ago

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?

Fuzzyma commented 2 years ago

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