如何制造“飞行碎片”?与jQuery的效果?
我正在制作一些动画并使用 jQuery 库。
资源的方法之一是 fly()
,这意味着如果父元素刚刚爆炸,就会飞离父元素。它应该看起来像飞行的碎片,即它应该向上飞走,然后屈服于重力并落下。 示例。
到目前为止,这是我的方法...
var parent = this.element.parent(),
direction = this.element.position().left < parent.width() / 2 ? '-' : '+';
this.element.animate({
left: direction + '=300',
top: '-=200'
}, duration);
这显然看起来根本不像飞行碎片,因为它只是向上移动。 direction
变量决定元素的飞行方向。由于每个元素相对于其父元素定位,因此左侧的元素会向左移动,反之亦然。
我不想实现一个成熟的物理引擎,例如 Box2D。
我知道我的代码本质上应该做什么,我相信是......
- 将元素向上(负
顶部
)并按照设置的方向(负或正左
),一些值衰减以模拟由于风阻等导致的水平运动损失和由于重力导致的垂直运动损失。 - 在某个阶段,重力将变得比爆炸产生的元素向上的力更强,在这种情况下,元素将需要下落。
我真的不知道如何解决这个问题。我希望我可以利用 jQuery 的 animate()
,但我不知道如何合并衰减价值。
创造这种效果的最佳方法是什么?
I am working on some animation and am using the jQuery library.
One of the assets' method is fly()
, which means to fly away from the parent element had that parent just exploded. It should look like flying debris, i.e. it should fly up and away and then succumb to gravity and fall. Example.
Here is my method so far...
var parent = this.element.parent(),
direction = this.element.position().left < parent.width() / 2 ? '-' : '+';
this.element.animate({
left: direction + '=300',
top: '-=200'
}, duration);
This obviously doesn't look at all like flying debris, as it just moves up and away. The direction
variable determines which way the element should fly. Because each element is relatively positioned to its parent, elements on the left hand side move to the left and vice versa.
I'd don't want to implement a full fledged physics engine such as Box2D.
I know what my code should essentially do, which I believe is...
- Fly the elment up (negative
top
) and in the direction set (negative or positiveleft
), with some value decaying to simulate loss of horizontal movement due to wind resistance etc and loss of vertical movement due to gravity. - The force of gravity will have grown stronger at some stage than the upward force of the element from the explosion, in which case the element will need to fall.
I don't really know how to approach this problem. I was hoping I could leverage jQuery's animate()
, but I don't know to incorporate a decaying value.
What would be the best way to create this effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(我会将其作为答案发布,因为这就是它的实际情况。)
您是否只需要一个美观的碎片动画,或者是否必须精确计算?我发现使用随机值实际上可以使其变得现实。请参阅http://jsfiddle.net/minitech/fSaGk
(I will post this as an answer since that’s what it actually is.)
Do you just need a debris animation for eye candy, or does it have to be precisely calculated? I found that using random values can actually make it realistic. See http://jsfiddle.net/minitech/fSaGk
您可能需要使用
animate()
调用的easing
属性,并利用 jQuery 缓动插件 以获得比 jQuery 默认提供的标准线性
缓动更微妙的效果。另一个技巧是链接对
animate()
的调用以获得您想要的多阶段效果。这是一个真的(我的意思是真的)快速模型 这有点给你一个想法。
它的核心:
关键元素是:
线性
- 这不是您正在寻找的衰减,但对于这个模型来说已经足够了animate()
立即开始我使用了 Easing 插件中的 easeOutQuint 缓动函数来实现重力效果 - 它远非完美,但您可以通过足够的排队和链接 <像这样的 code>animate() 调用,您应该能够得到一个非常漂亮的衰减 x 以及一个漂亮的受重力启发的 y...
You're probably going to need to use the
easing
property of theanimate()
call, and leverage the jQuery Easing Plugin to get an effect somewhat more subtle than the standardlinear
easing offered by default in jQuery.The other trick is to chain the calls to
animate()
to get that multiple-stage effect you're after.Here's a really (and I mean really) quick mockup that kinda- sorta gives you an idea.
The guts of it:
The key elements are:
linear
- this is not the decay you're looking for but it's sufficient for this mockupanimate()
in the chain starts immediatelyeaseOutQuint
easing function from the Easing Plugin to do the gravitational effect - it's nowhere near perfect but you get the ideaWith sufficient queueing and chaining of
animate()
calls like this you should be able to get a pretty nice decaying x together with a good-looking gravity-inspired y...我最终广泛修改了 minitech 的代码......
jsFiddle.
I ended up modifying minitech's code extensively...
jsFiddle.