Skip to content

Commit a5853ca

Browse files
Add CanvasColorType, with unorm8 and float16 support
The default behavior is unorm8, which matches the existing behavior of all user agents. The name unorm8 is chosen to match modern APIs which distinguish between 8-bit unsigned normalized and 8-bit unsigned integer (e.g, VK_FORMAT_R8G8B8A8_UNORM vs VK_FORMAT_R8G8B8A8_UINT in Vulkan, MTLPixelFormatRGBA8Unorm vs MTLPixelFormatRGBA8Uint in Metal, and "rgba8unorm" vs "rgba8uint" in WebGPU). The use of colorType (instead of pixelFormat) is used to avoid the complexity of selecting channel ordering (BGRA vs RGBA) and interactions with the alpha parameter (RGB, RGBX, or BGRX potentially being required if alpha is false).
1 parent 972b0c4 commit a5853ca

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

source

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65378,12 +65378,15 @@ typedef (<span>HTMLOrSVGImageElement</span> or
6537865378

6537965379
enum <dfn enum>PredefinedColorSpace</dfn> { "<span data-x="dom-PredefinedColorSpace-srgb">srgb</span>", "<span data-x="dom-PredefinedColorSpace-display-p3">display-p3</span>" };
6538065380

65381+
enum <dfn enum>CanvasColorType</dfn> { "<span data-x="dom-CanvasColorType-unorm8">unorm8</span>", "<span data-x="dom-CanvasColorType-float16">float16</span>" };
65382+
6538165383
enum <dfn enum>CanvasFillRule</dfn> { "<span data-x="dom-context-2d-fillRule-nonzero">nonzero</span>", "<span data-x="dom-context-2d-fillRule-evenodd">evenodd</span>" };
6538265384

6538365385
dictionary <dfn dictionary>CanvasRenderingContext2DSettings</dfn> {
6538465386
boolean <span data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</span> = true;
6538565387
boolean <span data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</span> = false;
6538665388
<span>PredefinedColorSpace</span> <span data-x="dom-CanvasRenderingContext2DSettings-colorSpace">colorSpace</span> = "<span data-x="dom-PredefinedColorSpace-srgb">srgb</span>";
65389+
<span>CanvasColorType</span> <span data-x="dom-CanvasRenderingContext2DSettings-colorType">colorType</span> = "<span data-x="dom-CanvasColorType-unorm8">unorm8</span>";
6538765390
boolean <span data-x="dom-CanvasRenderingContext2DSettings-willReadFrequently">willReadFrequently</span> = false;
6538865391
};
6538965392

@@ -65675,6 +65678,10 @@ interface <dfn interface>Path2D</dfn> {
6567565678
specifies the <span data-x="concept-canvas-color-space">color space</span> of the rendering
6567665679
context.</p>
6567765680

65681+
<p>The <code data-x="dom-CanvasRenderingContext2DSettings-colorType">colorType</code> member
65682+
specifies the <span data-x="concept-canvas-color-type">color type</span> of the rendering
65683+
context.</p>
65684+
6567865685
<p>If the <code
6567965686
data-x="dom-CanvasRenderingContext2DSettings-willReadFrequently">willReadFrequently</code>
6568065687
member is true, then the context is marked for <span
@@ -65699,6 +65706,10 @@ interface <dfn interface>Path2D</dfn> {
6569965706
a string indicating the context's <span data-x="concept-canvas-color-space">color
6570065707
space</span>.</li>
6570165708

65709+
<li><code data-x="dom-CanvasRenderingContext2DSettings-colorType">colorType</code> member is
65710+
a string indicating the context's <span data-x="concept-canvas-color-type">color
65711+
type</span>.</li>
65712+
6570265713
<li><code data-x="dom-CanvasRenderingContext2DSettings-willReadFrequently">willReadFrequently</code>
6570365714
member is true if the context is marked for <span data-x="concept-canvas-will-read-frequently">readback
6570465715
optimization</span>.</li>
@@ -65832,6 +65843,19 @@ context.fillRect(100,0,50,50); // only this square remains</code></pre>
6583265843

6583365844
<hr>
6583465845

65846+
<p>The <code>CanvasColorType</code> enumeration is used to specify the <span
65847+
data-x="concept-canvas-color-type">color type</span> of the canvas's backing store.</p>
65848+
65849+
<p>The "<dfn enum-value for="CanvasColorType"><code
65850+
data-x="dom-CanvasColorType-unorm8">unorm8</code></dfn>" value indicates that the type
65851+
for all color channels is 8-bit unsigned normalized.</p>
65852+
65853+
<p>The "<dfn enum-value for="CanvasColorType"><code
65854+
data-x="dom-CanvasColorType-float16">float16</code></dfn>" value indicates that the type
65855+
for all color channels is 16-bit floating point.</p>
65856+
65857+
<hr>
65858+
6583565859
<p>The <code>CanvasFillRule</code> enumeration is used to select the <dfn>fill rule</dfn>
6583665860
algorithm by which to determine if a point is inside or outside a path.</p>
6583765861

@@ -65974,6 +65998,12 @@ context.fillRect(100,0,50,50); // only this square remains</code></pre>
6597465998
data-x="concept-canvas-color-space">color space</span> indicates the color space for the
6597565999
<span>output bitmap</span>.</p>
6597666000

66001+
<p>The <code>CanvasSettings</code> object also has a <dfn
66002+
data-x="concept-canvas-color-type">color type</dfn> setting of type
66003+
<code>CanvasColorType</code>. The <code>CanvasSettings</code> object's <span
66004+
data-x="concept-canvas-color-type">color type</span> indicates the data type of the
66005+
color and alpha components of the pixels of the <span>output bitmap</span>.</p>
66006+
6597766007
<p>To <dfn data-x="canvas-setting-init-bitmap">initialize a <code>CanvasSettings</code> output
6597866008
bitmap</dfn>, given a <code>CanvasSettings</code> <var>context</var> and a
6597966009
<code>CanvasRenderingContext2DSettings</code> <var>settings</var>:</p>
@@ -65992,6 +66022,10 @@ context.fillRect(100,0,50,50); // only this square remains</code></pre>
6599266022
<var>settings</var>["<dfn dict-member for="CanvasRenderingContext2DSettings"><code
6599366023
data-x="dom-CanvasRenderingContext2DSettings-colorSpace">colorSpace</code></dfn>"].</p></li>
6599466024

66025+
<li><p>Set <var>context</var>'s <span data-x="concept-canvas-color-type">color type</span> to
66026+
<var>settings</var>["<dfn dict-member for="CanvasRenderingContext2DSettings"><code
66027+
data-x="dom-CanvasRenderingContext2DSettings-colorType">colorType</code></dfn>"].</p></li>
66028+
6599566029
<li><p>Set <var>context</var>'s <span data-x="concept-canvas-will-read-frequently">will read
6599666030
frequently</span> to <var>settings</var>["<dfn dict-member
6599766031
for="CanvasRenderingContext2DSettings"><code
@@ -66006,6 +66040,8 @@ context.fillRect(100,0,50,50); // only this square remains</code></pre>
6600666040
<span>this</span>'s <span data-x="concept-canvas-desynchronized">desynchronized</span>, "<code
6600766041
data-x="dom-CanvasRenderingContext2DSettings-colorSpace">colorSpace</code>" → <span>this</span>'s
6600866042
<span data-x="concept-canvas-color-space">color space</span>, "<code
66043+
data-x="dom-CanvasRenderingContext2DSettings-colorType">colorType</code>" → <span>this</span>'s
66044+
<span data-x="concept-canvas-color-type">color type</span>, "<code
6600966045
data-x="dom-CanvasRenderingContext2DSettings-willReadFrequently">willReadFrequently</code>" →
6601066046
<span>this</span>'s <span data-x="concept-canvas-will-read-frequently">will read frequently</span>
6601166047
]».</p>
@@ -71844,6 +71880,16 @@ interface <dfn interface>OffscreenCanvasRenderingContext2D</dfn> {
7184471880
data-x="dom-canvas-toBlob">toBlob()</code> method to the canvas, given the appropriate dimensions,
7184571881
has no visible effect beyond, at most, clipping colors of the canvas to a more narrow gamut.</p>
7184671882

71883+
<p>For image types that support multiple bit depths, the serialized image must use the bit depth
71884+
that best preserves content of the underlying bitmap.</p>
71885+
71886+
<p class="example">For example, when serializing a 2D context that has
71887+
<span data-x="concept-canvas-color-type">color type</span> of
71888+
<span data-x="dom-CanvasColorType-float16">float16</span> to <var>type</var>
71889+
"<code>image/png</code>", the resulting image would have 16 bits per sample.
71890+
This serialization will still lose significant detail (all values less than 0.5/65535
71891+
would be clamped to 0, and all values greater than 1 would be clamped to 1).</p>
71892+
7184771893
<p>If <var>type</var> is an image format that supports variable quality (such as
7184871894
"<code>image/jpeg</code>"), <var>quality</var> is given, and <var>type</var> is not
7184971895
"<code>image/png</code>", then, if <var>quality</var> <span data-x="js-Number">is a Number</span>
@@ -71983,9 +72029,9 @@ interface <dfn interface>OffscreenCanvasRenderingContext2D</dfn> {
7198372029
<p>As certain colors can only be represented under premultiplied alpha (for instance, additive
7198472030
colors), and others can only be represented under non-premultiplied alpha (for instance,
7198572031
"invisible" colors which hold certain red, green, and blue values even with no opacity); and
71986-
division and multiplication on 8-bit integers (which is how canvas's colors are currently stored)
71987-
entails a loss of precision, converting between premultiplied and non-premultiplied alpha is a
71988-
lossy operation on colors that are not fully opaque.</p>
72032+
division and multiplication using finite precision entails a loss of accuracy, converting between
72033+
premultiplied and non-premultiplied alpha is a lossy operation on colors that are not fully
72034+
opaque.</p>
7198972035

7199072036
<p>A <code>CanvasRenderingContext2D</code>'s <span>output bitmap</span> and an
7199172037
<code>OffscreenCanvasRenderingContext2D</code>'s <span>output bitmap</span> must use premultiplied

0 commit comments

Comments
 (0)