//点击复制二维码 function copyPic(url){ var canvas = document.createElement('canvas') // 创建一个画板 const image = new Image() image.setAttribute('crossOrigin', 'Anonymous') // 可能会有跨越问题 image.src = url image.onload = () => { // img加载完成事件 canvas.width = image.width // 设置画板宽度 canvas.height = image.height // 设置画板高度 canvas.getContext('2d').drawImage(image, 0, 0) // 加载img到画板 const url = canvas.toDataURL('image/png') // 转换图片为dataURL,格式为png clipboardImg(url) // 调用复制方法 } } async function clipboardImg(url) { try { const data = await fetch(url) const blob = await data.blob() await navigator.clipboard.write([ new window.ClipboardItem({ [blob.type]: blob }) ]) console.log('复制成功') } catch (err) { console.log('复制失败') } }
标签:canvas,const,url,image,js,复制,blob,图片 From: https://www.cnblogs.com/guxingzhe/p/17013115.html