Catalog npm
Back to packageRELEASE DETAILS

1.1.0

npm

2d-collision

💥 A lightweight and customisable JavaScript library for simulating particle physics, including collisions, gravity, and mouse interactivity. Perfect for creating dynamic visual effects.

2danimationcollidecollisioncollisionscustomizabledynamicsenginegravityinteractivejavascriptlightweightmouse-interactivityparticleparticlesphysics
All keywords (21)
physics-enginephysics-simulationsimulationvectorvisual-effects
Published17 May 2025
Direct dependencies0
Transitive dependenciesNot calculated

README

Current package documentation

💥 Particle Physics Simulation

npm license downloads

A lightweight and customisable JavaScript library for simulating particle physics, including collisions, gravity, and mouse interactivity. Perfect for creating dynamic visual effects.

Demo

🚀 Installation:

Option 1: via npm

npm install 2d-collision

🎮 Usage and Configuration

Functions:

The library provides the following key functions:

  • createParticle(radius, mass, element, container) – Creates a single particle and adds it to the simulation.

    • radius(number) – determines particle's size
    • mass(number) – determines particle's weight
    • element(string) – the HTML element that you'd like to turn into a particle
    • container(string) – the HTML element that you'd like to act as the container for the particle
  • createParticles(radii, masses, elements, container) – Creates multiple particles at once.

  • editParticles() – Modify properties of particles by selecting them via their class. Function provides the following arguments:

    • particlesClass (string) – The class of the HTML elements representing the particles.
    • setVelX, setVelY (number) – Sets the velocity of the particles along the X and Y axes.
    • addVelX, addVelY (number) – Adds to the current velocity of the particles along the X and Y axes.
    • multiplyVelX, multiplyVelY (number) – Multiplies the current velocity of the particles along the X and Y axes.
    • setPosX, setPosY (number) – Sets the position of the particles along the X and Y axes.
    • addPosX, addPosY (number) – Adds to the current position of the particles along the X and Y axes.
    • multiplyPosX, multiplyPosY (number) – Multiplies the current position of the particles along the X and Y axes.
    • setRadii (number) – Sets the radii of the particles.
    • addRadii (number) – Adds to the current radii of the particles.
    • multiplyRadii (number) – Multiplies the current radii of the particles.
    • setMasses (number) – Sets the masses of the particles.
    • addMasses (number) – Adds to the current masses of the particles.
    • multiplyMasses (number) – Multiplies the current masses of the particles.

Example:

// Create a single particle
createParticle(10, 1, document.querySelector('#circle'), document.querySelector('#container'));

// Create multiple particles
createParticles(12, 3, document.querySelectorAll('.circle'), document.querySelector('#container'));

// Edit particles
editParticles({
    particlesClass: 'circle',
    setVelX: 5,
    addPosY: 10,
    multiplyRadii: 1.5
});

Global Configuration:

The library includes several global settings to control particle behavior:

  • Particle.acceleration (Vector) – Controls gravity direction.
    Default: (0, 0.35)
    ➜ To edit values, assign like: Particle.acceleration.x = ..., Particle.acceleration.y = ...
  • Particle.collisionDamping (number) – Reduces velocity after collisions.
    Default: 0.5
  • Particle.enableCursorForce (boolean) – Enables or disables interactive forces from the cursor.
    Default: true
  • Particle.cursorForce (number) – Strength of the cursor's interactive force.
    Default: 0.5
  • Particle.containerPadding (number) – Sets padding around the container to prevent particles from touching edges.
    Default: 0

License: MIT
Contributing: Contributions welcome! Please feel free to submit a Pull Request.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 0

Native requirements
0 direct targetsNot calculated transitive count
No declared dependencies in this collected response

Declarations describe requirements; they are not a resolved dependency graph.

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "05467360c2cb3a13c03ef156800c12a8ea834630",
  "tarball": "https://registry.npmjs.org/2d-collision/-/2d-collision-1.1.0.tgz",
  "fileCount": 4,
  "integrity": "sha512-u8VFum8/aAtAXM3z7HID9SY3qTQrKCBnbCQDf3OXbKZzcBy/IsuTCrMk/w9SJNMJIKJb8hNIW2ZleKUvXoMK6w==",
  "signatures": [
    {
      "sig": "MEUCIQCSEJfiBUw1Q3pmmfXRs8TmYr/kmlxyoLzFnPcNEEq4LgIgMz4dw9sZRwYjK6Jflvk/6hrkaYL8C4Rko2IZVtCzF2o=",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 19055
}

Repository

PENDING
alyshukry/2d-collision

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:10 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:3cbde2a3748bc4e8b65d49ba865f8c144f5345befcf5f345290a797a92b8626b

Other collected versions

1.2.026 Jul1.1.219 May1.1.117 May1.0.016 MayView all collected versions →