How to append html content on canvas.I tried but not working. any way is there to append html on canvas.
http://jsfiddle.net/mktgnp3e/780/
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var html="<span><p>Content....</p></span>";
ctx.font = "20px Georgia";
ctx.fillText("Hello World!", 10, 50);
ctx.font = "30px Verdana";
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// Fill with gradient
ctx.fillStyle = gradient;
ctx.fillText(html, 10, 90);
The basic idea is to copy the HTML + the styles for the HTML to a
<foreignObject>
inside an SVG element. Then you take a screenshot of the SVG element and draw it on the canvas usingdrawImage
. There are lots of problems especially if you want to use images, since tainted canvases may not be exported. Next come some code but I wouldn't advise to use it in production.Please click the button do get the screenshot. A canvas (dark background #333) element will be prepended to the body.