I have a problem with .toDataURL()
for large canvas. I want to enconde in base64
and decode on php file but if I have a large canvas the strDataURI
variable is empty.
My code:
var strDataURI = canvas.toDataURL();
strDataURI = strDataURI.substr(22, strDataURI.length);
$.post("save.php",
{
str: strDataURI
};
Is there any alternative to .toDataURL()
or some way to change the size limit?
Thanks.
The toDataURL() function returns a data: URL representing the canvas at the time that the function is called. Using this function you can transfer the canvas to your server (using jQuery for example) as a base64 encoded string and then use server-side scripting (eg PHP, ASP) to decode the string and save it to a file.
Here is the code to do that: var canvas = document. getElementById("ex1"); var dataUrl = canvas. toDataURL(); window.
Yes, images are loaded asynchronously by the browser.
function convertCanvasToImage() { let canvas = document. getElementById("canvas"); let image = new Image(); image. src = canvas. toDataURL(); return image; } let pnGImage = convertCanvasToImage(); document.
It is possible to grab the contents of an HTML5 canvas using the canvas toDataURL () function. Here is a code example of that is done: var canvas = document.getElementById ("ex1"); var dataUrl = canvas.toDataURL (); The data returned from the toDataURL () function is a string that represents an encoded URL containing the grabbed graphical data.
A very real canvas, not to be confused with the digital kind. (Image source: Jackson’s Art) Let’s say you’ve created a web application that uses an HTML canvas to dynamically render something, generated automatically or based on user input. Great!
Put a listener for a click event on a Save button under the canvas that calls the saveFractal () function when activated. Called .toDataURL () on the currentCanvas and assigned it to another variable dataURL. POSTed the dataURL in a fetch to our back-end server under the image parameter, persisting the image in our database.
Wonderful company to buy art… This is a wonderful company to buy art from! I had an issue with my art print a... I’ve ordered numerous prints now from… I’ve ordered numerous prints now from Great Big Canvas and will never buy anywhe... Great prices and easy to search on line.
You should first consider this: the size of the upload is limited. The limit depends on browser, OS and server environment. You can have a look at this article: http://www.motobit.com/help/scptutl/pa98.htm
In general you can try something like this: first we need a function to convert the dataURI to a blob:
function convertDataURItoBlob(dataURI) {
'use strict'
var byteString,
mimestring
if(dataURI.split(',')[0].indexOf('base64') !== -1 ) {
byteString = atob(dataURI.split(',')[1])
} else {
byteString = decodeURI(dataURI.split(',')[1])
}
mimestring = dataURI.split(',')[0].split(':')[1].split(';')[0]
var content = new Array();
for (var i = 0; i < byteString.length; i++) {
content[i] = byteString.charCodeAt(i)
}
var rawContent = new Uint8Array(content),
returnBlob = new Blob([rawContent], {type: mimestring})
return returnBlob;
}
and next a function for the upload of the file, using XMLHttpRequest2:
function upload(blob) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/yourServerEndPoint', true);
xhr.onload = function(e) { ... };
xhr.send(blob);
}
Now you can pass your strDataURI
to the first function and then upload the file with the second function.
You can have a deeper look at XMLHTTPRequest2 here: http://www.html5rocks.com/en/tutorials/file/xhr2/ and about the blob constructor here: https://developer.mozilla.org/en-US/docs/DOM/Blob
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With