本文詳細介紹瞭如何在H5中實現長按保存圖片的功能。css
長按保存圖片是如今一些宣傳頁H5中很常見的需求,可是js沒有這樣的能力,因此要麼藉助android或ios的原生能力,要麼用canvas本身畫一個(截屏),相比較原生成本過高,且必須依賴於app,相對於流傳性很廣且跨平臺的H5來講不合時宜,因此
canvas
成爲咱們經常使用的手段。html
保存的圖片節點最好是img標籤: 想要截屏的節點最好是img標籤的圖片,經測試若是是
background-image
會有點模糊,須要特別注意下。android
npm i html2canvas --save
import html2canvas from 'html2canvas';
// 想要保存的圖片節點
const dom = document.querySelector('img');
// 建立一個新的canvas
const Canvas = document.createElement('canvas');
const width = document.body.offsetWidth; // 可見屏幕的寬
const height = document.body.offsetHeight; // 可見屏幕的高
const scale = window.devicePixelRadio; // 設備的devicePixelRadio
// 將Canvas畫布放大scale倍,而後放在小的屏幕裏,解決模糊問題
Canvas.width = width * scale;
Canvas.height = height * scale;
Canvas.getContext('2d').scale(scale, scale);
html2canvas(dom, {
canvas: Canvas,
scale,
useCORS: true,
logging: true,
width: width + 'px',
hegiht: height + 'px',
}).then((canvas) => {
const context = canvas.getContext('2d');
// 關閉抗鋸齒形
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
// canvas轉化爲圖片
canvas2Image(canvas, canvas.width, canvas.height);
});
複製代碼
通常狀況下轉爲jpeg格式就很不錯了。ios
canvas2Image(canvas, canvas.width, canvas.height) {
const retCanvas = document.createElement('canvas');
const retCtx = retCanvas.getContext('2d');
retCanvas.width = width;
retCanvas.height = height;
retCtx.drawImage(canvas, 0, 0, width, height, 0, 0, width, height);
const img = document.createElement('img');
img.src = retCanvas.toDataURL('image/jpeg'); // 能夠根據須要更改格式
return img;
}
複製代碼
先實現一個長按的方法,長按以後把生成的圖片append到body,透明浮在屏幕上。web
// 封裝一個長按方法
longPress(fn) {
let timeout = 0;
const $this = this;
for (let i = 0; i < $this.length; i++) {
$this[i].addEventListener('touchstart', () => {
timeout = setTimeout(fn, 800); // 長按時間超過800ms,則執行傳入的方法
}, false);
$this[i].addEventListener('touchend', () => {
clearTimeout(timeout); // 長按時間少於800ms,不會執行傳入的方法
}, false);
}
}
// 添加生成的圖片到body
const img = canvas2Image(canvas, canvas.width, canvas.height);
document.body.appendChild(img);
img.style.cssText = "width:100%;height:100%;position:absolute;top:0;left:0;right:0;bottom:0;opacity:0;";
複製代碼
$.fn.longPress = function(fn) {
let timeout = 0;
const $this = this;
for (let i = 0; i < $this.length; i++) {
$this[i].addEventListener('touchstart', () => {
timeout = setTimeout(fn, 800); // 長按時間超過800ms,則執行傳入的方法
}, false);
$this[i].addEventListener('touchend', () => {
clearTimeout(timeout); // 長按時間少於800ms,不會執行傳入的方法
}, false);
}
};
$('img').longPress(() => {
saveImg();
});
saveImg() {
// 想要保存的圖片節點
const dom = document.querySelector('img');
// 建立一個新的canvas
const Canvas = document.createElement('canvas');
const width = document.body.offsetWidth; // 可見屏幕的寬
const height = document.body.offsetHeight; // 可見屏幕的高
const scale = window.devicePixelRatio; // 設備的devicePixelRatio
// 將Canvas畫布放大scale倍,而後放在小的屏幕裏,解決模糊問題
Canvas.width = width * scale;
Canvas.height = height * scale;
Canvas.getContext('2d').scale(scale, scale);
html2canvas(dom, {
canvas: Canvas,
scale,
useCORS: true,
logging: true,
width: width + 'px',
hegiht: height + 'px',
}).then((canvas) => {
const context = canvas.getContext('2d');
// 關閉抗鋸齒形
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
// canvas轉化爲圖片
const img = canvas2Image(canvas, canvas.width, canvas.height);
document.body.appendChild(img);
img.style.cssText = "width:100%;height:100%;position:absolute;top:0;left:0;right:0;bottom:0;opacity:0;";
}
}
canvas2Image(canvas, width, height) {
const retCanvas = document.createElement('canvas');
const retCtx = retCanvas.getContext('2d');
retCanvas.width = width;
retCanvas.height = height;
retCtx.drawImage(canvas, 0, 0, width, height, 0, 0, width, height);
const img = document.createElement('img');
img.src = retCanvas.toDataURL('image/jpeg'); // 能夠根據須要更改格式
return img;
}
複製代碼
剛開始作的時候也是網上一堆文章亂看,不斷的試錯,最後愉快的實現了長按保存圖片的功能,作完才發現也很簡單哈,這篇文章完整的介紹了整個流程,拿走不謝!npm
更多精彩內容歡迎關注個人公衆號【天道酬勤Lewis】canvas