• Stars
    star
    281
  • Rank 147,023 (Top 3 %)
  • Language
    HTML
  • License
    MIT License
  • Created almost 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An extension for the svg.js library to make elements draggable

svg.draggable.js

A plugin for the svgdotjs.github.io library to make elements draggable.

svg.draggable.js is licensed under the terms of the MIT License.

Usage

Install the plugin:

npm install @svgdotjs/svg.js @svgdotjs/svg.draggable.js

Include this plugin after including the svg.js library in your html document.

<script src="node_modules/@svgdotjs/svg.js/dist/svg.js"></script>
<script src="node_modules/@svgdotjs/svg.draggable.js/dist/svg.draggable.js"></script>

Or for esm just require it:

import { SVG } from '@svgdotjs/svg.js'
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.

Events

The Plugin fires 4 different events

  • beforedrag (cancelable)
  • dragstart
  • dragmove (cancelable)
  • dragend

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')

event.detail

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.

Cancelable Events

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
})

Custom Drag Behavior

Constraints

// 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))
})

Snap to grid

rect.on('dragmove.namespace', e => {
  const { handler, box } = e.detail
  e.preventDefault()

  handler.move(box.x - (box.x % 50), box.y - (box.y % 50))
})

Remove

The draggable functionality can be removed calling draggable again with false as argument:

rect.draggable(false)

Restrictions

  • If your root-svg is transformed this plugin won't work properly in Firefox. Viewbox however is not affected.

Dependencies

This module requires svg.js >= v3.0.10

More Repositories

1

svg.js

The lightweight library for manipulating and animating SVG
JavaScript
11,031
star
2

svgdom

Straightforward DOM implementation to make SVG.js run headless on Node.js
JavaScript
267
star
3

svg.draw.js

An extension of svg.js which allows to draw elements with mouse
JavaScript
236
star
4

svg.filter.js

A plugin for svg.js adding svg filters
JavaScript
218
star
5

svg.import.js

A plugin for importing raw SVG into the svg.js library
JavaScript
98
star
6

svg.select.js

An extension of svg.js which allows to select an element
JavaScript
93
star
7

svg.panzoom.js

A plugin for svg.js which enables panzoom for viewbox elements
JavaScript
93
star
8

svg.resize.js

An extension of svg.js which allows to resize elements which are selected
JavaScript
90
star
9

svg.clock.js

A clock built with the svg.js library, inspired by the swiss railway clock
JavaScript
75
star
10

svg.pathmorphing.js

A plugin for the svg.js library to enable path morphing / animation
JavaScript
55
star
11

svg.easing.js

Easing equations for the fx module in the svg.js library
JavaScript
52
star
12

svg.topoly.js

A plugin for SVG.js to convert paths to polygons at a given sample rate
JavaScript
39
star
13

svg.shapes.js

A shapes plugin for the SVG.js library
JavaScript
36
star
14

svg.topath.js

A plugin for the svg.js library to convert any other shape to a path
JavaScript
29
star
15

svg.export.js

A plugin for the svg.js library to export raw svg
JavaScript
26
star
16

svg.parser.js

A helper library for svg.js providing parser functions to adopt and import existing svg's
JavaScript
17
star
17

svg.textmorph.js

A plugin for the svg.js library to enable text morphing / animation
JavaScript
16
star
18

svg.absorb.js

A plugin for the svg.js library adding the ability to absorb an existing svg into the dynamic svg.js structure
JavaScript
14
star
19

svg.screenbbox.js

A plugin for the svgjs library which gets the bbox of a path/polygon/polyline after all transformations applied.
JavaScript
14
star
20

svg.threed.js

An SVG.js plugin to visualise 3D scenes and objects
JavaScript
13
star
21

svgdotjs.github.io

Documentation and website for SVG.js
PHP
13
star
22

svg.pathhandles.js

Add handles to svg path as known in svg programs like incscape
JavaScript
8
star
23

svg.wiml.js

A plugin for the svg.js library adding support for the wiml markup language
JavaScript
3
star
24

svg.colorAt.js

An extension for svg.js which allows to pick a color from a gradint at a specific position
JavaScript
3
star
25

saucelab-configurator

A cli tool to create configurations objects for karma-saucelabs-launcher
JavaScript
1
star
26

svg.logo

The logo for svgdotjs
HTML
1
star