GravityJSPhysics-based animations that feel like real objects in Earth's gravity
These examples now live after the Air Drag section and show the richer deformation system:
• collapse pulls material toward the interaction point
• spread pushes material outward from the interaction point
• curvature increases how strongly the actual box contour bends around the contact zone
• trigger switches between click/press and live hover on supported components
These demo buttons use data-gravity-deformation-zone to pin the deformation hotspot.
Remove that attribute and the framework falls back to using the actual click position.
Tune the settings below and apply them to the playground button to compare collapse vs spread and click vs hover. Click to see the deformation!
Cards can now use click-trigger collapse or hover-trigger spread while keeping their lift and shadow behavior.
Inputs keep their focus-based trigger, but they now inherit collapse/spread and contour deformation from the shared deformation engine.
Add the data-gravity attribute to any element to enable gravity physics. Use data attributes to customize physics properties:
Material deformation only activates on supported GravityJS component elements. Add
data-gravity-deformation together with one of these component attributes:
data-gravity-button - Button-like elements such as <button>, <a>, <div>, or <span>data-gravity-card - Card-like containers such as <div>, <article>, or <section>data-gravity-input - Form controls and focusable wrappers such as <input>, <textarea>, <select>, or <div tabindex="0">Configuration attributes:
If data-gravity-deformation-zone is omitted, deformation continues to behave exactly as before and depends on where the user clicks, touches, or hovers.
Implementation examples:
<button data-gravity-button data-gravity-deformation>...</button><button data-gravity-button data-gravity-deformation data-gravity-deformation-zone="left">...</button><div data-gravity-card data-gravity-deformation>...</div><input data-gravity-input data-gravity-deformation /><textarea data-gravity-input data-gravity-deformation></textarea><select data-gravity-input data-gravity-deformation>...</select><div tabindex="0" data-gravity-input data-gravity-deformation>...</div>Deformation zones:
Modes and triggers:
data-gravity-button and data-gravity-card for live pointer-driven deformation.data-gravity-input elements use focus/click capture rather than live hover, while still using the same deformation engine.// Initialize with custom options - realistic Earth-like gravity (~3800 px/s²)
const engine = new GravityEngine({
gravity: new Vector2(0, 3800), // px/s² (Earth's 9.8 m/s² at ~386px/m scale)
airDrag: 0.01,
timeScale: 1
});
// Add elements by selector
engine.addFromSelector('[data-gravity]');
// Or add individually
const body = engine.addBody({
element: document.getElementById('myElement'),
mass: 2,
elasticity: 0.8,
friction: 0.3
});
// Control the engine
engine.dropAll(); // Drop all from top
engine.resetAll(); // Reset to original positions
engine.toggle(); // Pause/resume
engine.applyGlobalImpulse(0, -500); // Apply force
// Access individual bodies
const body = engine.getBodyByElement(element);
body.applyImpulse(new Vector2(100, 0));
body.setVelocity(0, 500);