<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Фотобудка</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
margin: 0;
background: #f0f2f5;
padding: 20px;
}
h1 { color: #333; }
video, canvas, img {
max-width: 90%;
border-radius: 12px;
background: #000;
}
#countdown {
font-size: 72px;
font-weight: bold;
color: #e63946;
height: 90px;
line-height: 90px;
}
button {
padding: 14px 32px;
font-size: 18px;
border: none;
border-radius: 30px;
background: #1d7bf3;
color: #fff;
cursor: pointer;
margin: 15px 0;
}
button:hover { background: #155fbf; }
button:disabled { background: #9bb8d9; cursor: default; }
a.download {
display: none;
margin-top: 10px;
text-decoration: none;
color: #fff;
background: #2a9d8f;
padding: 12px 28px;
border-radius: 30px;
}
</style>
</head>
<body>
<h1>📷 Фотобудка</h1>
<video id="video" autoplay playsinline muted></video>
<div id="countdown"></div>
<button id="startBtn">Сделать фото</button>
<canvas id="canvas" style="display:none;"></canvas>
<img id="photo" style="display:none;" alt="Ваше фото">
<a class="download" id="downloadLink" download="фото.png">Скачать фото</a>
<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const photo = document.getElementById('photo');
const countdown = document.getElementById('countdown');
const startBtn = document.getElementById('startBtn');
const downloadLink = document.getElementById('downloadLink');
let stream = null;
startBtn.addEventListener('click', async () => {
try {
// Запрашиваем доступ к камере
stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'user', width: { ideal: 1280 } },
audio: false
});
video.srcObject = stream;
video.style.display = 'block';
startBtn.disabled = true;
// Обратный отсчёт 5 секунд
let seconds = 5;
countdown.textContent = seconds;
const timer = setInterval(() => {
seconds--;
if (seconds > 0) {
countdown.textContent = seconds;
} else {
clearInterval(timer);
countdown.textContent = '';
takePhoto();
}
}, 1000);
} catch (err) {
alert('Не удалось включить камеру: ' + err.message);
startBtn.disabled = false;
}
});
function takePhoto() {
// Размеры снимка берём из реального потока видео
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Зеркалим, как в селфи — чтобы не смущало отражение
const ctx = canvas.getContext('2d');
ctx.translate(canvas.width, 0);
ctx.scale(-1, 1);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Показываем готовый снимок и выключаем камеру
photo.src = canvas.toDataURL('image/png');
photo.style.display = 'block';
video.style.display = 'none';
downloadLink.href = photo.src;
downloadLink.style.display = 'inline-block';
if (stream) {
stream.getTracks().forEach(t => t.stop());
stream = null;
}
startBtn.disabled = false;
startBtn.textContent = 'Сделать ещё фото';
}
</script>
</body>
</html>