当前位置: 首页 > 工具软件 > dom-to-image > 使用案例 >

dom-to-image生成图片并下载

姬正文
2023-12-01
  1. 下载必要的js库npm install dom-to-image
  2. 引用库import domtoimage from ‘dom-to-image’;
    ①生成png的图片插入的到当前页面
var node = document.getElementById('my-node');
domtoimage.toPng(node)
    .then(function (dataUrl) {
        var img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
    })
    .catch(function (error) {
        console.error('oops, something went wrong!', error);
    });

②生成图片并下载png格式

domtoimage.toBlob(document.getElementById('my-node'))
  .then(function (blob) {
       window.saveAs(blob, 'my-node.png');
   });

③生成图片并下载jpeg格式

domtoimage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })
    .then(function (dataUrl) {
        var link = document.createElement('a');
        link.download = 'my-image-name.jpeg';
        link.href = dataUrl;
        link.click();
    });
                        

④还两种生成SVG和pixel data的

domtoimage.toSvg(document.getElementById('my-node'), {filter: filter})
    .then(function (dataUrl) {
        /* do something */
    });
 var node = document.getElementById('my-node');
domtoimage.toPixelData(node)
    .then(function (pixels) {
        for (var y = 0; y < node.scrollHeight; ++y) {
          for (var x = 0; x < node.scrollWidth; ++x) {
            pixelAtXYOffset = (4 * y * node.scrollHeight) + (4 * x);
            /* pixelAtXY is a Uint8Array[4] containing RGBA values of the pixel at (x, y) in the range 0..255 */
            pixelAtXY = pixels.slice(pixelAtXYOffset, pixelAtXYOffset + 4);
          }
        }
    });

完整的Demo:

<html>
<head>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="dom-to-image.js"></script>
<script type="text/javascript" src="FileSaver.js"></script>

  <script type="text/javascript">
  $(function(){
      //
      $("#btn").click(function(){
      // 插入图片到html
      // var node = document.getElementById('table');
      //     domtoimage.toPng(node)
      //               .then(function (dataUrl) {
      //                   var img = new Image();
      //                   img.src = dataUrl;
      //                   document.body.appendChild(img);
      //               });
                     
                    
      // 下载png图片
       domtoimage.toBlob(document.getElementById('table'))
          .then(function (blob) {
              window.saveAs(blob, 'my-node.png');
          });
     

      // 下载jpeg图片
      // domtoimage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })
      //           .then(function (dataUrl) {
      //               var link = document.createElement('a');
      //               link.download = 'my-image-name.jpeg';
      //               link.href = dataUrl;
      //               link.click();
      //           });  

      // var node = document.getElementById('table');
      // domtoimage.toPixelData(node)
      //     .then(function (pixels) {
      //         for (var y = 0; y < node.scrollHeight; ++y) {
      //           for (var x = 0; x < node.scrollWidth; ++x) {
      //             pixelAtXYOffset = (4 * y * node.scrollHeight) + (4 * x);
      //             /* pixelAtXY is a Uint8Array[4] containing RGBA values of the pixel at (x, y) in the range 0..255 */
      //             pixelAtXY = pixels.slice(pixelAtXYOffset, pixelAtXYOffset + 4);
      //           }
      //         }
      //     });    
  })
  </script>
</head>
<body>
  <input type="button" id="btn" value="do it" />
  <h4>两行三列:</h4>
  <table id="table" border="1" style="width:200px;border-collapse: collapse;border:1px solid black;">
      <tr style="background:green;">
        <td rowspan="3">200</td>
        <td>200</td>
        <td>300</td>
      </tr>
      <tr style="background:green;">
        <td rowspan="2">500</td>
        <td>600</td>
      </tr>
      <tr style="background:green;">
        <td>600</td>
      </tr>
      <tr style="background:skyblue;">
        <td rowspan="3">100</td>
        <td>200</td>
        <td>300</td>
      </tr>
      <tr style="background:skyblue;">
        <td>500</td>
        <td>600</td>
      </tr>
      <tr style="background:skyblue;">
        <td>600</td>
        <td>600</td>
      </tr>
  </table>
</body>
</html>
 类似资料: