
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
svg.draggable.js
Advanced tools
A plugin for the svgjs.com library to make elements draggable.
Svg.draggable.js is licensed under the terms of the MIT License.
Install the plugin:
bower install 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>
To make an element draggable just call draggable()
in the element
var draw = SVG('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
})
// unbind
rect.off('dragstart.namespace')
beforedrag
, dragstart
, dragmove
and dragend
gives you the event
and the handler
which calculates the drag.
Except for beforedrag
the events also give you:
detail.m
transformation matrix to calculate screen coords to coords in the elements userspacedetail.p
pageX and pageY transformed into the elements userspaceThe drag functionality can be limited within a given box. You can pass the the constraint values as an object:
rect.draggable({
minX: 10
, minY: 15
, maxX: 200
, maxY: 100
})
For more dynamic constraints a function can be passed as well:
rect.draggable(function(x, y) {
return { x: x < 1000, y: y < 300 }
})
Note that every constraint given is evaluated in the elements coordinate system and not in the screen-space
The draggable functionality can be removed calling draggable again with false as argument:
rect.draggable(false)
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').viewbox(0, 0, 150, 100).size(600, 400)
This module requires svg.js >= v2.0.1
FAQs
An extension for svg.js which allows to drag elements with your mouse
The npm package svg.draggable.js receives a total of 488,369 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.