简体   繁体   English

在画布上用cors =“ anonimous”绘制(重新加载)图像(javascript)

[英]draw(reload) image with cors = “anonimous” on canvas (javascript)

I tryed to reload image to draw it canvas(couse of cors) 我试图重新加载图像以将其绘制在画布上

this is part of code: 这是代码的一部分:

    var divicons = $(".leaflet-marker-icon");
    var dx = [];
    var dy = [];
    var mx = [];
    var my = [];
    for (var k = 0; k < divicons.length; k++) {
        var curTransform = divicons[k].style.transform;
        var splitTransform = curTransform.split(",");
        var marginX = parseInt(divicons[k].style.marginTop.replace("px", ""))
        var marginY = parseInt(divicons[k].style.marginLeft.replace("px", ""))
        dx.push(parseFloat(splitTransform[0].split("(")[1].replace("px", "")));
        dy.push(parseFloat(splitTransform[1].replace("px", "")));
        divicons[k].style.transform = "";
        divicons[k].style.left = dx[k] + "px";
        divicons[k].style.top = dy[k] + "px";

        varIconSizeWidth = parseInt(divicons[k].style.width.replace("px", ""))
        varIconSizeHeight = parseInt(divicons[k].style.height.replace("px", ""))
        var img = new Image();
        img.setAttribute("crossorigin", "anonymous");
        img.src = divicons[k].src + '?d=' + Date.now();
        img.onload = function() {
            ctx.drawImage(img, dx[k] + mapX + marginY, dy[k] + mapY + marginX);
            console.log(img)
        };
    }

    $(canvas).insertBefore(svgE);
    var myImage = canvas.toDataURL("image/jpeg");
    var link = document.createElement('a');
    link.download = "test.jpeg";
    link.href = myImage;
    document.body.appendChild(link);
    $(link).css("display", "none");
    link.click();
    document.body.removeChild(link);

How to reload corrertly, because image did not drew... 由于图像未绘制,如何正确重新加载...

also, when I consoled img and divicons[k] - there are some difference(img haven't any style, even img.style = divicons[k].style).... Also, I'd like to use event, that will start download canvas. 另外,当我控制台img和divicons [k]时-有一些区别(img没有任何样式,甚至img.style = divicons [k] .style)。...此外,我想使用事件,开始下载画布。

Property names are case-sensitive and in so-called camel-case (with none-capitalized first letter) so the correct way is with capital O : 属性名称区分大小写,并且在所谓的驼峰式大小写 (首字母不大写)中,因此正确的方法是使用大写O

img.setAttribute("crossOrigin", "anonymous");

However, CORS is not a requirement to draw to canvas, it only applies when you try to extract pixels from it ( toDataURL() , toBlob() , getImageData() , createImageBitmap() ). 但是,CORS并不是在画布上绘制的要求,它仅在您尝试从画布中提取像素( toDataURL()toBlob()getImageData()createImageBitmap() )时适用。

Another problem with the code is that the onload callback is referencing the img variable. 该代码的另一个问题是onload回调引用了img变量。 However this variable is overridden in parent scope so when onload is called it contains a different value, ie another image that is likely not loaded yet since it will hold the last image source set due to the onload not being called until the loop finishes. 但是,此变量在父作用域中被覆盖,因此在onload时,它包含一个不同的值,即另一个可能尚未加载的图像,因为它将保留最后一个图像源集,这是因为直到循环结束之前才onload

To fix, reference this instead inside the handler which contains the calling context (image in this case): 要解决this问题,请在包含调用上下文(在本例中为图片)的处理程序中引用this

img.onload = function() {
    ctx.drawImage(this, dx[k] + mapX + marginY, dy[k] + mapY + marginX);
};

And the same goes with the variable k ; 变量k mapX and marginY do not seem to be defined etc. and will cause you other problems, but out of scope for this question. 似乎未定义mapXmarginY等,会导致其他问题,但超出了此问题的范围。

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

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