画布drawImage不起作用

时间:2013-09-07 02:36:21

标签: javascript html5 canvas

这张照片不会画到画布上。正在加载,我在body.appendChild(img)时看到它;叫做。有没有人知道为什么它没有在画布上显示?我正在关注udacity的html5课程中的代码示例。谢谢你的帮助!

<!DOCTYPE html>
    <html>
        <head>
            <title>First Game</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link type="text/css" rel="stylesheet" href="TestCanvasCSS.css"/>
        </head>
        <body id="body">
            <script>
                var canvas;
                var ctx = null;
                var img = null;
                var body = null;

                setup = function() {
                    body = document.getElementById("body");
                    canvas = document.createElement("canvas");

                    ctx = canvas.getContext('2d');

                    //canvas.setAttribute("class","canvasMain");
                    canvas.width = window.innerWidth;
                    canvas.height = window.innerHeight;
                    body.appendChild(canvas);

                    img = new Image();
                    img.onload = onImageLoad();
                    img.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
                };

                onImageLoad = function() {
                    console.log("Image has loaded");
                    body.appendChild(img);
                    ctx.drawImage(img, 0, 0);
                };
                setup();
            </script>
            <!--  <script type="text/javascript" src="TestCanvas.js"> </script> -->
        </body>
    </html>

1 个答案:

答案 0 :(得分:3)

img.onload = onImageLoad;
如果包含括号

不是onImageLoad();您正在执行该功能