user1668814
user1668814

Reputation: 513

Canvas drawImage is Not Working

This picture will not draw to the canvas. It is being loaded and I see it when body.appendChild(img); is called. Does anyone had an idea why it is not showing in canvas? I'm following the code example from the html5 course on udacity. Thank you for any help!

<!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>

Upvotes: 1

Views: 2652

Answers (1)

user1600124
user1600124

Reputation: 440

img.onload = onImageLoad;

not onImageLoad(); you're executing the function if you include the parenthesis

Upvotes: 3

Related Questions