简体   繁体   English

无法从画布中动态加载的图像源访问DataURL数据

[英]unable to access toDataURL data from dynamically loaded image source in canvas

Here's what I'm doing: I have a ul that contains li elements with an image sourced locally from "/images" folder in the main directory. 这是我正在做的事情:我有一个包含li元素的ul,其li图像来自主目录中的“ / images”文件夹。

<section class="main">
            <ul id="st-stack" class="st-stack-raw">
                <li><div class="st-item"><img src="images/cards-01.png" class ="cardButton" /></div><div class="st-title"></div></li>
                <li><div class="st-item"><img src="images/cards-02.png" class ="cardButton" /></div><div class="st-title"></div></li>
            </ul>
</section>

    <div class="rightside">
        <canvas id="sketchpad" class="sketchpad" height="400" width="300">
        </canvas>
        <canvas id="sketchpad2" class="sketchpad" height="400" width="300">
        </canvas>
    </div>

I then have a javascript file that my html links to where the following code is placed: 然后,我有一个javascript文件,我的html链接到放置以下代码的位置:

$(document).ready(function() {

var myCanvas = document.getElementById("sketchpad");
var context = myCanvas.getContext("2d");


var startX = 0,
startY = 0;

$(".main").click(function(event) {

var target = $(event.target);

if (target.hasClass("cardButton")) {
var srcimg = $(target).attr("src");
draw(srcimg);
}

else {
return false;
}

var image_data = myCanvas.toDataURL("image/png");
window.open(image_data, '_blank', "location=0, menubar=0");
});

function draw(source){
var img = document.createElement("img");
img.onload = function(){
context.drawImage(img, startX, startY, 300, 400);
}
img.src = source;

}

});

I want to simply access the toDataURL image and open it up in a new window when the li img element is clicked. 我只想访问toDataURL图像,并在单击li img元素时在新窗口中将其打开。 The li image is loaded into the canvas, however, all I seem to get is a transparent canvas when the new window pops open with the toDataURL information. li图像已加载到画布中,但是,当新窗口弹出并显示toDataURL信息时,我似乎得到的只是一个透明画布。 Does anyone have a solution for this? 有人对此有解决方案吗?

In the above code you are trying to copy data URL before image is saved into it so image seems to be transparent. 在上面的代码中,您尝试在将图像保存到其中之前复制数据URL,因此图像看起来是透明的。 Copy DataURL after image is passed to it. 将图像传递给它后复制DataURL。

Here is pen for your workig code : http://codepen.io/anon/pen/oYMoJm?editors=0010 Note that window.open is popup and will be blocked by browser we have to allow it manually. 这是您的工作代码的笔: http ://codepen.io/anon/pen/oYMoJm?editors=0010注意window.open是弹出窗口,将被浏览器阻止,我们必须手动允许它。

Here is your updated JS code : 这是您更新的JS代码:

$(document).ready(function() {

var myCanvas = document.getElementById("sketchpad");
var context = myCanvas.getContext("2d");


var startX = 0,
startY = 0;

$(".main").click(function(event) {

var target = $(event.target);

if (target.hasClass("cardButton")) {
var srcimg = $(target).attr("src");
draw(srcimg);
}

else {
return false;
}
});

function draw(source){
var img = document.createElement("img");
img.onload = function(){
context.drawImage(img, startX, startY, 300, 400);
  var image_data = myCanvas.toDataURL("image/png");
window.open(image_data, '_blank', "location=0, menubar=0");

}
img.src = source;

}

});

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

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