在画布上绘制图像阵列

draw array of images to the canvas

本文关键字:图像 阵列 绘制      更新时间:2023-09-26

我正在尝试为画布元素绘制一个图像数组,我的数据:image返回一个空图像。

var imgArray = ['images/image1.png','images/image2.png'];
        for(i = 0; i < 2; i++){
            var canvas = document.getElementById('textCanvas');
            var context = canvas.getContext("2d");
            var imageObj = new Image();
            imageObj.setAtX = i * 10;
imageObj.setAtY = i * 10;
imageObj.onload = function() {
 context.drawImage(this, this.setAtX, this.setAtY);
};
        }
    img = canvas.toDataURL("image/png");

您从未设置图像的来源:

var imageObj = new Image();
imageObj.src = imgArray[i]; // << addeed
imageObj.setAtX = i * 10;
imageObj.setAtY = i * 10;
imageObj.onload = function() {
 context.drawImage(this, this.setAtX, this.setAtY);
};