npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

popmotion-collision

v1.1.1

Published

Collision detection for Popmotion Actors

Downloads

1

Readme

Popmotion Collision Detector

Collision detection for Popmotion Actors

In-built support for DOM and SVG elements. Customisable Roles allow you to create your own support for Three.js, canvas, etc.

Actors receive onCollisionEnter, onCollisionExit and onCollisionStay events during the preRender step of the Popmotion render loop.

Currently supports circular and rectangular, axis-aligned Actors only.

Install

In your project root:

$ npm install popmotion-collision --save

In your module:

// New hotness
import CollisionDetector from 'popmotion-collision';

// Old and busted
var CollisionDetector = require('popmotion-collision');

Use

new CollisionDetector(actors[, role]);

Track collisions between Actors

new CollisionDetector(Iterator || array);

The CollisionDetector constructor accepts either a Popmotion Iterator or a plain array of Actors. Once created, all Actors are automatically checked for collisions.

Example: Track DOM elements

let elements = ui.select('div', {
    onCollisionEnter: function (actor, otherActor) {
        console.log('collision!')
    }
});

let collisionDetector = new CollisionDetector(elements);

Track collisions between Actors of the same shape

Collision Detector currently supports collision detection between two shapes of the same type, rectangular and circular.

Set an Actor's shape property for non-rectangular shapes:

new Actor({
    element: yourElement,
    shape: CollisionDetector.CIRCLE
});

Methods

  • sync() Sync Actor collider models with their actual physical representations. Called on initialisation, and should be called whenever an Actor.element changes physical position outside of Popmotion.

  • pause() Pause collision detection. Collision detection is a background Process, so it only runs during active frames. This means manually managing the collision detection process is not required under normal circumstances.

  • resume() Resume collision detection.

Actor properties

  • collider [object] READ-ONLY CollisionDetector's calculated model of the Actor.element's physical representation. This can be used to calculate collision resolutions.

  • onCollisionEnter [function] Fired the first frame an Actor collides with another Actor.

  • onCollisionExit [function] Fired the first frame an Actor stops colliding with another Actor.

  • onCollisionStay [function] Fired every frame an Actor is colliding with another Actor.

  • shape [string] (default CollisionDetector.RECT) The physical shape of the Actor.

Create a custom Role

By default, DOM and SVG Actors are automatically recognised and assigned appropriate Roles. However, a custom Role can be provided as an object of required methods as the final constructor argument.

Role methods

  • getAbsolutePosition(element, actor) (Returns {x, y, width, height}) Calculate the current absolute position of the element. Because Actor values are not neccessarily indicative of where the Actor's element physically resides (think two DOM elements with x of 0 yet floated next to each other), and every type of Actor (DOM, SVG etc) has a different method of calculating its absolute position, we use this method to abstract that calculation.