问题
Here is doc for toDataURL
:
Syntax
canvas.toDataURL(type, encoderOptions);
Parameters
type Optional
A DOMString indicating the image format. The default type is image/png.
encoderOptions Optional
A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
If this argument is anything else, the default value for image quality is used. Other arguments are ignored.
If this argument is anything else, the default value for image quality is used. Other arguments are ignored.
. What's the default value for image quality?
回答1:
Blink: 0.92 for JPEG and 0.80 for WEBP.
Gecko: 0.92 for JPEG (no webp support)
How I found it
I searched for "toDataURL" using https://code.google.com/p/chromium/codesearch
Relevant code https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/core/html/HTMLCanvasElement.cpp&q=toDataURL&sq=package:chromium&l=517
Looks like it sets the quality like so double quality = UndefinedQualityValue;
where UndefinedQualityValue;
is a constant of -1.0.
Following the call chain further down we get to https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/platform/graphics/ImageBuffer.cpp&sq=package:chromium&l=348&rcl=1448382621
bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality, Vector<unsigned char>* encodedImage) const
{
if (mimeType == "image/jpeg") {
int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality;
if (quality >= 0.0 && quality <= 1.0)
compressionQuality = static_cast<int>(quality * 100 + 0.5);
if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage))
return false;
} else if (mimeType == "image/webp") {
int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality;
if (quality >= 0.0 && quality <= 1.0)
compressionQuality = static_cast<int>(quality * 100 + 0.5);
if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage))
return false;
} else {
if (!PNGImageEncoder::encode(*this, encodedImage))
return false;
ASSERT(mimeType == "image/png");
}
return true;
}
Where the values are as follows:
JPEGImageEncoder::DefaultCompressionQuality = 92
sourceWEBPImageEncoder::DefaultCompressionQuality = 80
source
Gecko
We can follow a similar process as the above with mozilla code search, or just google "gecko image encoder" which takes you to the following value: JPEG: 0.92 source
来源:https://stackoverflow.com/questions/33904144/whats-default-image-quality-in-todataurl