1

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

4

1 回答 1

1

闪烁: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 资源
  • WEBPImageEncoder::DefaultCompressionQuality = 80 资源

壁虎

我们可以通过 mozilla 代码搜索遵循与上述类似的过程,或者只是谷歌“gecko 图像编码器”,它会将您带到以下值:JPEG:0.92

于 2015-11-24T21:40:02.630 回答