-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
92 lines (82 loc) · 2.56 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Off-Camera Reflection</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #222;
margin: 0;
color: #fff;
flex-direction: column;
}
.container {
position: relative;
}
video {
width: 320px;
height: 240px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
canvas {
position: absolute;
top: 120%; /* Shift reflection downward */
left: 0; /* Align reflection with video */
transform: scaleY(-1); /* Flip vertically */
opacity: 0.6; /* Transparent effect */
}
</style>
</head>
<body>
<h1>Webcam with Off-Camera Reflection</h1>
<div class="container">
<video id="webcam" autoplay></video>
<canvas id="reflection"></canvas>
</div>
<script>
const video = document.getElementById('webcam');
const canvas = document.getElementById('reflection');
const ctx = canvas.getContext('2d');
// Access user's webcam
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
video.srcObject = stream;
// Set canvas size to match video
video.addEventListener('loadeddata', () => {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
});
// Update reflection in real-time
video.addEventListener('play', () => {
function drawReflection() {
// Clear the canvas before drawing
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the mirrored webcam feed
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Apply fading gradient for realism
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, 'rgba(0, 0, 0, 0.5)');
gradient.addColorStop(1, 'transparent');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Keep the reflection dynamic
if (!video.paused && !video.ended) {
requestAnimationFrame(drawReflection);
}
}
drawReflection();
});
})
.catch((err) => {
console.error('Error accessing webcam:', err);
alert('Webcam access is required to see the effect.');
});
</script>
</body>
</html>