MCPcopy Create free account
hub / github.com/SplootCode/splootcode / getTextWidth

Function getTextWidth

packages/editor/src/layout/layout_constants.ts:27–34  ·  view source on GitHub ↗
(text: string, font: string)

Source from the content-addressed store, hash-verified

25 * @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
26 */
27export function getTextWidth(text: string, font: string) {
28 // re-use canvas object for better performance
29 const canvas = getTextWidth['canvas'] || (getTextWidth['canvas'] = document.createElement('canvas'))
30 const context = canvas.getContext('2d')
31 context.font = font
32 const metrics = context.measureText(text)
33 return metrics.width
34}
35
36export function getStringPreHeight(text: string, maxWidth: number) {
37 let pre: HTMLPreElement = getStringPreHeight['pre']

Callers 4

stringWidthFunction · 0.85
stringLiteralDimensionsFunction · 0.85
placeholderWidthFunction · 0.85
labelStringWidthFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected