Skip to content

Add opacity implementation, basic widget, and example#3400

Open
Ericky14 wants to merge 6 commits into
iced-rs:masterfrom
playtron-os:feat/opacity
Open

Add opacity implementation, basic widget, and example#3400
Ericky14 wants to merge 6 commits into
iced-rs:masterfrom
playtron-os:feat/opacity

Conversation

@Ericky14

@Ericky14 Ericky14 commented Jul 23, 2026

Copy link
Copy Markdown

Re-opening this PR with correct source branch: #3182
And fully rebased on latest master.

Features:

New Opacity widget - Wraps any content and applies transparency
Nested opacity support - Opacity values multiply when nested (e.g., 50% × 50% = 25%)
GPU-accelerated - Implemented in both wgpu and tiny-skia renderers
Affects everything - Backgrounds, borders, images, SVGs, and text all respect opacity
Example added demonstrating how simple it is to integrate and how to do fade-in/out animations.

Related issue: #912

image

@EmmanuelDodoo

Copy link
Copy Markdown
Contributor

Great work!

One thing to note, the image path in the example won't work on Windows. "/" is not the path separator so env!("CARGO_MANIFEST_DIR").replace("/examples/opacity", "") returns the wrong path.

@EmmanuelDodoo

Copy link
Copy Markdown
Contributor

Also, I shouldn't the opacity be handled by the layer stack like how transformations are?

@Ericky14

Copy link
Copy Markdown
Author

Also, I shouldn't the opacity be handled by the layer stack like how transformations are?

Hmm I did an implementation with layer stack, I liked the other impl because of how simple it was and didn't need to render to an offscreen texture, easy win for perf.
But this shouldn't be so bad either, tried to keep each opacity group bounded to a smaller rect instead of whole window, seems to be working well.

@EmmanuelDodoo

Copy link
Copy Markdown
Contributor

Yep. Overlapping opacity looks to be better. Were you able to benchmark this?

Also it might be worth creating a post on this in the discussions channel of the iced discord so others can give more feedback

@Ericky14

Copy link
Copy Markdown
Author

Yep. Overlapping opacity looks to be better. Were you able to benchmark this?

Also it might be worth creating a post on this in the discussions channel of the iced discord so others can give more feedback

I did this morning, wasn't sure if I should commit the benchmark code or not since benches folder is empty.
I added another cool bonus to this PR, with the layer stack implementation I was seeing bad perf at first, but then I implemented a way to batch and group layers and apply effects to them such as opacity, I am getting great performance now.

This is on the GPU drawing a row with cards.
At opacity 1, its no additional cost since we skip the opacity pipeline: 1000 groups at 0.51ms
At opacity 0.5:
100 0.59ms
1000 5.97ms

This is on the CPU (tiny_skia).
At opacity 1, its no additional cost since we skip the opacity pipeline: 1000 groups at 3.72ms
At opacity 0.5:
100 6.92ms

Also made that discord post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants