Home  >  Article  >  Web Front-end  >  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?

王林
王林forward
2023-09-05 22:09:11959browse

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>

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)
 });

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete