gpt4 book ai didi

javascript - 图像未使用 onload 完全加载

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:07 25 4
gpt4 key购买 nike

canvasArray包含拆分图像,在浏览器中打开时 <canvas>元素应该显示在表格中,例如 3x3
第一次打开页面时,屏幕上呈现一个空表(问题),再次单击时显示 <canvas>元素正确,大概是因为它们被缓存了
为什么他们不是第一次显示?我尝试使用 ctx.drawImage(img,xoffset,yoffset);在代码中两次,在 onload 内事件内外
考虑是如果图像在循环外没有正确绘制,一次 onload事件触发它会重试,但它不会..

Image URL<input type="text" id="image" value="images/hippo.jpg"/><br>


var canvasArray = Project.split( $("#image").val() , rowCount * rowCount);
// eg Project.split("https://image.com/funny.jpg, 3, 3");

var Project = {
split: function(imgsrc, tiles) {

var img = new Image(),
canvasArray = new Array(),
imgWidth,
imgHeight,
r,g,b = 0;
img.onload = function(){
xoffset = 0,
offset = 0;

for (var i = 0; i < tiles; i++){

//create canvas element and set attributes and get the canvas context
canvasArray[i] = document.createElement('canvas');
canvasArray[i].setAttribute('width', tileW);
canvasArray[i].setAttribute('height', tileH);
canvasArray[i].setAttribute('id', 'canvas'+i);
var ctx = canvasArray[i].getContext('2d');

ctx.drawImage(img,xoffset,yoffset);

//if i is a multiple of the total number of tiles to a row,
//move down a column and reset the row_col
if((i + 1) % row_col == 0){
yoffset -= tileH;
xoffset =0;
}else{
//otherwise move across the image
xoffset -= tileW;
}
}
};
img.src = imgsrc;

//get the number of tiles in a row or column (row == column )
var row_col = Math.sqrt(tiles),
tileH = img.height / row_col,
tileW = img.width / row_col,
canvasArray = [tiles];

var xoffset = 0,
yoffset = 0;

for (var i = 0; i < tiles; i++){

//create canvas element and set attributes and get the canvas context
canvasArray[i] = document.createElement('canvas');
canvasArray[i].setAttribute('width', tileW);
canvasArray[i].setAttribute('height', tileH);
canvasArray[i].setAttribute('id', 'canvas'+i);
var ctx = canvasArray[i].getContext('2d');

ctx.drawImage(img,xoffset,yoffset);

//if i is a multiple of the total number of tiles to a row,
//move down a column and reset the row_col
if((i + 1) % row_col == 0){
yoffset -= tileH;
xoffset =0;
}else{
//otherwise move across the image
xoffset -= tileW;
}
}

return canvasArray;
}};

最佳答案

需要检查img的complete,选择image本身而不是val,在onload handler中计算图片尺寸,同时图片加载是异步操作,需要使用回调,而不是返回数组:

// eg Project.split("https://image.com/funny.jpg, 3*3,function(canvasArray)");
var Project = {
    split: function(imgsrc, tiles, callback) {

        var canvasArray = new Array(),
            imgWidth, imgHeight, r, g, b = 0;
        var split = function() {
            console.log(1);
            var row_col = Math.sqrt(tiles),
                tileH = Math.round(img.height / row_col),
                tileW = img.width / row_col,

                xoffset = 0,
                yoffset = 0;
            for (var i = 0; i < tiles; i++) {
                //create canvas element and set attributes and get the canvas context
                canvasArray[i] = document.createElement('canvas');
                canvasArray[i].setAttribute('width', tileW);
                canvasArray[i].setAttribute('height', tileH);
                canvasArray[i].setAttribute('id', 'canvas' + i);
                var ctx = canvasArray[i].getContext('2d');

                ctx.drawImage(img, xoffset, yoffset);

                //if i is a multiple of the total number of tiles to a row,
                //move down a column and reset the row_col
                if ((i + 1) % row_col == 0) {
                    yoffset -= tileH;
                    xoffset = 0;
                } else {
                    //otherwise move across the image
                    xoffset -= tileW;
                }
            }
            callback(canvasArray);
        };
        var img = new Image();
        img.src = imgsrc;
        console.log(img.complete);
            if (img.complete) {split()} else  $(img).load(split);
    }
};
var tn = 3;
$("button").bind('click', function() {
    Project.split($("#image").val(), tn * tn, function(canvasArray) {
        for (var i in canvasArray) {
            $('body').append(canvasArray[i]);
            if ((i * 1 + 1) % tn == 0) {
                $('body').append($('<br>'))
            }
        }
        console.log(canvasArray);
    });


});

$("button").click();​

DEMO

关于javascript - 图像未使用 onload 完全加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com