【问题标题】:Canvas.drawImage simply just does not draw the imageCanvas.drawImage 只是不绘制图像
【发布时间】:2016-10-29 03:06:13
【问题描述】:

我正在尝试生成一个基于图块的地图,到目前为止它工作得很好,但是在用图像替换所有“测试矩形”来表示地面、路径、一些房屋等之后(在这个万一所有的图像都是相同的),没有一个图像被绘制下来。

我在这里做错了什么?我也得到零错误。

这里的代码被剪断了:

    // 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 输出:

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

【问题讨论】:

    标签: javascript html for-loop canvas drawimage


    【解决方案1】:

    它没有达到预期的效果,因为您将 onload 函数编写为 IIFE。它在分配 src 之前立即执行该函数,并且未将 onload 分配给该函数。

    将第一个 onload 分配更改为:

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

    我所做的只是移除了外部括号。 后面的作业也一样:

        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']; ?>);
        };
    

    我建议删除 ctx = null; 赋值,因为如果它在 onload 事件之前执行,那么它们将无法在画布上绘制图像,而是你会得到 未捕获的类型错误:无法读取 null 的属性“drawImage”

    此外,正如 Blindman67 所指出的,在所有 onload 事件触发后,需要进行更改才能从画布创建图像。 为此,您或许可以使用计数器,类似于 this example,在加载一组图像后会显示警报。

    【讨论】:

    • 您错过的一件事是最后两行将画布转换为带有toDataURL 的图像。在generate 函数退出之前,img.onload 事件不会触发。 OP 仍然不会得到他想要的图像。
    • 我按照您的编写方式更改了它,现在我收到此错误:未捕获的类型错误:无法读取 null 的属性“drawImage”
    • 现在我在我的 html 文件中添加了 &lt;img id="testpic" src="../img/terrain.png"&gt; 并尝试通过删除 onload 函数并将将图像放入数组中的行更改为 imgs[imgIndex] = document.getElementById("testpic"); 甚至使用这个没有图像被绘制的方式...刚刚在 Firefox 中尝试过,似乎这种方式在 Firefox 中有效,但在 Chrome 中无效。
    • @Alex 我在关于 TypeError 和 Blindman67 提出的问题的答案中添加了一些内容。
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2015-09-02
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多