actually center text

This commit is contained in:
Chris Wanstrath 2025-09-28 18:38:11 -07:00
parent 413149172b
commit 09267bcd12
2 changed files with 5 additions and 5 deletions

View File

@ -97,6 +97,6 @@ export function draw(ctx: GameContext) {
c.restore() c.restore()
if (dead) { if (dead) {
ctx.centerText("GAME OVER", ctx.height / 2, "red", 50) ctx.centerText("GAME OVER", "red", 50)
} }
} }

View File

@ -20,18 +20,18 @@ export class GameContext {
c.restore() c.restore()
} }
centerText(msg: string, y: number, color = "black", size = 16, font = "C64ProMono") { centerText(msg: string, color = "black", size = 16, font = "C64ProMono") {
const c = this.ctx const c = this.ctx
c.save() c.save()
c.fillStyle = color c.fillStyle = color
c.font = `${size}px ${font}` c.font = `${size}px ${font}`
c.textBaseline = "middle" c.textBaseline = "middle"
const metrics = c.measureText(msg) c.textAlign = "center"
const x = (this.width - metrics.width) / 2 c.fillText(msg, this.width / 2, this.height / 2)
c.fillText(msg, x, y)
c.restore() c.restore()
} }
circ(x: number, y: number, r: number, color = "black") { circ(x: number, y: number, r: number, color = "black") {
const c = this.ctx const c = this.ctx
c.save() c.save()