日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

h5页面保存img_一文彻底解决HTML5页面中长按保存图片功能

發(fā)布時(shí)間:2023/12/2 HTML 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 h5页面保存img_一文彻底解决HTML5页面中长按保存图片功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文詳細(xì)介紹了如何在H5中實(shí)現(xiàn)長按保存圖片的功能。

長按保存圖片是現(xiàn)在一些宣傳頁H5中很常見的需求,但是js沒有這樣的能力,所以要么借助android或ios的原生能力,要么用canvas自己畫一個(gè)(截屏),相比較原生成本太高,且必須依賴于app,相對(duì)于流傳性很廣且跨平臺(tái)的H5來說不合時(shí)宜,所以 canvas 成為我們常用的手段。

下面是詳細(xì)的步驟:

1. html2canvas截屏

保存的圖片節(jié)點(diǎn)最好是img標(biāo)簽: 想要截屏的節(jié)點(diǎn)最好是img標(biāo)簽的圖片,經(jīng)測試如果是 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; // 可見屏幕的寬

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格式就很不錯(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. 長按保存圖片

先實(shí)現(xiàn)一個(gè)長按的方法,長按之后把生成的圖片append到body,透明浮在屏幕上。

// 封裝一個(gè)長按方法

longPress(fn) {

let timeout = 0;

const $this = this;

for (let i = 0; i < $this.length; i++) {

$this[i].addEventListener('touchstart', () => {

timeout = setTimeout(fn, 800); // 長按時(shí)間超過800ms,則執(zhí)行傳入的方法

}, false);

$this[i].addEventListener('touchend', () => {

clearTimeout(timeout); // 長按時(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); // 長按時(shí)間超過800ms,則執(zhí)行傳入的方法

}, false);

$this[i].addEventListener('touchend', () => {

clearTimeout(timeout); // 長按時(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; // 可見屏幕的寬

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;

}

剛開始做的時(shí)候也是網(wǎng)上一堆文章亂看,不斷的試錯(cuò),最后愉快的實(shí)現(xiàn)了長按保存圖片的功能,做完才發(fā)現(xiàn)也很簡單哈,這篇文章完整的介紹了整個(gè)流程,拿走不謝!

總結(jié)

以上所述是小編給大家介紹的一文徹底解決HTML5頁面中長按保存圖片功能,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

總結(jié)

以上是生活随笔為你收集整理的h5页面保存img_一文彻底解决HTML5页面中长按保存图片功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。