zoukankan      html  css  js  c++  java
  • JS实现网页选取截屏 保存+打印 功能(转)

    源码地址:

    1.1 确定截图选取范围

    用户在开始截图后,需要在页面上选取一个截图范围,并且可以直观的看到,类似如下效果:

    image


    我们的选取范围就是鼠标开始按下的那个点到鼠标拖动然后松开的那个点之间所组成的矩形。
    为了能直观看到我们选取的范围,我们将这个矩形框随着鼠标拖动给画出来,利用canvas即可,为了方便绘制,这里使用了jcanvas

    1.2 将选取范围内的网页生成截图

    如何将选取框范围内的网页内容变成图像呢,我们可以使用html2canvas.jshtml2canvas可以将页面中的DOM元素生成canvas,是将网页生成图像的非常好的一个选择。使用非常简单:

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

    上面的代码就可以将body转成canvas。html2canvas使用到了Promise,得确保你的浏览器支持。
    html2canvas虽然可以将指定元素转成canvas,有了canvas我们就可以轻易的生成图像。但是并不能满足将选取框范围内的内容转成canvas的需求,选取框内可能有多个元素,并且可能是多个不完整的元素,元素只有部分。
    我们可以先将body整个转成canvas,然后将这个canvas进行剪裁(或生成image后剪裁),将选取框范围的内容剪裁出来。很简单,使用drawImage即可。
    drawImage 方法允许在 canvas 中插入其他图像( img 和 canvas 元素) 。drawImage函数有三种函数原型:

    drawImage(image, dx, dy)
    drawImage(image, dx, dy, dw, dh)
    drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)

    2 打印

    打印使用 jquery.print.js jquery.print.js是一款可以实现网页打印的插件,实用的方法非常简单。类似html2canvas可以转换元素为canvas这样,它可以选取元素进行部分打印。

    $(".print").click(function(){
        $(".need_print").print();
    });

    2 完整实现

    创建screenshotsPrint.js,内容如下:

    /**
     * 默认画笔线宽
     * @type {number}
     */
    var defaultStrokeWidth = 1; //画矩形选取框的线宽
    
    /**
     * 选取划线的canvasExt
     * @type {{drawRect: canvasExt.drawRect}}
     */
    var canvasExt = {
        /**
         *  画矩形
         * @param canvasId canvasId
         * @param penColor 画笔颜色
         * @param strokeWidth 线宽
         */
        drawRect: function (canvasId, penColor, strokeWidth) {
            var that = this;
    
            that.penColor = penColor;
            that.penWidth = strokeWidth;
            var canvas = document.getElementById(canvasId);
            //canvas 的矩形框
            var canvasRect = canvas.getBoundingClientRect();
            //canvas 矩形框的左上角坐标
            var canvasLeft = canvasRect.left;
            var canvasTop = canvasRect.top;
    
            // 要画的矩形的起点 xy
            var x = 0;
            var y = 0;
    
            //鼠标点击按下事件,画图准备
            canvas.onmousedown = function(e) {
    
                //设置画笔颜色和宽度
                var color = that.penColor;
                var penWidth = that.penWidth;
                // 确定起点
                x = e.clientX - canvasLeft;
                y = e.clientY - canvasTop;
                // 添加layer
                $("#"+canvasId).addLayer({
                    type: 'rectangle',
                    strokeStyle: color,
                    strokeWidth: penWidth,
                    name:'areaLayer',
                    fromCenter: false,
                    x: x, y: y,
                     1,
                    height: 1
                });
                // 绘制
                $("#"+canvasId).drawLayers();
                $("#"+canvasId).saveCanvas();
    
                //鼠标移动事件,画图
                canvas.onmousemove = function(e){
    
                    // 要画的矩形的宽高
                    var width = e.clientX-canvasLeft - x;
                    var height = e.clientY-canvasTop - y;
    
                    // 清除之前画的
                    $("#"+canvasId).removeLayer('areaLayer');
    
                    $("#"+canvasId).addLayer({
                        type: 'rectangle',
                        strokeStyle: color,
                        strokeWidth: penWidth,
                        name:'areaLayer',
                        fromCenter: false,
                        x: x, y: y,
                         width,
                        height: height
                    });
    
                    $("#"+canvasId).drawLayers();
                }
            };
            //鼠标抬起
            canvas.onmouseup=function(e){
    
                var color = that.penColor;
                var penWidth = that.penWidth;
    
                canvas.onmousemove = null;
    
                var width = e.clientX - canvasLeft - x;
                var height = e.clientY- canvasTop - y;
    
                $("#"+canvasId).removeLayer('areaLayer');
    
                $("#"+canvasId).addLayer({
                    type: 'rectangle',
                    strokeStyle: color,
                    strokeWidth: penWidth,
                    name:'areaLayer',
                    fromCenter: false,
                    x: x, y: y,
                     width,
                    height: height
                });
    
                $("#"+canvasId).drawLayers();
                $("#"+canvasId).saveCanvas();
    
                // 把body转成canvas
                html2canvas(document.body, {
                    scale: 1,
                    // allowTaint: true,
                    useCORS: true  //跨域使用
                }).then(canvas => {
                    var capture_x, capture_y
                    if (width > 0) {
                        //从左往右画
                        capture_x = x + that.penWidth
                    }else {
                        //从右往左画
                        capture_x = x + width + that.penWidth
                    }
                    if (height > 0) {
                        //从上往下画
                        capture_y = y + that.penWidth
                    }else {
                        //从下往上画
                        capture_y = y + height + that.penWidth
                    }
                    printClip(canvas, capture_x, capture_y, Math.abs(width), Math.abs(height))
                });
                // 移除画的选取框
                $("#"+canvasId).removeLayer('areaLayer');
                // 隐藏用于华画取框的canvas
                $("#"+canvasId).hide()
            }
        }
    };
    
    /**
     * 选取截屏
     * @param canvasId
     */
    function clipScreenshots(canvasId){
        canvasExt.drawRect(canvasId, "red", defaultStrokeWidth);
    }
    
    /**
     * 打印截取区域
     * @param canvas 截取的canvas
     * @param capture_x 截取的起点x
     * @param capture_y 截取的起点y
     * @param capture_width 截取的起点宽
     * @param capture_height 截取的起点高
     */
    function printClip(canvas, capture_x, capture_y, capture_width, capture_height) {
        // 创建一个用于截取的canvas
        var clipCanvas = document.createElement('canvas')
        clipCanvas.width = capture_width
        clipCanvas.height = capture_height
        // 截取
        clipCanvas.getContext('2d').drawImage(canvas, capture_x, capture_y, capture_width, capture_height, 0, 0, capture_width, capture_height)
        var clipImgBase64 = clipCanvas.toDataURL()
        // 生成图片
        var clipImg = new Image()
        clipImg.src = clipImgBase64
        var con = confirm('打印截图吗?取消则保存截图')
        if (con) {
            $(clipImg).print()
        }else {
            downloadIamge(clipImgBase64)
        }
    }
    
    /**
     * 下载保存图片
     * @param imgUrl 图片地址
     */
    function downloadIamge(imgUrl) {
        // 图片保存有很多方式,这里使用了一种投机的简单方法。
        // 生成一个a元素
        var a = document.createElement('a')
        // 创建一个单击事件
        var event = new MouseEvent('click')
        // 生成文件名称
        var timestamp = new Date().getTime();
        var name = imgUrl.substring(22, 30) + timestamp + '.png';
        a.download = name
        // 将生成的URL设置为a.href属性
        a.href = imgUrl;
        // 触发a的单击事件 开始下载
        a.dispatchEvent(event);
    }

    demo.html中进行使用

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>测试截取保存或打印</title>
        <script type="text/javascript" src="libs/jquery-2.1.0.js"></script>
        <script type="text/javascript" src="libs/html2canvas.js"></script>
        <script type="text/javascript" src="libs/jQuery.print.js"></script>
        <script type="text/javascript" src="libs/jcanvas.min.js"></script>
        <script type="text/javascript" src="js/screenshotsPrint.js"></script>
        <style>
            body, html {
                width: 100%;
                height: 100%;
            }
            .print {
                position: relative;
                z-index: 100;
            }
            h1 {
                color: orangered;
            }
            h2 {
                color: darkblue;
            }
            h2 {
                color: forestgreen;
            }
            #bg_canvas {
                position: absolute;
                z-index: 500;
                left: 0;
                top: 0;
            }
        </style>
    </head>
    <body>
    <button class="print">开始截图</button>
    <div>
        <h1>哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈</h1>
        <h2>嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿</h2>
        <h3>呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵</h3>
        <br/>
        <p>这是一张跨域图片</p>
        <img src="http://p7.qhimg.com/t01ceede0272d4b5a8b.png" alt="来个跨区的图片">
    </div>
    <!-- 用于画选取框的canvas -->
    <canvas id="bg_canvas" width="100%" height="100%" />
    <script>
        $(function(){
            var clientWidth = document.documentElement.clientWidth || document.body.clientWidth
            var clientHeight = document.documentElement.clientHeight || document.body.clientHeight
            // 更新canvas宽高
            $("#bg_canvas").attr("width", clientWidth);
            $("#bg_canvas").attr("height", clientHeight);
            $("#bg_canvas").hide();
            $(".print").click(function(){
                $("#bg_canvas").show()
                alert('现在你可以使用鼠标拖拽选取打印区域,松开后完成')
                //调用选取截屏
                clipScreenshots("bg_canvas");
            });
    
    
        });
    </script>
    </body>
    </html>

    作者:石文文的简书
    链接:https://www.jianshu.com/p/8c43576bdbe3
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    P1001 A+B Problem
    NOIP2015D1T2 信息传递
    海淀区赛游记。。。。
    P3375 【模板】KMP字符串匹配
    Print Article HDU
    BZOJ-2-4870: [Shoi2017]组合数问题 矩阵优化 DP
    BZOJ-1- 4868: [Shoi2017]期末考试-三分
    #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队
    湖南大学第十四届ACM程序设计新生杯(重现赛)
    Codeforces Round #530 (Div. 2)
  • 原文地址:https://www.cnblogs.com/xinchenhui/p/10886140.html
Copyright © 2011-2022 走看看