JSFiddle - React, Tailwind, and code Playground

by Osama Qassar

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<div id="mydiv">
    <img src="/img/logo-white.png" />
    <p>dssdfsdfsdfs!</p>
</div>
<br>
<br>
    
<div id="canvas">
    <p>Canvas:</p>
    </div>
    
    <div id="image">
        <p>Image:</p>
    </div>

CSS

#mydiv {
    background-color: lightblue;
    width: 200px;
    height: 200px
}

JavaScript

html2canvas([document.getElementById('mydiv')], {
    onrendered: function (canvas) {
        document.getElementById('canvas').appendChild(canvas);
        var data = canvas.toDataURL('image/png');
        // AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server

        var image = new Image();
        image.src = data;
        document.getElementById('image').appendChild(image);
    }
});