martin
martin

Reputation: 96889

Check for WebGL support in Dart

I'm playing with WebGL and Dart and I can't figure out how to check if some object has certain method. For example in JavaScript I would check if WebGL is available with window.WebGLRenderingContext but is there a way to the same in Dart?

Upvotes: 4

Views: 1104

Answers (2)

Everton
Everton

Reputation: 13835

EDIT: By the way, there is a Dart way of initializing WebGL context:

RenderingContext gl = canvas.getContext3d();
if (gl == null) {
  print("WebGL: initialization failure");
}

OLD ANSWER:

WebGLRenderingContext gl = canvas.getContext("experimental-webgl");
if (gl == null) {
    print("WebGL is not supported");
    return;
}

This code prints "WebGL is not supported" on Chrome JavaScript Console, when I turn on "Disable WebGL" under chrome://flags/.

However, with WebGL disabled under Firefox (about:config -> webgl.disabled;true), it yields error on Web Console:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLCanvasElement.getContext]

EDIT: This is the fixed bug for Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=645792

Upvotes: 2

Toji
Toji

Reputation: 34498

IIRC one of the goals of Dart was to normalize the inconsistencies of which browser defines what. As such, WebGL will always be "available" in that the interfaces for it will always exist in Dart wether or not the browser you are running on has them defined natively.

Wether or not WebGL is supported is probably best checked the same way you do in Javascript:

WebGLRenderingContext gl = canvas.getContext("experimental-webgl");

If gl is null then WebGL is not supported. Otherwise it is!

(BTW: just because window.WebGLRenderingContext exists in Javascript does NOT mean that WebGL is supported! That only tells you that the browser is capable of dispatching WebGL commands. The system that you are running on may not accept them. It's still best to try creating a context and detect when it fails.)

Upvotes: 8

Related Questions