Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canvas.toDataURL() for large canvas

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.

like image 318
ptCoder Avatar asked Apr 22 '13 20:04

ptCoder


People also ask

What is canvas toDataURL ()?

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.

How do I get canvas toDataURL?

Here is the code to do that: var canvas = document. getElementById("ex1"); var dataUrl = canvas. toDataURL(); window.

Is canvas toDataURL async?

Yes, images are loaded asynchronously by the browser.

How do I turn a photo into a canvas?

function convertCanvasToImage() { let canvas = document. getElementById("canvas"); let image = new Image(); image. src = canvas. toDataURL(); return image; } let pnGImage = convertCanvasToImage(); document.

How to grab the contents of an HTML5 canvas using todataurl?

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.

What is an HTML Canvas?

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!

How to save an image from a canvas to another canvas?

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.

Is great big canvas a good company to buy art?

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.


1 Answers

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

like image 130
Mimo Avatar answered Oct 30 '22 03:10

Mimo