Loading an image onto a canvas with javaScript

前端 未结 4 1872
我寻月下人不归
我寻月下人不归 2020-12-30 02:34

Hi all im currently testing using the canvas element to draw all of the backgrounds(i will add effects later to these images later and is the reason im not using css to load

4条回答
  •  春和景丽
    2020-12-30 02:56

    move the onload event listener to before you set the src for the image.

        var img1 = new Image();
    
        //drawing of the test image - img1
        img1.onload = function () {
            //draw background image
            ctx.drawImage(img1, 0, 0);
            //draw a box over the top
            ctx.fillStyle = "rgba(200, 0, 0, 0.5)";
            ctx.fillRect(0, 0, 500, 500);
    
        };
    
        img1.src = 'img/Home.jpg';
    

提交回复
热议问题