You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: css-paint-api/EXPLAINER.md
+80-11
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,37 @@ This means that the memory and cpu usage of the page would go down, the renderin
21
21
have to keep in memory the additional DOM nodes, in addition to performing style-recalc, layout,
22
22
painting for all these additional divs.
23
23
24
+
### Efficiency Gains ###
25
+
26
+
Providing a "hook" into the rendering engine allows for efficiency gains which are difficult to
27
+
achieve with current APIs.
28
+
29
+
#### Invalidation ####
30
+
31
+
As the CSS paint API Invalidation is based off style changes, this check can occur in the same pass
32
+
as the rest of the box tree. For example:
33
+
34
+
```css
35
+
my-button {
36
+
--property-which-invalidates-paint: no-hover;
37
+
}
38
+
39
+
my-button:hover {
40
+
--property-which-invalidates-paint: hover;
41
+
}
42
+
```
43
+
44
+
To achieve the same effect with current APIs you have to rebuild the engines invalidation logic
45
+
which is potentially less efficient.
46
+
47
+
#### Painting ####
48
+
49
+
Once a box has been invalidated, a rendering engine isn't required to paint it that frame. For
50
+
example some rendering engines just paint what is visible within the "visual viewport". This means
51
+
that only a smaller amount of work is needed to be done.
52
+
53
+
A naive implementation with existing APIs may try and paint everything within the document.
54
+
24
55
### Extensibility of CSS ###
25
56
26
57
We believe that allowing developers to extend CSS is good for the ecosystem. As an example if a
@@ -87,11 +118,15 @@ The `paint` function is your callback into the browsers paint phase in the rende
87
118
- `size`, the size of the area in which you should paint.
88
119
- `style`, the computed style of the element which are you currently painting.
89
120
90
-
The `style` is a Typed-OM style map. It will _only_ contain the CSS properties that you listed under the static `inputProperties` accessor.
121
+
The `style` is a Typed-OM style map. It will _only_ contain the CSS properties that you listed under
122
+
the static `inputProperties` accessor.
91
123
92
-
This is to allow the engine to cache results of your paint class. For example if `--some-other-property` changes the user-agent knows that this doesn't affect your paint class, and can re-use the stored result.
124
+
This is to allow the engine to cache results of your paint class. For example if
125
+
`--some-other-property` changes the user-agent knows that this doesn't affect your paint class, and
126
+
can re-use the stored result.
93
127
94
-
The only time when your paint class _must_ be called is when the element it is painting into is within the viewport, and the size or CSS input properties have changed.
128
+
The only time when your paint class _must_ be called is when the element it is painting into is
129
+
within the viewport, and the size or CSS input properties have changed.
95
130
96
131
Why classes?
97
132
------------
@@ -127,7 +162,8 @@ class BorderRectPainter extends RectPainter {
127
162
registerPaint('border-rect', BorderRectPainter);
128
163
```
129
164
130
-
Classes also give the developer a specific point in time to perform pre-initialization work. As an example:
165
+
Classes also give the developer a specific point in time to perform pre-initialization work. As an
166
+
example:
131
167
132
168
```js
133
169
registerPaint('lots-of-paths', class {
@@ -146,14 +182,18 @@ registerPaint('lots-of-paths', class {
146
182
});
147
183
```
148
184
149
-
In this example `performPathPreInit` is doing CPU intensive work which should only be done once. Without classes this would typically be done when the script is first run, instead this is performed when the class instance is first created (which may be much later in time).
185
+
In this example `performPathPreInit` is doing CPU intensive work which should only be done once.
186
+
Without classes this would typically be done when the script is first run, instead this is performed
187
+
when the class instance is first created (which may be much later in time).
150
188
151
189
Drawing Images
152
190
--------------
153
191
154
-
The API works in conjunction with the [CSS Properties and Values](https://drafts.css-houdini.org/css-properties-values-api/) proposal and the [CSS Typed OM](https://drafts.css-houdini.org/css-typed-om/) proposal.
192
+
The API works in conjunction with the [CSS Properties and Values](https://drafts.css-houdini.org/css-properties-values-api/)
193
+
proposal and the [CSS Typed OM](https://drafts.css-houdini.org/css-typed-om/) proposal.
155
194
156
-
Using the Properties and Values `registerProperty` method, developers can create a custom CSS property with the `<image>` type. E.g.
195
+
Using the Properties and Values `registerProperty` method, developers can create a custom CSS
196
+
property with the `<image>` type. E.g.
157
197
158
198
```js
159
199
registerProperty({
@@ -162,7 +202,8 @@ registerProperty({
162
202
});
163
203
```
164
204
165
-
This tells the rendering engine to treat the CSS property `--profile-image` as an image; and as a result the style engine will parse and begin loading that image.
205
+
This tells the rendering engine to treat the CSS property `--profile-image` as an image; and as a
206
+
result the style engine will parse and begin loading that image.
166
207
167
208
You can then directly draw this image from within your paint method:
168
209
@@ -200,7 +241,35 @@ The above example would be used in CSS by:
200
241
}
201
242
```
202
243
203
-
Paint Efficiency
204
-
----------------
244
+
Paint Arguments
245
+
---------------
246
+
247
+
It is also possible with this API to have additional arguments to the `paint()` function, for
0 commit comments