forked from shaneiadt/interactjs-and-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractable.js
69 lines (62 loc) · 1.82 KB
/
interactable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import interact from "interactjs";
export const interactable = el => {
interact(el)
.draggable({
modifiers: [
interact.modifiers.snap({
targets: [
interact.createSnapGrid({
x: 20,
y: 20
})
],
range: Infinity,
relativePoints: [
{
x: 0,
y: 0
}
]
}),
interact.modifiers.restrict({
restriction: el.parentNode,
elementRect: {
top: 0,
left: 0,
bottom: 1,
right: 1
},
endOnly: true
})
],
inertia: true
})
.on("dragmove", event => {
const items = document.getElementsByClassName("selected");
const { dx, dy } = event;
const parseDataAxis = target => axis =>
parseFloat(target.getAttribute(`data-${axis}`));
const translate = target => (x, y) => {
target.style.webkitTransform = "translate(" + x + "px, " + y + "px)";
target.style.transform = "translate(" + x + "px, " + y + "px)";
};
const updateAttributes = target => (x, y) => {
target.setAttribute("data-x", x);
target.setAttribute("data-y", y);
};
if (items.length > 0) {
for (const item of items) {
const x = parseDataAxis(item)("x") + dx;
const y = parseDataAxis(item)("y") + dy;
translate(item)(x, y);
updateAttributes(item)(x, y);
}
} else {
const { target } = event;
const x = (parseDataAxis(target)("x") || 0) + dx;
const y = (parseDataAxis(target)("y") || 0) + dy;
translate(target)(x, y);
updateAttributes(target)(x, y);
}
});
};