哪里可以学习这种高级行为代码?

发布于 2024-11-09 04:40:55 字数 508 浏览 0 评论 0原文

最近我发现了 Pete Blois 的行为,名为 Explode。这是链接:-

http://blois.us/blog/2009/07/ explode.html

当您单击网格时,网格就会爆炸,看起来像真正的 3D 效果,即使 Silverlight 没有真正的 3D 效果。

我研究了代码,但无法获得行为的核心逻辑。谁能解释一下函数PrepareShards、ApplyForce 是做什么的? Shards 类的目的是什么?我认为它试图模拟一个具有 X、Y 和 Z 的点。我研究了代码 1/2 小时,但完全不明白它在做什么。我知道我不需要理解这背后的逻辑。

但是如果我想创建一些像这样的高级效果怎么办?研究幕后发生的事情总是好的。有没有人写过一篇博客文章来解释这段代码是什么,或者有没有作者在书中教授此类现实生活中的内容?或者如果你们有时间可以解释一下它在做什么吗?

Recently I came across this Pete Blois's behavior named Explode. This is the link :-

http://blois.us/blog/2009/07/explode.html

When you click on the grid the grid just explodes looking like real 3D effect even though Silverlight doesn't have a true 3D effect.

I studied the code but couldn't get the core logic of the behavior. Can anybody explain me what is the function PrepareShards, ApplyForce doing? Also what is the purpose of Shards class? I think it is trying to simulate a point which has X,Y and Z. I studied the code for 1/2 hour but couldn't get at all what it is doing. I know I don't need to understand the logic behind this.

But what if I want to create some advanced effects like this? It's always good to study what's going under the hood. Has anybody written a blog post explaining what is this code or any book where the author teaches this type of real life stuff? Or if you all have the time can you explain me what is it doing?

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

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

发布评论

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

评论(1

一杆小烟枪 2024-11-16 04:40:55

作者给了你代码,但是没有注释。这就把它变成了一个谜题!

此代码的工作方式是:

  • 它是一个 < code>Behavior 使用 System.Windows.Interactivity
  • 该行为在 XAML 中附加到要爆炸的元素
  • Explode 挂钩鼠标事件,以便单击时它知道鼠标位置
  • StartExplode 中,它首先调用 PrepareShards
  • PrepareShards 创建一个包含 PopupPopup >Grid 包含一个 Rectangle 元素矩阵
  • 新的 Popup 看起来与原来的一模一样,但它不再是交互式的:它是假的
  • 它“隐藏”了原来的互动元素由将其 Opacity 设置为零
  • Shard 是每个 Rectangle 的包装器,用于跟踪其位置和动画
  • ApplyForce被调用来为所有矩形设置动画

ApplyForce 正在做什么?物理。这只是公式。

所以这只是伪造和巧妙的 2D 和 3D 图形的结合。一旦您了解了其工作原理的整体结构,各个步骤就不那么令人畏惧了:只需弄清楚如何让 API 完成您想做的事情即可。

例如,作者需要将原始交互框架元素的微小网格区域(2、3)转换为位图图像,并将其绘制到网格中相应的矩形上。一旦绘制,由于图形子系统的保留模式性质,就再也不需要担心了。

像这样的效果并不容易编写,但也不是不可能,而且可以很有趣。当你编写其他不那么“花哨”的应用程序时,你肯定会学到很多对你有用的技术!

The author gives you the code, but it is not commented. That turns it into a puzzle!

The way this code works is:

  • It is a Behavior using System.Windows.Interactivity
  • The behavior is attached in XAML to the element you want to explode
  • Explode hooks the mouse events so it knows the mouse position when clicked
  • In StartExplode it first calls PrepareShards
  • PrepareShards creates a Popup which contains a Grid which contains a matrix of Rectangle elements
  • The new Popup looks exactly like the original but it is no longer interactive: it's fake
  • It "hides" the original interactive element by setting its Opacity to zero
  • A Shard is a wrapper around each Rectangle to keep track of its position and animation
  • ApplyForce is called to animate all the rectangles

What is ApplyForce doing? Physics. It's just formulas.

So this is just a combination of fakery and clever 2D and 3D graphics. Once you understand the overall structure of how it works, the individual steps are less daunting: just figuring out how to get the API to do what you want to do.

For example, the author needs to convert the tiny grid region (2, 3) of the original interactive framework element into a bitmap image and draw it onto the corresponding rectangle in the grid. Once drawn, due to the retained-mode nature of the graphics subsystem, it never needs to be worried about again.

Effects like this are not easy to write but they are not impossible either and they can be a lot of fun. You will definitely learn a lot of techniques that will serve you well when write other less "gimmicky" applications!

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