BabylonJS/Babylon.js · error

getFontOffset is not implemented

Error message

getFontOffset is not implemented

What it means

AbstractEngine.getFontOffset returns font metrics (ascent/height/descent) needed for text rendering (GUI/dynamic textures). The pure base class throws because measuring font offset requires a rendering canvas with 2d context; only concrete engines implement it.

Source

Thrown at packages/dev/core/src/Engines/abstractEngine.pure.ts:2647

     * @param bufferWidth destination buffer width
     * @param bufferHeight destination buffer height
     */
    public resizeImageBitmap(image: HTMLImageElement | ImageBitmap, bufferWidth: number, bufferHeight: number): Uint8Array {
        throw new Error("resizeImageBitmap is not implemented");
    }

    /**
     * Get the current error code of the webGL context
     * @returns the error code
     */
    public abstract getError(): number;

    /**
     * Get Font size information
     * @param font font name
     */
    public getFontOffset(font: string): { ascent: number; height: number; descent: number } {
        throw new Error("getFontOffset is not implemented");
    }

    protected static _CreateCanvas(width: number, height: number): ICanvas {
        if (typeof document === "undefined") {
            return <ICanvas>(<any>new OffscreenCanvas(width, height));
        }
        const canvas = <ICanvas>(<any>document.createElement("canvas"));
        canvas.width = width;
        canvas.height = height;
        return canvas;
    }

    /**
     * Create a canvas. This method is overridden by other engines
     * @param width width
     * @param height height
     * @returns ICanvas interface
     */

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Use a real engine (WebGL/WebGPU) for text/GUI layout
  2. Override getFontOffset in your engine subclass, measuring with a 2d canvas (measureText with actualBoundingBoxAscent/Descent)
  3. Stub the method in tests returning fixed metrics
  4. Precompute font metrics and inject them instead of querying the engine

Example fix

// before
const off = nullEngine.getFontOffset('12px Arial'); // throws
// after
nullEngine.getFontOffset = (font: string) => {
  const ctx = document.createElement('canvas').getContext('2d')!;
  ctx.font = font;
  const m = ctx.measureText('Mg');
  return { ascent: m.actualBoundingBoxAscent, height: m.actualBoundingBoxAscent + m.actualBoundingBoxDescent, descent: m.actualBoundingBoxDescent };
};
Defensive patterns

Strategy: fallback

Validate before calling

if (engine.getFontOffset === AbstractEngine.prototype.getFontOffset) {
  // base version throws; use custom metrics via canvas measureText
}

Type guard

function canMeasureFont(engine: AbstractEngine): boolean {
  return engine.getFontOffset !== AbstractEngine.prototype.getFontOffset;
}

Try / catch

try {
  off = engine.getFontOffset(font);
} catch (e) {
  if (e instanceof Error && e.message.includes('getFontOffset is not implemented')) {
    off = measureFontOffsetWithCanvas(font); // 2d measureText fallback
  } else throw e;
}

Prevention

When it happens

Trigger: Calling engine.getFontOffset(font) — directly or indirectly through GUI text blocks / DynamicTexture text measurement — on an engine lacking the override (pure engine, NullEngine, minimal builds).

Common situations: Server-side rendering of GUI with NullEngine; unit tests touching Babylon.GUI text layout in headless environments; custom engine subclasses missing getFontOffset.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/55354c3729755d69. Report an issue: GitHub.