(This page includes affiliate links. If you click and purchase, I receive a small commission at no extra cost from you and that way you can support me. I only recommend tools that I have personally used and loved.)
Get Elementor Pro Today –
1 – Elementor Pro
Interactive Physics Elements with Falling Elements – Matter JS
The following code allows you to create a collision box that contains all the interactive elements. The below code is limited to 3 shapes, i.e Rectangle, Pill Shape & Circle. As of now, if the elements are moved too fast they can be thrown outside the box, that is because of the processing limit and can be changed to improve collision. Increasing that limit might also make browser use more processing power which is not really ideal for a simple website. Make sure all elements are position – absolute and make sure the Box has overflow-hidden.



UI/UX
WordPress
Elementor
CSS
JS
Photoshop
Figma
Wireframe
Subscribe on YT
Templates
My Skills
CSS, Javascript & Matter JS code
- matter-box is the CSS class of the container that contains all the physics elements.
- dm-matter-elem is the CSS class of the elements that have rectangle collision box.
- dm-matter-elem-circle is the CSS class of the elements that have circle collision box.
- dm-matter-elem-pill is the CSS class of elements that have a pill shape collision box.
- opacity is set to 0 for the matter js generated elements.
- restitution controls the bounciness of the elements. More is higher bounciness.
- The simulation will only start if the box gets in viewport, if you don’t want this feature then use the 2nd code below or watch the video above(@12:53 time).
Version 2 – Makes the physics engine start even if it isn’t in viewport