Skip to content

Function: getTextPixelWidth()

ts
function getTextPixelWidth(text, options): number;

Calculates the pixel width of the given text in the specified font.

Parameters

ParameterTypeDescription
textstringThe text to calculate the width of.
options{ font?: string; fontFamily?: string; fontSize?: string | number; fontWeight?: string | number; }Options for the font.
options.font?stringThe font string to use, e.g. Arial.
options.fontFamily?stringThe name of the font family, e.g. sans-serif.
options.fontSize?string | numberThe size of the font in pixels.
options.fontWeight?string | numberThe weight of the font, e.g. bold.

Returns

number

The pixel width of the text in the specified font.

Example

ts
getTextPixelWidth('Hello, World!', { fontFamily: 'Arial', fontSize: 20 }) // => 123

Since

1.0.0

Released under the MIT License.