| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> |
| 5 |
<title>svg.js</title> |
| 6 |
<style type="text/css"> |
| 7 |
html, |
| 8 |
body, |
| 9 |
#drawing { |
| 10 |
width: 100%; |
| 11 |
height: 100%; |
| 12 |
margin: 0; |
| 13 |
border: 1px solid black; |
| 14 |
} |
| 15 |
</style> |
| 16 |
</head> |
| 17 |
|
| 18 |
<body> |
| 19 |
<div id="drawing"></div> |
| 20 |
|
| 21 |
<script src="https://unpkg.com/@svgdotjs/svg.js"></script> |
| 22 |
<script src="dist/svg.draggable.js"></script> |
| 23 |
|
| 24 |
<script> |
| 25 |
var e, |
| 26 |
constrainingShape, |
| 27 |
ghostShape, |
| 28 |
draw = SVG() |
| 29 |
.addTo('#drawing') |
| 30 |
.size('100%', '400') |
| 31 |
.attr({ 'font-size': 10 }) |
| 32 |
.fill('#f06') |
| 33 |
|
| 34 |
/* plain draggable */ |
| 35 |
draw |
| 36 |
.rect(100, 100) |
| 37 |
.center(150, 150) |
| 38 |
.draggable() |
| 39 |
|
| 40 |
draw.plain('just plain draggable').center(150, 210) |
| 41 |
|
| 42 |
/* grouped draggable */ |
| 43 |
var g = draw.group().draggable() |
| 44 |
g.rect(100, 100).center(400, 150) |
| 45 |
g.plain('grouped draggable').center(400, 210) |
| 46 |
|
| 47 |
/* constrained with object */ |
| 48 |
constrainedWithObject = draw |
| 49 |
.rect(100, 100) |
| 50 |
.center(650, 150) |
| 51 |
.draggable() |
| 52 |
.on('dragstart', function () { |
| 53 |
ghostShape = draw.put(constrainedWithObject.clone().opacity(0.2)) |
| 54 |
|
| 55 |
constrainingShape = draw |
| 56 |
.rect(400, 350) |
| 57 |
.move(400, 50) |
| 58 |
.fill('none') |
| 59 |
.stroke('#0fa') |
| 60 |
}) |
| 61 |
.on('dragmove', e => { |
| 62 |
e.preventDefault() |
| 63 |
|
| 64 |
const { handler, box } = e.detail |
| 65 |
let { x, y } = box |
| 66 |
|
| 67 |
const constraints = constrainingShape.bbox() |
| 68 |
|
| 69 |
if (x < constraints.x) { |
| 70 |
x = constraints.x |
| 71 |
} |
| 72 |
|
| 73 |
if (y < constraints.y) { |
| 74 |
y = constraints.y |
| 75 |
} |
| 76 |
|
| 77 |
if (box.x2 > constraints.x2) { |
| 78 |
x = constraints.x2 - box.w |
| 79 |
} |
| 80 |
|
| 81 |
if (box.y2 > constraints.y2) { |
| 82 |
y = constraints.y2 - box.h |
| 83 |
} |
| 84 |
|
| 85 |
handler.move(x, y) |
| 86 |
ghostShape.animate(300, '>').move(x, y) |
| 87 |
}) |
| 88 |
.on('dragend', function () { |
| 89 |
constrainingShape.remove() |
| 90 |
ghostShape.remove() |
| 91 |
}) |
| 92 |
draw.plain('constrained with object and ghost').center(650, 210) |
| 93 |
|
| 94 |
/* constraind with function */ |
| 95 |
// Some constraints (x, y, width, height) |
| 96 |
const constraints = new SVG.Box(750, 0, 300, 300) |
| 97 |
|
| 98 |
draw |
| 99 |
.rect(100, 100) |
| 100 |
.center(900, 150) |
| 101 |
.draggable() |
| 102 |
.on('dragmove', e => { |
| 103 |
const { handler, box } = e.detail |
| 104 |
e.preventDefault() |
| 105 |
|
| 106 |
let { x, y } = box |
| 107 |
|
| 108 |
// In case your dragged element is a nested element, |
| 109 |
// you are better off using the rbox() instead of bbox() |
| 110 |
|
| 111 |
if (x < constraints.x) { |
| 112 |
x = constraints.x |
| 113 |
} |
| 114 |
|
| 115 |
if (y < constraints.y) { |
| 116 |
y = constraints.y |
| 117 |
} |
| 118 |
|
| 119 |
if (box.x2 > constraints.x2) { |
| 120 |
x = constraints.x2 - box.w |
| 121 |
} |
| 122 |
|
| 123 |
if (box.y2 > constraints.y2) { |
| 124 |
y = constraints.y2 - box.h |
| 125 |
} |
| 126 |
|
| 127 |
handler.move(x, y) |
| 128 |
}) |
| 129 |
|
| 130 |
draw.plain('constraint with function').center(900, 210) |
| 131 |
|
| 132 |
/* group with multiple levels of draggables (dragging a part doesn't drag the group) */ |
| 133 |
var g2 = draw.group().draggable() |
| 134 |
for (var i = 0; i < 4; i++) { |
| 135 |
var cx = i & 1 ? -25 : 25 |
| 136 |
var cy = i & 2 ? -25 : 25 |
| 137 |
g2.rect(50, 50) |
| 138 |
.center(cx, cy) |
| 139 |
.draggable() |
| 140 |
} |
| 141 |
g2.plain('grouped with multiple levels of draggable').center(0, 70) |
| 142 |
g2.move(1150, 150) |
| 143 |
</script> |
| 144 |
</body> |
| 145 |
</html> |