国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

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

2024-08-26 00:21:31
字體:
供稿:網(wǎng)友

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

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

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

1. html2canvas截屏

保存的圖片節(jié)點(diǎn)最好是img標(biāo)簽: 想要截屏的節(jié)點(diǎn)最好是img標(biāo)簽的圖片,經(jīng)測試如果是 background-image 會有點(diǎn)模糊,需要特別注意下。

npm i html2canvas --saveimport html2canvas from 'html2canvas';// 想要保存的圖片節(jié)點(diǎn)const dom = document.querySelector('img');// 創(chuàng)建一個(gè)新的canvasconst 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,不會執(zhí)行傳入的方法    }, false);  }}// 添加生成的圖片到bodyconst 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,不會執(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頁面中長按保存圖片功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時(shí)回復(fù)大家的!


注:相關(guān)教程知識閱讀請移步到HTML教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 西藏| 榆树市| 闵行区| 鄂温| 隆化县| 浏阳市| 青田县| 泰和县| 元阳县| 阳高县| 西乌珠穆沁旗| 和平区| 阿巴嘎旗| 宜宾县| 肇庆市| 武宁县| 武夷山市| 三门峡市| 伊吾县| 西峡县| 红安县| 宜兰市| 南召县| 郁南县| 富川| 天峻县| 嘉善县| 东乡县| 松江区| 晋中市| 新巴尔虎左旗| 珠海市| 喜德县| 凭祥市| 绩溪县| 丰县| 来宾市| 普洱| 翁牛特旗| 桓台县| 禹州市|