There is an immediate workaround, but the current behavior is one that could be considered suboptimal.
The multiview feature introduced in version 4.1 is very exciting, and I’ve begun to write an API around it.
If I create a canvas:
<canvas id='renderCanvas' width='500' height='500' />
for later rendering, and then proceed to create a ‘working’ canvas to start the Engine, and later register the renderCanvas as view, roughly as follows:
const workCanvas = document.createElement('canvas')
const canvas = document.getElementById('renderCanvas');
const engine = new B.Engine(workCanvas, true)
const [camera, scene] = createScene(canvas, engine)
engine.inputElement = canvas
let view = engine.registerView(canvas, camera)
You can assume createScene
includes all needed setup code, as per the online example.
Then, to my surprise, the canvas that originally had a width and height of 500 is mutated within registerView and now contains a height and width of 0, which crashes the program.
This can then be traced to lines 66-70 here, where the current canvas attributes are used to mutate the canvas-to-be-registered attributes. As in my example, I only created the element, the width and height are set to 0. A ‘working’ canvas is created just like this within the online example, but there it seems the CSS ensures that the width and height is not 0 when the mutation step occurs. My code is not relying on CSS here, and may often rely on external methods, like React, for setting width/height. I imagine this will be similar for many users down the road.
A simple fix is to follow up the function call with:
canvas.setAttribute('width', '500')
canvas.setAttribute('height', '500')
Once I made these changes, the code functioned as expected.
I’m concerned that this is going to lead to multiple difficulties down the road, especially if the canvas needs to resize.
Can the code be modified so that if the canvas-to-be-registered has an existing width and height, it is not unexpectedly mutated? I did not submit an issue on GitHub as I cannot follow any of the existing protocols, (i.e., unsure how to create a playground demonstrating this), and it suggests to post on the forum as well. This seemed a natural way to get a conversation going.
`