forked from waqer/MouseTester
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (147 loc) · 6.52 KB
/
Copy pathscript.js
File metadata and controls
172 lines (147 loc) · 6.52 KB
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
const { createApp, ref, reactive, computed, watch } = Vue;
createApp({
setup() {
// Theme — detect system preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = ref(prefersDark ? 'dark' : 'light');
function toggleTheme() {
theme.value = theme.value === 'dark' ? 'light' : 'dark';
}
// Apply theme to <html> so CSS vars cascade properly
watch(theme, v => {
document.documentElement.setAttribute('data-theme', v);
}, { immediate: true });
// ── State ──
const threshold = ref(300);
const btns = reactive([
{ id: 0, name: 'Left', clicks: 0, ghosts: 0, lastDown: 0, minGap: null },
{ id: 1, name: 'Middle', clicks: 0, ghosts: 0, lastDown: 0, minGap: null },
{ id: 2, name: 'Right', clicks: 0, ghosts: 0, lastDown: 0, minGap: null },
]);
const log = ref([]);
const ripples = ref([]);
let logId = 0, ripId = 0;
const arenaClass = ref('s-idle');
const aIcon = ref('🖱');
const aHint = ref('Click anywhere — Left, Middle, or Right button auto-detected');
const aLast = ref('');
const showBanner = ref(false);
const bannerBtn = ref('');
const bannerGap = ref(0);
const logOpen = ref(false);
const statsOpen = ref(false);
const settingsOpen = ref(false);
let idleTimer = null;
let bannerTimer = null;
// ── Computed ──
const totalClicks = computed(() => btns.reduce((s, b) => s + b.clicks, 0));
const totalGhosts = computed(() => btns.reduce((s, b) => s + b.ghosts, 0));
const faultRate = computed(() => {
if (!totalClicks.value) return '—';
return ((totalGhosts.value / totalClicks.value) * 100).toFixed(1) + '%';
});
const fastestGhost = computed(() => {
const vals = btns.map(b => b.minGap).filter(v => v != null);
return vals.length ? Math.min(...vals) + 'ms' : '—';
});
// ── Card helpers ──
function verdict(b) {
if (!b.clicks) return 'Untested';
if (!b.ghosts) return 'Healthy';
const r = b.ghosts / b.clicks;
if (r < 0.03) return 'Minor';
if (r < 0.12) return 'Faulty';
return 'Failing';
}
function vClass(b) {
return { Untested: 'v-idle', Healthy: 'v-good', Minor: 'v-warn', Faulty: 'v-bad', Failing: 'v-bad' }[verdict(b)];
}
function cardBorder(b) {
return { Healthy: 'c-good', Minor: 'c-warn', Faulty: 'c-bad', Failing: 'c-bad' }[verdict(b)] || '';
}
function dotClass(b) {
return { Healthy: 'd-good', Minor: 'd-warn', Faulty: 'd-bad', Failing: 'd-bad' }[verdict(b)] || '';
}
// ── Click handler ──
function onMousedown(e) {
const id = e.button;
if (id > 2) return;
const b = btns[id];
const now = Date.now();
const gap = b.lastDown ? now - b.lastDown : null;
const isGhost = gap !== null && gap <= threshold.value;
b.clicks++;
const rect = e.currentTarget.getBoundingClientRect();
const rx = e.clientX - rect.left;
const ry = e.clientY - rect.top;
if (isGhost) {
b.ghosts++;
if (b.minGap === null || gap < b.minGap) b.minGap = gap;
// red ripple
const rId = ripId++;
ripples.value.push({ id: rId, x: rx, y: ry, s: 280, c: 'var(--bad-rpl)' });
setTimeout(() => { ripples.value = ripples.value.filter(r => r.id !== rId); }, 700);
arenaClass.value = 's-bad';
aIcon.value = '⚡';
aHint.value = `Ghost double-click — ${b.name} button`;
aLast.value = `${gap}ms interval (threshold ${threshold.value}ms)`;
// bottom banner
showBanner.value = true;
bannerBtn.value = b.name;
bannerGap.value = gap;
clearTimeout(bannerTimer);
bannerTimer = setTimeout(() => { showBanner.value = false; }, 3000);
clearTimeout(idleTimer);
idleTimer = setTimeout(resetArena, 3200);
} else {
// green ripple
const rId = ripId++;
ripples.value.push({ id: rId, x: rx, y: ry, s: 280, c: 'var(--good-rpl)' });
setTimeout(() => { ripples.value = ripples.value.filter(r => r.id !== rId); }, 700);
arenaClass.value = 's-good';
aIcon.value = '✓';
aHint.value = `${b.name} button — single click`;
aLast.value = gap ? `${gap}ms since last click` : 'First click recorded';
clearTimeout(idleTimer);
idleTimer = setTimeout(resetArena, 1800);
}
// Log
const t = new Date();
const time = t.toLocaleTimeString('en-GB', { hour12: false }) +
'.' + String(t.getMilliseconds()).padStart(3, '0');
log.value.unshift({
id: logId++, time, btn: b.name, ghost: isGhost,
gap: gap != null ? gap + 'ms' : '—'
});
if (log.value.length > 200) log.value = log.value.slice(0, 200);
b.lastDown = now;
}
function resetArena() {
arenaClass.value = 's-idle';
aIcon.value = '🖱';
aHint.value = totalClicks.value > 0
? 'Keep clicking — results update in real time'
: 'Click anywhere — Left, Middle, or Right button auto-detected';
aLast.value = '';
}
function reset() {
btns.forEach(b => { b.clicks = 0; b.ghosts = 0; b.lastDown = 0; b.minGap = null; });
log.value = [];
ripples.value = [];
showBanner.value = false;
clearTimeout(idleTimer);
clearTimeout(bannerTimer);
resetArena();
}
return {
theme, toggleTheme,
threshold, btns, log, ripples,
arenaClass, aIcon, aHint, aLast,
showBanner, bannerBtn, bannerGap,
logOpen, statsOpen, settingsOpen,
totalClicks, totalGhosts, faultRate, fastestGhost,
verdict, vClass, cardBorder, dotClass,
onMousedown, reset,
};
}
}).mount('#app');