views:

155

answers:

2

hi,

i need to create a component using html5 canvas that given an image the user can paint on it and directly (via a kind of save button) upload it's customized version on the server.

Can i use html canvas for it ? Any suggestion ?

thx in advance

+5  A: 

You can get the image as data-url like this:

var dataUrl = document.getElementById('your-canvas').toDataUrl();

You could then send this (very long string) to the server and save it to a file after decoding it (it is encoded in base64).

EDIT: Remember to submit this via POST, as suggested in the comments. GET has some length-limits in various browsers, so its likely to exceed those limits with such a huge amout of data.

elusive
Remember to use `POST` as the Ajax method. A GET might break due to its size.
Pekka
Thanks for your addition!
elusive
A: 

I tried:

var canvas = document.getElementById('testcanvas');
var dataUrl = canvas.toDataUrl();

but I get this error in firefox error console:

Error: canvas.toDataUrl is not a function Source File: http://localhost/wordpress30/html5.html Line: 40

HungryCoder