WebGLRenderer: Fix for runtime error that happens when null is provided as context #23969
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
If someone passes a context to the
WebGLRenderer
, but the context isnull
(because the user's device doesn't supportwebgl
, orwebgl2
, or whatever context they tried to create), we have a runtime error, sayingTypeError: _context is null
.The problem was introduced with this commit: fb9d575
To make things a little bit more confusing, the official documentation says that the default value for
context
isnull
: https://threejs.org/docs/index.html?q=webg#api/en/renderers/WebGLRendererWhich is, of course, technically incorrect. It's not
null
, butundefined
by default. The root of the problems is that some of the checks inside theWebGLRenderer
's "constructor" is checking whether thecontext
isundefined
. If it's not, it assumes it has a valid value, although it can be easilynull
. Becausecanvas.getContext
either returns a context, ornull
.For a long time this was the official recommended way for creating
WebGLRenderer
with WebGL 2.0 context:I know this can be now considered "legacy code", but still, if
webgl2
is not available on a device, and someone still uses the code like this, it will give the user an error instead of trying to fall back towebgl
1.This PR replaces the strict comparisonscontext !== undefined
withcontext != undefined
, and provides a technically more accurate default value in the documentation.