保存 html 为图片

保存 html 为图片

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="http://html2canvas.hertzen.com/dist/html2canvas.js"></script>
  <script src="https://www.jqueryscript.net/demo/Capture-HTML-Elements-Screenshot/canvas2image.js"></script>
  <script src="https://unpkg.com/dom-to-image@2.6.0/dist/dom-to-image.min.js"></script>
</head>
<body>
  <div id="capture" style="padding: 10px; background: #f5da55">
      <h4 style="color: #000; ">Hello world!</h4>
  </div>
  <button id="save" onclick="save();">保存图片</button>
  <script>
    function save() {
      // Canvas2Image.saveAsImage(document.querySelector(`canvas`), 1200, 148, `png`, `test.png`)

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



    }
    html2canvas(document.body).then(function(canvas) {
      document.body.appendChild(canvas);
    });



  </script>
</body>
</html>
?

保存 html 为图片

上一篇:小白的前端之路-HTMl


下一篇:浏览器控制台警告 A cookie associated with a cross-site resource at was set without the `SameSite` attribute