-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathOverview.bs
357 lines (283 loc) · 16.4 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<pre class='metadata'>
Title: CSS Will Change Module Level 1
Shortname: css-will-change
Level: 1
Status: ED
Prepare for TR: no
Work Status: Testing
Group: CSSWG
ED: https://drafts.csswg.org/css-will-change/
TR: https://www.w3.org/TR/css-will-change/
Previous Version: https://www.w3.org/TR/2022/CRD-css-will-change-1-20220505/
Previous Version: https://www.w3.org/TR/2015/CR-css-will-change-1-20151203/
Previous Version: https://www.w3.org/TR/2014/WD-css-will-change-1-20140429/
Test Suite: https://wpt.fyi/results/css/css-will-change
Implementation Report: https://wpt.fyi/results/css/css-will-change
Abstract: This document defines the 'will-change' CSS property, which allows an author to inform the UA ahead of time of what kinds of changes they are likely to make to an element. This allows the UA to optimize how they handle the element ahead of time, performing potentially-expensive work preparing for an animation before the animation actually begins.
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
Link Defaults: css-transforms-1 (property) transform
</pre>
<h2 id='intro'>
Introduction</h2>
Modern CSS renderers perform a number of complex optimizations in order to render webpages quickly and efficiently.
Unfortunately, employing these optimizations often has a non-trivial start-up cost,
which can have a negative impact on the responsiveness of a page.
<div class='example'>
For example, when using CSS 3D Transforms to move an element around the screen,
the element and its contents might be promoted to a “layer”,
where they can render independently from the rest of the page and be composited in later.
This isolates the rendering of the content so that the rest of the page doesn't have to be rerendered
if the element’s transform is the only thing that changes between frames,
and often provides significant speed benefits.
However, setting up the element in a fresh layer is a relatively expensive operation,
which can delay the start of a 'transform' animation by a noticeable fraction of a second.
</div>
The 'will-change' property defined in this specification allows an author to declare ahead-of-time what properties are likely to change in the future,
so the UA can set up the appropriate optimizations some time before they're needed.
This way, when the actual change happens,
the page updates in a snappy manner.
<h3 id='values'>
Value Definitions</h3>
This specification follows the <a href="https://www.w3.org/TR/CSS2/about.html#property-defs">CSS property definition conventions</a> from [[!CSS2]]
using the <a href="https://www.w3.org/TR/css-values-3/#value-defs">value definition syntax</a> from [[!CSS-VALUES-3]].
Value types not defined in this specification are defined in CSS Values & Units [[!CSS-VALUES-3]].
Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions,
all properties defined in this specification
also accept the <a>CSS-wide keywords</a> as their property value.
For readability they have not been repeated explicitly.
<h3 id='using'>
Using 'will-change' Well</h3>
The 'will-change' property,
like all performance hints,
can be somewhat difficult to learn how to use “properly”,
particularly since it has very little, if any, effect an author can directly detect.
However, there are several simple “Dos and Don'ts”
which hopefully will help develop a good intuition about how to use 'will-change' well.
<h4 id='dont-global' class='no-num no-toc'>
Don't Spam 'will-change' Across Too Many Properties or Elements</h4>
A common initial response to seeing 'will-change' is to assume that code like this is a good idea:
<pre>* { will-change: transform, opacity /* , ... */; }</pre>
After all, this tells the browser to go ahead and optimize everything,
which has to be good right?
Wrong. The browser <em>already</em> tries as hard as it can to optimize everything.
Telling it to do so explicitly doesn't help anything,
and in fact has the capacity to do a lot of harm;
some of the stronger optimizations that are likely to be tied to 'will-change'
end up using a lot of a machine's resources,
and when overused like this can cause the page to slow down or even crash.
In addition, 'will-change' does have <strong>some</strong> side-effects,
and it's very unlikely that pages actually want all those side-effects on every element.
<h4 id='css-sparingly' class='no-num no-toc'>
Use 'will-change' Sparingly In Stylesheets</h4>
Using 'will-change' directly in a stylesheet
implies that the targeted elements are always a few moments away from changing.
This is <em>usually</em> not what you actually mean;
instead, 'will-change' should usually be flipped on and off via scripting
before and after the change occurs (see <a section href="#dont-waste"></a>).
However, there are some common circumstances in which it is appropriate to use 'will-change' directly in a stylesheet.
<div class='example'>
For example,
specifying 'will-change' for a small number of persistent UI elements in a page
which should react snappily to the user
is appropriate:
<pre>
body > .sidebar {
will-change: transform;
/* Will use 'transform' to slide it out
when the user requests. */
}
</pre>
Because this is limited to a small number of elements,
the fact that the optimization is rarely actually used
doesn't hurt very much.
</div>
<div class='example'>
Sometimes an element really <em>does</em> change a property nearly constantly.
Perhaps it responds to the user's mouse movements,
or just regularly takes some action that causes an animation.
In this case, just declaring the 'will-change' value in the stylesheet is fine,
as it accurately describes that the element will regularly/constantly change,
and so should be kept optimized.
<pre>
.cats-flying-around-the-screen {
will-change: left, top;
}
</pre>
</div>
<h4 id='give-time' class='no-num no-toc'>
Give 'will-change' Sufficient Time To Work</h4>
Another common bad pattern is to apply 'will-change' to an element
<em>immediately</em> before starting the animation or property change that it's meant to help with.
Unfortunately, most of those optimizations need time to be applied,
and so they don't have enough time to set-up when this is done,
and the 'will-change' has little to no effect.
Instead, find some way to predict at least slightly ahead of time that something will change,
and set 'will-change' <em>then</em>.
<div class='example'>
For example,
if an element is going to change when a user clicks on it,
setting 'will-change' on hover will usually give at least 200 milliseconds
for the optimizations to be set up,
as human reaction time is relatively slow.
This can be done either via scripting,
or rather simply with a CSS rule:
<pre>
.element { transition: opacity .2s; opacity: 1; }
.element:hover { will-change: opacity; }
.element:active { opacity: .3; }
</pre>
However, a rule like that is useless if the effect is going to happen on hover.
In cases like these, it is often still possible to find some way to predict the action before it occurs.
For example, hovering an ancestor may give enough lead time:
<pre>
.element { transition: opacity .2s; opacity: 1; }
.container:hover > .element { will-change: opacity; }
.element:hover { opacity: .3; }
</pre>
</div>
<h4 id='dont-waste' class='no-num no-toc'>
Don't Waste Resources On Elements That Have Stopped Changing</h4>
Because the optimizations browsers use for changing some properties are expensive,
browsers remove them and revert to normal behavior as soon as they can in normal circumstances.
However, 'will-change' will generally override this behavior,
maintaining the optimizations for much longer than the browser would otherwise do.
As such, whenever you add 'will-change' to an element,
especially via scripting,
don't forget to <em>remove</em> it after the element is done changing,
so the browser can recover whatever resources the optimizations are claiming.
<h2 id='will-change'>
Hinting at Future Behavior: the 'will-change' property</h2>
<pre class='propdef'>
Name: will-change
Value: auto | <<animateable-feature>>#
Initial: auto
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: specified value
Animation type: not animatable
</pre>
<pre class='prod'><dfn><animateable-feature></dfn> = scroll-position | contents | <<custom-ident>></pre>
The 'will-change' property provides a rendering hint to the user agent,
stating what kinds of changes the author expects to perform on the element.
This allows the user agent to perform ahead-of-time any optimizations necessary for rendering those changes smoothly,
avoiding “jank” when the author does begin changing or animating that feature.
<div class='note'>
Different browsers can use the information from 'will-change' in different ways,
and even a single browser might use it in different ways at different time.
For example, a browser that promotes elements to their own “GPU layer”
when they have ''will-change: transform'' specified
might avoid doing that when there are <em>too many</em> elements declaring that,
to avoid exhausting GPU memory.
</div>
Values have the following meanings:
<dl dfn-for="will-change" dfn-type="value">
<dt><dfn>auto</dfn>
<dd>
Expresses no particular intent;
the user agent should apply whatever heuristics and optimizations it normally does.
<dt><dfn>scroll-position</dfn>
<dd>
Indicates that the author expects to animate or change the scroll position of the element in the near future.
<p class='example'>
For example, browsers often only render the content in the "scroll window" on a scrollable element,
and some of the content past that window,
balancing memory and time savings from the skipped rendering against making scrolling look nice.
A browser might take this value as a signal to expand the range of content around the scroll window that is rendered,
so that longer/faster scrolls can be done smoothly.
<dt><dfn>contents</dfn>
<dd>
Indicates that the author expects to animate or change something about the element's contents in the near future.
<div class='example'>
For example, browsers often “cache” rendering of elements over time,
because most things don't change very often,
or only change their position.
However, if an element <em>does</em> change its contents continually,
producing and maintaining this cache is a waste of time.
A browser might take this value as a signal to cache less aggressively on the element,
or avoid caching at all and just continually re-render the element from scratch.
This value is mostly intended to help browsers optimize JS-based animations of content,
which change aspects of an element's contents many times per second.
This kind of optimization, when possible,
is already done automatically by browsers when declarative animations are used.
</div>
Note: This value more-or-less applies to the entire subtree of the element its declared on,
as it indicates the browser should count on *any* of the descendants changing in some way.
Using this on an element “high up” in your document might be very bad for your page's performance;
try to only use this on elements near the “bottom” of your document tree,
containing as little of the document as possible.
<dt><dfn><<custom-ident>></dfn>
<dd>
If the <<custom-ident>> is an [=ASCII case-insensitive=] match for the name of a built-in CSS property,
it indicates that the author expects to animate or change the property with the given name on the element in the near future.
If the property given is a shorthand,
it indicates the expectation for all the longhands the shorthand expands to.
<p class='example'>
For example, setting ''will-change: background;''
is identical to setting ''will-change: background-image, background-position, ...''
for all the properties that 'background' expands into.
The <<custom-ident>> production used here excludes the keywords <css>will-change</css>, <css>none</css>, <css>all</css>, ''auto'', ''scroll-position'', and ''contents'',
in addition to the keywords normally excluded from <<custom-ident>>.
Note: Note that most properties will have no effect when specified,
as the user agent doesn't perform any special optimizations for changes in most properties.
It is still <em>safe</em> to specify them, though;
it'll simply have no effect.
Specifying a custom property must have no effect,
which means that effects that happen through custom properties
do not count for the rules below that are conditioned on any non-initial value of a property causing something.
Note: Specifying a value that's not recognized as a property is fine;
it simply has no effect.
This allows you to safely specify <em>new</em> properties that exist in some user agents
without negatively affecting down-level user agents
that don't know about that property.
<p class='example'>
For example, browsers often handle elements with 'transform' set to a non-initial value very differently from normal elements,
perhaps rendering them to their own “GPU layer”
or using other mechanisms to make it easier to quickly make the sort of transformations that 'transform' can produce.
A browser might take a value of 'transform' as a signal
that it should go ahead and promote the element to its own layer immediately,
before the element starts to be transformed,
to avoid any delay involved in rerendering the old and new layers.
If any non-initial value of a property would create a stacking context on the element,
specifying that property in 'will-change' must create a stacking context on the element.
If any non-initial value of a property would cause the element to generate a containing block for absolutely positioned elements,
specifying that property in 'will-change' must cause the element to generate a containing block for absolutely positioned elements.
If any non-initial value of a property would cause the element to generate a containing block for fixed positioned elements,
specifying that property in 'will-change' must cause the element to generate a containing block for fixed positioned elements.
If any non-initial value of a property would cause rendering differences on the element
(such as using a different anti-aliasing strategy for text),
the user agent should use that alternate rendering when the property is specified in 'will-change',
to avoid sudden rendering differences when the property is eventually changed.
<p class='example'>
For example, setting 'opacity' to any value other than ''1'' creates a stacking context on the element.
Thus, setting ''will-change: opacity'' also creates a stacking context,
even if 'opacity' is <em>currently</em> still equal to ''1''.
</dl>
The 'will-change' property has no <em>direct</em> effect on the element it is specified on,
beyond the creation of stacking contexts and containing blocks as specified above.
It is solely a rendering hint to the user agent,
allowing it set up potentially-expensive optimizations for certain types of changes
before the changes actually start occurring.
Security Considerations {#security}
===================================
No Security concerns have been raised against this document
Privacy Considerations {#privacy}
===================================
No Privacy concerns have been raised against this document
Acknowledgements {#acks}
========================
Thanks to Benoit Girard for originally suggesting the <css>will-animate</css> property,
and doing a lot of the initial design work.
Changes {#changes}
========================================================
Since the <a href="http://www.w3.org/TR/2015/CR-css-will-change-1-20151203/">03 December 2015 CR</a>:
* Added Security and Privacy sections
* Clarified that unknown values are fine, and have no effect
* Specified that ASCII Case-Insensitive matching is used against property names
* Changed the animation type of the will-change property to not animatable
* Dropped the "Media:" entry from propdef tables, as with other CSS specifications
* Minor editorial clarifications, markup improvements
Since the <a href="https://www.w3.org/TR/2014/WD-css-will-change-1-20140429/">April 29 2014 Working Draft</a>:
* Added an explanatory section giving guidance on how to use 'will-change' well.
* Specified the behavior of shorthands