MCPcopy Index your code
hub / github.com/ajatkj/scriptable / getTextWidth

Function getTextWidth

LSWeather.js:1202–1219  ·  view source on GitHub ↗
(text, font)

Source from the content-addressed store, hash-verified

1200FUNCTION getTextWidth
1201------------------------------------------------------------------*/
1202async function getTextWidth(text, font){
1203 const buffer = 5; // This is a temporary solution until I figure out why width returned is a bit short.
1204 const html = `<canvas id="canvas"></canvas>`;
1205 const js = `
1206 function getTextWidth() {
1207 let canvas = document.getElementById("canvas");
1208 let ctx = canvas.getContext("2d");
1209 ctx.font = "${font}";
1210 let metrics = ctx.measureText("${text}");
1211 return metrics.width;
1212 }
1213 getTextWidth();
1214 `;
1215 let wv = new WebView();
1216 await wv.loadHTML(html);
1217 const width = await wv.evaluateJavaScript(js);
1218 return Math.ceil(width) + buffer;
1219}
1220/*------------------------------------------------------------------
1221FUNCTION writeLOG
1222------------------------------------------------------------------*/

Callers 2

createOverlayFunction · 0.85
prefixImageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected