Skip to content

Commit bb7462d

Browse files
Merge pull request #584 from elsassph/feat/alpha-hidden-optimisation
Feature: don't render very transparent elements
2 parents ae50dde + c21fd6a commit bb7462d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

docs/RenderEngine/Elements/Rendering.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ You can set the visibility of an element in the following ways:
3333
* Using the `visible` property. If the value of this property is set to 'false', the element is not rendered (which saves performance). If an element is invisible, the off-screen elements are invisible as well, so you do not have to hide those manually to maintain a good performance.
3434
* Using the `alpha` property, which defines the opacity of an element and its descendants. If the value of this property is set to 0 (zero), the element is not rendered.
3535

36+
### Alpha-based Performance Optimization
37+
38+
Lightning automatically optimizes rendering performance by skipping elements with very low alpha values. Elements with an effective alpha of `0.001` or less are considered invisible and are automatically excluded from:
39+
40+
* Rendering operations
41+
* Mouse hit testing and interaction detection
42+
43+
This optimization helps improve performance by avoiding unnecessary rendering work for elements that are effectively invisible to users, while still allowing for texture loading and smooth fade-in/fade-out animations.
44+
45+
Note: actual alpha test is `< 0.002` to avoid floating point errors when animating to `0.001`.
46+
47+
3648
## Color
3749

3850

src/tree/core/ElementCore.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import FlexTarget from "../../flex/FlexTarget.mjs";
2121

22+
const MIN_ALPHA_RENDER = 0.002;
23+
2224
export default class ElementCore {
2325

2426
constructor(element) {
@@ -1821,7 +1823,7 @@ export default class ElementCore {
18211823
this.sortZIndexedChildren();
18221824
}
18231825

1824-
if (this._outOfBounds < 2 && this._renderContext.alpha) {
1826+
if (this._outOfBounds < 2 && this._renderContext.alpha >= MIN_ALPHA_RENDER) {
18251827
let renderState = this.renderState;
18261828

18271829
if ((this._outOfBounds === 0) && this._displayedTextureSource) {
@@ -1861,7 +1863,7 @@ export default class ElementCore {
18611863
this.sortZIndexedChildren();
18621864
}
18631865

1864-
if (this._outOfBounds < 2 && this._renderContext.alpha) {
1866+
if (this._outOfBounds < 2 && this._renderContext.alpha >= MIN_ALPHA_RENDER) {
18651867
let renderState = this.renderState;
18661868

18671869
let mustRenderChildren = true;
@@ -2197,7 +2199,7 @@ export default class ElementCore {
21972199

21982200
collectAtCoord(x, y, children) {
21992201
// return when branch is hidden
2200-
if (this._renderContext.alpha === 0) {
2202+
if (this._renderContext.alpha < MIN_ALPHA_RENDER) {
22012203
return;
22022204
}
22032205

0 commit comments

Comments
 (0)