4

我正在尝试使用 philogl 库的东西,当我写的时候,

<!DOCTYPE html>

<html>
<head>
    <title>PGL2</title>
    <script type="text/javascript" src="PhiloGL.js"></script>
    <script type="text/javascript">
    function webGLStart(){
        alert('I m alive');
    }
</script>
</head>

<body onload="webGLStart();">
<canvas id="c" style="width:500px; height:500px;"></canvas>


</body>
</html>

一切正常,但是如果我在其中写一些 phillogl 之类的,

<!DOCTYPE html>

<html>
<head>
    <title>PGL2</title>
    <script type="text/javascript" src="PhiloGL.js"></script>
    <script type="text/javascript">
    function webGLStart(){
        var triangle = new PhiloGL.O3D.Model({
            vertices: [[0,1,0],[-1,-1,0],[1,-1,0]],
            colors: [[1,0,0,1],[0,1,0,1],[0,0,1,1]]
            });
        var square = new PhiloGL.O3D.Model({
            vertices: [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0]],
            colors: [[0.5,0.5,1,1],[0.5,0.5,1,1]],[0.5,0.5,1,1]
            });
    }
</script>
</head>

<body onload="webGLStart();">
<canvas id="c" style="width:500px; height:500px;"></canvas>


</body>
</html>

chrome 和 firefox 给我一个错误,说 webGLStart() 没有定义。我的代码有什么问题?

4

2 回答 2

8

这一行:

        colors: [[0.5,0.5,1,1],[0.5,0.5,1,1]],[0.5,0.5,1,1]

在语法上不正确:结束的“]”在错误的位置。因此,可以说函数定义“失败”,并且该函数并不真正存在。

我真的刚刚醒来,所以我不确定我有什么问题,我能发现它。

于 2011-02-25T13:04:19.857 回答
0

您使用此“philogl”的方式一定有错误,导致无法完全解释该功能。特别是,您的方括号有语法错误。然后该函数保持未定义。

将来,您可以在 Firebug(或您喜欢的浏览器的等效项)中打开错误控制台,以查看您做错了什么。

此外,更喜欢document.onload = function() { ... }在脚本中,而不是在 HTML 标记中嵌入脚本。

于 2011-02-25T13:05:16.780 回答