-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (95 loc) · 2.73 KB
/
Copy pathscript.js
File metadata and controls
120 lines (95 loc) · 2.73 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
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Stick positions
const leftCenter = { x: 200, y: 200 };
const rightCenter = { x: 500, y: 200 };
const radius = 120;
const deadzone = 50;
let trailL = [];
let trailR = [];
function drawCircleFrame(center) {
// Background fill
const gradient = ctx.createRadialGradient(
center.x, center.y, 10,
center.x, center.y, radius
);
gradient.addColorStop(0, "rgba(40,40,40,0.6)");
gradient.addColorStop(1, "rgba(0,0,0,0.9)");
ctx.beginPath();
ctx.arc(center.x, center.y, radius, 0, Math.PI * 2);
ctx.fillStyle = gradient;
ctx.fill();
// Outer ring
ctx.beginPath();
ctx.arc(center.x, center.y, radius, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(255,105,180,0.7)";
ctx.lineWidth = 3;
ctx.stroke();
// Deadzone
ctx.beginPath();
ctx.arc(center.x, center.y, deadzone, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(255,255,255,0.25)";
ctx.lineWidth = 2;
ctx.stroke();
}
function drawStick(center, x, y, trail, color) {
let px = center.x + x * radius;
let py = center.y + y * radius;
trail.push({ x: px, y: py });
if (trail.length > 30) trail.shift();
// Trail
ctx.beginPath();
for (let i = 0; i < trail.length; i++) {
if (i === 0) ctx.moveTo(trail[i].x, trail[i].y);
else ctx.lineTo(trail[i].x, trail[i].y);
}
ctx.strokeStyle = color;
ctx.globalAlpha = 0.25;
ctx.lineWidth = 2;
ctx.stroke();
ctx.globalAlpha = 1;
// Direction line
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.lineTo(px, py);
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.stroke();
// Dot
ctx.beginPath();
ctx.arc(px, py, 6, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
}
function update() {
const gp = navigator.getGamepads()[0];
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawCircleFrame(leftCenter);
drawCircleFrame(rightCenter);
if (gp) {
let l = normalizeStick(gp.axes[0] || 0, gp.axes[1] || 0);
let r = normalizeStick(gp.axes[2] || 0, gp.axes[3] || 0);
let lx = l.x;
let ly = l.y;
let rx = r.x;
let ry = r.y;
drawStick(leftCenter, lx, ly, trailL, "#ff69b4");
drawStick(rightCenter, rx, ry, trailR, "#00bfff");
document.getElementById("leftData").innerText =
`${lx.toFixed(2)}, ${ly.toFixed(2)}`;
document.getElementById("rightData").innerText =
`${rx.toFixed(2)}, ${ry.toFixed(2)}`;
}
function normalizeStick(x, y) {
const length = Math.sqrt(x * x + y * y);
if (length > 1) {
return {
x: x / length,
y: y / length
};
}
return { x, y };
}
setTimeout(update, 16); // ~60fps cap
}
update();