Потужнометр з красивим UI та звуковими ефектами
Ось код для веб-додатка "Потужнометр" з гарним інтерфейсом та звуковими ефектами:
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Потужнометр</title>
<style>
body {
font-family: 'Roboto', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #1e3c72, #2a5298);
margin: 0;
color: white;
}
.container {
text-align: center;
background-color: rgba(0, 0, 0, 0.3);
border-radius: 20px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
h1 {
margin-top: 0;
margin-bottom: 20px;
font-size: 2.5em;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
canvas {
display: block;
margin: 20px auto;
}
.controls {
margin-top: 30px;
display: flex;
justify-content: center;
gap: 20px;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 12px 24px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}
button:hover {
background-color: #3e8e41;
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.3);
}
button:active {
transform: translateY(1px);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#powerValue {
font-size: 2em;
font-weight: bold;
margin: 20px 0;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<div class="container">
<h1>Потужнометр</h1>
<div id="powerValue">0 Вт</div>
<canvas id="powerMeter" width="400" height="200"></canvas>
<div class="controls">
<button id="increasePower">Збільшити потужність</button>
<button id="decreasePower">Зменшити потужність</button>
<button id="resetPower">Скинути</button>
</div>
</div>
<script>
// Ініціалізація звукових ефектів
let audioContext;
function initAudio() {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
function playSound(frequency, type, duration) {
if (!audioContext) initAudio();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.type = type;
oscillator.frequency.value = frequency;
gainNode.gain.value = 0.3;
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + duration);
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + duration);
}
function playIncreaseSound() {
playSound(440 + power * 5, 'sine', 0.3);
}
function playDecreaseSound() {
playSound(330 - power * 2, 'triangle', 0.3);
}
function playResetSound() {
playSound(220, 'square', 0.5);
}
// Основна логіка потужнометра
const canvas = document.getElementById('powerMeter');
const ctx = canvas.getContext('2d');
const powerValueDisplay = document.getElementById('powerValue');
let power = 0;
const maxPower = 100;
function drawMeter() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Фон шкали
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height, canvas.width * 0.8, Math.PI, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
// Шкала потужності
const angle = Math.PI + (Math.PI * power / maxPower);
// Градієнт для шкали
const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop(0, '#00ff00');
gradient.addColorStop(0.5, '#ffff00');
gradient.addColorStop(1, '#ff0000');
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height, canvas.width * 0.8, Math.PI, angle);
ctx.lineWidth = 30;
ctx.strokeStyle = gradient;
ctx.stroke();
// Позначки на шкалі
ctx.fillStyle = '#fff';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
for (let i = 0; i <= maxPower; i += 10) {
const markAngle = Math.PI + (Math.PI * i / maxPower);
const x1 = canvas.width / 2 + Math.cos(markAngle) * (canvas.width * 0.65);
const y1 = canvas.height + Math.sin(markAngle) * (canvas.width * 0.65);
const x2 = canvas.width / 2 + Math.cos(markAngle) * (canvas.width * 0.75);
const y2 = canvas.height + Math.sin(markAngle) * (canvas.width * 0.75);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.stroke();
if (i % 20 === 0) {
const textX = canvas.width / 2 + Math.cos(markAngle) * (canvas.width * 0.55);
const textY = canvas.height + Math.sin(markAngle) * (canvas.width * 0.55);
ctx.fillText(i, textX, textY);
}
}
// Стрілка
const needleLength = canvas.width * 0.7;
const needleX = canvas.width / 2 + Math.cos(angle) * needleLength;
const needleY = canvas.height + Math.sin(angle) * needleLength;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, canvas.height);
ctx.lineTo(needleX, needleY);
ctx.strokeStyle = '#fff';
ctx.lineWidth = 3;
ctx.stroke();
// Центральне коло
ctx.beginPath();
ctx.arc(canvas.width / 2, canvas.height, 10, 0, 2 * Math.PI);
ctx.fillStyle = '#fff';
ctx.fill();
// Оновлення відображення значення потужності
powerValueDisplay.textContent = `${power} Вт`;
}
// Обробники подій
document.getElementById('increasePower').addEventListener('click', () => {
if (power < maxPower) {
power += 5;
playIncreaseSound();
drawMeter();
}
});
document.getElementById('decreasePower').addEventListener('click', () => {
if (power > 0) {
power -= 5;
playDecreaseSound();
drawMeter();
}
});
document.getElementById('resetPower').addEventListener('click', () => {
power = 0;
playResetSound();
drawMeter();
});
// Ініціалізація
document.addEventListener('DOMContentLoaded', () => {
drawMeter();
});
</script>
</body>
</html>
Хотіли б, щоб я пояснив або розкрив деталі цього коду?