/** * Minimal canvas2d→WebGL text helper for frameworkv3 (GL) scenes. * Scene.context is now a WebGLRenderingContext, so fillText/measureText no longer work. * We rasterize once to a 2D canvas and draw via the framework `image` sprite path. */ class GlText { static _cache = {}; /** * @param {Scene} scene * @param {string} text * @param {number} x center x in scene space * @param {number} y baseline-ish center y in scene space * @param {{ font?: string, fill?: string, maxWidth?: number }} [opts] */ static drawCentered (scene, text, x, y, opts) { opts = opts || {}; const font = opts.font || "20px Monospace"; const fill = opts.fill || "black"; const key = font + "|" + fill + "|" + text; let sprite = GlText._cache[key]; if (!sprite) { const measure = document.createElement("canvas"); const mctx = measure.getContext("2d"); mctx.font = font; const metrics = mctx.measureText(text); const w = Math.max(1, Math.ceil(metrics.width) + 4); const h = 28; const c = document.createElement("canvas"); c.width = w; c.height = h; const ctx = c.getContext("2d"); ctx.font = font; ctx.fillStyle = fill; ctx.textBaseline = "middle"; ctx.textAlign = "left"; ctx.fillText(text, 2, h / 2); sprite = new image(c, 1, 1); GlText._cache[key] = sprite; } const dw = opts.maxWidth ? Math.min(sprite.width, opts.maxWidth) : sprite.width; const dh = sprite.height; sprite.draw(scene, x, y, dw, dh, 0, 0, 0, 1, 0); } }