[whatwg] [canvas] Proposal for supportsContext

Ashley Gullen ashley at scirra.com
Mon Sep 10 15:01:11 PDT 2012


On 10 September 2012 20:39, Dean Jackson <dino at apple.com> wrote:

>
> On Sep 10, 2012, at 12:35 PM, Ashley Gullen <ashley at scirra.com> wrote:
>
> Can't Modernizr just lazy load the WebGL context?  (i.e. only try to
> create a context if the web page actually asks if WebGL is supported)
>
>
> Yes, it could. But we don't control Modernizr or any other scripts people
> might use. I'd rather provide something at the browser-level to protect
> from bad practice than expect every page to behave nicely.
>

We can't expect everyone to use good practices, but I think we can expect
authors of widely-used libraries to use good practice!

I think browser makers would still be tempted to implement
supportsContext() in terms of creating a context and seeing if there's an
error, since that's the only way to be 100% sure the answer is correct.
 This does not really solve anything.  Also, realistically, any web app
actually interested in using WebGL will first create a WebGL context, and
if that fails then fall back to something else, so I'm not sure Modernizr
actually need a better test for this than their new fixed code.

Hiding/showing stuff in Javascript also doesn't really get around the need
that you have to create a context to know for sure.  Also, nothing so far
could be extended to determine software vs. hardware rendering.

Perhaps an approach could be taken similarly to HTMLMediaElement's
canPlayType().  supportsContext() could return a string, which is one of:
"probably" - the context appears to be supported (but this is not a
guarantee)
"maybe" - it is impossible to tell whether the context is supported without
creating it.
"" (empty string) - the context is definitely not supported.
This avoids returning a simple true/false which implies some kind of
guarantee in the true case.

For supportsContext(), it could return some other strings as well since
there are cases where we know more than HTMLMediaElement:
"yes" - context definitely can be created. I think this would apply for
"2d", since I believe in all browsers regardless of the setup it can fall
back to a software renderer and the content will work, even if slowly.
 This also applies to Chrome's SwiftShader support for WebGL, meaning
creating a WebGL context is also guaranteed to succeed.
"slow" or "software-rendered" or "emulated" or some other term that needs
careful definition: context can definitely be created but uses software
rendering, e.g. meaning Chrome's SwiftShader will be used for WebGL, or
possibly that the user's system does not support hardware-accelerated
canvas "2d".  Since this can apply simultaneously with "yes", perhaps it
could also return "yes emulated" or similar.

Ashley


More information about the whatwg mailing list