如何在不离开边缘的情况下为三角函数设置动画 [英] How to animate a trigonometric function without leaving the edges

查看:80
本文介绍了如何在不离开边缘的情况下为三角函数设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请问我如何制作此动画不会持续很长时间
到达最右边


在XAML中:

hello, please how I can make this animation does not continue long
to get to the far right


in XAML :

<Canvas Height="262" Name="MyCanvas"  Width="481" />


在代码中:


in Code :

double time = 0;
double dt = 0.05;
Polyline pl;

public MainWindow()
{
    InitializeComponent();
}

private void buStart_Click(object sender, RoutedEventArgs e)
{
    pl = new Polyline();
    pl.Stroke = Brushes.Red;
    pl.StrokeThickness = 1;
    MyCanvas.Children.Add(pl);
    CompositionTarget.Rendering += StartAnimation;
}
private void StartAnimation(object sender, EventArgs e)
{
    Double PosX = pl.ActualWidth;
    if (PosX < MyCanvas.ActualWidth)
    {
        pl.Points.Add(new Point(time*20, Math.Sin(time)*20));
        time += dt;
    }
    else
    {
       // I do not know what to put here, is it possible : ?
         pl.Points.RemoveAt(1); ¿ ..... ?
          pl.Points.Add(new Point(time * 20, Math.Sin(time) * 20));
          time += dt;
       // CompositionTarget.Rendering -= StartAnimation;
    }
}

推荐答案

好吧,我不完全了解您的问题,如果您想重新启动动画,可以执行以下操作:
Well I dont completely understand your question, I fyou want to restart the animation you could do this:
private void StartAnimation(object sender, EventArgs e)
{
    Double PosX = pl.ActualWidth;
    if (PosX < MyCanvas.ActualWidth)
    {
        pl.Points.Add(new Point(time*20, Math.Sin(time)*20));
        time += dt;
    }
    else
    {
       // I do not know what to put here, is it possible : ?
         pl.Points.Clear();
         time = 0;
          pl.Points.Add(new Point(time * 20, Math.Sin(time) * 20));
          time += dt;
       // CompositionTarget.Rendering -= StartAnimation;
    }
}



如果要执行此操作,则应考虑使用DispatcherTimer或情节提要.但是,如果我需要帮助,您应该提供其他信息:)



You should consider using DispatcherTimer or the Storyboard if you want to do this. But you should provide additional information if I should help you :)


我认为他希望正弦通过画布进行动画处理,就像条形图记录器"一样.
这样吗?
I think he wants the sine to animate through the canvas, like a "strip-chart recorder".
Like this?
public partial class MainWindow : Window
{
  double time = 0;
  double dtFactor = 0.05;
  Polyline pl;
  TranslateTransform translate;

  public MainWindow()
  {
    InitializeComponent();
  }

  private void buStart_Click(object sender, RoutedEventArgs e)
  {
    pl = new Polyline();
    pl.Stroke = Brushes.Red;
    pl.StrokeThickness = 1;
    MyCanvas.Children.Add(pl);
    translate = new TranslateTransform(0.0, MyCanvas.ActualHeight / 2);
    pl.RenderTransform = translate;
    CompositionTarget.Rendering += StartAnimation;
  }
  private void StartAnimation(object sender, EventArgs e)
  {
    Double PosX = pl.ActualWidth;
    if (PosX >= MyCanvas.ActualWidth)
    {
      pl.Points.RemoveAt(0);
      translate.X--;
    }
    pl.Points.Add(new Point(time, Math.Sin(time * dtFactor) * 0.4 * MyCanvas.ActualHeight));
    time++;
  }

}


这篇关于如何在不离开边缘的情况下为三角函数设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆