Debian Codegraph @

README.md

raw
1 # svg.draggable.js
2
3 A plugin for the [svgdotjs.github.io](https://svgdotjs.github.io/) library to make elements draggable.
4
5 svg.draggable.js is licensed under the terms of the MIT License.
6
7 ## Usage
8
9 Install the plugin:
10
11 ```sh
12 npm install @svgdotjs/svg.js @svgdotjs/svg.draggable.js
13 ```
14
15 Include this plugin after including the svg.js library in your html document.
16
17 ```html
18 <script src="node_modules/@svgdotjs/svg.js/dist/svg.js"></script>
19 <script src="node_modules/@svgdotjs/svg.draggable.js/dist/svg.draggable.js"></script>
20 ```
21
22 Or for esm just require it:
23
24 ```js
25 import { SVG } from '@svgdotjs/svg.js'
26 import '@svgdotjs/svg.draggable.js'
27 ```
28
29 To make an element draggable just call `draggable()` on the element
30
31 ```javascript
32 var draw = SVG().addTo('#canvas').size(400, 400)
33 var rect = draw.rect(100, 100)
34
35 rect.draggable()
36 ```
37
38 Yes indeed, that's it! Now the `rect` is draggable.
39
40 ## Events
41
42 The Plugin fires 4 different events
43
44 - beforedrag (cancelable)
45 - dragstart
46 - dragmove (cancelable)
47 - dragend
48
49 You can bind/unbind listeners to this events:
50
51 ```javascript
52 // bind
53 rect.on('dragstart.namespace', function (event) {
54 // event.detail.event hold the given data explained below
55 // this == rect
56 })
57
58 // unbind
59 rect.off('dragstart.namespace')
60 ```
61
62 ### event.detail
63
64 `beforedrag`, `dragstart`, `dragmove` and `dragend` gives you the mouse / touch `event` and the `handler` which calculates the drag. The `dragmove` event also gives you the `dx` and `dy` values for your convenience.
65 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.
66
67 You can use this property to implement custom drag behavior as seen below.
68
69 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.
70
71 ### Cancelable Events
72
73 You can prevent the default action of `beforedrag` and `dragmove` with a call to `event.preventDefault()` in the callback function.
74 The shape won't be dragged in this case. That is helpfull if you want to implement your own drag handling.
75
76 ```javascript
77 rect.draggable().on('beforedrag', (e) => {
78 e.preventDefault()
79 // no other events are bound
80 // drag was completely prevented
81 })
82
83 rect.draggable().on('dragmove', (e) => {
84 e.preventDefault()
85 e.detail.handler.move(100, 200)
86 // events are still bound e.g. dragend will fire anyway
87 })
88 ```
89
90 ### Custom Drag Behavior
91
92 #### Constraints
93
94 ```js
95 // Some constraints (x, y, width, height)
96 const constraints = new SVG.Box(100, 100, 400, 400)
97
98 rect.on('dragmove.namespace', (e) => {
99 const { handler, box } = e.detail
100 e.preventDefault()
101
102 let { x, y } = box
103
104 // In case your dragged element is a nested element,
105 // you are better off using the rbox() instead of bbox()
106
107 if (x < constraints.x) {
108 x = constraints.x
109 }
110
111 if (y < constraints.y) {
112 y = constraints.y
113 }
114
115 if (box.x2 > constraints.x2) {
116 x = constraints.x2 - box.w
117 }
118
119 if (box.y2 > constraints.y2) {
120 y = constraints.y2 - box.h
121 }
122
123 handler.move(x - (x % 50), y - (y % 50))
124 })
125 ```
126
127 #### Snap to grid
128
129 ```js
130 rect.on('dragmove.namespace', (e) => {
131 const { handler, box } = e.detail
132 e.preventDefault()
133
134 handler.move(box.x - (box.x % 50), box.y - (box.y % 50))
135 })
136 ```
137
138 ## Remove
139
140 The draggable functionality can be removed calling draggable again with false as argument:
141
142 ```javascript
143 rect.draggable(false)
144 ```
145
146 ## Restrictions
147
148 - If your root-svg is transformed this plugin won't work properly in Firefox. Viewbox however is not affected.
149
150 ## Dependencies
151
152 This module requires svg.js >= v3.0.10