To convert a div
element to an image using Javascript, we can make use of the HTML5 canvas element. The basic steps are:
div
element that needs to be converted to image.div
element on it.toDataURL()
method.img
element and set its source to the image.index.tsx666 chars24 lines
In the code above, replace "myDiv"
with the id
of the div
element that you want to convert to image. This code will add the converted image at the end of the body
element. You may modify it to add the image in a different position as per your requirements.
gistlibby LogSnag