运用html2canvas完成浏览器截图

运用html2canvas完成浏览器截图,必须在服务器环境下才完成。

作用

html2canvas能够经由过程纯JS对浏览器端经行截屏,但截图的精确度还有待进步,部份css不可辨认,所以在canvas中不能圆满显现原画面款式

/*多行溢出省略就不可,只能超越隐蔽了*/
     .book_inf{
            position: relative; 
            overflow : hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

支撑的浏览器

  1. Firefox 3.5+
  2. Google Chrome
  3. Opera 12+
  4. IE9+
  5. Safari 6+

基础语法

/*参数:
* #screenshots 所须要截图的元素id,截图后要实行的函数,
* backgroundColor 设置项背景致
* canvas为截图后返回的末了一个canvas
*/
function screenshotsImg(){
       html2canvas(document.querySelector("#screenshots"),{
            backgroundColor: 'transparent',// 设置背景通明
        }).then(canvas => {
            canvasTurnImg(canvas) //保留的图片花样转换要领
        });
    }

可用设置项

参数称号范例默许值形貌
allowTaintbooleanfalseWhether to allow cross-origin images to taint the canvas—许可跨域
backgroundstring#fffCanvas background color, if none is specified in DOM. Set undefined for transparent—canvas的背景色彩,假如没有设定默许白色此处被坑,我改成backgroundColor可用
heightnumbernullDefine the heigt of the canvas in pixels. If null, renders with full height of the window.—canvas高度设定
letterRenderingbooleanfalseWhether to render each letter seperately. Necessary if letter-spacing is used.—在设置了字间距的时刻有效
loggingbooleanfalseWhether to log events in the console.—在console.log()中输出信息
proxystringundefinedUrl to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won’t be loaded.—代办地点
taintTestbooleantrueWhether to test each image if it taints the canvas before drawing them—是不是在衬着前测试图片
timeoutnumber0Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.—图片加载耽误,默许耽误为0,单元毫秒
widthnumbernullDefine the width of the canvas in pixels. If null, renders with full width of the window.—canvas宽度
useCORSbooleanfalseWhether to attempt to load cross-origin images as CORS served, before reverting back to proxy–跨域代办

设置图片花样

1、从canvas中直接提取图片元数据

  // 图片导出为 png 花样
        var type = 'png';
        var imgData = canvas.toDataURL(type);

2、将mime-type改成image/octet-stream,强迫让浏览器直接download

/**
 * 猎取mimeType
 * @param  {String} type the old mime-type
 * @return the new mime-type
 */
var _fixType = function(type) {
    type = type.toLowerCase().replace(/jpg/i, 'jpeg');
    var r = type.match(/png|jpeg|bmp|gif/)[0];
    return 'image/' + r;
};
   
// 加工image data,替代mime type
imgData = imgData.replace(_fixType(type),'image/octet-stream');

3、图片download到当地

/**
 * 在当地举行文件保留
 * @param  {String} data     要保留到当地的图片数据
 * @param  {String} filename 文件名
 */
var saveFile = function(data, filename){
    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
    save_link.href = data;
    save_link.download = filename;
   
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    save_link.dispatchEvent(event);
};
   
// 下载后的文件名
var filename = 'baidufe_' + (new Date()).getTime() + '.' + type;
// download
saveFile(imgData,filename);

案例

案例

参考资料

html2canvas
html2canvas
怎样运用js将canvas保留为图片文件,而且能够自定义文件名

    原文作者:月影
    原文地址: https://segmentfault.com/a/1190000012976851
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞