简体   繁体   English

如何使用 Javascript 创建重力效果?

[英]How to create a gravity effect with Javascript?

Google gravity and gravity script are two nice demonstrations.Google gravitygravity script是两个很好的演示。 but no source code or tutorials are available.但没有可用的源代码或教程。 and the original JS files are very big.并且原始的 JS 文件非常大。 How can I create a Gravity effect with Drag & drop(specially being " Throw able " and " rotatable " like google gravity) on a certain element?如何在某个元素上通过拖放(特别是“可抛出”和“可旋转”,如谷歌重力)创建重力效果?

You will want to start with a physics engine, the one Google Gravity uses is Box2Djs which is a javascript port of Box2D .您需要从物理引擎开始,Google Gravity 使用的是Box2Djs ,它是Box2D的 javascript 端口。 You can read the manual for Box2D to learn how to use it, though the manual itself states that you will have little idea what you are doing without some knowledge of rigid body physics (force, impulse, torque, etc), though these examples may help you get started.您可以阅读 Box2D 的手册以了解如何使用它,尽管手册本身指出如果没有一些刚体物理知识(力、冲量、扭矩等),您将几乎不知道自己在做什么,尽管这些示例可能帮助您入门。

If you want to write the physics engine yourself you will have to at least implement 2D rigid body dynamics and collision detection for it to look like the examples you gave.如果您想自己编写物理引擎,您至少必须实现 2D 刚体动力学和碰撞检测,才能使其看起来像您提供的示例。 A tutorial for doing that would be called a computer simulation class and would have a linear algebra and physics I prerequisite, it's not a trivial task.这样做的教程将被称为计算机模拟课程,并且具有线性代数和物理 I 的先决条件,这不是一项微不足道的任务。

Afterwards, you will have to learn about javascript animation techniques.之后,您将不得不学习 javascript 动画技术。 I recommend learning about window.requestAnimationFrame .我建议学习window.requestAnimationFrame Using setInterval(stepFunction, time) will work but it won't be as efficient as it could be in modern browsers.使用setInterval(stepFunction, time)会起作用,但它不会像在现代浏览器中那样高效。

在 github JQuery.throwable上查看这个 jquery 插件只需执行$("Selector").throwable()并且对象将在重力下

One other framework to consider: Matter.js — Demo: Easy Physics Sandbox in JavaScript .另一个要考虑的框架:Matter.js —演示:JavaScript 中的 Easy Physics Sandbox

The Physics物理学

Gravity is hard.重力很难 That's because gravity is curved spacetime, and we can only make 2d representations of this warping that happens in the third dimension.那是因为引力是弯曲的时空,我们只能对发生在三维空间中的这种扭曲进行二维表示。

On the other hand -- moving an element at a consistent 9.8 m/s^2 in a linear direction towards one direction, that's actually not too hard to implement.另一方面——以一致的 9.8 m/s^2 的速度沿线性方向朝一个方向移动元素,这实际上并不难实现。

The CSS Solution CSS 解决方案

All we really need is transition: all 1s linear;我们真正需要的只是transition: all 1s linear; to control the speed of the animation, margin-top to animate the element moving downward, and transition-timing-function with a cubic-bezier that's fairly representative of 9.8 m/s^2.控制 animation 的速度, margin-top为元素向下移动设置动画, transition-timing-function具有相当代表 9.8 m/s^2 的cubic-bezier贝塞尔曲线。

The Demo演示

 document.addEventListener('DOMContentLoaded', function(event) { document.getElementById('drop').addEventListener('click', function(ev) { div = document.createElement('div'); div.classList.add('gravity-affected-object'); image = document.createElement('img'); image.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/640px-Red_Apple.jpg'; image.width = 100; image.classList.add('gravity-affected-object'); div.style.position = 'absolute'; div.style.left = '50%'; div.appendChild(image); document.getElementById('page-top').appendChild(div); setTimeout(function() { div.style.marginTop = '1000px'; }, 100); }); });
 .gravity-affected-object { transition: all 1s linear; transition-timing-function: cubic-bezier(0.33333, 0, 0.66667, 0.33333); z-index: -5000; }
 <div id="page-top"></div> <button id="drop">Drop</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM