使用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;
        }
支持的浏览器
- Firefox 3.5+
 - Google Chrome
 - Opera 12+
 - IE9+
 - Safari 6+
 
基本语法
/*参数:
* #screenshots 所需要截图的元素id,截图后要执行的函数,
* backgroundColor 配置项背景色
* canvas为截图后返回的最后一个canvas
*/
function screenshotsImg(){
       html2canvas(document.querySelector("#screenshots"),{
            backgroundColor: 'transparent',// 设置背景透明
        }).then(canvas => {
            canvasTurnImg(canvas) //保存的图片格式转换方法
        });
    }
可用配置项
    
        
            参数名称 
            类型 
            默认值 
            描述 
        
    
    
        
            allowTaint
            boolean
            false
            Whether to allow cross-origin images to taint the canvas---允许跨域
        
        
            background
            string
            #fff
            Canvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景颜色,如果没有设定默认白色此处被坑,我改为backgroundColor可用
        
        
            height
            number
            null
            Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度设定
        
        
            letterRendering
            boolean
            false
            Whether to render each letter seperately. Necessary if letter-spacing is used.---在设置了字间距的时候有用
        
        
            logging
            boolean
            false
            Whether to log events in the console.---在console.log()中输出信息
        
        
            proxy
            string
            undefined
            Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址
        
        
            taintTest
            boolean
            true
            Whether to test each image if it taints the canvas before drawing them---是否在渲染前测试图片
        
        
            timeout
            number
            0
            Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---图片加载延迟,默认延迟为0,单位毫秒
        
        
            width
            number
            null
            Define the width of the canvas in pixels. If null, renders with full width of the window.---canvas宽度
        
        
            useCORS
            boolean
            false
            Whether 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);
案例
案例
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
 - 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
 - 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
 - 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
 - 群星《2024好听新歌42》AI调整音效【WAV分轨】
 - 王思雨-《思念陪着鸿雁飞》WAV
 - 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
 - 李健《无时无刻》[WAV+CUE][590M]
 - 陈奕迅《酝酿》[WAV分轨][502M]
 - 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
 - 群星《吉他王(黑胶CD)》[WAV+CUE]
 - 齐秦《穿乐(穿越)》[WAV+CUE]
 - 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
 - 邝美云《邝美云精装歌集》[DSF][1.6G]
 - 吕方《爱一回伤一回》[WAV+CUE][454M]