如何允许用户对页面进行截图?

时间:2019-04-30 05:04:43

标签: javascript html wordpress

我正在用WordPress创建一个网站。我希望普通用户进行练习。然后在完成练习后就可以记录/截取页面的屏幕截图。然后可以与其他媒体共享,或保存到用户计算机。

我尝试使用此代码,但无法正常工作

$(document).ready(function() {
  $(".print").click(function() {
    function crateimage(img, selection) {
      //console.log(selection);
      //console.log(img);    
      var canvas = document.getElementById('output');
      var context = canvas.getContext('2d');
      var imageObj = new Image();
      var imagename;

      //selection.width;
      imageObj.onload = function() {
        canvas.width = selection.width;
        canvas.height = selection.height;
        //alert(selection.width);
        //alert(selection.height);
        //alert(selection.x1);
        //alert(selection.y1);

        context.drawImage(imageObj, selection.x1, selection.y1, selection.width, selection.height, 0, 0, selection.width, selection.height);

        console.log(canvas.toDataURL("image/png"));
        var d = new Date();
        $(".saveimg").attr("href", canvas.toDataURL("image/png"));
        $(".saveimg").attr("download", "captue" + d.getDate() + d.getDate() + d.getFullYear() + d.getHours() + d.getMilliseconds());
        //  $("#linkimg").attr("href") = canvas.toDataURL("image/png");
        //<a id="downloadLnk" download="YourFileName.jpg">Download as image</a>

      }

      imageObj.src = img.src;
    }

    html2canvas($(".screen"), {
      onrendered: function(canvas) {

        var image = new Image();
        var img = '<img id="image_edit" src="' + canvas.toDataURL("image/png") + '">';
        var imgselect;

        //console.log(canvas.toDataURL("image/png"));
        $(".onscreen").html(img).promise().done(function() {
          $(".screen").hide();
          $(".print").hide();
          $(".onscreen").show();
          $(".save").show();

          imgselect = $('#image_edit').imgAreaSelect({
            handles: true,
            instance: true,
            show: true,
            onSelectEnd: crateimage
          });

          //imgselect.setSelection(50,50,200,200);

          $(".save").click(function() {
            imgselect.cancelSelection();
            $(".print").show();
            $(".onscreen").hide();
            $(".screen").show();
            $(this).hide();
          });
        });
      }
    });
  });
});
.screenshotbt {
  background: rgba(0, 0, 0, .6);
  width: 100%;
  position: fixed;
  bottom: 0px;
  padding: 5px;
  text-align: center;
  color: #fff;
  font-size: 2em;
  z-index: 2;
  cursor: pointer;
}

.screen {
  width: 100%;
}

#image_edit {
  line-height: 0;
}

.onscreen {
  background: #fff;
  width: 100%;
  bottom: 0px;
  text-align: center;
  color: #fff;
  overflow-y: auto;
  height: auto;
  padding: 0;
  margin: 0;
}

#output {
  display: none;
}

.save {
  z-index: 99999;
}
<head>
  <title>JS SCREEN SHOT</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/bootstrap-theme.min.css">
  <script src="js/bootstrap.min.js"></script>
  <script src="js/html2canvas.js"></script>

</head>

<body>


  <div class="onscreen"></div>
  <div class="screenshotbt print">
    SCREE SHOT <span class="glyphicon glyphicon-picture" aria-hidden="true"></span>
  </div>
  <div class="screenshotbt save" style="display:none;">
    <a class="saveimg" style="color:#fff;display:block;width:100%;" target="_blank">
         กรุณาลากเมาล์เลืกส่วนที่ต้องการ และ กดบันทึก <span class="	glyphicon glyphicon-save" aria-hidden="true"></span>
        </a>
  </div>
  <div class="screen">
    <div style="background:#F39C12;text-align:center">
      <div class="container" style="padding:25px;">
        <img src="mac.png" style="max-width:25%">
      </div>
      <div style="clear:both;"></div>
    </div>
    <div class="container" style="padding:25px;">
      <div class="col-md-12">
        <h1>JAVASCRIPT SCREEN SHOT</h1>
      </div>
      <div class="col-md-6">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
        desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
      <div class="col-md-6">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
        desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
    </div>
    <div class="container" style="padding:25px;background:#E0E0E0">
      <div class="col-md-12">
        <h1>JAVASCRIPT SCREEN SHOT</h1>
      </div>
      <div class="col-md-6">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
        desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
      <div class="col-md-6">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
        desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
    </div>
  </div>
  <canvas id="output"></canvas>
</body>

我希望用户能够记录和下载

0 个答案:

没有答案