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

svg.draggy.js

v1.1.1

Published

A JavaScript library for dragging SVG things.

Downloads

401

Readme

svg.draggy.js Version Downloads

A JavaScript library for dragging SVG things.

Usage

Include this library after including svg.js in your html document. Here you can see a demo.

To make an element draggable, just do:

var draw = new SVG('svg-container').size(400, 400);
var rect = draw.rect(100, 100);

rect.draggy();

Yes indeed, that's it! Now the rect is draggable. :sparkles:

Events

There are four different events available you can llisten to: beforedrag, dragstart, dragmove and dragend. This is how you assign them:

The event object has some custom data added by this library:

  • event.detail.delta is an object containing:

    • x and y: The element coordinates.
    • movedX and movedY: The element total movement values (available only in dragmove event).
  • event.detail.event is an object containing the original event

dragstart

rect.on('dragstart', function(event) {
    // Do something
});

beforedrag

rect.on('beforedrag', function(event) {
    // Do something
});

dragmove

rect.on('dragmove', function(event) {
    // Do something
});

Constraint

The drag functionality can be limited within a given box. You can pass the the constraint values as an object:

rect.draggy({
    minX: 10,
    minY: 15,
    maxX: 200,
    maxY: 100
});

For more dynamic constraints a function can be passed as well:

rect.draggy(function(x, y) {
    return { x: x < 1000, y: y < 300 };
});

With this you can also easily achieve some snapping functionality:

var snapRange = 50;
rect.draggy(function (x, y, elem) {
    var res = {};

    res.x = x - (x % snapRange);
    res.y = y - (y % snapRange);

    return res;
});

Remove

The draggable functionality can be removed with the fixed() method:

rect.fixed();

Viewbox

This plugin is viewBox aware but there is only one thing that you need to keep in mind. If you work with a viewBox on the parent element you need to set the width and height attributes to have the same aspect ratio. So let's say you are using viewbox='0 0 150 100' you have to make sure the aspect ratio of width and height is the same:

var draw = SVG('paper').attr('viewBox', '0 0 150 100').size(600, 400);

svg.draggy.js

:cloud: Installation

Check out the src directory to download the needed files and include them on your page.

If you're using this module in a CommonJS environment, you can install it from npm and require it:

$ npm i --save svg.draggy.js

:memo: Documentation

draggy(constraint)

Makes an element draggable.

Params

  • Object|Function constraint: An object containing the constraint values or a function which gets the x and y values and returns a boolean or an object containing the x and y boolean values.false skips moving while true allows it.

Return

  • SVG The SVG element.

:yum: How to contribute

Have an idea? Found a bug? See [how to contribute][contributing].

:scroll: License

MIT © [jillix][website]

[website]: [contributing]: /CONTRIBUTING.md [docs]: /DOCUMENTATION.md