Code Monkey home page Code Monkey logo

magnet.js's Introduction

Magnet.js

Magnet.js is a JavaScript library for grouping HTML elements then make them draggable and attractable each other.

Demo

Basic demo

Configure magnet attract distance, stay in parent element, align outside/inside edge and align x/y center.

Group demo

4 magnet groups that can be attract in their own groups or configure to attract all the other group members.

Install

NodeJS

npm install https://github.com/lf2com/magnet.js
Build

The required codes are magnet.js and lib/*.js. All dependencies in package.json are only used for building a packaged and minified magnet.min.js. It is easy to build a browser-used magnet.js with your own ways.

There is a script to build magnet.min.js:

npm run build

Which runs

browserify magnet.js -t [ babelify --presets [ es2015 ] ] | uglifyjs -cm > magnet.min.js

Browser

Download magnet.min.js from this repository.

<script src="PATH/TO/magnet.min.js"></script>

How to use

Create magnet group

let magnet = new Magnet();

Add HTML elements

magnet.add(document.querySelectorAll('.magnet'));

Or add HTML element when creating magnet group.

let magnet = new Magnet(document.querySelectorAll('.magnet'));

Remove HTML elements

magnet.remove(document.querySelector('.magnet'));

Clear magnet group

magnet.clear();

Set attract distance

Dragging magnet element with ctrl pressed has the same effect as setting distance to 0.

magnet.distance(15); // unit px, default is 0
magnet.distance(); // 15

Set alignment types

magnet.enabledAlignOuter(true); // align element outside edges, default is true
magnet.enabledAlignInner(true); // align element inside edges, default is true
magnet.enabledAlignCenter(true); // align element middle line, default is true
magnet.enabledAlignOuter(); // true

Set to keep element in the parent element

magnet.stayInParentEdge(true); // default is false
magnet.stayInParentEdge(); // true

Or

magnet.stayInParentElem(true); // default is false
magnet.stayInParentElem(); // true

Both stayInParentEdge and stayInParentElem do the same thing.

Event listener for magnet events

Magnet enter/leave events would not be triggered if ctrl is pressed.

// triggered when elements attract each other
magnet.on('magnetenter', function(e) {
  console.log('magnetenter', e.detail); // detail info
});

// triggered when mouseup or no elements attract each other
magnet.on('magnetleave', function(e) {
  console.log('magnetleave');
});

magnet.off('magnetenter magnetleave'); // remove event listeners

Event listener for magnet group elements

Magnet element events would not be triggered if ctrl is pressed.

let elem = document.querySelector('.block');
magnet.add(elem);

// triggered when element were dragging and attract any other magnet elements
elem.addEventListener('attract', function(e) {
  console.log('attract', e.detail); // detail info
});

// triggered when element mouseup or were dragging and unattract any of other magnet elements
elem.addEventListener('unattract', function(e) {
  console.log('unattract');
});

// triggered when magnet element were attracted by a dragging magnet element
elem.addEventListener('attracted', function(e) {
  console.log('attracted', e.detail); // detail info
});

// triggered when magnet element were no longer attracted by a dragging magnet element
elem.addEventListener('unattracted', function(e) {
  console.log('unattracted');
});

Find the nearby elements from magnet group elements

magnet.nearby(document.querySelector('.block')); // NearbyResult object

Magnet methods

Check if parameter is rect

let rect = { top: 1, right: 2, bottom: 3, left: 4 };
Magnet.isRect(rect); // false, rect.right should larger then rect.left
rect.right = 5;
Magnet.isRect(rect); // true

rect.x = 3;
Magnet.isRect(rect); // false, rect.x sould equal to rect.left
rect.x = rect.left;

rect.width = 2;
Magnet.isRect(rect); // false, rect.width should equal to (rect.right - rect.left)

Standardize rect

Magnet.stdRect(rect); // Rect object

Measure distance result between 2 elements

let rectA = { top: 0, right: 3, bottom: 1, left: 2 };
let rectB = { top: 10, right: 13, bottom: 11, left: 12 };
Magnet.measure(rectA, rectB); // MeasureResult object

Find the nearby elements

let rect = { top: 1, right: 4, bottom: 2, left: 3 };
Magnet.nearby(rect, document.querySelectorAll('.magnet')); // NearbyResult object

Object properties

Rect

property type description
top Number top, inherit from parameter
right Number right, inherit from parameter
bottom Number bottom, inherit from parameter
left Number left, inherit from parameter
x Number left
y Number top
width Number right - left
height Number bottom - top

MeasureResult

property type description
source Object rect for rect; if source is element, element for it
target Object rect for rect; if target is element, element for it
topToTop Number distance between source top and target top (inside)
topToBottom Number distance between source top and target bottom (outside)
rightToRight Number distance between source right and target right (inside)
rightToLeft Number distance between source right and target left (outside)
bottomToTop Number distance between source bottom and target top (outside)
bottomToBottom Number distance between source bottom and target bottom (inside)
xCenter Number distance between source x center and target x center (middle)
yCenter Number distance between source y center and target y center (middle)
ranking Array ascending-order array of above distance properties
min String minimum distance property
max String maximum distance property

NearbyResult

property type description
source Object rect for rect; if source is element, element for it
topToTop MeasureResult the nearest top to top measure result (inside)
topToBottom MeasureResult the nearest top to bottom measure result (outside)
rightToRight MeasureResult the nearest right to right measure result (inside)
rightToLeft MeasureResult the nearest right to left measure result (outside)
bottomToTop MeasureResult the nearest bottom to top measure result (outside)
bottomToBottom MeasureResult the nearest bottom to bottom measure result (inside)
leftToRight MeasureResult the nearest left to right measure result (outside)
leftToLeft MeasureResult the nearest left to left measure result (inside)
xCenter MeasureResult the nearest x centers measure result (middle)
yCenter MeasureResult the nearest y centers measure result (middle)
min String minimum distance property
max String maximum distance property
ranking Array ascending-order array of above distance properties

magnet.js's People

Contributors

lf2com avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.