0

Is there any option to detect in the view function if the browser from which the user is coming is webGL compatible or not?

Ignasi
  • 501
  • 1
  • 5
  • 19

2 Answers2

0

Detecting WebGL is a matter of using JavaScript on the frontend (see Proper way to detect WebGL support?), so I don't think there is a way to do this directly in the backend in a Django view.

What you could do is perform the detection with JavaScript on the first page the user visits and use AJAX to store the result in a server-side variable which you can use in subsequent requests to decide whether to use WebGL features or not.

Community
  • 1
  • 1
voodoo-burger
  • 1,753
  • 2
  • 19
  • 26
0

Expanding on voodoo's answer, here is the javascript to quickly determine wheter a client has webgl support

    var canvas = document.getElementById("canvas");
    var names = ["webgl", "experimental-webgl", "webkit-3d", "mozwebgl"];
    var webgl_supported = false;

    for(var i in names) {
        try {
            gl = canvas.getContext(names[i], { });

            if (gl && typeof gl.getParameter == "function") {
                webgl_supported = true;
                break;
            }
        } catch(e) { }
    }

   var xmlhttp = new XMLHttpRequest();
   xmlhttp.open("POST", "/webgl_enabled_handler");
   xmlhttp.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
   xmlhttp.send(JSON.stringify({webgl_enabled: webgl_supported }));

This assumes that in your html page/template there's a canvas element with id "canvas".

The resulting json would be sent as a POST request to django after page loads

Unfortunately I doubt there's a way to determine webgl support through a general GET request from django

Domenico
  • 138
  • 8
  • 2
    FYI: "webkil-3d" and "mozwebgl" are not needed. They were a thing for a few months in like 2010 and are long gone. Similarly "experimental-webgl" is only used by Edge at this point because it's WebGL implementation is still not spec compliant. – gman Jan 06 '17 at 03:06