有什么技巧可以绘制动画路径吗?

发布于 2024-07-27 19:20:43 字数 311 浏览 3 评论 0原文

有没有办法用石英模拟手写?

我的意思是,A、B、C 点之间有一条路径。 我希望路径从 A 点出来,然后以动画方式到达 B 点,然后到达 C 点。

我想到的是两种选择:

  1. 丑陋 - 创建路径,然后遮罩它并移动遮罩以显示路径。 需要花费大量时间来创建不可靠且丑陋的黑客

  2. 移动点 A、B、C 并在它们之间绘制线。

  3. 有什么方法可以沿着离开轨迹的路径制作一个圆圈的动画吗?

有什么技巧、例子吗?

谢谢。

Is there a way to simulate handwriting using quartz?

I mean there is a path between points A, B and C.
I want path to come out of point A and go animated to point B and then C.

What comes to mind is two options to do it:

  1. Ugly- Create path then mask it and move mask around to reveal a path.
    Takes a lot of time to create and unreliable and ugly hack

  2. move points A,B,C and draw line between them.

  3. Some way to animate a circle along a path leaving the trail?

Any techniques, examples?

Thanks.

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

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

发布评论

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

评论(3

埋情葬爱 2024-08-03 19:20:44

制作一个CAShapeLayer,然后为其路径设置动画。

Make a CAShapeLayer and then animate its path.

守护在此方 2024-08-03 19:20:44

正如上面评论中提到的,理想的 API 应该是可以让您沿着路径绘制任意线段的 API,但我还没有看到任何这样的 API。

另一种方法是将路径定义为谨慎的分段。 然后使用 NSBezierPath 的元素方法沿着路径行走,并在计时器上或使用 NSAnimation 绘制沿途的每个分段。 这种方法的问题是它不允许您使用任何任意路径。

As mentioned in the comment above an ideal API would be one that would let you draw any arbitrary segment along the path but I have not seen any such API.

Another approach would be to define your path is discreet segments. Then use NSBezierPath's element methods to walk along the path and draw each segment along the way on a timer or using NSAnimation. The problem with this approach is that is does not let you use any arbitrary path.

鸵鸟症 2024-08-03 19:20:44

贝塞尔曲线定义了一种基于不相关参数(通常称为 t)获取一组点的方法。 要渲染完整曲线,请评估 0 到 1 之间的 t,并从每个点到下一个点绘制一条线。 要渲染小于完整的曲线,请将 t 计算为从 0 到小于 1 的数字。 要动画绘制曲线,您可以评估点并在计时器上绘制线段。

您可以在任意 t 处分割贝塞尔曲线。 这样做将允许您将曲线传递给系统进行绘制,或在 CAShapeLayer 中使用。

手写字母通常是一系列贝塞尔曲线或贝塞尔样条线。 一条曲线的终点是下一条曲线的起点。 可以将 t 视为从零到样条曲线中的段数。 如果有 3 条曲线,则认为 t 从 0 到 3。当 t 在 1 和 2 之间时,您可以将整个第一段和第二段的一部分传递给系统进行绘制。

您可以阅读有关分割贝塞尔曲线的 DeCasteljau 算法。 下面是平面上三次贝塞尔曲线的代码示例:

// initial curve is four point x0,y0 , x1,y1 , x2,y2 , x3,y3
// x0,y0 and x3,y3 are the anchors
// point to split curve at is 0<t<1

nt    = 1.0 - t;
x01   = nt * x0   + t * x1;
y01   = nt * y0   + t * y1;
x12   = nt * x1   + t * x2;
y12   = nt * y1   + t * y2;
x23   = nt * x2   + t * x3;
y23   = nt * y2   + t * y3;
x012  = nt * x01  + t * x12;
y012  = nt * y01  + t * y12;
x123  = nt * x12  + t * x23;
y123  = nt * y12  + t * y23;
x0123 = nt * x012 + t * x123;
y0123 = nt * y012 + t * y123;

// now the new curve you want is
// x0,y0 , x01,y01 , x012,y012 , x0123,y0123

// the other half of the curve, discarded in your case, is
// x0123,y0123 , x123,y123 , x23,y23 , x3,y3

因此,给定一系列描述手写字符的曲线,您可以从 0 到 T 进行动画处理,其中 T 是曲线的数量。 计算 t=T-floor(T),当 t 不为零时,用它在 n=floor(T) 处分割曲线。

A bezier curve defines a way to get a set of points based on an unrelated parameter, usually called t. To render the full curve, you evaluate t between 0 and 1 and draw a line from each point to the next. To render less than the full curve, you evaluate t from 0 to a number less than one. To animate drawing the curve, you could evaluate the points and draw the segments on a timer.

You can split a bezier curve at an arbitrary t. Doing that will allow you to pass the curve to the system to draw, or to use in a CAShapeLayer.

A handwritten letter will usually be a series of bezier curves, or a bezier spline. The end point of one curve is the start point of the next. Think of t as going from zero to the number of segments in the spline. If there are 3 curves, think of t as going from 0 to 3. When t is between 1 and 2, you would pass the whole first segment and part of the second segment to the system to draw.

You can read about DeCasteljau's algorithm for splitting bezier curves. Here is a code sample for a cubic bezier curve on a plane:

// initial curve is four point x0,y0 , x1,y1 , x2,y2 , x3,y3
// x0,y0 and x3,y3 are the anchors
// point to split curve at is 0<t<1

nt    = 1.0 - t;
x01   = nt * x0   + t * x1;
y01   = nt * y0   + t * y1;
x12   = nt * x1   + t * x2;
y12   = nt * y1   + t * y2;
x23   = nt * x2   + t * x3;
y23   = nt * y2   + t * y3;
x012  = nt * x01  + t * x12;
y012  = nt * y01  + t * y12;
x123  = nt * x12  + t * x23;
y123  = nt * y12  + t * y23;
x0123 = nt * x012 + t * x123;
y0123 = nt * y012 + t * y123;

// now the new curve you want is
// x0,y0 , x01,y01 , x012,y012 , x0123,y0123

// the other half of the curve, discarded in your case, is
// x0123,y0123 , x123,y123 , x23,y23 , x3,y3

So given a series of curves that describe your handwritten character, you would animate from 0 to T, where T is the number of curves. calculate t=T-floor(T) and when t is not zero, use it to split the curve at n=floor(T).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文