gpt4 book ai didi

html - getContext ('webgl' ) 与 getContext ('3d' ) 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 13:21:07 27 4
gpt4 key购买 nike

我开始学习WebGL,因为我找到了一些旧教程,我不知道在2014年什么是正确的方法?

我启动了 <canvas> (基本),并且在教程中他们说了类似的话:

use getContext('2d') and if you want to use WebGL then you put 3d instead of 2d

但现在我正在学习,我发现教程谈论 getContext('webgl')而不是 getContext('3d') .

句法有变化吗?

还有 this文章说没有真正的 3D,但他们只使用光线转换?!

最佳答案

Mozilla Developer Netowrk (MDN) 文档是这样说的:

getContext(in DOMString contextId) RenderingContext Returns a drawing context on the canvas, or null if the context ID is not supported. A drawing context lets you draw on the canvas. Calling getContext with "2d" returns a CanvasRenderingContext2D object, whereas calling it with "experimental-webgl" (or "webgl") returns a WebGLRenderingContext object. This context is only available on browsers that implement WebGL.

结果:
|语境 | Chrome(网络工具包)|火狐浏览器(gekko) |
| ------------------ | ---------------------- | ---------------------- |
|二维 | CanvasRenderingContext2D | CanvasRenderingContext2D |
| 3d |空 |空 |
|网站 | WebGLRenderingContext | WebGLRenderingContext |
|实验-webgl | WebGLRenderingContext |空 |

我建议阅读 webgl维基:http://www.khronos.org/webgl/wiki/FAQ

这是完整的 What is the recommended way to initialize WebGL?部分:(尽管我建议您直接从 wiki 阅读它,以防它发生变化!)


推荐的初始化 WebGL 的方法是什么?

建议您检查初始化是否成功。如果 WebGL 初始化失败,建议您区分失败是因为浏览器不支持 WebGL 还是由于其他原因导致的失败。如果浏览器不支持 WebGL,则向用户显示指向“http://get.webgl.org”的链接。如果 WebGL 由于某些其他原因失败,则向用户显示指向“http://get.webgl.org/troubleshooting/”的链接

您可以通过检查 WebGLRenderingContext 是否存在来确定浏览器是否支持 WebGL。

if (window.WebGLRenderingContext) {
// browser supports WebGL
}

如果浏览器支持 WebGL 且 canvas.getContext("webgl") 返回 null,则 WebGL 因用户浏览器以外的其他原因(无 GPU、内存不足等)而失败

  if (!window.WebGLRenderingContext) {
// the browser doesn't even know what WebGL is
window.location = "http://get.webgl.org";
} else {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("webgl");
if (!ctx) {
// browser supports WebGL but initialization failed.
window.location = "http://get.webgl.org/troubleshooting";
}
}

注意:您必须检查浏览器是否支持 WebGL 以了解从 canvas.getContext() 获取 null 意味着 There is a wrapper that will do all of this for you here.

使用包装器的例子

<html>
<body>
<script type="text/javascript" src="localpath/webgl-utils.js"></script>
<script>
function init() {
canvas = document.getElementById("c");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
return;
}
...
}

window.onload = init;
</script>
<canvas id="c"></canvas>
</body>
</html>

关于html - getContext ('webgl' ) 与 getContext ('3d' ) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com