Home > Web Front-end > H5 Tutorial > body text

How to put pictures in HTML5 Canvas and save them as pictures

不言
Release: 2018-06-22 15:35:28
Original
3198 people have browsed it

This article mainly introduces the method of placing pictures in HTML5 Canvas and saving them as pictures. Especially saving the picture content as pictures is a very practical function. Friends who need it can refer to it

Use JavaScript to copy the image into the canvas

To put the image into the canvas, we use the drawImage method of the canvas element:

 // Converts image to canvas; returns new canvas element

  function convertImageToCanvas(image) {

  var canvas = document.createElement("canvas");

  canvas.width = image.width;

  canvas.height = image.height;

  canvas.getContext("2d").drawImage(image, 0, 0);</p>
<p> return canvas;

  }
Copy after login

0 here, 0 parameter coordinate point on the canvas, the image will be copied to this place.

Use JavaScript to save the canvas into an image format

If your work on the canvas has been completed, you can use the following simple method to convert the canvas data into an image format :

// Converts canvas to an image

  function convertCanvasToImage(canvas) {

  var image = new Image();

  image.src = canvas.toDataURL("image/png");

  return image;

  }
Copy after login

This code can magically convert canvas into PNG format!

These techniques for converting between images and canvases may be much simpler than you think.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

HTML5 and JS to implement local image cropping and uploading functions

Image compression through HTML5 mobile development Upload function

The above is the detailed content of How to put pictures in HTML5 Canvas and save them as pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template