Save HTML5 canvas with images as an image

前端 未结 1 387
盖世英雄少女心
盖世英雄少女心 2021-01-03 04:36

I\'m trying to save a canvas with images to PNG, but when I try this:

var myCanvas = document.getElementById(\"myCanvas\");
var img = document.createElement(         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 05:17

    You need to put the window.open call in the load handler since this happens asynchronously.

    img.onload = function () {  
        ctx.drawImage(img, 0, 0, myCanvas.width, myCanvas.height);
    
        var data = myCanvas.toDataURL("image/png");
        if (!window.open(data)) {
            document.location.href = data;
        }
    }
    

    0 讨论(0)
提交回复
热议问题