Post

Replies

Boosts

Views

Activity

Reply to Resizing on-screen WebGL canvas in iOS Safari causes memory leak
Here is the jsfiddle code. HTML: <canvas>Your browser does not support HTML5 canvas.</canvas> CSS: { margin:0; padding:0; } html, body { width: 100%; height: 100%; } canvas { display: block; } JavaScript: var _canvas; var _gl; var _buffer; var _shaderProgram; var _coordinatesVar; var _widths = [768, 1024]; var _heights = [1024, 768]; var _dimIndex = 0; var _resizeStartTime = undefined; function _setupScene() { &#9;_canvas = document.getElementsByTagName("canvas")[0]; &#9;_gl = _canvas.getContext("webgl") || _canvas.getContext("experimental-webgl"); &#9;if (!_gl) &#9;&#9;throw new Error("Cannot create WebGL context!"); &#9;var vertices = [ &#9;&#9;-0.5, 0.5, &#9;&#9;-0.5, -0.5, &#9;&#9;0.5, -0.5, &#9;&#9;0.5, -0.5, &#9;&#9;-0.5, 0.5, &#9;&#9;0.5, 0.5, &#9;]; &#9;_buffer = _gl.createBuffer(); &#9;_gl.bindBuffer(_gl.ARRAY_BUFFER, _buffer); &#9;_gl.bufferData(_gl.ARRAY_BUFFER, new Float32Array(vertices), _gl.STATIC_DRAW); &#9; &#9;var vertCode = &#9;&#9;'attribute vec2 coordinates;' + &#9;&#9;'void main(void) {' + &#9;&#9;'&#9;gl_Position = vec4(coordinates, 0.0, 1.0);' + &#9;&#9;'}'; &#9;var vertShader = _gl.createShader(_gl.VERTEX_SHADER); &#9;_gl.shaderSource(vertShader, vertCode); &#9;_gl.compileShader(vertShader); &#9;if (!_gl.getShaderParameter(vertShader, _gl.COMPILE_STATUS)) &#9;&#9;throw new Error(_gl.getShaderInfoLog(vertShader)); &#9;var fragCode = &#9;&#9;'void main(void) {' + &#9;&#9;'&#9; gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);' + &#9;&#9;'}'; &#9;var fragShader = _gl.createShader(_gl.FRAGMENT_SHADER); &#9;_gl.shaderSource(fragShader, fragCode); &#9;_gl.compileShader(fragShader); &#9;if (!_gl.getShaderParameter(fragShader, _gl.COMPILE_STATUS)) &#9;&#9;throw new Error(_gl.getShaderInfoLog(fragShader)); &#9;_shaderProgram = _gl.createProgram(); &#9;_gl.attachShader(_shaderProgram, vertShader); &#9;_gl.attachShader(_shaderProgram, fragShader); &#9;_gl.linkProgram(_shaderProgram); &#9;if (!_gl.getProgramParameter(_shaderProgram, _gl.LINK_STATUS)) &#9;&#9;throw new Error(_gl.getProgramInfoLog(_shaderProgram)); &#9;_gl.useProgram(_shaderProgram); &#9;_coordinatesVar = _gl.getAttribLocation(_shaderProgram, "coordinates"); &#9;_gl.enableVertexAttribArray(_coordinatesVar); &#9;_gl.bindBuffer(_gl.ARRAY_BUFFER, _buffer); &#9;_gl.vertexAttribPointer(_coordinatesVar, 2, _gl.FLOAT, false, 0, 0); &#9; &#9;/* initial auto-resize */ _canvas.width = _widths[_dimIndex]; _canvas.height = _heights[_dimIndex]; &#9;_dimIndex = _dimIndex > 0 ? 0 : 1; } function _drawScene() { &#9;_gl.drawArrays(_gl.TRIANGLES, 0, 6); } function _render(timestamp) { /* The following makes the canvas resizing based on browser window (fullscreen): &#9;&#9;_canvas.width = window.innerWidth; &#9;&#9;_canvas.height = window.innerHeight; &#9;&#9;*/ &#9; &#9;/* The following automatically changes the canvas dimensions back and forth between two dimensions every second: */ if (undefined === _resizeStartTime) &#9; _resizeStartTime = timestamp; &#9;const elapsed = timestamp - _resizeStartTime; &#9;if (elapsed > 1000) { &#9;_canvas.width = _widths[_dimIndex]; &#9;&#9;_canvas.height = _heights[_dimIndex]; &#9;&#9;_dimIndex = _dimIndex > 0 ? 0 : 1; &#9;&#9;_resizeStartTime = undefined; &#9;} &#9;_gl.viewport(0, 0, _canvas.width, _canvas.height); &#9;_gl.clearColor(0.8, 0.8, 0.8, 1.0); &#9;_gl.clear(_gl.COLOR_BUFFER_BIT); &#9; &#9;_drawScene(); &#9; window.requestAnimationFrame(_render); } window.addEventListener("load", function() { _setupScene(); window.requestAnimationFrame(_render); }, false);
Dec ’20