[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Public WebGL] How to set a canvas backing store to display units?
On Tue, Jun 12, 2012 at 6:12 PM, Gregg Tavares (çç) <firstname.lastname@example.org> wrote:
> So now that there are a few hd-dpi devices any thoughts has to how to set a canvas's backing store to 1x1 to display pixels?
> Ideally it would be as simple as
> widthInCSSUnits = canvas.clientWidth;
> heightInCSSUnits = canvas.clientHeight;
> canvas.width = convertCSSUnitsToDeviceUnits(widthInCSSUnits);
> canvas.height =convertCSSUnitsToDeviceUnits(widthInCSSUnits);
These properties are already in CSS units, not device pixels.Â If you say "canvas.width = 100; canvas.height = 150;" on eg. an iPhone Retina display (two device pixels to one CSS pixel), the backing store created should be 200x300.Â This is how HTMLCanvasElement is specced, and it's how 2D canvas already works, so it's how WebGL should work as well.Â The size of the underlying buffer is exposed with drawingBufferWidth and drawingBufferHeight, and aren't necessarily the same (otherwise there'd be no need for those properties).
This isn't a perfect system: it's easy for people developing only on
1:1 systems to get things wrong.Â In WebGL's case, you'd need to use drawingBufferHeight rather than canvas.height any time you use readPixels, as well as if you're creating renderbuffers that you mean to be the same resolution as the backing store.Â 2D canvas has the same issues.Â This is annoying, but I don't think WebGL should deviate by itself; if this issue is to be improved, it should be
uniformly across all context types (on whatwg).
That aside, the language in the HTMLCanvasElement spec probably needs some adjustment.Â It says that @width and @height determine the coordinate space, which only makes sense for 2D canvas.Â For WebGL, this probably should say "the size of the viewport" (eg. the value you pass to gl.viewport is canvas.width/canvas.height, not gl.drawingBufferWidth and gl.drawingBufferHeight).Â If you think this needs to be clarified, it probably needs to be raised on whatwg, too.