Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering HTML elements to <canvas>

Is there a way to have an arbitrary HTML element rendered in a canvas (and then access its buffer...).

like image 450
gotch4 Avatar asked Sep 29 '12 12:09

gotch4


People also ask

Can you put HTML elements in canvas?

Because browsers will display either a canvas element or HTML controls that you put inside that element, but not both, you must place your controls outside of your canvas elements. To make it appear as though HTML controls are inside a canvas, you can use CSS to place the controls above the canvas.

Can canvas render HTML?

You won't get real HTML rendering to <canvas> per se currently, because canvas context does not have functions to render HTML elements.

How do you add elements to canvas?

You cannot add elements into the canvas like that. The innerHTML of the canvas is shown when a browser does not suport <canvas> . You should extra elements beside the canvas, or a <div> layer above the canvas.


2 Answers

You won't get real HTML rendering to <canvas> per se currently, because canvas context does not have functions to render HTML elements.

There are some emulations:

html2canvas project http://html2canvas.hertzen.com/index.html (basically a HTML renderer attempt built on Javascript + canvas)

HTML to SVG to <canvas> might be possible depending on your use case:

https://github.com/miohtama/Krusovice/blob/master/src/tools/html2svg2canvas.js

Also if you are using Firefox you can hack some extended permissions and then render a DOM window to <canvas>

https://developer.mozilla.org/en-US/docs/HTML/Canvas/Drawing_Graphics_with_Canvas?redirectlocale=en-US&redirectslug=Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas

like image 125
Mikko Ohtamaa Avatar answered Oct 07 '22 07:10

Mikko Ohtamaa


Take a look on MDN (archived)

It will render html element using creating SVG images.

For Example: There is <em>I</em> like <span style="color:white; text-shadow:0 0 2px blue;">cheese</span> HTML element. And I want to add it into <canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas> Canvas Element.

Here is Javascript Code to add HTML element to canvas.

var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d');  var data = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">' +   '<foreignObject width="100%" height="100%">' +   '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:40px">' +   '<em>I</em> like <span style="color:white; text-shadow:0 0 2px blue;">cheese</span>' +   '</div>' +   '</foreignObject>' +   '</svg>';  var DOMURL = window.URL || window.webkitURL || window;  var img = new Image(); var svg = new Blob([data], {   type: 'image/svg+xml;charset=utf-8' }); var url = DOMURL.createObjectURL(svg);  img.onload = function() {   ctx.drawImage(img, 0, 0);   DOMURL.revokeObjectURL(url); }  img.src = url;
<canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas>
like image 22
Suresh Mahawar Avatar answered Oct 07 '22 06:10

Suresh Mahawar