Windows的API里有B样条曲线的函数吗?顺便谈谈WPF的动画技术

jameshooo jameshooo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我希望有API,能直接根据X算出B样条曲线的Y值,不想要GDI+的,最好是API,或者开源的函数。

最近实现了WPF中的From/To/By动画技术、关键帧动画技术和属性动画技术,可以通过XML或者脚本直接创建2D/3D动画,在时间线的控制上还缺乏样条控制功能(意思是时间值不一定均匀分布,而是通过样条决定时间快慢),只是还未找到轻量级的样条曲线函数,有经验的朋友帮忙提供一点线索。

WPF中还有路径动画技术,这个比较简单,只是现在还没有实现,留待以后吧。WPF的动画依赖高效定时器,我也实现了一个多线程的高精度定时器,效率很高。

动画都是相通的。WPF的动画功能很全,据我观察,在3DSMAX中支持的动画技术都能在WPF中实现,所以对于我而言,WPF是一个非常好的参照平台,可以少走很多弯路,我会尽可能把WPF的特性都COPY过来,并非求全,但是精髓一定要有。同时,因为有专用设计工具,XAML文件看起来很庞大且过于复杂,不利于手写,但我没设计工具,只好在用XML描述时尽量减少大小。

描述一段动画的例子:
WPF用XAML描述:

<!-- 这是From/To/By动画例子 -->
<Rectangle Name="fromToAnimatedRectangle"
Height="10" Width="100" HorizontalAlignment="Left"
Fill="Black">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown">
<BeginStoryboard>
<Storyboard>
<!-- Demonstrates the From and To properties used together.
Animates the rectangle's Width property from 50 to 300 over 10 seconds. -->
<DoubleAnimation
Storyboard.TargetName="fromToAnimatedRectangle"
Storyboard.TargetProperty="Width"
From="50" To="300" Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>

<!-- 这是关键帧动画例子 -->
<Border Background="#99FFFFFF" BorderThickness="28"
Margin="0,60,0,20" Padding="20" >
<Border.BorderBrush>
<SolidColorBrush x:Name="MyAnimatedBrush" Color="Green" />
</Border.BorderBrush>
<Border.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard>
<Storyboard>

<!-- Animate from green to red using a linear key frame, from red to
yellow using a discrete key frame, and from yellow back to green with
a spline key frame. This animation repeats forever. -->
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="Color"
Storyboard.TargetName="MyAnimatedBrush"
Duration="0:0:6" FillBehavior="HoldEnd" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames.KeyFrames>

<!-- Go from green to red in the first 2 seconds. LinearColorKeyFrame creates
a smooth, linear animation between values. -->
<LinearColorKeyFrame Value="Red" KeyTime="0:0:2" />

<!-- In the next half second, go to yellow. DiscreteColorKeyFrame creates a
sudden jump between values. -->
<DiscreteColorKeyFrame Value="Yellow" KeyTime="0:0:2.5" />

<!-- In the final 2 seconds of the animation, go from yellow back to green. SplineColorKeyFrame
creates a variable transition between values depending on the KeySpline property. In this example,
the animation starts off slow but toward the end of the time segment, it speeds up exponentially.-->
<SplineColorKeyFrame Value="Green" KeyTime="0:0:4.5" KeySpline="0.6,0.0 0.9,0.00" />
</ColorAnimationUsingKeyFrames.KeyFrames>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>

<TextBlock>
This example shows how to use the ColorAnimationUsingKeyFrames to create
an animation on the BorderBrush property of a Border.
</TextBlock>
</Border>


我用XML描述:

<!-- From/To/By 动画XML描述例子 -->
<StoryBoard>
<FrameSet TargetType="Int" Target="myimg" TargetAttribute="left" Reverse="true" Repeat="forever"
From="0" To="125" Duration="0:0:5" />
<FrameSet TargetType="Int" Target="myimg" TargetAttribute="top" Reverse="true" Repeat="forever"
From="20" To="120" Duration="0:0:8.2" />
</StoryBoard>

<!-- 关键帧动画XML描述例子 -->
<StoryBoard>
<KeyFrameSet TargetType="Int" Target="myimg2" TargetAttribute="left" Reverse="true" Repeat="forever">
<KeyFrame KeyTime="0" KeyValue="120" />
<KeyFrame KeyTime="2" KeyValue="160" />
<KeyFrame KeyTime="0:3" KeyValue="210" />
<KeyFrame KeyTime="0:0:5.0" KeyValue="250" />
</KeyFrameSet>
<KeyFrameSet TargetType="Int" Target="myimg2" TargetAttribute="top" Reverse="true" Repeat="forever">
<KeyFrame KeyTime="0" KeyValue="120" />
<KeyFrame KeyTime="3" KeyValue="170" />
<KeyFrame KeyTime="10.0" KeyValue="200" />
<KeyFrame KeyTime="0:0:13" KeyValue="250" />
</KeyFrameSet>
</StoryBoard>

...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
jameshooo
图形处理/算法 19,463 成员 50,678 内容