To use the on-screen canvas rendering, instantiate WorldOnscreen by passing a configuration object. The constructor accepts several lifecycle callbacks to handle initialization, theme loading, roll results, and die removal.
Key configuration requirements:
canvas: The HTML canvas element to render into.width and height: Dimensions for the canvas.options: A sub-object containing dice-specific settings (e.g., assetPath, enableShadows, scale).
Available callbacks in the options object:
onInitComplete: Called when the BabylonJS scene is ready.onThemeLoaded: Called when a theme has been successfully loaded.onRollResult: Called when a die stops moving and its value is determined.onRollComplete: Called when all dice in a roll have come to rest.onDieRemoved: Called when a die is removed from the scene.
const world = new WorldOnscreen({
canvas: myCanvasElement,
width: 800,
height: 600,
onInitComplete: () => console.log('Ready!'),
onRollResult: ({ rollId, value }) => console.log(`Die ${rollId} rolled: ${value}`),
options: {
assetPath: '/assets/dice/',
enableShadows: true,
scale: 1
}
});