patriciogonzalezvivo / glslCanvas

Simple tool to load GLSL shaders on HTML Canvas using WebGL
http://patriciogonzalezvivo.github.io/glslCanvas/
MIT License
1.97k stars 183 forks source link

Adding webgl2 support and other future releases. #71

Open Larphoid opened 3 years ago

Larphoid commented 3 years ago

Let me start by complimenting on an awesome repository! It's what I was looking for, because I'm pretty sure I would have been trying to work out how to even get WebGL working in the first place.

When trying to prepend 'webgl2' to names[] in function create3DContext(), I kept getting the error "It does not appear your computer can support WebGL...". But I knew WebGL works in my browser, so after some trial and error I found that there was an itty-bitty bug in the code. It turns out that currently the 'experimental-webgl' is always selected, even if 'webgl' would return a valid context because the catch (e) is never reached to break out of the for loop.

Here's what I made of it:

function create3DContext(canvas, optAttribs) { var names = ['webgl2', 'webgl', 'experimental-webgl']; var context = null; for (var i = 0; i < names.length; ++i) { try { context = canvas.getContext(names[i], optAttribs); if (context) break; } catch (e) { if (context) break; /// Can probably just leave this out all together. } } return context; }

Hope this helps.

Cheers.

PS: Wow, the code formatting (Insert code) realy made it unreadable 🤮. Have seen better on other sites.