-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlag-radar-ui.js
148 lines (145 loc) · 4.04 KB
/
lag-radar-ui.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
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
/**
* lagRadar
* Licence: ISC copyright: @mobz 2018
*/
export default function lagRadarUI() {
const global = this;
if(global._killLagRadarUI) {
global._killLagRadarUI();
return;
}
const $style = document.createElement('style');
$style.appendChild(document.createTextNode(`
BODY #lagRadarUI.lagRadarUI-radar {
font-family: Futura, "Trebuchet MS", sans-serif;
font-style: normal;
text-align: center;
font-size: 16px;
}
.lagRadarUI-radar {
position: fixed;
z-index: 999999;
top: 100px;
left: 100px;
user-select: none;
}
.lagRadarUI-radar.active {
outline: 3px dotted rgba(100,100,100,0.5);
cursor: ns-resize;
}
.lagRadarUI-radar.active .lagRadarUI-title,
.lagRadarUI-radar:hover .lagRadarUI-title {
visibility: visible;
}
.lagRadarUI-title {
position: absolute;
display: flex;
width: 210px;
left: calc(50% - 210px/2);
box-sizing: content-box;
height: 24px;
line-height: 24px;
top: calc(50% - 24px/2);
visibility: hidden;
color: #8f8;
background-color: #080;
border: 2px solid #0b0;
border-radius: 10px;
opacity: 0.85;
}
.lagRadarUI-title > * {
padding: 0 10px;
}
.lagRadarUI-title > .active,
.lagRadarUI-title > *:hover {
background: #4a0;
color: #efd;
}
.lagRadarUI-title > *:first-child {
border-radius: 8px 0 0 8px;
}
.lagRadarUI-title > *:last-child {
border-radius: 0 8px 8px 0;
}
.lagRadarUI-close {
cursor: pointer;
}
.lagRadarUI-label {
flex-grow: 1;
cursor: -webkit-grab;
cursor: grab;
}
.lagRadarUI-label.active {
cursor: -webkit-grabbing;
cursor: grabbing;
}
.lagRadarUI-rsize {
line-height: 19px;
cursor: ns-resize;
}
.lagRadarUI-sweep {
pointer-events: none;
}
`));
function $e(cls, children = []) {
const el = document.createElement('div');
el.className = 'lagRadarUI-' + cls;
children.forEach( child => el.appendChild( child ) );
return el;
}
const $close = $e('close', [ document.createTextNode('\u00D7') ]);
const $rsize = $e('rsize', [ document.createTextNode('\u25f3') ]);
const $label = $e('label', [ document.createTextNode('lag-radar' )]);
const $title = $e('title', [ $rsize, $label, $close ] );
const $sweep = $e('sweep');
const $radar = $e('radar', [ $style, $title, $sweep ] );
$radar.id = 'lagRadarUI';
const RESIZE = 1, MOVE = 2;
let mode = null, origSize = 300, size = 300, offsetX, offsetY;
const mouseDown = ev => {
if(mode === null) {
if(ev.target === $label) {
mode = MOVE;
offsetX = $radar.offsetLeft - ev.screenX;
offsetY = $radar.offsetTop - ev.screenY;
} else if(ev.target === $rsize) {
mode = RESIZE;
offsetY = size - ev.screenY;
$radar.classList.add('active');
}
ev.target.classList.add('active');
document.addEventListener('mousemove', mouseMove);
document.addEventListener('mouseup', mouseUp);
}
};
const mouseUp = ev => {
mode = null;
document.removeEventListener('mousemove', mouseMove);
document.removeEventListener('mouseup', mouseUp);
$label.classList.remove('active');
$rsize.classList.remove('active');
$radar.classList.remove('active');
};
const mouseMove = ev => {
if(mode === MOVE) {
$radar.style.left = offsetX + ev.screenX + 'px';
$radar.style.top = offsetY + ev.screenY + 'px';
} else if(mode === RESIZE) {
size = Math.max(16, offsetY + ev.screenY);
$radar.style.height = size + 'px';
$radar.style.width = size + 'px';
$sweep.style.zoom = (size / origSize);
}
};
$title.addEventListener('mousedown', mouseDown);
document.body.appendChild( $radar );
const destroy = lagRadar({
parent: $sweep
});
global._killLagRadarUI = function() {
$radar.remove();
destroy();
delete global._killLagRadarUI;
};
$close.addEventListener('click', global._killLagRadarUI );
};