It seems like there are two ways to send <canvas>
data to the server. One is to use canvas.getImageData()
to get an array of pixels and their 8-bit color values. The other method is to use canvas.toDataURL())
to send a file attachment. This method is demonstrated here.
I want to build a site where people can save their canvas drawings. Which method would be more scalable and faster for my users?
To open your options: Using fabric.js you could serialize your fabric.js canvas to JSON.
Not only does it provide an additional layer of editing capabilities but allows you to do the following (Not to mention being able to edit their images at a later stage) :
Now when you want to serialize this canvas you simply call the
JSON.stringify
function on the fabric canvas object;JSON.stringify(canvas);
Which gives you something like the following for our example above:
De serializing the canvas back to its state is reversed by using:
Some Additional Resources:
Kitchen Sink Demo - View the capabilities of fabric.js (Including free drawing; modifying the size and position of the free drawing afterwards)
Homepage
You can .toDataURL() it
or with jQuery
And then send that string through to the server via XHR, which should be the quickest.
Try this it works for me in the same case :-
to get json data for whole canvas use
JSON.stringify(canvas);
and to load again from json use below code :-