threejs fails to use pre-created canvas

Three.js only throws this error in one place, and luckily it’s doing something very simple: Getting a context from a canvas. It uses HTMLCanvasElement.getContext to do this, and only throws the error if the result is null.

HTMLCanvasElement.getContext will only allow you to request one context type. You can request the same type (or compatible, in the case of webgl and webgl2) again, but it will return the original context created on the canvas. After the first request establishes the in-use context, subsequent requests for incompatible types will return null.

let ctx_2d_1 = mycanvas.getContext( '2d' )
let ctx_2d_2 = mycanvas.getContext( '2d' )
console.log( ctx_2d_1 === ctx_2d_2 ) // true
let ctx_2d = mycanvas.getContext( '2d' )
let ctx_webgl = mycanvas.getContext( 'webgl' )

console.log( ctx_2d ) // CanvasRenderingContext2D
console.log( ctx_webgl  ) // null
let ctx_webgl = mycanvas.getContext( 'webgl' )
let ctx_2d = mycanvas.getContext( '2d' )

console.log( ctx_webgl  ) // WebGLRenderingContext
console.log( ctx_2d ) // null

Because you are creating the 2d context before calling the WebGLRenderer constructor, the constructor can’t get a valid WebGLRenderingContext from the canvas. Instead, it gets a null, and so it throws the error.

If you want to draw 2D on top of 3D (or vice versa), you will either need to layer canvases of different contexts, or use a plane to render the 2D information as a texture within the WebGL (three.js) context.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top