-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchpad.html
321 lines (269 loc) · 9.18 KB
/
touchpad.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<html>
<head>
<title>Touchpad Gesture Practice</title>
<style>
.frame {
position: absolute;
left: calc(50% - 100px);
top: calc(50% - 100px);
width: 150px;
height: 150px;
display: flex;
background: #aaa;
align-content: center;
align-items: center;
border-radius: 20px;
font-size: 25px;
color: #fff;
opacity: 0.5;
border: 2px solid black;
}
.active {
background: yellow;
color: black;
opacity: 0.7;
font-size: 20px;
}
p {
margin: 0 auto;
text-align: center;
width: 80%;
user-select: none;
pointer-events: none;
}
body {
height: 100%;
font-family: Arial, Helvetica, sans-serif;
touch-action: none;
overflow: hidden;
margin: 0;
}
html {
margin: 0;
}
.target {
position: absolute;
color: white;
display: flex;
align-content: center;
align-items: center;
font-size: 100px;
}
.A {
background: #9b9;
border-radius: 1vh;
height: 10vh;
width: 10vh;
}
.B {
background: #b99;
border-radius: 2.5vh;
height: 25vh;
width: 25vh;
}
.C {
background: #99b;
border-radius: 5vh;
height: 50vh;
width: 50vh;
}
.instructions {
position: absolute;
bottom: 0;
right: 0;
left: 0;
height: 65px;
width: 100%;
display: flex;
align-content: center;
align-items: center;
background: black;
color: white;
font-size: 18px;
}
</style>
</head>
<body>
<div class="target A">
<p>A</p>
</div>
<div class="target B">
<p>B</p>
</div>
<div class="target C">
<p>C</p>
</div>
<div class="frame f1">
<p>(A)<br>Right-click to activate me.</p>
</div>
<div class="frame f2">
<p>(B)<br>Right-click to activate me.</p>
</div>
<div class="frame f3">
<p>(C)<br>Right-click to activate me.</p>
</div>
<div class="instructions">
<p>
Use your laptop's trackpad to move and resize the grey boxes to cover up the A, B, and C boxes.<br>
<b>Click here and take a screenshot when finished.</b>
</p>
</div>
<script>
var node;
var gestureStartScale = 0;
var scale = 1;
var posX = 0;
var posY = 0;
var startX;
var startY;
function checkVisible(elm) {
var rect = elm.getBoundingClientRect();
var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}
function collision($div1, $div2) {
var x1 = $div1.getBoundingClientRect().left;
var y1 = $div1.getBoundingClientRect().top;
var h1 = $div1.getBoundingClientRect().height;
var w1 = $div1.getBoundingClientRect().width;
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $div2.getBoundingClientRect().left;
var y2 = $div2.getBoundingClientRect().top;
var h2 = $div2.getBoundingClientRect().height;
var w2 = $div2.getBoundingClientRect().width;
var b2 = y2 + h2;
var r2 = x2 + w2;
if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}
$('.C').style.top = (Math.random() * 40 + 2) + 'vh';
$('.C').style.left = (Math.random() * 48 + 2) + 'vw';
while (true) {
$('.B').style.top = (Math.random() * 65 + 2) + 'vh';
$('.B').style.left = (Math.random() * 73 + 2) + 'vw';
if (!collision($('.B'), $('.C'))) break;
}
while (true) {
$('.A').style.top = (Math.random() * 80 + 2) + 'vh';
$('.A').style.left = (Math.random() * 88 + 2) + 'vw';
if (!collision($('.A'), $('.C')) && !collision($('.A'), $('.B'))) break;
}
let pw = window.innerWidth - 170;
let ph = window.innerHeight - 250;
while (true) {
let fx = Math.random() * pw + 30 - pw / 2;
let fy = Math.random() * ph - 32 - ph / 2;
$('.f1').style.transform = `translate3D(${fx}px, ${fy}px, 0px) scale(1)`;
$('.f1').user_posX = fx;
$('.f1').user_posY = fy;
if (!collision($('.f1'), $('.C')) && !collision($('.f1'), $('.B')) && !collision($('.f1'), $('.A'))) break;
}
while (true) {
let fx = Math.random() * pw + 30 - pw / 2;
let fy = Math.random() * ph - 32 - ph / 2;
$('.f2').style.transform = `translate3D(${fx}px, ${fy}px, 0px) scale(1)`;
$('.f2').user_posX = fx;
$('.f2').user_posY = fy;
if (!collision($('.f2'), $('.C')) && !collision($('.f2'), $('.B')) && !collision($('.f2'), $('.A')) && !collision($('.f2'), $('.f1'))) break;
}
while (true) {
let fx = Math.random() * pw + 30 - pw / 2;
let fy = Math.random() * ph - 32 - ph / 2;
$('.f3').style.transform = `translate3D(${fx}px, ${fy}px, 0px) scale(1)`;
$('.f3').user_posX = fx;
$('.f3').user_posY = fy;
if (!collision($('.f3'), $('.C')) && !collision($('.f3'), $('.B')) && !collision($('.f3'), $('.A')) && !collision($('.f3'), $('.f1')) && !collision($('.f2'), $('.f3'))) break;
}
function $(e_str) {
return document.querySelector(e_str);
}
var node = null; //$('.frame.f1')
var render = () => {
window.requestAnimationFrame(() => {
if (node) {
var val = `translate3D(${posX}px, ${posY}px, 0px) scale(${scale})`
node.style.transform = val
node.user_posX = posX;
node.user_posY = posY;
node.user_gest = gestureStartScale;
node.user_scale = scale;
}
})
}
function clearAll() {
$('.f1').innerHTML = '<p>(A)<br>Right-click to activate me.</p>';
$('.f2').innerHTML = '<p>(B)<br>Right-click to activate me.</p>';
$('.f3').innerHTML = '<p>(C)<br>Right-click to activate me.</p>';
$('.f1').classList.remove('active');
$('.f2').classList.remove('active');
$('.f3').classList.remove('active');
node = null;
}
['.f1', '.f2', '.f3'].forEach(e => {
$(e).addEventListener('contextmenu', evt => {
evt.preventDefault();
clearAll();
node = evt.target;
gestureStartScale = node.user_gest || 0;
scale = node.user_scale || 1;
posX = node.user_posX || 0;
posY = node.user_posY || 0;
evt.target.innerHTML = `<p>Move and resize me to cover box ${e == '.f1' ? 'A' : e == '.f2' ? 'B' : 'C'}</p>`;
evt.target.classList.add('active');
}, {
passive: false
});
});
$('.instructions').addEventListener('click', clearAll);
window.addEventListener('wheel', (e) => {
e.preventDefault();
let oldPosX = posX;
let oldPosY = posY;
if (e.ctrlKey) {
scale -= e.deltaY * 0.01;
} else {
posX -= e.deltaX * 2;
posY -= e.deltaY * 2;
}
if (window.innerWidth / 2 - Math.abs(posX) - 10 < 0) {
posX = oldPosX;
}
if (window.innerHeight / 2 - Math.abs(posY) - 10 < 0) {
posY = oldPosY;
}
render();
}, {
passive: false
});
window.addEventListener("gesturestart", function (e) {
e.preventDefault();
startX = e.pageX - posX;
startY = e.pageY - posY;
gestureStartScale = scale;
}, {
passive: false
});
window.addEventListener("gesturechange", function (e) {
e.preventDefault();
scale = gestureStartScale * e.scale;
let oldPosX = posX;
let oldPosY = posY;
posX = e.pageX - startX;
posY = e.pageY - startY;
if (window.innerWidth / 2 - Math.abs(posX) - 10 < 0) {
posY = oldPosY;
}
if (window.innerHeight / 2 - Math.abs(posY) - 10 < 0) {
posY = oldPosY;
}
render();
})
window.addEventListener("gestureend", function (e) {
e.preventDefault();
}, {
passive: false
});
</script>
</body>
</html>