Home > Web Front-end > HTML Tutorial > How to save a DIV as an image with extension using HTML5 canvas?

How to save a DIV as an image with extension using HTML5 canvas?

王林
Release: 2023-09-05 22:09:11
forward
970 people have browsed it

How to save a DIV as an image with extension using HTML5 canvas?

#DIV content can be saved as an image with the help of the html2canvas() function in JavaScript. The DIV tag defines a section in an HTML document.

Example

<div id = &rdquo;cpimg&rdquo;   style = &rdquo;padding:  25px ; &rdquo; >  
   <h4>Welcome</h4>
</div>
Copy after login

This shows a divided region named cpimg.

html2canvas() function uses the following code to save a div as an image -

html2canvas(document.querySelector(&ldquo;#cpimg&rdquo;)).then(canvas
 {    
    document.body.appendChild(canvas)
 });
Copy after login

It saves the referenced div part "cpimg" into an image.

The above is the detailed content of How to save a DIV as an image with extension using HTML5 canvas?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
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