简体   繁体   English

画布的 toDataURL() 函数可能的数据类型是什么?

[英]What are the possible data types for canvas' toDataURL() function?

canvas.toDataURL(type, encoderOptions);

MDN 's description about the type parameter says the following: MDN关于type参数的描述如下:

type Optional类型可选
A DOMString indicating the image format.指示图像格式的DOMString The default type is image/png .默认类型是image/png

I have yet to find what all the types are, as I'm trying to evaluate what is possible to use in different cases.我还没有找到所有类型是什么,因为我正在尝试评估在不同情况下可以使用的内容。

Edit: MSDN's article about toDataURL() doesn't really help either.编辑: MSDN关于 toDataURL() 的文章也没有真正的帮助。


I know about the following:我知道以下几点:

  • image/png quality (encoder options) doesn't seem to influence output image/png质量(编码器选项)似乎不会影响输出
  • image/jpeg quality (encoder options) influences output image/jpeg质量(编码器选项)影响输出
  • image/webp quality influences output. image/webp质量影响输出。 (Chrome-only according to MDN). (根据 MDN,仅 Chrome)。

But after looking around I can't seem to find a list of possible types and their encoder options... that's pretty much all I could find.但是环顾四周后,我似乎找不到可能的类型及其编码器选项的列表……我能找到的几乎就是这些。 What are the other possibilities?还有哪些可能性?

Per the firefoxsource code , they seems to support:根据firefox源代码,它们似乎支持:

  • png PNG
  • jpeg JPEG格式
  • ico图标
  • bmp bmp

Chrome per the source code , should support: Chrome根据源代码,应该支持:

  • webp网页版
  • png PNG
  • jpeg JPEG格式
  • bmp bmp

Internet explorer modern versions, should be alike to Firefox (crossing fingers). Internet Explorer现代版本,应该和 Firefox 类似(交叉手指)。

If I need to vote the "per today" available options, I will go with: PNG, JPEG, and BMP如果我需要对“今天”可用选项进行投票,我会选择: PNG、JPEG 和 BMP

Quality influences options:质量影响选项:

  • JPEG, quality percent, where 0 is 0%, 0.5 is 50% and 1 is 100% JPEG,质量百分比,其中 0 为 0%,0.5 为 50%,1 为 100%
  • BMP, BPP, bytes per pixel (thanks to @apsillers for pointing out) BMP、BPP、每像素字节数(感谢@apsillers 指出)

Browser support for image output formats is largely implementation dependent.浏览器对图像输出格式的支持很大程度上取决于实现。 Here's the most relevant sentence in the WHATWG living standard :这是WHATWG 生活标准中最相关的一句话

User agents must support PNG (" image/png ").用户代理必须支持 PNG (" image/png ")。 User agents may support other types.用户代理可能支持其他类型。 If the user agent does not support the requested type, it must create the file using the PNG format.如果用户代理不支持请求的类型,它必须使用 PNG 格式创建文件。

PNG is required; PNG 是必需的; all other formats are optional.所有其他格式都是可选的。 This is explained generally in the standard's toDataURL description :这通常在标准的toDataURL描述中解释

canvas . toDataURL( [ type, ... ] )

Returns a data: URL for the image in the canvas.返回一个data:画布中图像的 URL。

The first argument, if provided, controls the type of the image to be returned ( eg PNG or JPEG).第一个参数(如果提供)控制要返回的图像的类型(例如PNG 或 JPEG)。 The default is image/png ;默认为image/png that type is also used if the given type isn't supported.如果不支持给定类型,也会使用该类型。 The other arguments are specific to the type, and control the way that the image is generated, as given in the table below.其他参数特定于类型,并控制生成图像的方式,如下表所示。

The spec contains a table as mentioned above , but it only has one entry:该规范包含一个如上所述的表格,但它只有一个条目:

Arguments for serialisation methods序列化方法的参数

Type Other arguments image/jpeg The second argument, if it is a number in the range 0.0 to 1.0 inclusive, must be treated as the desired quality level. If it is not a number or is outside that range, the user agent must use its default value, as if the argument had been omitted.

So, we can see that PNG is explicitly required as the default, and JPEG must support a quality argument if the browser chooses to support JPEG .因此,我们可以看到 PNG 明确要求作为默认值,如果浏览器选择支持 JPEG ,则 JPEG 必须支持质量参数。 In the future, I except Web standards community could add more entries to that table, in order to specify standard arguments for vendors that choose to support optional image types.将来,除了 Web 标准社区之外,我还可以向该表添加更多条目,以便为选择支持可选图像类型的供应商指定标准参数。

The spec suggests how to test for a browser's support of any particular format (but unfortunately not how to get all formats supported, eg, as a list):该规范建议了如何测试浏览器对任何特定格式的支持(但不幸的是,并没有说明如何获得所有格式的支持,例如,作为列表):

When trying to use types other than " image/png ", authors can check if the image was really returned in the requested format by checking to see if the returned string starts with one of the exact strings " data:image/png, " or " data:image/png; ".当尝试使用“ image/png ”以外的类型时,作者可以通过检查返回的字符串是否以确切的字符串“ data:image/png, ”之一开头来检查图像是否确实以请求的格式返回“ data:image/png; ”。

There is one additional note about optional browser support:还有一个关于可选浏览器支持的附加说明:

For example, the value " image/png " would mean to generate a PNG image, the value " image/jpeg " would mean to generate a JPEG image, and the value " image/svg+xml " would mean to generate an SVG image (which would require that the user agent track how the bitmap was generated, an unlikely, though potentially awesome, feature).例如,值“ image/png ”表示生成PNG图像,值“ image/jpeg ”表示生成JPEG图像,值“ image/svg+xml ”表示生成SVG图像(这将要求用户代理跟踪位图的生成方式,这是一个不太可能但可能很棒的功能)。

This clearly leaves a huge range of allowed formats, but only one required format.这显然留下了大量允许的格式,但只有一种必需的格式。

Whether a browser supports a particular image serialization format is purely up to each browser.浏览器是否支持特定的图像序列化格式完全取决于每个浏览器。

https://bugzilla.mozilla.org/show_bug.cgi?id=1559743中所述,Firefox当前不支持webp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM