【发布时间】:2010-11-17 16:57:27
【问题描述】:
我有一个非常“基于连接”的应用程序,即多个输入/输出。
“电缆”的 UI 概念正是我想要让用户清楚了解的概念。 Propellerhead 在其用于音频组件的 Reason 软件中采用了类似的方法,如 this YouTube video (fast forward to 2m:50s) 所示。
我可以通过绘制从 A 点到 B 点的样条线来使这个概念在 GDI 中发挥作用,必须有一种更优雅的方式来使用 WPF 中的路径或其他东西,但是你从哪里开始呢?有没有什么好的方法可以模拟绳索摇晃时的动画?
如果这个轮子已经为 WPF 发明了,我也愿意控制库(商业或开源)。
更新:感谢到目前为止答案中的链接,我快到了。
我以编程方式创建了一个BezierCurve,点 1 是 (0, 0),点 2 是底部“挂起”点,点 3 是鼠标光标所在的位置。我为第 2 点创建了一个PointAnimation,并对其应用了一个ElasticEase 缓动函数以提供“摇摆”效果(即,使中间点稍微弹跳)。
唯一的问题是,动画似乎有点晚了。每次鼠标移动时我都会启动情节提要,有没有更好的方法来制作这个动画?到目前为止,我的解决方案位于此处:
代码:
private Path _path = null;
private BezierSegment _bs = null;
private PathFigure _pFigure = null;
private Storyboard _sb = null;
private PointAnimation _paPoint2 = null;
ElasticEase _eEase = null;
private void cvCanvas_MouseMove(object sender, MouseEventArgs e)
{
var position = e.GetPosition(cvCanvas);
AdjustPath(position.X, position.Y);
}
// basic idea: when mouse moves, call AdjustPath and draw line from (0,0) to mouse position with a "hang" in the middle
private void AdjustPath(double x, double y)
{
if (_path == null)
{
_path = new Path();
_path.Stroke = new SolidColorBrush(Colors.Blue);
_path.StrokeThickness = 2;
cvCanvas.Children.Add(_path);
_bs = new BezierSegment(new Point(0, 0), new Point(0, 0), new Point(0, 0), true);
PathSegmentCollection psCollection = new PathSegmentCollection();
psCollection.Add(_bs);
_pFigure = new PathFigure();
_pFigure.Segments = psCollection;
_pFigure.StartPoint = new Point(0, 0);
PathFigureCollection pfCollection = new PathFigureCollection();
pfCollection.Add(_pFigure);
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures = pfCollection;
_path.Data = pathGeometry;
}
double bottomOfCurveX = ((x / 2));
double bottomOfCurveY = (y + (x * 1.25));
_bs.Point3 = new Point(x, y);
if (_sb == null)
{
_paPoint2 = new PointAnimation();
_paPoint2.From = _bs.Point2;
_paPoint2.To = new Point(bottomOfCurveX, bottomOfCurveY);
_paPoint2.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
_eEase = new ElasticEase();
_paPoint2.EasingFunction = _eEase;
_sb = new Storyboard();
Storyboard.SetTarget(_paPoint2, _path);
Storyboard.SetTargetProperty(_paPoint2, new PropertyPath("Data.Figures[0].Segments[0].Point2"));
_sb.Children.Add(_paPoint2);
_sb.Begin(this);
}
_paPoint2.From = _bs.Point2;
_paPoint2.To = new Point(bottomOfCurveX, bottomOfCurveY);
_sb.Begin(this);
}
【问题讨论】:
-
您是否要绘制悬链线曲线? en.wikipedia.org/wiki/Catenary
-
@Gabe,是的,这似乎是我正在寻找的曲线类型
-
您可以通过这种方式在某个时间开始播放 YouTube 视频:youtube.com/watch?v=wj7b0esJB_o&t=2m50s :-)
标签: c# wpf silverlight graphics gdi+