App下載

怎么解決在網(wǎng)頁(yè)長(zhǎng)按保存圖片問題?圖片保存解決方法詳情!

猿友 2021-08-06 11:43:41 瀏覽數(shù) (11125)
反饋

在之前的網(wǎng)頁(yè)瀏覽器中我們可以通過長(zhǎng)按保存圖片來(lái)實(shí)現(xiàn)保存圖片等等的操作,現(xiàn)在的話就被去掉了,那么我們就來(lái)講講有關(guān)于“怎么解決在網(wǎng)頁(yè)長(zhǎng)按保存圖片問題?”這個(gè)問題吧!

下面是詳細(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;  // 可見屏幕的寬
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. 長(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í)間超過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í)間超過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;  // 可見屏幕的寬
  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)了長(zhǎng)按保存圖片的功能,做完才發(fā)現(xiàn)也很簡(jiǎn)單哈,這篇文章完整的介紹了整個(gè)流程,拿走不謝!

總結(jié)

那么以上所述的就是今天小編給大家介紹的有關(guān)于“怎么解決在網(wǎng)頁(yè)長(zhǎng)按保存圖片問題?”這方面的相關(guān)內(nèi)容分享!更多的相關(guān)內(nèi)容都可以在W3Cschool中進(jìn)行學(xué)習(xí)! 


0 人點(diǎn)贊