Retrospective changes: viewports

The WebGL spec recently changed to require that you call the gl.viewport before rendering to your canvas; it was previously done for you automatically, but the people working on the spec realised that this could cause problems in certain specific circumstances. The function tells WebGL which part of the rendered image should be shown on the canvas, and as most of the time this will be “all of it”, the code to use will usually be:

      gl.viewport(0, 0, canvas.width, canvas.height);

Now, if you don’t do this call before you start rendering, the results are undefined, and Kevin Theisen (creator of the excellent Chemdoodle) just let me know that last night’s WebKit builds — Safari and Chromium both — now require it. So I’ve retrospectively changed all of the lessons and updated the Cookbook. If you’re building WebGL pages, you’ll need to do the same thing.

(Also: if you’re supporting resizable canvases, you’ll need to add code to re-set the viewport whenever the canvas size changes, not just when you initialise it.)

You can leave a response, or trackback from your own site.

One Response to “Retrospective changes: viewports”

  1. murphy says:

    Firefox just kill the canvas (everything’s going black) when I resize it. Any idea why this happens? Safari is doing fine.

Leave a Reply

Subscribe to RSS Feed Follow Learning WebGL on Twitter