0

In my javascript i draw onto a canvas, after that i whant the canvas to be converted to a png and shown in a div with the folowing code:

var dataURL = document.getElementById('myCanvas').toDataURL("image/png");
var imgObj = new Image();
imgObj.src = dataURL;
imgObj.onload = function() {document.getElementById('myImg').appendChild(imgObj); };

When i call this directly after drawing the canvas i get a completly blank image in "myImg" but if i call exactly the same code with the click on a link it gets drawn properly:

<a id="viewImgLnk" href="javascript:void()" onclick=" return convertCanvasToImage();">View Image</a>

<script>
    function convertCanvasToImage(){
        var dataURL = document.getElementById('myCanvas').toDataURL("image/png");
        var imgObj = new Image();
        imgObj.src = dataURL;
        imgObj.onload = function() {document.getElementById('myImg').appendChild(imgObj); };
    }
</script>

How can i get the image loaded directly without the link?

4

1 に答える 1

0

アレルギーの助けを借りてコードが機能するようになりました!

setTimeout(function() {
    var dataURL = document.getElementById('myCanvas').toDataURL("image/png");
    var imgObj = new Image();
    imgObj.src = dataURL;
    imgObj.onload = function() {document.getElementById('myImg').appendChild(imgObj); };
}, 300);
于 2013-01-05T15:09:12.187 に答える