A complete solution to the long press and save picture function in HTML5 page

Time:2020-3-19

This paper introduces how to realize the function of long press to save pictures in H5.

Long press to save pictures is a very common requirement in H5, but JS does not have such a capability, so it can either use Android or IOS’s native capability, Or we can use canvas to draw a (screenshot) by ourselves. Compared with the original cost is too high, and we have to rely on the app. Compared with H5, which has a wide spread and cross platform, it is not appropriate. So canvas has become a common means.

Here are the detailed steps:

1. Screenshot of html2canvas

The best saved image node is img tag: the best node for screenshot is img tag image. If the image is background image, it will be a little fuzzy. You need to pay special attention to it.

npm i html2canvas --save
import html2canvas from 'html2canvas';
//Picture node to save
const dom = document.querySelector('img');
//Create a new canvas
const Canvas = document.createElement('canvas');
Const width = document.body.offsetwidth; // the width of the visible screen
Const height = document.body.offsetheight; // the height of the visible screen
Const scale = window.devicepixelradio; // device's devicepixelradio
//Enlarge the canvas by scale, and then put it on the small screen to solve the blur problem
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');
  //Turn off antialiasing
  context.mozImageSmoothingEnabled = false;
  context.webkitImageSmoothingEnabled = false;
  context.msImageSmoothingEnabled = false;
  context.imageSmoothingEnabled = false;
  //Canvas to image
  canvas2Image(canvas, canvas.width, canvas.height);
});

2. Convert canvas2image to image

In general, it’s good to change to JPEG format.

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 '); // format can be changed as needed
  return img;
}

3. Long press to save the picture

First, implement a long press method. After long press, append the generated image to the body and float it on the screen transparently.

//Encapsulate a long press method
longPress(fn) {
  let timeout = 0;
  const $this = this;
  for (let i = 0; i < $this.length; i++) {
    $this[i].addEventListener('touchstart', () => {
      Timeout = setTimeout (FN, 800); // if the long press time exceeds 800ms, the incoming method will be executed 
    }, false);
    $this[i].addEventListener('touchend', () => {
      Cleartimeout (timeout); // if the long press time is less than 800ms, the incoming method will not be executed
    }, false);
  }
}
//Add the generated picture to the 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. The complete code is as follows

$.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); // if the long press time exceeds 800ms, the incoming method will be executed 
    }, false);
    $this[i].addEventListener('touchend', () => {
      Cleartimeout (timeout); // if the long press time is less than 800ms, the incoming method will not be executed
    }, false);
  }
};
$('img').longPress(() => {
  saveImg();
});saveImg() {
  //Picture node to save
  const dom = document.querySelector('img');
  //Create a new canvas
  const Canvas = document.createElement('canvas');
  Const width = document.body.offsetwidth; // the width of the visible screen
  Const height = document.body.offsetheight; // the height of the visible screen
  Const scale = window.devicepixelratio; // devicepixelratio of the device
  //Enlarge the canvas by scale, and then put it on the small screen to solve the blur problem
  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');
    //Turn off antialiasing
    context.mozImageSmoothingEnabled = false;
    context.webkitImageSmoothingEnabled = false;
    context.msImageSmoothingEnabled = false;
    context.imageSmoothingEnabled = false;
    //Canvas to image
    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 '); // format can be changed as needed
  return img;
}

At the beginning, I also read a lot of articles on the Internet, tried and tried again and again. At last, I happily realized the function of long press to save pictures. Only when I finished, I found that it was very simple. This article completely introduced the whole process, and I can’t thank you for taking it away!

summary

The above is an article introduced by Xiaobian to you to completely solve the long press and save picture function in HTML 5 page. I hope it can help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time!