WPF:像 CSS 中那样使用三次贝塞尔曲线的缓动函数

发布于 2025-01-17 19:21:18 字数 1458 浏览 3 评论 0 原文

我的目标是通过WPF中的动画复制这款鼠标:

目前我在代码中有类似的东西:

var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(280)))
{
    FillBehavior = FillBehavior.HoldEnd,
    EasingFunction = new CubicEase()
};

但是结果并不完美。

是否可以像CSS一样使用Cutic Bezier曲线创建自定义缓解?

transition: box-shadow 280ms cubic-bezier(0.4, 0, 0.2, 1)

我看到我们可以通过继承来创建自定义宽松功能 easingFunctionBase

我需要指定曲线上的点坐标以获得相同的结果。
https://cubic-bezier.com/#.4,0,0,。 2,1

我可以创建一个自定义的轻松函数:

internal class MaterialEasingFunction : EasingFunctionBase
    {
        public MaterialEasingFunction()
            : base()
        {
            EasingMode = EasingMode.EaseInOut;
        }

        protected override double EaseInCore(double normalizedTime) => Math.Pow(normalizedTime, 3);

        protected override Freezable CreateInstanceCore() => new MaterialEasingFunction();

    }

但是我不知道如何指定曲线上点的坐标。

您知道我该怎么做吗?

My goal is to reproduce this mouse over animation in WPF :
https://xaalek.w3spaces.com/saved-from-Tryit-2022-03-21-n3xl2.html?bypass-cache=74310593

Currently I have something like this in my code :

var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(280)))
{
    FillBehavior = FillBehavior.HoldEnd,
    EasingFunction = new CubicEase()
};

But the result is not perfect.
Is it possible to create a custom easing with a Cubic Bezier Curve like in CSS ?

transition: box-shadow 280ms cubic-bezier(0.4, 0, 0.2, 1)

I saw that we can create custom Easing Function by inheriting
EasingFunctionBase

I need to specify the coordinates of points on the curve to get the same result.
https://cubic-bezier.com/#.4,0,.2,1

I could create a Custom Easing Function like this :

internal class MaterialEasingFunction : EasingFunctionBase
    {
        public MaterialEasingFunction()
            : base()
        {
            EasingMode = EasingMode.EaseInOut;
        }

        protected override double EaseInCore(double normalizedTime) => Math.Pow(normalizedTime, 3);

        protected override Freezable CreateInstanceCore() => new MaterialEasingFunction();

    }

But I don't know how I can specify the coordinates of the points on the curve.

Do you have any idea how I could do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-24 19:21:18

我们可以使用 AccelerationRatio 减速> doublemeyimation
https://lealen.microsoft.com/fr-fr/dotnet/desktop/wpf/wpf/graphics-multimedia/how-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-te-t-te-cel---------------------------------- /a>

这是Cupic Bezier的CSS语法:

cubic-bezier(x1,y1,x2,y2)

在这种情况下,我们仅使用 x1 x2
由于坐标从0到1,我们可以将点的X配音用作加速度和减速比率,

这是最终结果:

var doubleAnimation = new DoubleAnimation()
{
    To = 1,
    Duration = new Duration(TimeSpan.FromMilliseconds(280)),
    FillBehavior = FillBehavior.HoldEnd,
    EasingFunction = new CubicEase(),
    AccelerationRatio = 0.4,
    DecelerationRatio = 0.2
};

We can use AccelerationRatio and DecelerationRatio properties on a DoubleAnimation :
https://learn.microsoft.com/fr-fr/dotnet/desktop/wpf/graphics-multimedia/how-to-accelerate-or-decelerate-an-animation?view=netframeworkdesktop-4.8

This is the CSS syntax for cubic bezier :

cubic-bezier(x1,y1,x2,y2)

In this case, we just using x1 and x2.
Since the coordinates go from 0 to 1, we can use the x-coordinates of the points as acceleration and deceleration ratios

This is the final result :

var doubleAnimation = new DoubleAnimation()
{
    To = 1,
    Duration = new Duration(TimeSpan.FromMilliseconds(280)),
    FillBehavior = FillBehavior.HoldEnd,
    EasingFunction = new CubicEase(),
    AccelerationRatio = 0.4,
    DecelerationRatio = 0.2
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文