如何启动Raphael.js?

Jac*_*ope 3 raphael

        <script type="text/javascript" src="raphael.js"></script>
    <script>
        var paper = new Raphael("holder", 320, 200);
        function testPaper(){
            var width10 = paper.width * 0.1;
            var height10 = paper.height * 0.1;
            var width80 = paper.width * 0.8;
            var height80 = paper.height * 0.8
            var c = paper.rect( width10, height10, width80, height80, Math.min( width10, height10 ) );
    }
    </script>
</head>
<body onload = "testPaper()">
    <div id="holder"></div>
</body>
Run Code Online (Sandbox Code Playgroud)

上面的代码有什么问题?我一直在努力让Raphael工作一个多小时,但它总是抱怨:

未捕获的TypeError:无法读取未定义的raphael.js的属性“ x”:11

use*_*740 6

使用Raphael的脚本可能已在创建“ holder”元素之前运行。

ready / onload事件中创建Raphael,或者像下面这样简单地排列HTML:

<body>
    <div id="holder"></div> <!-- also use correct closing tag -->
    <script src="raphael.js"></script>
    <script>
        var paper = new Raphael("holder", 320, 200);
        // ..
    </script>
</body>
Run Code Online (Sandbox Code Playgroud)