Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send DIV content to html5 CANVAS

Is it possible to "draw" the content of a div to a canvas... i have done the div manipulation with css, but need canvas to "save" the content to jpg with the .dataToURL function

so the question is... do you know a HTML, CSS, jQuery function that transfer the content of a div and draw it to a canvas

thanks in advance

like image 808
menardmam Avatar asked Oct 04 '11 20:10

menardmam


People also ask

Can you put a div in a canvas?

You cannot place elements inside a canvas (and have both displayed); they are only displayed if the browser does not understand the canvas element.

How do I add HTML elements to canvas?

According to the HTML specification you can't access the elements of the Canvas. You can get its context, and draw in it manipulate it, but that is all. BUT, you can put both the Canvas and the html element in the same div with a a position: relative and then set the canvas and the other element to position: absolute .

Does HTML5 support canvas element?

The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images.

What is the difference between canvas and div?

Html5 Canvas is an artbook with a pencil. A Div is the desk you put the Canvas on to.


2 Answers

<canvas> does not directly support placing HTML content on it, as combining this with <IFRAME> could potentially lead to lost of private information.

What you can do is that you dynamically create SVG image and then draw this on <canvas>. SVG has better support for rich-text formatting than <canvas>.

jQuery library for dynamic SVG creation:

http://keith-wood.name/svg.html

(See Text example)

like image 96
Mikko Ohtamaa Avatar answered Sep 18 '22 13:09

Mikko Ohtamaa


Check out html2Canvas, it takes a dom element and converts it to a canvas, and then you can draw that canvas onto another canvas something like this:

http://html2canvas.hertzen.com/

var domElement = document.getElementById('myElementId');
html2canvas(domElement, {
    onrendered: function (domElementCanvas) {
        var canvas = document.createElement('canvas');
        canvas.getContext('2d').drawImage(domElementCanvas, 0, 0, 100, 100);

        // do something with canvas
    }
}
like image 38
Owen Avatar answered Sep 18 '22 13:09

Owen