简体   繁体   English

在Chrome中使用toDataURL是否有其他选择?

[英]Are there any alternatives for using toDataURL in Chrome?

The main problem is that I have to work with many images. 主要问题是我必须处理许多图像。 And I can't use crossOrigin Attribute for all of them. 而且我不能对所有这些都使用crossOrigin Attribute。

My code looks like this:
<script>
var c=document.getElementById('example');
var ctx=c.getContext('2d');
var LeftHand = new Image();
LeftHand.id="imq1";
var RightHand = new Image();
RightHand.id="imq2";
var Body = new Image();
Body.id="imq6";

boyBody.src = "https://getout-s3.s3.amazonaws.com/baseBody/boy-02.png";
LeftHand.src = "https://getout-s3.s3.amazonaws.com/NK4XtQvkZ4MGctZf_.hand(unisex)_13.png ";
RightHand.src = "https://getout-s3.s3.amazonaws.com/OPdFPcU2sORgNmTy_.hand(unisex)_9.png ";
Body.src = "https://getout-s3.s3.amazonaws.com/HRZqrTYSdJXGedgX_.Body_(m)7.png ";

boyBody.onload = function() {
ctx.drawImage(boyBody, 0, 0, boyBody.width/2, boyBody.height/2);
ctx.drawImage(LeftHand, (899 - LeftHand.width/2)/2, (867 - LeftHand.height/2)/2, LeftHand.width/2, LeftHand.height/2);
ctx.drawImage(Underwear, (599 - Underwear.width/2)/2, (845 - Underwear.height/2)/2, Underwear.width/2, Underwear.height/2);
ctx.drawImage(Body, (599 - Body.width/2)/2, (557 - Body.height/2)/2, Body.width/2, Body.height/2);
var img = c.toDataURL("image/png");
document.write('<img src="' + img + '" />');
};
</script>

Browsers don't let programmers export cross-domain content for very good security reasons. 浏览器不允许程序员出于很好的安全原因而导出跨域内容。 Your private banking info is cross-domain content that you don't want give to thieves using the canvas as an export device. 您的私人银行业务信息是跨域内容,您不想使用画布作为导出设备给小偷。

Therefore, context.toDataURL is disabled immediately after you drawImage a cross-domain image onto canvas. 因此,将跨域图像drawImage绘制到画布上后, context.toDataURL将立即被禁用。 The same disabling is true for context.getImageData . 对于context.getImageData ,同样的禁用也适用。 (context.getImageData is another way to export canvas content). (context.getImageData是导出画布内容的另一种方法)。

To allow exporting the canvas content to your users, you will have to host all the images on the same domain as your webpage. 为了允许将画布内容导出到用户,您必须将所有图像托管在与网页相同的域中。

BTW, you must give all your elements time to load before drawing them. 顺便说一句,您必须在绘制所有元素之前给所有元素加载时间。 Here's an image loader that loads all your images in advance and then calls start() when all the images are fully loaded. 这是一个图像加载器,它可以预先加载所有图像,然后在所有图像完全加载后调用start()。 Put your ctx.drawImage's in start(). 将您的ctx.drawImage放在start()中。

// put the paths to your images in imageURLs[]
var imageURLs=[];  
imageURLs.push("https://getout-s3.s3.amazonaws.com/baseBody/boy-02.png");
imageURLs.push("https://getout-s3.s3.amazonaws.com/NK4XtQvkZ4MGctZf_.hand(unisex)_13.png");
// ...etc, for all images

// the loaded images will be placed in imgs[]
var imgs=[];

var imagesOK=0;
loadAllImages(start);

function loadAllImages(callback){
    for (var i=0; i<imageURLs.length; i++) {
        var img = new Image();
        imgs.push(img);
        img.onload = function(){ 
            imagesOK++; 
            if (imagesOK>=imageURLs.length ) {
                callback();
            }
        };
        img.onerror=function(){alert("image load failed");} 
        img.crossOrigin="anonymous";
        img.src = imageURLs[i];
    }      
}

function start(){

    // the imgs[] array now holds fully loaded images
    // the imgs[] are in the same order as imageURLs[]

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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