简体   繁体   English

Canvas.drawImage只是不绘制图像

[英]Canvas.drawImage simply just does not draw the image

i'm trying to generate a tile-based-map, which works just fine so far, but after replacing all the "test-rectangles" with images to represent the ground, the path, some houses etc. (in this case its the same image for all of them), not a single image gets drawn down. 我正在尝试生成一个基于图块的地图,到目前为止工作得很好,但在用图像替换所有“测试矩形”以表示地面,路径,一些房屋等(在这种情况下是它的所有这些图像相同的图像),没有一个图像被绘制。

What am i doing wrong here? 我在这做错了什么? I also get zero errors. 我也得到零错误。

Heres the code snipped: 下面是代码剪辑:

    // generate a large map     
    Map.prototype.generate = function(){
        var ctx = document.createElement("canvas").getContext("2d");        
        ctx.canvas.width = this.width;
        ctx.canvas.height = this.height;        

        var rows = ~~(this.width/<?php echo $GAME['map']['tileSize'] + $GAME['map']['tileBorder']; ?>) + 1;
        var columns = ~~(this.height/<?php echo $GAME['map']['tileSize'] + $GAME['map']['tileBorder']; ?>) + 1;

        ctx.save(); 

        // Here i wanted to check if the image gets drawn right besides the player
        var testImage = document.createElement('img'); // Also tried new Image();
        testImage.onload = (function () {
            console.log(testImage + "-test");
            ctx.drawImage(testImage, 1000, 1000, <?php echo $GAME['map']['tileSize']; ?>, <?php echo $GAME['map']['tileSize']; ?>);
        }());
        testImage.src = "http://192.168.0.140/img/terrain.png";

        var imgs = [];
        var imgIndex = 0;

        <?php echo "for (var y = 0, i = ".$tilesToLoad['xStart']."; i < ".($tilesToLoad['xStart'] + $GAME['map']['chunkSize'])."; y+=".($GAME['map']['tileSize'] + $GAME['map']['tileBorder']).", i++) {"; ?>

            <?php echo "for (var x = 0, j = ".$tilesToLoad['yStart']."; j < ".($tilesToLoad['yStart'] + $GAME['map']['chunkSize'])."; x+=".($GAME['map']['tileSize'] + $GAME['map']['tileBorder']).", j++) {"; ?>
                imgs[imgIndex] = document.createElement('img');                 
                imgs[imgIndex].onload = (function () {
                    console.log(imgs[imgIndex] + "-" + imgIndex + "-" + x + "-" + y);
                    ctx.drawImage(imgs[imgIndex], x, y, <?php echo $GAME['map']['tileSize']; ?>, <?php echo $GAME['map']['tileSize']; ?>);
                }());
                imgs[imgIndex].src = "http://192.168.0.140/img/terrain.png";
                imgIndex += 1;
            }
        }

        ctx.restore();  

        // store the generate map as this image texture
        this.image = new Image();
        this.image.src = ctx.canvas.toDataURL("image/png");                 

        // clear context
        ctx = null;
    }

console.log output: console.log输出:

 [object HTMLImageElement]-test
 [object HTMLImageElement]-0-0-0
 [object HTMLImageElement]-1-41-0
 [object HTMLImageElement]-2-82-0
 [object HTMLImageElement]-3-123-0

It is not having the desired effect because you have written the onload function as an IIFE . 它没有达到预期的效果,因为您已将onload函数写为IIFE It is executing the function immediately, before the src is assigned, and onload is not getting assigned to the function. 它在分配src之前立即执行该函数,并且没有将onload分配给该函数。

Change the first onload assignment to this: 将第一个onload分配更改为:

    testImage.onload = function () {
        console.log(testImage + "-test");
        ctx.drawImage(testImage, 1000, 1000, <?php echo $GAME['map']['tileSize']; ?>, <?php echo $GAME['map']['tileSize']; ?>);
    };

All I have done there is removed the outer brackets. 我在那里做的就是删除外括号。 And the same for the later assignment: 后来的任务也是如此:

    imgs[imgIndex].onload = function () {
            console.log(imgs[imgIndex] + "-" + imgIndex + "-" + x + "-" + y);
            ctx.drawImage(imgs[imgIndex], x, y, <?php echo $GAME['map']['tileSize']; ?>, <?php echo $GAME['map']['tileSize']; ?>);
    };

I suggest removing the ctx = null; 我建议删除ctx = null; assignment because if this executes before the onload events, then they will fail to draw the images on the canvas, instead you will get Uncaught TypeError: Cannot read property 'drawImage' of null 赋值因为如果这在onload事件之前执行,那么它们将无法在画布上绘制图像,而是会得到Uncaught TypeError:无法读取null的属性'drawImage'

Also, as Blindman67 pointed out, a change is needed to get the image to be created from the canvas after all the onload events have fired. 另外,正如Blindman67指出的那样,需要进行一些更改,以便在所有onload事件被触发后从画布中创建图像。 To do this you could perhaps use a counter, similar to this example where an alert is shown after a set of images are loaded. 为此,您可以使用计数器,类似于此示例 ,其中在加载一组图像后显示警报。

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

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