I have a canvas with webGL-drawings, created by Blend4Web framework.
I tried to save image using toDataURL()
:
image= $('canvas')[0].toDataURL();
All platforms works perfect, except iOS (iphone & ipad)
I know about webGL aspects: Canvas toDataURL() returns blank image only in Firefox, preserveDrawingBuffer
is enabled.
Also, I know about limitations in iOS: iOS HTML5 Canvas toDataURL, but canvas is small, even 100×500px images are blank (it is 0,05MP, limit is 3MP).
I use toDataURL()
to send graphic information on server.
toDataURL() The HTMLCanvasElement. toDataURL() method returns a data URL containing a representation of the image in the format specified by the type parameter.
toDataURL is a synchronous operation that does not trigger any events. Being synchronous, it's a blocking operation so no alert is required to prevent the next command from executing before toDataURL is fully complete. The next command will execute when toDataURL is fully complete.
Saving HTML canvas as an image is pretty easy, it can be done by just right-clicking on the canvas and save it as an image.
To get the image data URL of the canvas, we can use the toDataURL() method of the canvas object which converts the canvas drawing into a 64 bit encoded PNG URL. If you'd like for the image data URL to be in the jpeg format, you can pass image/jpeg as the first argument in the toDataURL() method.
The following is a polyfill of toDataURL
. In order to get toBlob
to work on iOS, you need an additional polyfill, I recommend the following polyfill: https://github.com/blueimp/JavaScript-Canvas-to-Blob. Basically just download the canvas-to-blob.min.js. I would have recommended a direct toDataURL
polyfill by someone else, but I could not find one.
if (typeof HTMLCanvasElement.prototype.toDataURL !== "function") {
HTMLCanvasElement.prototype.toDataURL = function() {
this.toBlob(function (blob) {
var fileReader = new FileReaderSync();
return fileReader.readAsDataURL(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