Question

What's the problem with toDataURL() function in my code!?
My problem is, the p0 canvas will remain empty!
After uploading my image in dataurl.net, i found out, it DataUrl code and my DataUrl code that was saved in LocalStorage are different!! and that's why it can't show anything!

<canvas id="p0" width="512" height="176" style="margin-left: 200px; margin-top: 216px; position: absolute"></canvas>
<script>
function init() {
    var p0 = document.getElementById("p0");
    var P0 = p0.getContext('2d');
    var img = new Image;
    img.onload = function () {
        P0.drawImage(img, 0, 0);
    };
    if (localStorage.getItem("version") == Version) {
        img.src = localStorage.getItem("p0");
    } else {
        localStorage.setItem("version", Version);
        img.src = "Index/2/logo.png";
        localStorage.setItem("p0", p0.toDataURL("image/png"));
    }
}
init();
</script>
Was it helpful?

Solution

Thanks to Bergi,
It just need to put this line:

localStorage.setItem("p0", p0.toDataURL("image/png"));

into the img.onload function.

img.onload = function () {
    P0.drawImage(img, 0, 0);
    localStorage.setItem("p0", p0.toDataURL("image/png"));
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top