加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

行使html2canvas实现赏识器截图的示例代码

发布时间:2020-05-12 11:31:56 所属栏目:编程 来源:站长网
导读:行使html2canvas实现赏识器截图,必需在处事器情形下才气实现。 浸染 html2canvas可以通过纯JS对赏识器端经行截屏,但截图的准确度尚有待进步,部门css不行辨认,以是在canvas中不能美满泛起原画面样式 /*多行溢出省略就不可,只能超出潜匿了*/.book_inf{po

行使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);

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读