简体   繁体   English

带有JavaScript类属性的canvas drawImage错误

[英]canvas drawImage error with JavaScript Class Attribute

I am trying to use class on drawImage function. 我试图在drawImage函数上使用类。

But it seems that drawImage function couldn't get the attribute img of variable monster. 但是似乎drawImage函数无法获取可变Monster的属性img。

I have no idea what would caused this error. 我不知道什么会导致此错误。

Not quite understand why browser give me the below error code ... please help me. 不太明白为什么浏览器会给我下面的错误代码...请帮助我。

Uncaught TypeError: Cannot read property 'img' of undefined. 未捕获的TypeError:无法读取未定义的属性'img'。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Tower Defense</title>
    <style>
        canvas {
            padding: 0;
            margin: auto;
            display: block;
        }
    </style>
</head>

<body>
    <script>
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        canvas.width = 500;
        canvas.height = 500;
        document.body.appendChild(canvas);

        function Monster(img, x = 0, y = 0, w,h) {
            this.img = img;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }

        var monster;

        var DrawMonster = function(mons) {
            ctx.drawImage(mons.img, mons.x, mons.y, mons.w, mons.h);
        }

        var PreLoadImages = function() {
            var img = new Image();
            img.addEventListener("load", function() {     // or img.onload = function() { ...
                monster = new Monster(this, 0, 0, 100, 100);            
            });
            img.src = "res/Mons_Pirate.jpg";
        }

        PreLoadImages();
        DrawMonster(monster);

    </script>
</body>

</html>

Please change the link (res/Mons_Pirate.jpg) of image while testing. 测试时请更改图像的链接(res / Mons_Pirate.jpg)。

DrawImage should be inside the image load function.Instance of Monster is created only inside the Image load function.Image will be loaded in async. DrawImage应该位于图像加载函数内部,Monster实例仅在图像加载函数内部创建,图像将异步加载。

   <!DOCTYPE html>
    <html lang="en">

     <head>
      <meta charset="UTF-8">
      <title>Tower Defense</title>
      <style>
        canvas {
            padding: 0;
            margin: auto;
            display: block;
        }
       </style>
     </head>

   <body>
       <script>
           var canvas = document.createElement("canvas");
           var ctx = canvas.getContext("2d");
           canvas.width = 500;
           canvas.height = 500;
           document.body.appendChild(canvas);

        function Monster(img, x = 0, y = 0, w,h) {
            this.img = img;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }

        var monster;

        var DrawMonster = function(mons) {
            ctx.drawImage(mons.img, mons.x, mons.y, mons.w, mons.h);
        }

        var PreLoadImages = function() {
            var img = new Image();
            img.addEventListener("load", function() {     // or img.onload = 
            function() { 
                monster = new Monster(this, 0, 0, 100, 100);            
                DrawMonster(monster);
            });
            img.src = "res/Mons_Pirate.jpg";
        }

        PreLoadImages();

    </script>
</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM