-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathindex.html
More file actions
135 lines (120 loc) · 3.94 KB
/
index.html
File metadata and controls
135 lines (120 loc) · 3.94 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
<!doctype html>
<html lang="en">
<head>
<title>Camera Project</title>
<meta charset="utf-8">
<style>
body {
font-family: monospace;
font-size: 11px;
}
#overlay {
position: absolute;
top: 0;
left: 0;
/* Mirror horizontally for a natural camera preview */
transform: scaleX(-1);
}
#camera {
/* Mirror horizontally for a natural camera preview */
transform: scaleX(-1);
border: 1px black dotted;
}
#tryon {
position: relative;
margin: 0 auto;
}
#start {
margin:0 auto;
width:100px;
display: block;
}
#debug {
width: 640px;
margin: 0 auto;
line-height: 12px;
}
#hint {
color: #0044aa;
margin-top: 10px;
text-align: center;
}
</style>
</head>
<body>
<!-- clmtrack -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clmtrackr/1.1.2/clmtrackr.min.js"></script>
<div id="tryon">
<video id="camera" loop></video>
<canvas id="overlay"></canvas>
</div>
<button id="start">Start</button>
<div id="debug"></div>
<div id="hint">Turn your face towards the camera and move your head to adjust the glasses position.</div>
<script type="module">
import { TryOnFace } from './js/tryon-face.js';
const startButton = document.getElementById('start');
startButton.style.display = 'none';
const debugDiv = document.getElementById('debug');
const object = {
outside: {
left: './glasses/left.png',
right: './glasses/right.png',
front: './glasses/front.png'
}
};
let tryOn = null;
let debugInterval = null;
function safeFixed(val, digits = 1) {
return (typeof val === 'number' && isFinite(val)) ? val.toFixed(digits) : 'N/A';
}
function updateDebug() {
if (!tryOn) return;
const status = tryOn.getStatus();
const params = tryOn.getParameters();
debugDiv.innerHTML = `<strong>Status:</strong> ${status}<br>`
+ `<strong>Position:</strong> x=${safeFixed(params.position.x)}, y=${safeFixed(params.position.y)}, z=${safeFixed(params.position.z)}<br>`
+ `<strong>Rotation:</strong> y=${safeFixed(params.rotation.y, 3)}, z=${safeFixed(params.rotation.z, 3)}<br>`
+ `<strong>Size:</strong> x=${safeFixed(params.size.x)}, y=${safeFixed(params.size.y)}, z=${safeFixed(params.size.z)}`;
}
window.addEventListener('load', function() {
tryOn = new TryOnFace({
width: 640,
height: 480,
object: object,
statusHandler: function(status) {
switch(status) {
case "STATUS_READY": {
startButton.style.display = 'block';
startButton.textContent = 'Start';
} break;
case "STATUS_CAMERA_ERROR": {
alert('Camera error. Please allow camera access.');
} break;
case "STATUS_SEARCH": {
/* Searching for face */
} break;
case "STATUS_FOUND": {
/* Face found and tracking */
}
}
}
});
startButton.addEventListener('click', function() {
if (!tryOn) return;
if (tryOn.stream) {
tryOn.stop();
clearInterval(debugInterval);
updateDebug();
startButton.textContent = 'Start';
} else {
tryOn.start();
debugInterval = setInterval(updateDebug, 250);
startButton.textContent = 'Stop';
}
});
updateDebug();
});
</script>
</body>
</html>