/* 针对系统中众多svg的图表/沙盘想要复制粘到微信中 */		
  import bus from '@/api/bus.js';

  export const copyBlob = (url,callback=null,ratio=1) => {
    const copyImg = new Image()
    copyImg.crossOrigin = 'anonymous';
    copyImg.src = svgToBase64(url);

    copyImg.onload = ()=> {
      const canvas = document.createElement('canvas');
      canvas.width = copyImg.width * ratio;
      canvas.height = copyImg.height * ratio;
      const ctx = canvas.getContext("2d");
      ctx.drawImage(copyImg,0,0,canvas.width,canvas.height)
      ctx.scale(ratio,ratio)

      callback && callback();
      
      canvas.toBlob(async(blob) => {
        console.log('toblob')
        console.log(blob)   
        const data = [new ClipboardItem({ [blob.type]: blob })];
        await navigator.clipboard.write(data).then(
          () => {
            bus.$message.success('复制成功！')
          },
          () => {
            bus.$message.success('浏览器暂不支持')
          }
        );
      })
    }	
 
  }

/* 转base64 */
export const svgToBase64 = (svg) => {
  const base64img = `data:image/svg+xml;base64,${window.btoa(
    unescape(encodeURI(svg))
  )}`;
  return base64img;
}