toDataURL 中的默认图像质量是多少?

What's default image quality in toDataURL?

这里是 doc 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.。图像质量的默认值是多少?

闪烁:JPEG 为 0.92,WEBP 为 0.80。

Gecko:JPEG 为 0.92(不支持 webp)

我是怎么找到它的

我使用 https://code.google.com/p/chromium/codesearch

搜索“toDataURL”

相关代码 https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/core/html/HTMLCanvasElement.cpp&q=toDataURL&sq=package:chromium&l=517

看起来像这样设置质量 double quality = UndefinedQualityValue; 其中 UndefinedQualityValue; 是常数 -1.0。

沿着调用链向下,我们到达 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;
}

其中值如下:

  • JPEGImageEncoder::DefaultCompressionQuality = 92 source
  • WEBPImageEncoder::DefaultCompressionQuality = 80 source

壁虎

我们可以按照与上述类似的过程使用 mozilla 代码搜索,或者只是 google“gecko 图像编码器”,它将带您到以下值: JPEG:0.92 source