samlax12 commited on
Commit
a77c389
·
verified ·
1 Parent(s): f7108d0

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +761 -0
index.html ADDED
@@ -0,0 +1,761 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
6
+ <title>AR美甲工作室</title>
7
+ <script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js" crossorigin="anonymous"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
10
+ <script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
11
+ <style>
12
+ * {
13
+ margin: 0;
14
+ padding: 0;
15
+ box-sizing: border-box;
16
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
17
+ }
18
+ body {
19
+ background-color: #f8f8f8;
20
+ overflow: hidden;
21
+ position: fixed;
22
+ width: 100%;
23
+ height: 100%;
24
+ touch-action: none;
25
+ }
26
+ .container {
27
+ position: relative;
28
+ width: 100%;
29
+ height: 100%;
30
+ overflow: hidden;
31
+ }
32
+ .input_video {
33
+ position: absolute;
34
+ width: 100%;
35
+ height: 100%;
36
+ left: 0;
37
+ top: 0;
38
+ object-fit: cover;
39
+ opacity: 0;
40
+ pointer-events: none;
41
+ }
42
+ .output_canvas {
43
+ position: absolute;
44
+ width: 100%;
45
+ height: 100%;
46
+ left: 0;
47
+ top: 0;
48
+ object-fit: cover;
49
+ }
50
+ .controls-container {
51
+ position: absolute;
52
+ bottom: 0;
53
+ left: 0;
54
+ width: 100%;
55
+ padding: 20px;
56
+ background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
57
+ z-index: 10;
58
+ }
59
+ .controls-content {
60
+ max-width: 500px;
61
+ margin: 0 auto;
62
+ }
63
+ .control-section {
64
+ margin-bottom: 25px;
65
+ }
66
+ .section-title {
67
+ color: white;
68
+ font-size: 14px;
69
+ font-weight: 600;
70
+ margin-bottom: 12px;
71
+ letter-spacing: 0.5px;
72
+ text-transform: uppercase;
73
+ }
74
+ .design-options {
75
+ display: flex;
76
+ overflow-x: auto;
77
+ padding-bottom: 8px;
78
+ gap: 12px;
79
+ scrollbar-width: none;
80
+ }
81
+ .design-options::-webkit-scrollbar {
82
+ display: none;
83
+ }
84
+ .design-option {
85
+ width: 60px;
86
+ height: 60px;
87
+ border-radius: 12px;
88
+ overflow: hidden;
89
+ position: relative;
90
+ border: 2px solid transparent;
91
+ transition: all 0.2s ease;
92
+ flex-shrink: 0;
93
+ }
94
+ .design-option.selected {
95
+ border-color: #fff;
96
+ transform: scale(1.05);
97
+ box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
98
+ }
99
+ .slider-container {
100
+ margin: 15px 0;
101
+ color: white;
102
+ }
103
+ .slider-label {
104
+ display: flex;
105
+ justify-content: space-between;
106
+ font-size: 12px;
107
+ margin-bottom: 8px;
108
+ }
109
+ .slider {
110
+ -webkit-appearance: none;
111
+ appearance: none;
112
+ width: 100%;
113
+ height: 4px;
114
+ background: rgba(255, 255, 255, 0.3);
115
+ border-radius: 2px;
116
+ outline: none;
117
+ }
118
+ .slider::-webkit-slider-thumb {
119
+ -webkit-appearance: none;
120
+ appearance: none;
121
+ width: 18px;
122
+ height: 18px;
123
+ border-radius: 50%;
124
+ background: white;
125
+ cursor: pointer;
126
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
127
+ }
128
+ .toggle-section {
129
+ display: flex;
130
+ justify-content: space-between;
131
+ align-items: center;
132
+ }
133
+ .toggle-container {
134
+ display: flex;
135
+ align-items: center;
136
+ }
137
+ .toggle-label {
138
+ color: white;
139
+ font-size: 14px;
140
+ margin-right: 10px;
141
+ }
142
+ .toggle {
143
+ position: relative;
144
+ width: 46px;
145
+ height: 24px;
146
+ border-radius: 12px;
147
+ background-color: rgba(255, 255, 255, 0.3);
148
+ transition: all 0.3s ease;
149
+ }
150
+ .toggle.active {
151
+ background-color: #3a86ff;
152
+ }
153
+ .toggle-handle {
154
+ position: absolute;
155
+ top: 2px;
156
+ left: 2px;
157
+ width: 20px;
158
+ height: 20px;
159
+ border-radius: 50%;
160
+ background-color: white;
161
+ transition: all 0.3s ease;
162
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
163
+ }
164
+ .toggle.active .toggle-handle {
165
+ left: 24px;
166
+ }
167
+ .snap-button {
168
+ position: absolute;
169
+ bottom: 240px;
170
+ left: 50%;
171
+ transform: translateX(-50%);
172
+ width: 64px;
173
+ height: 64px;
174
+ border-radius: 50%;
175
+ background-color: white;
176
+ display: flex;
177
+ justify-content: center;
178
+ align-items: center;
179
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
180
+ z-index: 20;
181
+ border: none;
182
+ outline: none;
183
+ }
184
+ .snap-button:active {
185
+ transform: translateX(-50%) scale(0.95);
186
+ }
187
+ .snap-button::after {
188
+ content: '';
189
+ width: 52px;
190
+ height: 52px;
191
+ border-radius: 50%;
192
+ border: 2px solid #f8f8f8;
193
+ }
194
+ .camera-flip {
195
+ position: absolute;
196
+ top: 20px;
197
+ right: 20px;
198
+ background: rgba(0, 0, 0, 0.5);
199
+ color: white;
200
+ border: none;
201
+ border-radius: 50%;
202
+ width: 40px;
203
+ height: 40px;
204
+ display: flex;
205
+ justify-content: center;
206
+ align-items: center;
207
+ z-index: 20;
208
+ font-size: 18px;
209
+ }
210
+ .loading-screen {
211
+ position: fixed;
212
+ top: 0;
213
+ left: 0;
214
+ width: 100%;
215
+ height: 100%;
216
+ background-color: #000;
217
+ display: flex;
218
+ flex-direction: column;
219
+ justify-content: center;
220
+ align-items: center;
221
+ z-index: 9999;
222
+ transition: opacity 0.5s ease;
223
+ }
224
+ .loading-spinner {
225
+ width: 50px;
226
+ height: 50px;
227
+ border: 3px solid rgba(255, 255, 255, 0.2);
228
+ border-radius: 50%;
229
+ border-top-color: white;
230
+ animation: spin 1s linear infinite;
231
+ margin-bottom: 20px;
232
+ }
233
+ .loading-text {
234
+ color: white;
235
+ font-size: 18px;
236
+ font-weight: 500;
237
+ }
238
+ @keyframes spin {
239
+ to {
240
+ transform: rotate(360deg);
241
+ }
242
+ }
243
+ .app-title {
244
+ position: absolute;
245
+ top: 20px;
246
+ left: 20px;
247
+ color: white;
248
+ font-size: 18px;
249
+ font-weight: 600;
250
+ text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
251
+ z-index: 20;
252
+ }
253
+
254
+ .color-block {
255
+ width: 100%;
256
+ height: 100%;
257
+ }
258
+
259
+ .camera-permission {
260
+ position: fixed;
261
+ top: 0;
262
+ left: 0;
263
+ width: 100%;
264
+ height: 100%;
265
+ background-color: rgba(0, 0, 0, 0.8);
266
+ display: flex;
267
+ flex-direction: column;
268
+ justify-content: center;
269
+ align-items: center;
270
+ z-index: 9998;
271
+ color: white;
272
+ text-align: center;
273
+ padding: 20px;
274
+ }
275
+
276
+ .camera-permission h2 {
277
+ margin-bottom: 20px;
278
+ font-size: 24px;
279
+ }
280
+
281
+ .camera-permission p {
282
+ margin-bottom: 30px;
283
+ font-size: 16px;
284
+ max-width: 500px;
285
+ }
286
+
287
+ .allow-camera-btn {
288
+ background-color: #3a86ff;
289
+ color: white;
290
+ border: none;
291
+ padding: 12px 24px;
292
+ border-radius: 24px;
293
+ font-size: 16px;
294
+ font-weight: 500;
295
+ cursor: pointer;
296
+ }
297
+
298
+ .debug-info {
299
+ position: fixed;
300
+ top: 70px;
301
+ left: 20px;
302
+ color: white;
303
+ background-color: rgba(0,0,0,0.7);
304
+ padding: 10px;
305
+ border-radius: 5px;
306
+ font-size: 12px;
307
+ z-index: 100;
308
+ max-width: 300px;
309
+ display: none;
310
+ }
311
+ </style>
312
+ </head>
313
+
314
+ <body>
315
+ <div class="loading-screen">
316
+ <div class="loading-spinner"></div>
317
+ <div class="loading-text">正在加载AR美甲工作室...</div>
318
+ </div>
319
+
320
+ <div class="camera-permission" id="cameraPermission">
321
+ <h2>需要摄像头权限</h2>
322
+ <p>AR美甲工作室需要使用您的摄像头来实时显示美甲效果。请点击下方按钮允许使用摄像头。</p>
323
+ <button class="allow-camera-btn" id="allowCameraBtn">允许使用摄像头</button>
324
+ </div>
325
+
326
+ <div class="debug-info" id="debugInfo"></div>
327
+
328
+ <div class="container">
329
+ <video class="input_video" playsinline></video>
330
+ <canvas class="output_canvas"></canvas>
331
+
332
+ <div class="app-title">AR美甲工作室</div>
333
+
334
+ <button class="camera-flip">
335
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
336
+ <path d="M9 16V10H15"></path>
337
+ <path d="M15 16L9 10"></path>
338
+ <path d="M12 3C16.971 3 21 7.029 21 12C21 16.971 16.971 21 12 21C7.029 21 3 16.971 3 12"></path>
339
+ <path d="M3 4V8H7"></path>
340
+ </svg>
341
+ </button>
342
+
343
+ <button class="snap-button"></button>
344
+
345
+ <div class="controls-container">
346
+ <div class="controls-content">
347
+ <div class="control-section">
348
+ <div class="section-title">美甲设计</div>
349
+ <div class="design-options">
350
+ <div class="design-option selected" data-design="pink">
351
+ <div class="color-block" style="background: linear-gradient(45deg, #ff9a9e, #fad0c4);"></div>
352
+ </div>
353
+ <div class="design-option" data-design="blue">
354
+ <div class="color-block" style="background: linear-gradient(45deg, #2193b0, #6dd5ed);"></div>
355
+ </div>
356
+ <div class="design-option" data-design="purple">
357
+ <div class="color-block" style="background: linear-gradient(45deg, #c471f5, #fa71cd);"></div>
358
+ </div>
359
+ <div class="design-option" data-design="gold">
360
+ <div class="color-block" style="background: linear-gradient(45deg, #f6d365, #fda085);"></div>
361
+ </div>
362
+ <div class="design-option" data-design="green">
363
+ <div class="color-block" style="background: linear-gradient(45deg, #43c6ac, #f8ffae);"></div>
364
+ </div>
365
+ <div class="design-option" data-design="black">
366
+ <div class="color-block" style="background: linear-gradient(45deg, #232526, #414345);"></div>
367
+ </div>
368
+ </div>
369
+ </div>
370
+
371
+ <div class="control-section">
372
+ <div class="section-title">调整</div>
373
+
374
+ <div class="slider-container">
375
+ <div class="slider-label">
376
+ <span>大小</span>
377
+ <span id="size-value">100%</span>
378
+ </div>
379
+ <input type="range" min="50" max="150" value="100" class="slider" id="size-slider">
380
+ </div>
381
+
382
+ <div class="slider-container">
383
+ <div class="slider-label">
384
+ <span>长度</span>
385
+ <span id="length-value">100%</span>
386
+ </div>
387
+ <input type="range" min="80" max="200" value="100" class="slider" id="length-slider">
388
+ </div>
389
+
390
+ <div class="slider-container">
391
+ <div class="slider-label">
392
+ <span>透明度</span>
393
+ <span id="opacity-value">100%</span>
394
+ </div>
395
+ <input type="range" min="30" max="100" value="100" class="slider" id="opacity-slider">
396
+ </div>
397
+ </div>
398
+
399
+ <div class="control-section toggle-section">
400
+ <div class="toggle-container">
401
+ <span class="toggle-label">真实阴影</span>
402
+ <div class="toggle active" id="shadow-toggle">
403
+ <div class="toggle-handle"></div>
404
+ </div>
405
+ </div>
406
+
407
+ <div class="toggle-container">
408
+ <span class="toggle-label">显示手部轮廓</span>
409
+ <div class="toggle" id="lines-toggle">
410
+ <div class="toggle-handle"></div>
411
+ </div>
412
+ </div>
413
+ </div>
414
+ </div>
415
+ </div>
416
+ </div>
417
+
418
+ <script>
419
+ // 定义美甲颜色及其属性
420
+ const nailDesigns = {
421
+ pink: {
422
+ color: 'linear-gradient(45deg, #ff9a9e, #fad0c4)',
423
+ shadowColor: 'rgba(255, 154, 158, 0.5)'
424
+ },
425
+ blue: {
426
+ color: 'linear-gradient(45deg, #2193b0, #6dd5ed)',
427
+ shadowColor: 'rgba(33, 147, 176, 0.5)'
428
+ },
429
+ purple: {
430
+ color: 'linear-gradient(45deg, #c471f5, #fa71cd)',
431
+ shadowColor: 'rgba(196, 113, 245, 0.5)'
432
+ },
433
+ gold: {
434
+ color: 'linear-gradient(45deg, #f6d365, #fda085)',
435
+ shadowColor: 'rgba(246, 211, 101, 0.5)'
436
+ },
437
+ green: {
438
+ color: 'linear-gradient(45deg, #43c6ac, #f8ffae)',
439
+ shadowColor: 'rgba(67, 198, 172, 0.5)'
440
+ },
441
+ black: {
442
+ color: 'linear-gradient(45deg, #232526, #414345)',
443
+ shadowColor: 'rgba(35, 37, 38, 0.5)'
444
+ }
445
+ };
446
+
447
+ // 初始化变量
448
+ let currentDesign = 'pink';
449
+ let sizeScale = 1.0;
450
+ let lengthScale = 1.0;
451
+ let opacity = 1.0;
452
+ let showShadows = true;
453
+ let showLines = false;
454
+ let facingMode = 'environment'; // 'user' = 前置摄像头, 'environment' = 后置摄像头
455
+ let camera;
456
+ let cameraInitialized = false;
457
+
458
+ // 初始化MediaPipe Hands实例
459
+ let hands;
460
+
461
+ // 调试工具
462
+ const debugInfo = document.getElementById('debugInfo');
463
+ function showDebug(text) {
464
+ debugInfo.style.display = 'block';
465
+ debugInfo.textContent = text;
466
+ }
467
+
468
+ // 获取UI元素
469
+ const videoElement = document.getElementsByClassName('input_video')[0];
470
+ const canvasElement = document.getElementsByClassName('output_canvas')[0];
471
+ const cameraPermissionElement = document.getElementById('cameraPermission');
472
+ const allowCameraButton = document.getElementById('allowCameraBtn');
473
+
474
+ // 设置画布尺寸
475
+ canvasElement.width = window.innerWidth;
476
+ canvasElement.height = window.innerHeight;
477
+ const canvasCtx = canvasElement.getContext('2d');
478
+
479
+ // 获取其他UI元素
480
+ const sizeSlider = document.getElementById('size-slider');
481
+ const sizeValue = document.getElementById('size-value');
482
+ const lengthSlider = document.getElementById('length-slider');
483
+ const lengthValue = document.getElementById('length-value');
484
+ const opacitySlider = document.getElementById('opacity-slider');
485
+ const opacityValue = document.getElementById('opacity-value');
486
+ const shadowToggle = document.getElementById('shadow-toggle');
487
+ const linesToggle = document.getElementById('lines-toggle');
488
+ const flipButton = document.querySelector('.camera-flip');
489
+ const snapButton = document.querySelector('.snap-button');
490
+ const designOptions = document.querySelectorAll('.design-option');
491
+
492
+ // 隐藏加载屏幕当页面准备就绪
493
+ document.addEventListener('DOMContentLoaded', function() {
494
+ document.querySelector('.loading-screen').style.opacity = 0;
495
+ setTimeout(() => {
496
+ document.querySelector('.loading-screen').style.display = 'none';
497
+ }, 500);
498
+ });
499
+
500
+ // 摄像头权限按钮点击事件
501
+ allowCameraButton.addEventListener('click', () => {
502
+ initializeARApp();
503
+ });
504
+
505
+ // 为指尖应用美甲设计的函数
506
+ function applyNailDesign(ctx, fingerTip, fingerBase, design) {
507
+ // 提取坐标
508
+ const tipX = fingerTip.x * canvasElement.width;
509
+ const tipY = fingerTip.y * canvasElement.height;
510
+ const baseX = fingerBase.x * canvasElement.width;
511
+ const baseY = fingerBase.y * canvasElement.height;
512
+
513
+ // 计算指甲参数
514
+ const fingerAngle = Math.atan2(tipY - baseY, tipX - baseX);
515
+ const fingerLength = Math.sqrt(Math.pow(tipX - baseX, 2) + Math.pow(tipY - baseY, 2));
516
+
517
+ // 指甲的大小(通过滑块调整)
518
+ const nailWidth = fingerLength * 0.6 * sizeScale;
519
+ const nailLength = fingerLength * 0.7 * lengthScale;
520
+
521
+ // 保存上下文状态
522
+ ctx.save();
523
+
524
+ // 移动到指尖并旋转
525
+ ctx.translate(tipX, tipY);
526
+ ctx.rotate(fingerAngle);
527
+
528
+ // 如果启用,绘制指甲阴影
529
+ if (showShadows) {
530
+ ctx.save();
531
+ ctx.shadowColor = nailDesigns[design].shadowColor;
532
+ ctx.shadowBlur = 15;
533
+ ctx.shadowOffsetX = 0;
534
+ ctx.shadowOffsetY = 5;
535
+ ctx.fillStyle = 'rgba(0,0,0,0)';
536
+ ctx.beginPath();
537
+ ctx.ellipse(0, 0, nailWidth/2, nailLength/2, 0, 0, Math.PI * 2);
538
+ ctx.fill();
539
+ ctx.restore();
540
+ }
541
+
542
+ // 绘制指甲形状(椭圆)
543
+ ctx.beginPath();
544
+ ctx.ellipse(0, 0, nailWidth/2, nailLength/2, 0, 0, Math.PI * 2);
545
+ ctx.clip();
546
+
547
+ // 为指甲颜色创建渐变
548
+ const gradient = ctx.createLinearGradient(-nailWidth/2, -nailLength/2, nailWidth/2, nailLength/2);
549
+
550
+ try {
551
+ // 从设计解析渐变颜色 - 修复后的代码
552
+ const gradientStr = nailDesigns[design].color;
553
+ // 提取所有十六进制颜色值
554
+ const colorMatches = gradientStr.match(/#[0-9a-f]{6}/gi);
555
+
556
+ if (colorMatches && colorMatches.length >= 2) {
557
+ const colorStart = colorMatches[0]; // 第一个颜色
558
+ const colorEnd = colorMatches[1]; // 第二个颜色
559
+
560
+ gradient.addColorStop(0, colorStart);
561
+ gradient.addColorStop(1, colorEnd);
562
+ } else {
563
+ // 如果无法解析颜色,则使用默认颜色
564
+ gradient.addColorStop(0, '#ff9a9e');
565
+ gradient.addColorStop(1, '#fad0c4');
566
+ console.warn('无法解析颜色,使用默认值');
567
+ }
568
+ } catch (error) {
569
+ // 错误处理
570
+ console.error('创建渐变时出错:', error);
571
+ // 使用默认颜色
572
+ gradient.addColorStop(0, '#ff9a9e');
573
+ gradient.addColorStop(1, '#fad0c4');
574
+ }
575
+
576
+ // 使用渐变绘制指甲设计
577
+ ctx.globalAlpha = opacity;
578
+ ctx.fillStyle = gradient;
579
+ ctx.fillRect(-nailWidth/2, -nailLength/2, nailWidth, nailLength);
580
+
581
+ // 恢复上下文
582
+ ctx.restore();
583
+ }
584
+
585
+ // MediaPipe Hands 模型回调
586
+ function onResults(results) {
587
+ // 清除画布并绘制摄像头画面
588
+ canvasCtx.save();
589
+ canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
590
+ canvasCtx.drawImage(
591
+ results.image, 0, 0, canvasElement.width, canvasElement.height
592
+ );
593
+
594
+ // 处理手部
595
+ if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
596
+ for (const landmarks of results.multiHandLandmarks) {
597
+ // 如果启用,绘制手部骨架
598
+ if (showLines) {
599
+ drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS,
600
+ {color: 'rgba(255, 255, 255, 0.5)', lineWidth: 2});
601
+ }
602
+
603
+ // 为每个指尖应用美甲设计
604
+ // 拇指
605
+ applyNailDesign(canvasCtx, landmarks[4], landmarks[3], currentDesign);
606
+ // 食指
607
+ applyNailDesign(canvasCtx, landmarks[8], landmarks[7], currentDesign);
608
+ // 中指
609
+ applyNailDesign(canvasCtx, landmarks[12], landmarks[11], currentDesign);
610
+ // 无名指
611
+ applyNailDesign(canvasCtx, landmarks[16], landmarks[15], currentDesign);
612
+ // 小指
613
+ applyNailDesign(canvasCtx, landmarks[20], landmarks[19], currentDesign);
614
+ }
615
+ }
616
+
617
+ canvasCtx.restore();
618
+ }
619
+
620
+ // 初始化AR应用
621
+ function initializeARApp() {
622
+ try {
623
+ cameraPermissionElement.style.display = 'none';
624
+
625
+ if (!cameraInitialized) {
626
+ showDebug("正在初始化手部跟踪...");
627
+
628
+ // 创建 MediaPipe Hands 实例
629
+ hands = new Hands({locateFile: (file) => {
630
+ return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
631
+ }});
632
+
633
+ hands.setOptions({
634
+ maxNumHands: 2,
635
+ modelComplexity: 1,
636
+ minDetectionConfidence: 0.5,
637
+ minTrackingConfidence: 0.5
638
+ });
639
+
640
+ hands.onResults(onResults);
641
+
642
+ // 设置摄像头
643
+ setupCamera();
644
+ cameraInitialized = true;
645
+
646
+ showDebug("手部跟踪已初始化");
647
+ }
648
+ } catch (error) {
649
+ showDebug("初始���失败: " + error.message);
650
+ console.error("初始化AR应用失败:", error);
651
+ }
652
+ }
653
+
654
+ // 设置事件监听器
655
+ sizeSlider.addEventListener('input', (e) => {
656
+ sizeScale = parseInt(e.target.value) / 100;
657
+ sizeValue.textContent = `${e.target.value}%`;
658
+ });
659
+
660
+ lengthSlider.addEventListener('input', (e) => {
661
+ lengthScale = parseInt(e.target.value) / 100;
662
+ lengthValue.textContent = `${e.target.value}%`;
663
+ });
664
+
665
+ opacitySlider.addEventListener('input', (e) => {
666
+ opacity = parseInt(e.target.value) / 100;
667
+ opacityValue.textContent = `${e.target.value}%`;
668
+ });
669
+
670
+ shadowToggle.addEventListener('click', () => {
671
+ shadowToggle.classList.toggle('active');
672
+ showShadows = shadowToggle.classList.contains('active');
673
+ });
674
+
675
+ linesToggle.addEventListener('click', () => {
676
+ linesToggle.classList.toggle('active');
677
+ showLines = linesToggle.classList.contains('active');
678
+ });
679
+
680
+ flipButton.addEventListener('click', () => {
681
+ facingMode = facingMode === 'user' ? 'environment' : 'user';
682
+ // 使用新的facingMode重启摄像头
683
+ if (camera) {
684
+ camera.stop();
685
+ }
686
+ setupCamera();
687
+ });
688
+
689
+ designOptions.forEach(option => {
690
+ option.addEventListener('click', () => {
691
+ designOptions.forEach(opt => opt.classList.remove('selected'));
692
+ option.classList.add('selected');
693
+ currentDesign = option.getAttribute('data-design');
694
+ });
695
+ });
696
+
697
+ snapButton.addEventListener('click', () => {
698
+ const dataUrl = canvasElement.toDataURL('image/png');
699
+
700
+ // 创建临时链接元素
701
+ const link = document.createElement('a');
702
+ link.href = dataUrl;
703
+ link.download = 'AR-美甲设计.png';
704
+ document.body.appendChild(link);
705
+ link.click();
706
+ document.body.removeChild(link);
707
+
708
+ // 拍照的视觉反馈
709
+ snapButton.style.backgroundColor = '#3a86ff';
710
+ setTimeout(() => {
711
+ snapButton.style.backgroundColor = 'white';
712
+ }, 300);
713
+ });
714
+
715
+ // 设置带有适当facingMode的摄像头
716
+ function setupCamera() {
717
+ try {
718
+ showDebug("正在初始化摄像头...");
719
+
720
+ camera = new Camera(videoElement, {
721
+ onFrame: async () => {
722
+ if (hands) {
723
+ try {
724
+ await hands.send({image: videoElement});
725
+ } catch (error) {
726
+ showDebug("手部跟踪出错: " + error.message);
727
+ }
728
+ }
729
+ },
730
+ width: 1280,
731
+ height: 720,
732
+ facingMode: facingMode
733
+ });
734
+
735
+ camera.start()
736
+ .then(() => {
737
+ showDebug("摄像头已启动");
738
+ setTimeout(() => {
739
+ debugInfo.style.display = 'none';
740
+ }, 3000);
741
+ })
742
+ .catch(error => {
743
+ console.error('摄像头启动失败: ', error);
744
+ showDebug("摄像头启动失败: " + error.message);
745
+ // 显示摄像头权限界面
746
+ cameraPermissionElement.style.display = 'flex';
747
+ });
748
+ } catch (error) {
749
+ showDebug("摄像头初始化失败: " + error.message);
750
+ console.error("摄像头初始化失败:", error);
751
+ }
752
+ }
753
+
754
+ // 处理窗口调整大小
755
+ window.addEventListener('resize', () => {
756
+ canvasElement.width = window.innerWidth;
757
+ canvasElement.height = window.innerHeight;
758
+ });
759
+ </script>
760
+ </body>
761
+ </html>