Copy image to clipboard

后端 未结 7 2394
抹茶落季
抹茶落季 2020-11-27 16:14

Seemingly, you can\'t (yet) programmatically copy an image to the clipboard from a JavaScript web app?

I have tried to copy a text in clipboard , and it\'s worked.<

7条回答
  •  攒了一身酷
    2020-11-27 17:02

    So, i created the perfect solution with 1 liner-kinda solution to convert something with html2canvas to a canvas and then produce the image of it and then save it to clipboard as png. For example,

    HTML:

    Hello World!

    JavaScript:

    $("#copyToImage").click(function() {
        html2canvas(document.querySelector("#copyToImage")).then(canvas => canvas.toBlob(blob => navigator.clipboard.write([new ClipboardItem({'image/png': blob})])));
    });
    

提交回复
热议问题