I have switched to modules in JavaScript and now I am experiencing the following issue:
I have a helper glutils.js
file with a collection of js function. One of the functions looks like this:
function updateGLTexture(texture, format, genMipmaps, data)
{
gl.bindTexture(gl.TEXTURE_2D, texture);
//some code
gl.bindTexture(gl.TEXTURE_2D, 0);
}
The file is included from the html page like this:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="#">
<meta charset="utf-8" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="gluitls.js"></script>
<script type="module" src="webgl_test.js"></script>
</head>
<body>
</body>
</html>
Inside webgl_test.js
I initialize gl context and store it in a global variable:
var gl,canvas = null;
function init()
{
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
gl = canvas.getContext('webgl2', { antialias: false });
//some more gl related setup and call to:
//updateGLTexture();
}
Then at some point calling updateGLTexture
which uses gl
object to access WebGL API but gl
is not seen by the scope of the function. It did work before I have switched to modules.What is the elegant way to solve this, without passing gl
as a param into the helper functions.