Fiddle 1 (Using html2canvas): http://liveweave.com/HM9hSY
Fiddle 2 (Using html2canvas without Codemirror): http://liveweave.com/rS8yxI
Fiddle 3 (Using html2canvas with Codemirror): http://liveweave.com/TF3Ono
Fiddle 4 (Using html2canvas with Codemirror): http://liveweave.com/hqPoQg
Today I wanted to try and export an iframe as an Image. I did some research on this and came across a plugin called html2canvas which can save html as an image. (The documentation can be viewed here).
Capture HTML Canvas as gif/jpg/png/pdf?
Using HTML5/Canvas/JavaScript to take in-browser screenshots
The first fiddle is just an experiment so save html as an image. (This fiddle is not using Codemirror)
The second fiddle saves the image but doesn't grab the iframe preview given from source. (This fiddle is not using Codemirror)
The third fiddle does not save an image. (This fiddle is not using Codemirror)
My problem is with the 2nd and 3rd fiddles where [html2canvas][17] does not save the iframe's preview.
I've been playing with this throughout yesterday and today and I haven't been successful.
Today I decided to try and use Codemirror's code editor to render what I want instead.
Basically I take a screenshot of the body and embed an image into it.
(This is Fiddle 4 I'm referring to).
I then open the image in a new window using window.open
after that I reset the code editor back to where it was.
Is it possible to save an iframe's preview/source as an image? Am I doing anything wrong to achieve this effect?
$(document).ready(function() {
$(#saveimg).click(function() {
html2canvas($(#preview), {
onrendered: function(canvas) {
var myImage = canvas.toDataURL(image/png);
$(#imgprev).html(<img src='+ myImage +' />);
}
});
});
});
<script src=https://raw.github.com/niklasvh/html2canvas/gh-pages/build/html2canvas.js></script>
<script type=text/javascript src=http://code.jquery.com/jquery-latest.min.js></script>
<link type='text/css' rel='stylesheet' href='http://necolas.github.io/normalize.css/3.0.1/normalize.css' />
<div align=center>
<p>
<button id=saveimg>
Save as Image
</button>
</p>
</div>
<iframe id=preview src=http://duckduckgo.com/></iframe>
<div id=imgprev></div>
<canvas id=mycanvas></canvas>