We first look at 渲染流水线
the process:
Next, we introduce the future as a basis to redraw and return, and let another way to update the view - synthesis.
Reflux
First introduced 回流
. 回流
Also called 重排
.
Triggering conditions
Simply put, when we initiated DOM geometry changes to modify the DOM structure will occur 回流
process.
Specific point, the following actions may trigger reflux:
-
Geometric properties of a DOM element of change, common geometric attributes
width
,height
,padding
,margin
,left
,top
,border
and so on, this is well understood. -
DOM node occurs so
增减
or移动
. -
Write
offset
family,scroll
group, andclient
when the group attribute, a browser in order to obtain these values, the need for reflow operation. -
Call
window.getComputedStyle
method.
Reflow process
In accordance with the above rendering pipeline, triggered when returning, if DOM structure changes, the re-render the DOM tree, and then later process (including tasks outside the main thread) all go again.
Equivalent to parse and process re-synthesis and went one, the cost is very large.
Redraw
Triggering conditions
When DOM changes resulted in a change in style, and does not affect the geometric properties of time, it will lead to 重绘
( repaint
).
Redraw process
Since there is no change in DOM geometric properties of lead, and therefore no need to update the location information element, thus eliminating layout process. Process is as follows:
Skipped 生成布局树
and 建图层树
the stage, directly generate a list of drawing and continued block, behind a series of operations to generate a bitmap.
It can be seen redraw not necessarily lead to reflux, but some reflux occurred redrawn.
synthesis
In another case, a direct synthesis. Such as the use of CSS3 transform
, opacity
, filter
these properties can be achieved synthesis of the results, that is, we often say that GPU acceleration .
The reason GPU acceleration
In the case of the synthesis, and may skip the layout drawing process, directly into the 非主线程
part of the process, i.e., directly to the 合成线程
process. Referred to it has two advantages:
-
We can give full play to
GPU
advantages. Synthesis process thread resulting bitmap will call the thread pool, and whereinGPU
for generating acceleration, while the GPU is good at handling bitmap data. -
Do not consume resources of the main thread, the main thread stuck even though the effect is still able to smoothly display.
Practical significance
Once you know the principle of the above, what significance does to the development process?
- Avoid frequent use of style, instead of using the modified
class
approach. - Use
createDocumentFragment
batch DOM manipulation. - For resize, scroll, etc. Stabilizer / throttling process.
- Add will-change: tranform, so that its rendering engine to achieve a single layer, when these transformations take place, just using synthetic thread to handle these transformations, and not involve the main thread, greatly improve the rendering efficiency. Of course, this change is not limited to
tranform
, any synthetic effects can be achieved with CSS properties can bewill-change
declared. Here's a practical example, a linewill-change: tranform
to save a project, click Direct .