zoukankan      html  css  js  c++  java
  • HTML页面截屏,html2canvas ,完整demo

    <!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>html2canvas example</title>
        <script src="../../assets/gis/jquery/jquery-1.8.3.js"></script>
        <script type="text/javascript" src="http://html2canvas.hertzen.com/dist/html2canvas.js"></script>
    </head>
    <script type="text/javascript">
        function html2Image() {
            console.log('test');
            html2canvas(document.getElementById("view")).then(function (canvas) {
                console.log(canvas);
                var dataUrl = canvas.toDataURL();
                document.getElementById("html2Image").src = dataUrl;
            });
        }
    </script>
    <script type="text/javascript">
      $(document).ready(function () {
          // canvas生成图片
          $("#btn").on("click"function () {
              var getPixelRatio = function (context) { // 获取设备的PixelRatio
                  var backingStore = context.backingStorePixelRatio ||
                      context.webkitBackingStorePixelRatio ||
                      context.mozBackingStorePixelRatio ||
                      context.msBackingStorePixelRatio ||
                      context.oBackingStorePixelRatio ||
                      context.backingStorePixelRatio || 0.5;
                  return (window.devicePixelRatio || 0.5) / backingStore;
              };
              //生成的图片名称  在html2canvas插件中,用jquery获取到DOM对象是无法读取的,因此需要用document.getElementById("pic")来获取原生js对象,图中是用jquery对象转化为js对象:$("#pic")[0]或者$("#pic").get(0)
              var imgName = "@(webSiteName)@(weekName).jpg";
              var shareContent =  document.getElementsByTagName("canvas")[0]; // $(".mapboxgl-canvas-container").get(0) ;  //document.getElementById("imgDiv");
              var width = shareContent.offsetWidth;
              var height = shareContent.offsetHeight;
              var canvas = document.createElement("canvas");
              var context = canvas.getContext('2d');
              var scale = getPixelRatio(context); //将canvas的容器扩大PixelRatio倍,再将画布缩放,将图像放大PixelRatio倍。
              canvas.width = width * scale;
              canvas.height = height * scale;
              canvas.style.width = width + 'px';
              canvas.style.height = height + 'px';
              context.scale(scalescale);

              var opts = {
                  scale: 1,
                  background: '#FFFFFF'
              };
              window.pageYoffset = 0;
              document.documentElement.scrollTop = 0;
              document.body.scrollTop = 0;
              
              //html2canvas(shareContent, opts).then(function (canvas) {
                html2canvas(shareContent, {dpi: window.devicePixelRatio widthscale: 1useCORS: true}).then(function (canvas) {
                  context.imageSmoothingEnabled = false;
                  context.webkitImageSmoothingEnabled = false;
                  context.msImageSmoothingEnabled = false;
                  context.imageSmoothingEnabled = false;
                  var dataUrl = canvas.toDataURL('image/jpeg'1.0);
                  dataURIToBlob(imgNamedataUrlcallback);
              });
          });
      })


      // edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill
      var dataURIToBlob = function (imgNamedataURIcallback) {
          var binStr = atob(dataURI.split(',')[1]),
              len = binStr.length,
              arr = new Uint8Array(len);

          for (var i = 0i < leni++) {
              arr[i] = binStr.charCodeAt(i);
          }

          callback(imgNamenew Blob([arr]));
      }

      var callback = function (imgNameblob) {
          var triggerDownload = $("<a>").attr("href"URL.createObjectURL(blob)).attr("download"imgName).appendTo("body").on("click"function () {
              if (navigator.msSaveBlob) {
                  return navigator.msSaveBlob(blobimgName);
              }
          });
          triggerDownload[0].click();
          triggerDownload.remove();
      };
      </script>
    <body>
      <div style="float:right;"><input type="button" id="btn" class="k-button" value="保存为图片" /></div>
      <div id="imgDiv" style="padding-top:30px;">
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      </div>
    <div id="view" style=" 600px; height: 400px;">
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
    </div>
    <input type="button" value="截图" onclick="html2Image()"      / >

    <div>
        <img id="html2Image" src="" alt="">
    </div>
    </body>

    </html>


  • 相关阅读:
    Mac下Android studio搭建Android开发环境【新手】
    2016年美国数学建模比赛(MCM/ICM) E题环境科学 Are we heading towards a thirsty planet? 人工精准翻译。
    一张图让你快速学会UML(聚合、组合、依赖、继承、接口、类)
    《黑客与画家》——读书笔记
    YC(Y Combinator)斯坦福大学《如何创业》课程要点记录(粗糙)
    《你的灯亮着吗?发现问题的真正所在》——读书笔记
    sublime使用
    spring AOP面向切面编程
    LinkedHashMap源码浅析jdk1.7
    增强型for循环与集合
  • 原文地址:https://www.cnblogs.com/Fooo/p/15193688.html
Copyright © 2011-2022 走看看