Skip to content Skip to sidebar Skip to footer

Convert Div To Single Image With Canvas-javascript Css

I try to convert content div below to a single image downloadable How is the piece with the canvas or other code after convert show Download Image button how i do it

Solution 1:

You should use the html2canvas library for this. Make sure you set allowTaint to true so that it renders the cross-origin images as well. See this JSFiddle for an example.

The html2canvas function returns a promise that provides a <canvas> element which you can put wherever you want to display the rendered image. You can then treat it like you would any other canvas, including right-clicking it to download as an image.

This updated JSFiddle includes a link to download the image. Keep in mind that this only works in browsers that support the download attribute on a tags.

Solution 2:

Download and import this JS file to the page where you need to convert div to image. Link

Image format can be changed to PNG or JPG.

Use the below function on onclick functionality.

var tagName = document.getElementsByClassName("grid-div-tag").id;
html2canvas(document.getElementById(tagName)).then(function (canvas) {
    var anchorTag = document.createElement("a");
    document.body.appendChild(anchorTag);
    anchorTag.download = "Picture.png";
    anchorTag.href = canvas.toDataURL();
    anchorTag.target = '_blank';
    anchorTag.click();
});

Reference link: https://codepedia.info/convert-html-to-image-in-jquery-div-or-table-to-jpg-png

Post a Comment for "Convert Div To Single Image With Canvas-javascript Css"