Convert an image to canvas that is already loaded

前端 未结 2 372
难免孤独
难免孤独 2020-12-16 07:34

I am working on a plugin in which I\'m converting Image into Canvas and storing as data url .This function triggers on \'load\' event but how can I convert an image which is

2条回答
  •  隐瞒了意图╮
    2020-12-16 08:16

    You can bind click listener to the image and save data-url on click.

    var img = document.getElementById("image");
    img.addEventListener("click",function(){
        var c = document.createElement("canvas");
        var ctx = c.getContext("2d");
        ctx.canvas.width  = img.getAttribute("width");
        ctx.canvas.height = img.getAttribute("height");
        ctx.drawImage(img,0,0);
        var imgInfo = c.toDataURL("image/png");
        localStorage.setItem("imgInfo", imgInfo);
    });
    

提交回复
热议问题