Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
svg.draggable.js
Advanced tools
Readme
A plugin for the svgdotjs.github.io library to make elements draggable.
svg.draggable.js is licensed under the terms of the MIT License.
Install the plugin:
npm install @svgdotjs/svg.draggable.js
Include this plugin after including the svg.js library in your html document.
<script src="svg.js"></script>
<script src="svg.draggable.js"></script>
Or for esm just require it:
import '@svgdotjs/svg.draggable.js'
To make an element draggable just call draggable()
on the element
var draw = SVG().addTo('canvas').size(400, 400)
var rect = draw.rect(100, 100)
rect.draggable()
Yes indeed, that's it! Now the rect
is draggable.
The Plugin fires 4 different events
You can bind/unbind listeners to this events:
// bind
rect.on('dragstart.namespace', function (event) {
// event.detail.event hold the given data explained below
// this == rect
})
// unbind
rect.off('dragstart.namespace')
beforedrag
, dragstart
, dragmove
and dragend
gives you the mouse / touch event
and the handler
which calculates the drag.
Except for beforedrag
the events also give you detail.box
which holds the initial or new bbox of the element before or after the drag.
You can use this property to implement custom drag behavior as seen below.
Please note that the bounding box is not what you would expect for nested svgs because those calculate their bbox based on their content and not their x, y, width and height values. Therefore stuff like constraints needs to be implemented a bit differently.
You can prevent the default action of beforedrag
and dragmove
with a call to event.preventDefault()
in the callback function.
The shape won't be dragged in this case. That is helpfull if you want to implement your own drag handling.
rect.draggable().on('beforedrag', (e) => {
e.preventDefault()
// no other events are bound
// drag was completely prevented
})
rect.draggable().on('dragmove', (e) => {
e.preventDefault()
e.detail.handler.move(100, 200)
// events are still bound e.g. dragend will fire anyway
})
// Some constraints (x, y, width, height)
const constraints = new SVG.Box(100, 100, 400, 400)
rect.on('dragmove.namespace', (e) => {
const {handler, box} = e.detail
e.preventDefault()
let {x, y} = box
// In case your dragged element is a nested element,
// you are better off using the rbox() instead of bbox()
if (x < constraints.x) {
x = constraints.x
}
if (y < constraints.y) {
y = constraints.y
}
if (box.x2 > constraints.x2) {
x = constraints.x2 - box.w
}
if (box.y2 > constraints.y2) {
y = constraints.y2 - box.h
}
handler.move(x - (x%50), y - (y%50))
})
rect.on('dragmove.namespace', (e) => {
const {handler, box} = e.detail
e.preventDefault()
handler.move(box.x - box.x % 50, box.y - box.y % 50)
})
The draggable functionality can be removed calling draggable again with false as argument:
rect.draggable(false)
This module requires svg.js >= v3.0.10
FAQs
An extension for svg.js which allows to drag elements with your mouse
The npm package svg.draggable.js receives a total of 600,651 weekly downloads. As such, svg.draggable.js popularity was classified as popular.
We found that svg.draggable.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.