How can I get a field of WebGlContextAttributes?

150 views Asked by At

My case is like this, I just want to get the field of WebGlContextAttributes struct.

let antialias_info = gl.get_context_attributes().unwrap();

match antialias_info.antialias {
   // ...
}

I get the following error:

attempted to take value of method `antialias` on type `web_sys::WebGlContextAttributes` method, not a field.

And I have read the doc WebGlContextAttributes, but still don't know how to get field value of it.

1

There are 1 answers

1
kmdreko On

It does look like the WebGlContextAttributes type is solely designed as a builder to be used with HtmlCanvasElement::get_context_with_context_options() but that disregards that it should also be used to get attributes from WebGlRenderingContext::get_context_attributes(). I see you have already created an issue to address this.

As a workaround, you should be able to use the generic mechanism for getting object properties. From Accessing Properties of Untyped JavaScript Values in the wasm-bindgen guide you can use js_sys::Reflect::get() like so:

let attributes = gl.get_context_attributes().unwrap();
let antialias = js_sys::Reflect::get(&attributes, &JsValue::from_str("antialias"))
    .unwrap()
    .as_bool()
    .unwrap();