You can pretty much determine if the file is compressed by looking at the file type. If you inspect the string toDataURL()
produces, you will see a mime-type defining either a PNG or JPEG file - in some cases where browsers support other file formats you can also see BMP and ICO file formats.
We know that a PNG file is always compressed as the PNG standard only support compression type 0 which is LZ77 compression (on top of line filters which affects the final compressed size).
JPEG always compresses as it uses DCT.
Compression for BMP is optional as well as for TIFF, though no browsers I know of support TIFF out of the box. It's reasonable to assume BMP and ICO files are uncompressed. They do exist in compression forms such as RLE but these are rare and can cause problems for some BMP parsers. To be absolutely sure though, you would have to parse the binary data to look in the header for compression flags.
Notice that toDataURL()
always work on a raw uncompressed bitmap. It does not matter if the original image drawn to the canvas was compressed or not - the original image is always converted to a raw bitmap before drawn (actually when it's loaded).
After calling toDataURL()
however, the binary image that it produces internally is converted to a Base-64 string. This means an increase of size by 33% due to how Base-64 works. On top of that: each char in JavaScript occupies 2 bytes (this is not a problem of course when in a JavaScript environment). So the length of the string is not a good indicator as it may possibly exceed the raw size (width x height x 4) in some cases (toBlob()
is in any case a better alternative than toDataURL()
due to its higher performance and reduced size as well as being async/non-blocking).