-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXBox.html
More file actions
546 lines (498 loc) · 28.9 KB
/
XBox.html
File metadata and controls
546 lines (498 loc) · 28.9 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>哈威SSR团队作品前端展示</title>
<!-- 引入SB Admin 2的CSS -->
<link href="css/sb-admin-2.min.css" rel="stylesheet">
<!-- 引入Font Awesome图标库 -->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<!-- 引入Toastr CSS(用于通知弹窗) -->
<link href="vendor/toastr/toastr.min.css" rel="stylesheet">
<!-- 引入Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<!-- 引入Toastr JS -->
<script src="vendor/toastr/toastr.min.js"></script>
<!-- 添加自定义CSS -->
<style>
/* 自定义图表容器样式 */
.chart-container {
width: 100%;
margin-bottom: 50px;
width: 500px;
height: 444px;
}
/* 使用Bootstrap的网格系统,将图表排列为一行两列 */
.row-cols-1.row-cols-md-2 {
display: flex;
flex-wrap: wrap;
}
.col-md-6 {
flex: 0 0 50%;
max-width: 50%;
padding: 10px;
}
/* 调整canvas大小以适应卡片 */
canvas {
max-width: 100%;
height: 300px; /* 固定高度以确保一致性 */
}
.m-gamepadItemJoysticks {
background: none;
padding: 20px;
margin-left: 333px;
}
button {
padding: 10px;
margin: 5px;
cursor: pointer;
}
</style>
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">
<!-- Sidebar Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="first_page.html">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
</div>
<div class="sidebar-brand-text mx-3"><font _mstmutation="1" _msttexthash="10666266" _msthash="1">SSR团队</font></div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item">
<a class="nav-link" href="first_page.html">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span _msttexthash="8308430" _msthash="3">首页展示</span></a>
</li>
<hr class="sidebar-divider">
<div class="sidebar-heading" _msttexthash="4554719" _msthash="4">数据图表</div>
<li class="nav-item">
<a class="nav-link" href="chart.html">
<i class="fas fa-fw fa-chart-area"></i>
<span _msttexthash="5658250" _msthash="24">图表</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="tables.html">
<i class="fas fa-fw fa-table"></i>
<span _msttexthash="3177720" _msthash="25">表格</span></a>
</li>
<!-- 你可以添加更多导航项 -->
<li class="nav-item active">
<a class="nav-link collapsed" href="XBox.html">
<i class="fas fa-fw fa-cog"></i>
<span _msttexthash="5055388" _msthash="5">手柄效果</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link collapsed" href="camera.html">
<i class="fas fa-fw fa-camera"></i>
<span _msttexthash="5055388" _msthash="5">摄像头读取</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://localhost:8001/vendor/three.js-r161/examples/sinmple.html">
<i class="fas fa-fw fa-wrench"></i>
<span>模型展示</span>
</a>
</li>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- 可添加更多内容 -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600 small" _msttexthash="22648327" _msthash="55">哈威SSR团队研发</span>
<img class="img-profile rounded-circle" src="img/picture.png">
</a>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800 text-center">欢迎使用手柄检测系统</h1>
<!-- 图表区域 -->
<!-- 游戏手柄控制区域 -->
<div class="m-gamepadItemJoysticks">
<svg width="700" viewBox="0 0 441 383" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="XBox">
<path id="LOutline" d="M220.5 294.5C220.5 294.5 195 294.5 150 294.5C105 294.5 81.5 378.5 49.5 378.5C17.5 378.5 4 363.9 4 317.5C4 271.1 43.5 165.5 55 137.5C66.5 109.5 95.5 92.0001 128 92.0001C154 92.0001 200.5 92.0001 220.5 92.0001" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></path>
<path id="ROutline" d="M220 294.5C220 294.5 245.5 294.5 290.5 294.5C335.5 294.5 359 378.5 391 378.5C423 378.5 436.5 363.9 436.5 317.5C436.5 271.1 397 165.5 385.5 137.5C374 109.5 345 92.0001 312.5 92.0001C286.5 92.0001 240 92.0001 220 92.0001" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></path>
<circle id="LStickOutline" cx="113" cy="160" r="37.5" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></circle>
<circle id="LeftStick" cx="113" cy="160" r="28" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></circle>
<circle id="RStickOutline" cx="278" cy="238" r="37.5" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></circle>
<circle id="RightStick" cx="278" cy="238" r="28" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></circle>
<circle id="DOutline" cx="166" cy="238" r="37.5" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></circle>
<g id="DUp">
<mask id="path-8-inside-1" fill="white">
<path d="M177.669 222.335C180.793 219.21 180.816 213.997 176.868 212.014C176.327 211.743 175.776 211.491 175.215 211.258C172.182 210.002 168.931 209.355 165.648 209.355C162.365 209.355 159.114 210.002 156.081 211.258C155.521 211.491 154.969 211.743 154.429 212.014C150.48 213.997 150.503 219.21 153.627 222.335L159.991 228.698C163.116 231.823 168.181 231.823 171.305 228.698L177.669 222.335Z"></path>
</mask>
<path d="M177.669 222.335C180.793 219.21 180.816 213.997 176.868 212.014C176.327 211.743 175.776 211.491 175.215 211.258C172.182 210.002 168.931 209.355 165.648 209.355C162.365 209.355 159.114 210.002 156.081 211.258C155.521 211.491 154.969 211.743 154.429 212.014C150.48 213.997 150.503 219.21 153.627 222.335L159.991 228.698C163.116 231.823 168.181 231.823 171.305 228.698L177.669 222.335Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-8-inside-1)"></path>
</g>
<g id="DRight">
<mask id="path-9-inside-2" fill="white">
<path d="M181.447 249.669C184.571 252.793 189.785 252.816 191.768 248.868C192.039 248.327 192.291 247.776 192.523 247.215C193.78 244.182 194.426 240.931 194.426 237.648C194.426 234.365 193.78 231.114 192.523 228.081C192.291 227.521 192.039 226.969 191.768 226.429C189.785 222.48 184.571 222.503 181.447 225.627L175.083 231.991C171.959 235.116 171.959 240.181 175.083 243.305L181.447 249.669Z"></path>
</mask>
<path d="M181.447 249.669C184.571 252.793 189.785 252.816 191.768 248.868C192.039 248.327 192.291 247.776 192.523 247.215C193.78 244.182 194.426 240.931 194.426 237.648C194.426 234.365 193.78 231.114 192.523 228.081C192.291 227.521 192.039 226.969 191.768 226.429C189.785 222.48 184.571 222.503 181.447 225.627L175.083 231.991C171.959 235.116 171.959 240.181 175.083 243.305L181.447 249.669Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-9-inside-2)"></path>
</g>
<g id="DDown">
<mask id="path-10-inside-3" fill="white">
<path d="M154.113 253.447C150.989 256.571 150.966 261.785 154.914 263.767C155.455 264.039 156.006 264.291 156.566 264.523C159.6 265.78 162.85 266.426 166.134 266.426C169.417 266.426 172.667 265.78 175.701 264.523C176.261 264.291 176.812 264.039 177.353 263.767C181.301 261.785 181.279 256.571 178.154 253.447L171.79 247.083C168.666 243.959 163.601 243.959 160.477 247.083L154.113 253.447Z"></path>
</mask>
<path d="M154.113 253.447C150.989 256.571 150.966 261.785 154.914 263.767C155.455 264.039 156.006 264.291 156.566 264.523C159.6 265.78 162.85 266.426 166.134 266.426C169.417 266.426 172.667 265.78 175.701 264.523C176.261 264.291 176.812 264.039 177.353 263.767C181.301 261.785 181.279 256.571 178.154 253.447L171.79 247.083C168.666 243.959 163.601 243.959 160.477 247.083L154.113 253.447Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-10-inside-3)"></path>
</g>
<g id="DLeft">
<mask id="path-11-inside-4" fill="white">
<path d="M150.335 226.113C147.21 222.989 141.997 222.966 140.014 226.914C139.743 227.455 139.491 228.006 139.258 228.566C138.002 231.6 137.355 234.85 137.355 238.134C137.355 241.417 138.002 244.667 139.258 247.701C139.491 248.261 139.743 248.812 140.014 249.353C141.997 253.301 147.21 253.279 150.335 250.154L156.698 243.79C159.823 240.666 159.823 235.601 156.698 232.477L150.335 226.113Z"></path>
</mask>
<path d="M150.335 226.113C147.21 222.989 141.997 222.966 140.014 226.914C139.743 227.455 139.491 228.006 139.258 228.566C138.002 231.6 137.355 234.85 137.355 238.134C137.355 241.417 138.002 244.667 139.258 247.701C139.491 248.261 139.743 248.812 140.014 249.353C141.997 253.301 147.21 253.279 150.335 250.154L156.698 243.79C159.823 240.666 159.823 235.601 156.698 232.477L150.335 226.113Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-11-inside-4)"></path>
</g>
<circle id="BOutline" cx="329" cy="160" r="37.5" stroke="hsl(210,50%,85%)" stroke-width="3" stroke-opacity="1"></circle>
<g id="BTop">
<mask id="path-13-inside-5" fill="white">
<path d="M340.669 144.335C343.793 141.21 343.816 135.997 339.868 134.014C339.327 133.743 338.776 133.491 338.215 133.258C335.182 132.002 331.931 131.355 328.648 131.355C325.365 131.355 322.114 132.002 319.081 133.258C318.521 133.491 317.969 133.743 317.429 134.014C313.48 135.997 313.503 141.21 316.627 144.335L322.991 150.698C326.116 153.823 331.181 153.823 334.305 150.698L340.669 144.335Z"></path>
</mask>
<path d="M340.669 144.335C343.793 141.21 343.816 135.997 339.868 134.014C339.327 133.743 338.776 133.491 338.215 133.258C335.182 132.002 331.931 131.355 328.648 131.355C325.365 131.355 322.114 132.002 319.081 133.258C318.521 133.491 317.969 133.743 317.429 134.014C313.48 135.997 313.503 141.21 316.627 144.335L322.991 150.698C326.116 153.823 331.181 153.823 334.305 150.698L340.669 144.335Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-13-inside-5)"></path>
</g>
<g id="BRight">
<mask id="path-14-inside-6" fill="white">
<path d="M344.447 171.669C347.571 174.793 352.785 174.816 354.768 170.868C355.039 170.327 355.291 169.776 355.523 169.215C356.78 166.182 357.426 162.931 357.426 159.648C357.426 156.365 356.78 153.114 355.523 150.081C355.291 149.521 355.039 148.969 354.768 148.429C352.785 144.48 347.571 144.503 344.447 147.627L338.083 153.991C334.959 157.116 334.959 162.181 338.083 165.305L344.447 171.669Z"></path>
</mask>
<path d="M344.447 171.669C347.571 174.793 352.785 174.816 354.768 170.868C355.039 170.327 355.291 169.776 355.523 169.215C356.78 166.182 357.426 162.931 357.426 159.648C357.426 156.365 356.78 153.114 355.523 150.081C355.291 149.521 355.039 148.969 354.768 148.429C352.785 144.48 347.571 144.503 344.447 147.627L338.083 153.991C334.959 157.116 334.959 162.181 338.083 165.305L344.447 171.669Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-14-inside-6)"></path>
</g>
<g id="BBottom">
<mask id="path-15-inside-7" fill="white">
<path d="M317.113 175.447C313.989 178.571 313.966 183.785 317.914 185.767C318.455 186.039 319.006 186.291 319.566 186.523C322.6 187.78 325.85 188.426 329.134 188.426C332.417 188.426 335.667 187.78 338.701 186.523C339.261 186.291 339.812 186.039 340.353 185.767C344.301 183.785 344.279 178.571 341.154 175.447L334.79 169.083C331.666 165.959 326.601 165.959 323.477 169.083L317.113 175.447Z"></path>
</mask>
<path d="M317.113 175.447C313.989 178.571 313.966 183.785 317.914 185.767C318.455 186.039 319.006 186.291 319.566 186.523C322.6 187.78 325.85 188.426 329.134 188.426C332.417 188.426 335.667 187.78 338.701 186.523C339.261 186.291 339.812 186.039 340.353 185.767C344.301 183.785 344.279 178.571 341.154 175.447L334.79 169.083C331.666 165.959 326.601 165.959 323.477 169.083L317.113 175.447Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-15-inside-7)"></path>
</g>
<g id="BLeft">
<mask id="path-16-inside-8" fill="white">
<path d="M313.335 148.113C310.21 144.989 304.997 144.966 303.014 148.914C302.743 149.455 302.491 150.006 302.258 150.566C301.002 153.6 300.355 156.851 300.355 160.134C300.355 163.417 301.002 166.668 302.258 169.701C302.491 170.261 302.743 170.812 303.014 171.353C304.997 175.301 310.21 175.279 313.335 172.154L319.698 165.79C322.823 162.666 322.823 157.601 319.698 154.477L313.335 148.113Z"></path>
</mask>
<path d="M313.335 148.113C310.21 144.989 304.997 144.966 303.014 148.914C302.743 149.455 302.491 150.006 302.258 150.566C301.002 153.6 300.355 156.851 300.355 160.134C300.355 163.417 301.002 166.668 302.258 169.701C302.491 170.261 302.743 170.812 303.014 171.353C304.997 175.301 310.21 175.279 313.335 172.154L319.698 165.79C322.823 162.666 322.823 157.601 319.698 154.477L313.335 148.113Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="6" mask="url(#path-16-inside-8)"></path>
</g>
<g id="LMeta">
<circle cx="185" cy="162" r="10" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></circle>
</g>
<g id="RMeta">
<circle cx="259" cy="162" r="10" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></circle>
</g>
<rect id="L1" x="111.5" y="61.5" width="41" height="13" rx="6.5" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></rect>
<rect id="R1" x="289.5" y="61.5" width="41" height="13" rx="6.5" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></rect>
<path id="L2" d="M152.5 37C152.5 41.1421 149.142 44.5 145 44.5H132C127.858 44.5 124.5 41.1421 124.5 37V16.5C124.5 8.76801 130.768 2.5 138.5 2.5C146.232 2.5 152.5 8.76801 152.5 16.5V37Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></path>
<path id="R2" d="M317.5 37C317.5 41.1421 314.142 44.5 310 44.5H297C292.858 44.5 289.5 41.1421 289.5 37V16.5C289.5 8.76801 295.768 2.5 303.5 2.5C311.232 2.5 317.5 8.76801 317.5 16.5V37Z" fill="rgba(0,0,0,0)" stroke="rgba(0,0,0,1)" stroke-width="3"></path>
<line x1="30" y1="210" x2="130" y2="300" stroke-width="3" stroke="hsl(210,50%,85%)" opacity="0.3"></line>
<line x1="411" y1="210" x2="311" y2="300" stroke-width="3" stroke="hsl(210,50%,85%)" opacity="0.3"></line>
</g>
</svg>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<!-- Footer -->
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>© 2025 哈威SSR团队研发</span>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- 引入必要的JavaScript库 -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="js/sb-admin-2.min.js"></script>
<!-- 添加自定义脚本 -->
<script>
// 获取所有需要操作的元素
const elements = {
leftStick: document.getElementById('LeftStick'),
rightStick: document.getElementById('RightStick'),
dUp: document.querySelector('#DUp > path'),
dRight: document.querySelector('#DRight > path'),
dDown: document.querySelector('#DDown > path'),
dLeft: document.querySelector('#DLeft > path'),
bTop: document.querySelector('#BTop > path'),
bRight: document.querySelector('#BRight > path'),
bBottom: document.querySelector('#BBottom > path'),
bLeft: document.querySelector('#BLeft > path'),
LMeta: document.querySelector('#LMeta > circle'),
RMeta: document.querySelector('#RMeta > circle'),
L1: document.getElementById('L1'),
R1: document.getElementById('R1'),
L2: document.getElementById('L2'),
R2: document.getElementById('R2')
};
/**
* 解析颜色字符串为 RGBA 组件
* 支持 #RRGGBB, #RRGGBBAA, rgb(...), rgba(...)
* @param {string} color - 颜色字符串
* @returns {object} - { r, g, b, a }
*/
function parseColor(color) {
let r, g, b, a = 1;
// 处理 16 进制颜色
if (color.startsWith('#')) {
let hex = color.slice(1);
if (hex.length === 3) {
// #RGB
r = parseInt(hex[0] + hex[0], 16);
g = parseInt(hex[1] + hex[1], 16);
b = parseInt(hex[2] + hex[2], 16);
} else if (hex.length === 6) {
// #RRGGBB
r = parseInt(hex.slice(0, 2), 16);
g = parseInt(hex.slice(2, 4), 16);
b = parseInt(hex.slice(4, 6), 16);
} else if (hex.length === 8) {
// #RRGGBBAA
r = parseInt(hex.slice(0, 2), 16);
g = parseInt(hex.slice(2, 4), 16);
b = parseInt(hex.slice(4, 6), 16);
a = parseInt(hex.slice(6, 8), 16) / 255;
} else {
throw new Error('不支持的16进制颜色格式');
}
}
// 处理 rgb() 或 rgba()
else if (color.startsWith('rgb')) {
const regex = /rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(?:\s*,\s*(\d*\.?\d+))?\)/;
const result = regex.exec(color);
if (result) {
r = parseInt(result[1], 10);
g = parseInt(result[2], 10);
b = parseInt(result[3], 10);
if (result[4] !== undefined) {
a = parseFloat(result[4]);
}
} else {
throw new Error('不支持的 RGB/RGBA 颜色格式');
}
}
else {
throw new Error('不支持的颜色格式');
}
return { r, g, b, a };
}
/**
* 将 RGBA 组件转换为 RGBA 字符串
* @param {object} rgba - { r, g, b, a }
* @returns {string} - rgba(...) 格式字符串
*/
function rgbaToString(rgba) {
return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`;
}
/**
* 将 RGBA 组件转换为 16 进制字符串
* @param {object} rgba - { r, g, b, a }
* @param {boolean} includeAlpha - 是否包含 alpha 通道
* @returns {string} - #RRGGBB 或 #RRGGBBAA 格式字符串
*/
function rgbaToHex(rgba, includeAlpha = false) {
const toHex = (c) => {
const hex = c.toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
let hex = `#${toHex(rgba.r)}${toHex(rgba.g)}${toHex(rgba.b)}`;
if (includeAlpha) {
const alpha = Math.round(rgba.a * 255);
hex += toHex(alpha);
}
return hex.toUpperCase();
}
/**
* 插值计算颜色
* @param {string} startColor - 起始颜色(支持 #RRGGBB, #RRGGBBAA, rgb(...), rgba(...))
* @param {string} endColor - 终止颜色(支持 #RRGGBB, #RRGGBBAA, rgb(...), rgba(...))
* @param {number} percentage - 百分比(0-100)
* @param {string} outputFormat - 输出格式('rgba' 或 'hex')
* @returns {string} - 插值后的颜色字符串
*/
function interpolateColor(startColor, endColor, percentage, outputFormat = 'rgba') {
// 确保百分比在 0 到 100 之间
percentage = Math.max(0, Math.min(100, percentage));
const p = percentage / 100;
// 解析颜色
const start = parseColor(startColor);
const end = parseColor(endColor);
// 计算插值后的 RGBA 组件
const r = Math.round(start.r + (end.r - start.r) * p);
const g = Math.round(start.g + (end.g - start.g) * p);
const b = Math.round(start.b + (end.b - start.b) * p);
const a = parseFloat((start.a + (end.a - start.a) * p).toFixed(2));
const interpolated = { r, g, b, a };
// 返回指定格式的颜色
if (outputFormat === 'hex') {
return rgbaToHex(interpolated, endColor.length === 9); // 判断是否需要包含 alpha
} else {
return rgbaToString(interpolated);
}
}
// 初始位置记录
const originalPositions = {
leftStick: { cx: 113, cy: 160 },
rightStick: { cx: 278, cy: 238 }
};
// 移动左摇杆
function moveLeftStick(cx, cy) {
elements.leftStick.setAttribute('cx', cx * 12 + originalPositions.leftStick.cx);
elements.leftStick.setAttribute('cy', cy * 12 + originalPositions.leftStick.cy);
const propertion = Math.sqrt(cx ** 2 + cy ** 2);
const color = interpolateColor('rgba(0,0,0,0)', '#201818', propertion * 100);
elements.leftStick.setAttribute('fill', color)
}
// 移动右摇杆
function moveRightStick(cx, cy) {
elements.rightStick.setAttribute('cx', cx * 12 + originalPositions.rightStick.cx);
elements.rightStick.setAttribute('cy', cy * 12 + originalPositions.rightStick.cy);
const propertion = Math.sqrt(cx ** 2 + cy ** 2);
const color = interpolateColor('rgba(0,0,0,0)', '#201818', propertion * 100);
elements.rightStick.setAttribute('fill', color)
}
// 改变LT, RT键颜色
function changeLTRTColor([LT, RT]) {
const colors = [
interpolateColor('rgba(0,0,0,0)', '#201818', (LT + 1) * 50),
interpolateColor('rgba(0,0,0,0)', '#201818', (RT + 1) * 50)
];
elements.L2.setAttribute('fill', colors[0]);
elements.R2.setAttribute('fill', colors[1]);
}
// 改变LB, RB键颜色
function changeLBRBColor([LB, RB]) {
const colors = {
active: '#201818',
inactive: 'rgba(0,0,0,0)'
}
elements.L1.setAttribute('fill', LB == 1 ? colors.active : colors.inactive);
elements.R1.setAttribute('fill', RB == 1 ? colors.active : colors.inactive);
}
// 改变方向键颜色
function changeDpadColor([X, Y]) {
const colors = {
active: '#201818',
inactive: 'rgba(0,0,0,0)'
};
elements.dUp.setAttribute('fill', Y == 1 ? colors.active : colors.inactive);
elements.dRight.setAttribute('fill', X == 1 ? colors.active : colors.inactive);
elements.dDown.setAttribute('fill', Y == -1 ? colors.active : colors.inactive);
elements.dLeft.setAttribute('fill', X == -1 ? colors.active : colors.inactive);
}
// 改变ABXY键颜色
function changeBpadColor([A, B, X, Y]) {
const colors = {
active: '#201818',
inactive: 'rgba(0,0,0,0)'
};
elements.bTop.setAttribute('fill', Y ? colors.active : colors.inactive);
elements.bRight.setAttribute('fill', B ? colors.active : colors.inactive);
elements.bBottom.setAttribute('fill', A ? colors.active : colors.inactive);
elements.bLeft.setAttribute('fill', X ? colors.active : colors.inactive);
}
// 改变Start, back键颜色
function changeStartBackColor([start, back]) {
elements.LMeta.setAttribute('fill', start == 1 ? '#201818' : 'rgba(0,0,0,0)');
elements.RMeta.setAttribute('fill', back == 1 ? '#201818' : 'rgba(0,0,0,0)');
}
// 复位所有元素
function resetAll() {
// 复位左摇杆
elements.leftStick.setAttribute('cx', originalPositions.leftStick.cx);
elements.leftStick.setAttribute('cy', originalPositions.leftStick.cy);
elements.leftStick.setAttribute('stroke', 'rgba(0,0,0,1)');
// 复位方向键
const dpadElements = [elements.dUp, elements.dRight, elements.dDown, elements.dLeft];
dpadElements.forEach(el => {
el.style.stroke = 'rgba(0,0,0,1)';
el.style.strokeWidth = '6';
});
// 复位B按钮
const bButtons = [elements.bTop, elements.bRight, elements.bBottom, elements.bLeft];
bButtons.forEach(el => {
el.style.transform = 'rotate(0deg)';
el.style.stroke = 'rgba(0,0,0,1)';
});
}
// 定义GraphQL查询
const GET_JOYSTICK_DATA = `
query {
joystick {
available
axes
buttons
hats
}
}
`;
// 使用fetch发送GraphQL请求
function fetchJoystickData() {
fetch('http://localhost:5000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: GET_JOYSTICK_DATA,
}),
})
.then(response => response.json())
.then(data => {
const joystickData = data.data.joystick;
if (!joystickData.available) {
console.log('Joystick not available');
return;
}
else {
// 更新左摇杆位置
moveLeftStick(joystickData.axes[0], joystickData.axes[1]);
// 更新右摇杆位置
moveRightStick(joystickData.axes[2], joystickData.axes[3]);
// 更新方向键颜色
changeDpadColor(joystickData.hats);
// 更新ABXY键颜色
changeBpadColor(joystickData.buttons.slice(0, 4));
// 更新Start, back键颜色
changeStartBackColor(joystickData.buttons.slice(6, 8));
// 更新LB, RB键颜色
changeLBRBColor(joystickData.buttons.slice(4, 6));
// 更新LT, RT键颜色
changeLTRTColor(joystickData.axes.slice(4, 6));
}
})
.catch(error => {
console.error('Error fetching joystick data:', error);
});
}
// 设置定时器,每隔0.01秒调用一次GraphQL查询
setInterval(fetchJoystickData, 10); // 1000ms = 1秒
</script>
</body>
</html>