參數(shù)名稱 | 類型 | 默認(rèn)值 | 描述 |
---|---|---|---|
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的背景顏色,如果沒有設(shè)定默認(rèn)白色此處被坑,我改為backgroundColor可用 |
height | number | null | Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設(shè)定 |
letterRendering | boolean | false | Whether to render each letter seperately. Necessary if letter-spacing is used.---在設(shè)置了字間距的時(shí)候有用 |
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---是否在渲染前測(cè)試圖片 |
timeout | number | 0 | Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---圖片加載延遲,默認(rèn)延遲為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--跨域代理 |
設(shè)置圖片格式
1、從canvas中直接提取圖片元數(shù)據(jù)
// 圖片導(dǎo)出為 png 格式 var type = 'png'; var imgData = canvas.toDataURL(type);
2、將mime-type改為image/octet-stream,強(qiáng)制讓瀏覽器直接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到本地
/** * 在本地進(jìn)行文件保存 * @param {String} data 要保存到本地的圖片數(shù)據(jù) * @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);
案例
案例
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:蘇州 紅河 甘南 文山 萍鄉(xiāng) 惠州 營口 咸陽
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用html2canvas實(shí)現(xiàn)瀏覽器截圖的示例代碼》,本文關(guān)鍵詞 使用,html2canvas,實(shí)現(xiàn),瀏覽器,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。上一篇:canvas煙花特效錦集