Copy image to clipboard

后端 未结 7 2393
抹茶落季
抹茶落季 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:
    <div id="copyToImage">Hello World!</div>

    JavaScript:

    $("#copyToImage").click(function() {
        html2canvas(document.querySelector("#copyToImage")).then(canvas => canvas.toBlob(blob => navigator.clipboard.write([new ClipboardItem({'image/png': blob})])));
    });
    
    0 讨论(0)
提交回复
热议问题