简体   繁体   中英

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:

 [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 . It is executing the function immediately, before the src is assigned, and onload is not getting assigned to the function.

Change the first onload assignment to this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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