一文徹底解決HTML5頁面中長按保存圖片功能

本文詳細(xì)介紹了如何在H5中實現(xiàn)長按保存圖片的功能。
長按保存圖片是現(xiàn)在一些宣傳頁H5中很常見的需求,但是js沒有這樣的能力,所以要么借助android或ios的原生能力,要么用canvas自己畫一個(截屏),相比較原生成本太高,且必須依賴于app,相對于流傳性很廣且跨平臺的H5來說不合時宜,所以 canvas 成為我們常用的手段。
下面是詳細(xì)的步驟:
1. html2canvas截屏
保存的圖片節(jié)點最好是img標(biāo)簽: 想要截屏的節(jié)點最好是img標(biāo)簽的圖片,經(jīng)測試如果是 background-image 會有點模糊,需要特別注意下。
npm i html2canvas --save import html2canvas from 'html2canvas'; // 想要保存的圖片節(jié)點 const dom = document.querySelector('img'); // 創(chuàng)建一個新的canvas const Canvas = document.createElement('canvas'); const width = document.body.offsetWidth; // 可見屏幕的寬 const height = document.body.offsetHeight; // 可見屏幕的高 const scale = window.devicePixelRadio; // 設(shè)備的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'); // 關(guān)閉抗鋸齒形 context.mozImageSmoothingEnabled = false; context.webkitImageSmoothingEnabled = false; context.msImageSmoothingEnabled = false; context.imageSmoothingEnabled = false; // canvas轉(zhuǎn)化為圖片 canvas2Image(canvas, canvas.width, canvas.height); });
2. canvas2Image轉(zhuǎn)化為圖片
一般情況下轉(zhuǎn)為jpeg格式就很不錯了。
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'); // 可以根據(jù)需要更改格式 return img; }
3. 長按保存圖片
先實現(xiàn)一個長按的方法,長按之后把生成的圖片append到body,透明浮在屏幕上。
// 封裝一個長按方法 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,則執(zhí)行傳入的方法 }, false); $this[i].addEventListener('touchend', () => { clearTimeout(timeout); // 長按時間少于800ms,不會執(zhí)行傳入的方法 }, 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;";
4. 完整代碼如下
$.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,則執(zhí)行傳入的方法 }, false); $this[i].addEventListener('touchend', () => { clearTimeout(timeout); // 長按時間少于800ms,不會執(zhí)行傳入的方法 }, false); } }; $('img').longPress(() => { saveImg(); });saveImg() { // 想要保存的圖片節(jié)點 const dom = document.querySelector('img'); // 創(chuàng)建一個新的canvas const Canvas = document.createElement('canvas'); const width = document.body.offsetWidth; // 可見屏幕的寬 const height = document.body.offsetHeight; // 可見屏幕的高 const scale = window.devicePixelRatio; // 設(shè)備的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'); // 關(guān)閉抗鋸齒形 context.mozImageSmoothingEnabled = false; context.webkitImageSmoothingEnabled = false; context.msImageSmoothingEnabled = false; context.imageSmoothingEnabled = false; // canvas轉(zhuǎn)化為圖片 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'); // 可以根據(jù)需要更改格式 return img; }
剛開始做的時候也是網(wǎng)上一堆文章亂看,不斷的試錯,最后愉快的實現(xiàn)了長按保存圖片的功能,做完才發(fā)現(xiàn)也很簡單哈,這篇文章完整的介紹了整個流程,拿走不謝!
總結(jié)
以上所述是小編給大家介紹的一文徹底解決HTML5頁面中長按保存圖片功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
HTML5 Canvas實現(xiàn)的鼠標(biāo)點擊長按粒子動畫特效源碼
是一款基于HTML5+Canvas繪制散開的粒子,鼠標(biāo)按住粒子超光速動畫效果,非常有意思,歡迎喜歡此效果的朋友們前來下載使用2019-05-24- 這篇文章主要介紹了移動端html5模擬長按事件的實現(xiàn)方法的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-30
詳解移動端html5頁面長按實現(xiàn)高亮全選文本內(nèi)容的兼容解決方案
這篇文章主要介紹了詳解移動端html5頁面長按實現(xiàn)高亮全選文本內(nèi)容的兼容解決方案 ,具有一定的參考價值,有興趣的可以了解一下。2016-12-03- 這篇文章主要介紹了HTML5對手機頁面長按會粘貼復(fù)制禁用的解決方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-19