如何设置WebGL相机的宽高比与画布(窗口)的宽高比?
我已经使用以下方法让我的 webGL 内容填充浏览器窗口:
css:
canvas
{
float: left;
width: 100%;
height: 100%;
}
而且:
function webGLStart() {
var canvas = document.getElementById("cool3D");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
...
到目前为止,无论窗口大小还是全屏,这对于保持完全覆盖都是有效的。问题是分配相机的纵横比以避免失真。
如何将其设置为〜“window.innerWidth/window.innerHeight”? OnWindow 调整大小?
I've made my webGL content fill the browser window using:
css:
canvas
{
float: left;
width: 100%;
height: 100%;
}
And also:
function webGLStart() {
var canvas = document.getElementById("cool3D");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
...
So far, this is effective for maintaining full coverage, no matterthe size of the window or fullscreen. The problem is assigning the aspect ratio of the camera to avoid distortion.
How can I set it to ~"window.innerWidth/window.innerHeight"? OnWindowResize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与视口无关。它实际上是透视矩阵的函数。您的代码会有所不同,但在我自己的每次检测到窗口大小调整时,我都会执行以下操作:
更新我的投影矩阵以使用适当的宽高比(宽度/高度)。
This doesn't have anything to do with the viewport. It's actually a function of the perspective matrix. Your code will vary, but in my own every time I detect that the window is resized I do the following:
Which updates my projection matrix to use the appropriate aspect ratio (width/height).