Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas .toDataURL() returns partially corrupted image

We have a web application that (very intermittently) is returning a partially corrupted PNG image data URL. The image has a large black section covering some of the image but not all of it. These images are customer signatures.
For example:

enter image description here

We are using the signature pad component from this. The image URL is returned using the getSignatureImage() routine.

The only thing I have seen from the access logs seems to show the affected requests coming from "Samsung Galaxy Tab" devices, but using Chrome 59. However other requests from the same devices work OK.

Any help appreciated.

like image 846
tip2tail Avatar asked Aug 01 '17 09:08

tip2tail


People also ask

How do I save canvas as an image in canvas toDataURL?

You can save a canvas to an image file by using the method canvas. toDataURL() , that returns the data URI for the canvas' image data. The method can take two optional parameters canvas.

What is canvas toDataURL?

toDataURL() method returns a data URL containing a representation of the image in the format specified by the type parameter. The desired file format and image quality may be specified. If the file format is not specified, or if the given format is not supported, then the data will be exported as image/png .

Is canvas toDataURL async?

Yes, images are loaded asynchronously by the browser.

How do I get canvas 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.


2 Answers

I think it is not a memory issue. When image consumes too much memory, an image will by simply black (empty pixels rgb(0,0,0)). Try to get blob from canvas - canvas.toBlob(function(blob) {}). Probably your image not loaded or draw yet. Try to put window.setTimeout somewhere before you get the image data (base64 or blob)

like image 113
Alex Nikulin Avatar answered Oct 20 '22 17:10

Alex Nikulin


We have now confirmed that the issue was device dependant.

The end users affected have changed from older Samsung Galaxy Tab 4 devices to Tab S2 devices. The problem no longer occurs.

It appears to have been a string length limitation on the older devices.

Thanks for the help!

like image 1
tip2tail Avatar answered Oct 20 '22 17:10

tip2tail