您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何實(shí)現(xiàn)HTML5頁(yè)面中長(zhǎng)按保存圖片功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何實(shí)現(xiàn)HTML5頁(yè)面中長(zhǎng)按保存圖片功能”這篇文章吧。
長(zhǎng)按保存圖片是現(xiàn)在一些宣傳頁(yè)H5中很常見(jiàn)的需求,但是js沒(méi)有這樣的能力,所以要么借助android或ios的原生能力,要么用canvas自己畫(huà)一個(gè)(截屏),相比較原生成本太高,且必須依賴(lài)于app,相對(duì)于流傳性很廣且跨平臺(tái)的H5來(lái)說(shuō)不合時(shí)宜,所以 canvas 成為我們常用的手段。
下面是詳細(xì)的步驟:
1. html2canvas截屏
保存的圖片節(jié)點(diǎn)最好是img標(biāo)簽: 想要截屏的節(jié)點(diǎn)最好是img標(biāo)簽的圖片,經(jīng)測(cè)試如果是 background-image 會(huì)有點(diǎn)模糊,需要特別注意下。
npm i html2canvas --save import html2canvas from 'html2canvas'; // 想要保存的圖片節(jié)點(diǎn) const dom = document.querySelector('img'); // 創(chuàng)建一個(gè)新的canvas const Canvas = document.createElement('canvas'); const width = document.body.offsetWidth; // 可見(jiàn)屏幕的寬 const height = document.body.offsetHeight; // 可見(jiàn)屏幕的高 const scale = window.devicePixelRadio; // 設(shè)備的devicePixelRadio // 將Canvas畫(huà)布放大scale倍,然后放在小的屏幕里,解決模糊問(wèn)題 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格式就很不錯(cuò)了。
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. 長(zhǎng)按保存圖片
先實(shí)現(xiàn)一個(gè)長(zhǎng)按的方法,長(zhǎng)按之后把生成的圖片append到body,透明浮在屏幕上。
// 封裝一個(gè)長(zhǎng)按方法 longPress(fn) { let timeout = 0; const $this = this; for (let i = 0; i < $this.length; i++) { $this[i].addEventListener('touchstart', () => { timeout = setTimeout(fn, 800); // 長(zhǎng)按時(shí)間超過(guò)800ms,則執(zhí)行傳入的方法 }, false); $this[i].addEventListener('touchend', () => { clearTimeout(timeout); // 長(zhǎng)按時(shí)間少于800ms,不會(huì)執(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); // 長(zhǎng)按時(shí)間超過(guò)800ms,則執(zhí)行傳入的方法 }, false); $this[i].addEventListener('touchend', () => { clearTimeout(timeout); // 長(zhǎng)按時(shí)間少于800ms,不會(huì)執(zhí)行傳入的方法 }, false); } }; $('img').longPress(() => { saveImg(); });saveImg() { // 想要保存的圖片節(jié)點(diǎn) const dom = document.querySelector('img'); // 創(chuàng)建一個(gè)新的canvas const Canvas = document.createElement('canvas'); const width = document.body.offsetWidth; // 可見(jiàn)屏幕的寬 const height = document.body.offsetHeight; // 可見(jiàn)屏幕的高 const scale = window.devicePixelRatio; // 設(shè)備的devicePixelRatio // 將Canvas畫(huà)布放大scale倍,然后放在小的屏幕里,解決模糊問(wèn)題 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; }
剛開(kāi)始做的時(shí)候也是網(wǎng)上一堆文章亂看,不斷的試錯(cuò),最后愉快的實(shí)現(xiàn)了長(zhǎng)按保存圖片的功能,做完才發(fā)現(xiàn)也很簡(jiǎn)單哈,這篇文章完整的介紹了整個(gè)流程,拿走不謝!
以上是“如何實(shí)現(xiàn)HTML5頁(yè)面中長(zhǎng)按保存圖片功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。