Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/puppet/test/assets/input/mouse-helper.js
2631 views
1
// This injects a box into the page that moves with the mouse;
2
// Useful for debugging
3
(function(){
4
const box = document.createElement('div');
5
box.classList.add('mouse-helper');
6
const styleElement = document.createElement('style');
7
styleElement.innerHTML = `
8
.mouse-helper {
9
pointer-events: none;
10
position: absolute;
11
top: 0;
12
left: 0;
13
width: 20px;
14
height: 20px;
15
background: rgba(0,0,0,.4);
16
border: 1px solid white;
17
border-radius: 10px;
18
margin-left: -10px;
19
margin-top: -10px;
20
transition: background .2s, border-radius .2s, border-color .2s;
21
}
22
.mouse-helper.button-1 {
23
transition: none;
24
background: rgba(0,0,0,0.9);
25
}
26
.mouse-helper.button-2 {
27
transition: none;
28
border-color: rgba(0,0,255,0.9);
29
}
30
.mouse-helper.button-3 {
31
transition: none;
32
border-radius: 4px;
33
}
34
.mouse-helper.button-4 {
35
transition: none;
36
border-color: rgba(255,0,0,0.9);
37
}
38
.mouse-helper.button-5 {
39
transition: none;
40
border-color: rgba(0,255,0,0.9);
41
}
42
`;
43
document.head.appendChild(styleElement);
44
document.body.appendChild(box);
45
document.addEventListener('mousemove', event => {
46
box.style.left = `${event.pageX }px`;
47
box.style.top = `${event.pageY }px`;
48
updateButtons(event.buttons);
49
}, true);
50
document.addEventListener('mousedown', event => {
51
updateButtons(event.buttons);
52
box.classList.add(`button-${ event.which}`);
53
}, true);
54
document.addEventListener('mouseup', event => {
55
updateButtons(event.buttons);
56
box.classList.remove(`button-${ event.which}`);
57
}, true);
58
function updateButtons(buttons) {
59
for (let i = 0; i < 5; i++)
60
box.classList.toggle(`button-${ i}`, buttons & (1 << i));
61
}
62
})();
63
64