From 23a96ff6cdbedd56515e846165c20bd439f50fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Tue, 29 Aug 2023 10:12:24 +0200 Subject: [PATCH] Keep text centered in canvas --- js/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/index.js b/js/index.js index bf0caef..9a59d84 100644 --- a/js/index.js +++ b/js/index.js @@ -37,7 +37,9 @@ document.addEventListener('DOMContentLoaded', () => { } // Draw instrument ID as text ctx.font = '48px sans-serif'; - ctx.fillText(idInstr, 45, 350); + const txtWidth = Math.round(ctx.measureText(idInstr).width); + const txtLeftDist = (200 - txtWidth) / 2 + 25; + ctx.fillText(idInstr, txtLeftDist, 350); erihsLogo.onload = () => { ctx.drawImage(erihsLogo, 25, 375, 200, 41)