Physics on web page elements.

Overview

MatterDropdown 0.1

Applies physics to DOM elements and lets you bounce them around with the mouse. Built with matter.js and ismailman/decompose-dommatrix.

Example: dropdown.robsite.net
Simple Example: dropdown.robsite.net/simple-example

dropdown.mp4

Usage

Don't. This is just for fun and not meant for people to use, yet. It doesn't really work with changing browser widths or touch controls. Try elopezga/matter-dom-plugin for a somewhat similar thing with more features and support.

But:

  1. Include matter.js, matter-dropdown.js:
    <script src="matter.js" type="text/javascript"></script>
    <script src="matter-dropdown.js" type="text/javascript"></script>
    
  2. Add a #matter-canvas element and hide it. Optionally do this to see it in debug mode:
    <canvas id="matter-canvas"></canvas>
    <style>
        body {
            position: relative;
        }
        #matter-canvas {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 100%;
            height: 100%;
            opacity: 0.5;
            z-index: 100000;
            pointer-events: none;
        }
        body.debug #matter-canvas {
            display: block;
        }
        .matter {
            z-index: 90000;
        }
    </style>
    
  3. Add class matter to all elements that should be affected by physics.
    1. Add class matter-static to all fixed elements, on which others bounce off.
    2. Add class matter-static-animated to fixed elements whose position changes (like with CSS- or JS-animations)
  4. Initialize MatterDropdown with optional options:
    <script>
        window.addEventListener('load', function() {
            var dropdown = MatterDropdown({
                bounceDebounce: 50,               // how often are bounce-mouse interactions done? in ms
                timeToInteraction: 3000,          // how long until we can interact after creating the first interactive object with dropdown.addElement(el)?
                mouseBounceForce: {x: 0, y: -0.5} // for applyForce() when mouse touches non-static object. lower y = bouncier
            });
            dropdown.init();
        });
    </script>
    
    Now all elements with only the class matter fall down and bounce off the static elements.
  5. Add other interactive elements later. For example, show a dropdown submenu and apply physics after 1 second:
    var menuItems = document.querySelectorAll('.menu > li > a');
    menuItems.forEach(function(el) {
        el.addEventListener('click', function(ev) {
            ev.preventDefault();
            var submenu = ev.currentTarget.parentElement.querySelector('ul');
            submenu.classList.add('visible');
            setTimeout(function() {
                dropdown.addElement(submenu);
            }, 1000);
        })
    })
    
  6. A border-radius gets recognized automatically. Give the element the class has-boxshadow if it has one. Can only be rgba(0,0,0,0.2) 0 10px 20px; (or change it in matter-dropdown.js)
  7. An existing element with class matter is added to the physics objects immediately after init(). Give it the attribute data-matter-init-delay="1000" to delay this by 1000ms.
You might also like...

Awesome books app is a basic website that allows users to add/remove books from a list. It is a single page app (SPA) which allow switching to different pages of the app without page load. Built with JavaScript.

Awesome Books ES6 In this project, I build a basic website that allows users to add/remove books from a list. using ES6 syntax and make it more organi

Jul 1, 2022

This restaurant project is a SPA (single-page application) website. The user can navigate between the home, menu and contact page. I used the MealDB API to display some menu items.

This restaurant project is a SPA (single-page application) website. The user can navigate between the home, menu and contact page. I used the MealDB API to display some menu items.

Fresh Cuisine This restaurant project is from the Odin Project and it is a SPA (single-page application) website. The user can navigate between the ho

Nov 2, 2022

Progressive Web App (PWA) built in Node.js & Express that automatically reloads/refreshes your browser, web page, and app when developing.

Expresso ☕️ Checks for changes in your source and automatically reloads your browser, or web page, and app window. Makes development easier. Report Bu

Oct 6, 2022

CodePlay is a Web App that helps the user to input HTML,CSS and JS code in the Code editors and display the resultant web page in real-time

CodePlay is a Web App that helps the user to input HTML,CSS and JS code in the Code editors and display the resultant web page in real-time

CodePlay A codepen clone Setup Clone repository Create and go to the directory where you want to place the repository cd my-directory Clone the proj

Sep 24, 2022

A JavaScript library to create html elements with js easily

A JavaScript library to create html elements with js easily

applecake is a javascript library for making HTML elements with javascript really easy . Why applecake ? Really easy to use It is not heavy It has a s

Jul 21, 2021

Pretty Cool Elements

Pretty Cool Elements Social Media Photo by Jamison McAndie on Unsplash This module is a follow up of this Medium post, and it provides element mixins/

Dec 23, 2022

A simple guide to HTML head elements

🤯 HEAD A simple guide to HTML head elements Table of Contents Recommended Minimum Elements Meta Link Icons Social Facebook Open Graph Twitter Card

Jan 1, 2023

curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.

curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.

What is it ? Shaders are the new front-end web developpment big thing, with the ability to create very powerful 3D interactions and animations. A lot

Jan 1, 2023

An easy way to animate SVG elements.

Walkway I loved the animations for the polygon ps4 review a few months back and decided to create a small library to re-create them (simple demo). It

Jan 2, 2023
Owner
Robert Gerlach
┌( ಠ_ಠ)┘
Robert Gerlach
A 3rd year University physics project for simulating satellites motion in orbit.

Satellite Simulator VI - Deluxe Edition A university physics project for simulating satellites in orbit. Installation instructions Clone the git repos

Rami Sabbagh 8 Jun 26, 2022
This is an example of Quantum physics congress.

This is an example of Quantum physics congress. This is an imaginary congress in which are shown 2 pages with information about the congress, about activities which will be held there and speakers who will speak about the topic

Sanja Mandic 7 Jun 24, 2022
JavaScript 2D physics library

p2.js 2D rigid body physics engine written in JavaScript. Includes collision detection, contacts, friction, restitution, motors, springs, advanced con

Stefan Hedman 2.5k Dec 28, 2022
Open game experiment with vehicles and physics in Three.js

TNTGame Open game experiment with vehicles and physics in Three.js You can try it online here: Crater scene: https://yomboprime.github.io/TNTGame/app?

Juan Jose Luna Espinosa 3 Oct 5, 2022
The awesomebooks project is a simple list, but separated into 3 parts and given a retro feel. The main page is where we can add books, and on another page we can see the list, and remove items. There is also a "contact-us" page.

Awesome Books This is the restructured version of the famous awesome-books project! Here you can find JavaScript broken into modules, using import-exp

Matt Gombos 12 Nov 15, 2022
A pure JavaScript Web Page to retrieve real-time OTP through a web page and generate/scan QR codes.

2FA-Solver A pure JavaScript Web Page to retrieve real-time OTP through a web page and generate/scan QR codes. It can be used as an offline web page b

Yuthan K 8 Dec 7, 2022
A javascript library to animate elements on scroll page events

ScrollJS by Sam Sirianni ScrollJS is a library written in Javascript. With ScrollJS you can animate elements on scroll events. Visit the ScrollJS webs

Sam Sirianni 1 Mar 21, 2021
A lightweight JavaScript utility to fade elements in and out of view on page scroll.

ScrollFade ScrollFade is used to fade elements in and out of view while scrolling through a page. Tag any elements you want to fade with the class scr

Jordan Trbuhovic 14 Dec 15, 2022
Material Design implemented in Web Components (Custom Elements v1)

Material Web Components Material Design implemented in Web Components (Custom Elements v1) https://dannymoerkerke.github.io/material-webcomponents/ Ge

Danny Moerkerke 110 Nov 14, 2022
A landing page, as well as a results page, that utliize both Yelp API and Google Maps API.

Economic Eats Group Project #1 Maintained By: Austin Donovan John Hysong John Guzzetta Jahnathan Exantus Description This project contains a landing p

John Guzzetta IV 7 Aug 4, 2022