使用html2canvas实现浏览器截图的示例代码
- 作者: 不知深浅不长不短
- 来源: 51数据库
- 2021-08-09
使用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.51sjk.com/Upload/Articles/1/0/265/265277_20210708005045093.jpg', 'a');
save_link.
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);
案例
案例
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
