哪里可以学习这种高级行为代码?
最近我发现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作者给了你代码,但是没有注释。这就把它变成了一个谜题!
此代码的工作方式是:
System.Windows.Interactivity
Explode
挂钩鼠标事件,以便单击时它知道鼠标位置StartExplode
中,它首先调用PrepareShards
PrepareShards
创建一个包含Popup
的Popup
>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:
Behavior
usingSystem.Windows.Interactivity
Explode
hooks the mouse events so it knows the mouse position when clickedStartExplode
it first callsPrepareShards
PrepareShards
creates aPopup
which contains aGrid
which contains a matrix ofRectangle
elementsPopup
looks exactly like the original but it is no longer interactive: it's fakeOpacity
to zeroShard
is a wrapper around eachRectangle
to keep track of its position and animationApplyForce
is called to animate all the rectanglesWhat 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!