Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GradientBrush in CompositionCustomVisualHandler results in wrong rendering #10972

Closed
llfab opened this issue Apr 10, 2023 · 4 comments · Fixed by #14598
Closed

GradientBrush in CompositionCustomVisualHandler results in wrong rendering #10972

llfab opened this issue Apr 10, 2023 · 4 comments · Fixed by #14598
Labels

Comments

@llfab
Copy link

llfab commented Apr 10, 2023

Describe the bug
I created a composition animation using a CompositionCustomVisualHandler like in ControlCatalog. I basically draw a rectangle with a gradient brush. When I use an X-offset in DrawRectangle the gradient always stays static in the the original / wrong position. Only when I use a push transform and then draw the rectangle without offset, the visualization is correct.

To Reproduce
Steps to reproduce the behavior:

  1. Load the sample app in VS: https://github.com/llfab/Samples/tree/main/AvaloniaAnimationTest
  2. Run like it is => correct
  3. Comment / uncomment the drawing commands => incorrect

Expected behavior
I would expect the gradient brush to "move" with the rectangle and render correctly in both ways of the sample app.

Videos
Correct:
https://user-images.githubusercontent.com/31031996/230961029-061456fd-c544-4fb8-9eac-81c7a7c2ff24.mp4

Incorrect:
https://user-images.githubusercontent.com/31031996/230960984-1b1b7309-70d0-4a97-979c-34b8deb4d9a3.mp4

Desktop (please complete the following information):

  • OS: Windows 10
  • Version: 11.0.0-preview6

Additional context
Please find this portion of code in the sample app:

            public override void OnRender(ImmediateDrawingContext drawingContext)
            {
                if (_running)
                {
                    if (_lastServerTime.HasValue)
                    {
                        _animationElapsed += (CompositionNow.TotalMilliseconds - _lastServerTime.Value);
                        _animationElapsed %= _animationLength;
                    }
                    
                    _lastServerTime = CompositionNow.TotalMilliseconds;
                }

                double borderWidth = EffectiveSize.X;
                double borderHeight = EffectiveSize.Y;
                double indicatorWidth = 256;
                double animationStage = _animationElapsed / _animationLength;

                double offsetX = (borderWidth + indicatorWidth) * animationStage - indicatorWidth;

                // Gradient brush only works using push transform
                // if used without push transform and Rect with
                // offset instead, the rendering is wrong. The gradient seems
                // to be stuck at the original position.
                using (drawingContext.PushPreTransform(Matrix.CreateTranslation(offsetX, 0)))
                {
                    drawingContext.DrawRectangle(_brush, null, new Rect(0, 0, indicatorWidth, borderHeight));
                }

                // uncomment below to get the wrong rendering
                //drawingContext.DrawRectangle(_brush, null, new Rect(offsetX, 0, indicatorWidth, borderHeight));
            }
@llfab llfab added the bug label Apr 10, 2023
@kekekeks
Copy link
Member

Working as intended, brush coordinates are relative to the current (0,0), not to the rectangle origin.

@kekekeks
Copy link
Member

See also: https://skia.org/docs/user/coordinates/

@llfab
Copy link
Author

llfab commented Apr 11, 2023

Ok. I understand. So, in the upper sample, where I used 0 and 1 as relative coordinates for the brush, would push transform be the way to go or would you rather create the brush with absolute coordinates including the rect x-offset?

@kekekeks
Copy link
Member

Actually, I think it should depend on the RelativeUnit passed to the gradient stop.

@grokys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants