html2canvas 识别 svg 解决方案

参考: 预览地址 http://jsfiddle.net/bv16o50f/1/

html

<div class="visualization">
  <svg xmlns="http://www.w3.org/2000/svg" class="chart-container" width="900" height="440">
    <g transform="translate(90, 10)">
      <g transform="translate(0, 302)">
        <path fill="none" stroke="#777" d="M 0 6 V 0 H 779 V 6" />
      </g>
      <g>
        <line stroke="#2e9ccc" stroke-width="50" transform="translate(253)" x1="0" y1="302" x2="0" y2="14" />
        <line stroke="#2e9ccc" stroke-width="100" transform="translate(253)" x1="150" y1="302" x2="150" y2="119" />
      </g>
    </g>
  </svg>
</div>

js

html2canvas([$('div.visualization')[0]], {
    useCORS: true
  }).then(function (canvas) {
    if (navigator.msSaveBlob) {
      console.log('this is IE');
      var URL=window.URL;
      var BlobBuilder = window.MSBlobBuilder;
      navigator.saveBlob=navigator.msSaveBlob;
      var imgBlob = canvas.msToBlob();
      if (BlobBuilder && navigator.saveBlob) {
        var showSave =  function (data, name, mimetype) {
          var builder = new BlobBuilder();
          builder.append(data);
          var blob = builder.getBlob(mimetype||"application/octet-stream");
          if (!name)
            name = "Download.bin";
          navigator.saveBlob(blob, name);
        };
        showSave(imgBlob, 'barchart.png',"image/png");
      }
    } else {
      if ($('#export-image-container').length == 0)
          $('body').append('<a id="export-image-container" download="barchart.jpg">')
      img = canvas.toDataURL("image/jpeg")
      img = img.replace('data:image/jpeg;base64,', '')
      finalImageSrc = 'data:image/jpeg;base64,' + img

      $('#export-image-container').attr('href', finalImageSrc)
      $('#export-image-container')[0].click()
      $('#export-image-container').remove()
    }
  });

css

div.visualization {
   width:1009px;
   height:440px;
   overflow:visible;
   background-color: #fff;
   color:transparent;
}

 

转载于:https://www.cnblogs.com/richard1015/p/10083141.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值