forked from shaneiadt/interactjs-and-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.svelte
159 lines (140 loc) · 3.83 KB
/
App.svelte
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<script>
import { onMount } from "svelte";
import { interactable } from "./interactable";
onMount(() => {
const elements = [...document.querySelectorAll(".item")];
if (elements) {
for (const el of elements) {
interactable(el);
}
}
});
let canvas;
let element;
const mouse = {
x: 0,
y: 0,
startX: 0,
startY: 0
};
function mouseDown(e) {
if (e.target.id === "container") {
const rects = [...canvas.querySelectorAll(".selection")];
if (rects) {
for (const rect of rects) {
canvas.removeChild(rect);
}
}
mouse.startX = mouse.x;
mouse.startY = mouse.y;
element = document.createElement("div");
element.className = "selection";
element.style.border = "1px dashed black";
element.style.position = "absolute";
element.style.left = mouse.x + "px";
element.style.top = mouse.y + "px";
canvas.appendChild(element);
}
}
function setMousePosition(e) {
const ev = e || window.event;
if (ev.pageX) {
mouse.x = ev.pageX + window.pageXOffset;
mouse.y = ev.pageY + window.pageYOffset;
} else if (ev.clientX) {
mouse.x = ev.clientX + document.body.scrollLeft;
mouse.y = ev.clientY + document.body.scrollTop;
}
}
function mouseMove(e) {
setMousePosition(e);
if (element) {
element.style.width = Math.abs(mouse.x - mouse.startX) + "px";
element.style.height = Math.abs(mouse.y - mouse.startY) + "px";
element.style.left =
mouse.x - mouse.startX < 0 ? mouse.x + "px" : mouse.startX + "px";
element.style.top =
mouse.y - mouse.startY < 0 ? mouse.y + "px" : mouse.startY + "px";
}
}
function mouseUp(e) {
element = null;
const rect = canvas.querySelector(".selection");
const boxes = [...canvas.querySelectorAll(".item")];
if (rect) {
const inBounds = [];
for (const box of boxes) {
if (isInBounds(rect, box)) {
inBounds.push(box);
} else {
box.style.boxShadow = "none";
box.classList.remove("selected");
}
}
if (inBounds.length >= 2) {
for (const box of inBounds) {
box.style.boxShadow = "0 0 3pt 3pt hsl(141, 53%, 53%)";
box.classList.add("selected");
}
}
if (rect) canvas.removeChild(canvas.querySelector(".selection"));
}
}
function isInBounds(obj1, obj2) {
const a = obj1.getBoundingClientRect();
const b = obj2.getBoundingClientRect();
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
</script>
<style>
#container {
background: lightgray;
height: 100vh;
}
.item {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
background: lightblue;
width: 100px;
height: 100px;
cursor: pointer;
border-radius: 10px;
padding: 1em;
touch-action: none;
transition: box-shadow 0.5s;
}
.item:hover {
-webkit-box-shadow: 0px 10px 15px 0px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0px 10px 15px 0px rgba(0, 0, 0, 0.1);
box-shadow: 0px 10px 15px 0px rgba(0, 0, 0, 0.1);
}
.item:nth-child(1) {
top: 100px;
left: 100px;
}
.item:nth-child(2) {
top: 30px;
left: 300px;
}
.item:nth-child(3) {
top: 400px;
left: 320px;
}
.item:nth-child(4) {
top: 250px;
left: 200px;
}
</style>
<div bind:this={canvas} id="container" on:mouseup={mouseUp} on:mousedown={mouseDown} on:mousemove={mouseMove}>
<div class="item" data-x="0" data-y="0"><span>Item 1</span></div>
<div class="item" data-x="0" data-y="0"><span>Item 2</span></div>
<div class="item" data-x="0" data-y="0"><span>Item 3</span></div>
<div class="item" data-x="0" data-y="0"><span>Item 4</span></div>
</div>