Code To Create Simple Barometer

Code To Create Simple Barometer
<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>Weather Barometer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: #f0f0f0;
        }

 

        .barometer {
            position: relative;
            width: 300px;
            height: 300px;
            border: 10px solid #333;
            border-radius: 50%;
            background: #fff;
        }

 

        .needle {
            position: absolute;
            width: 50%;
            height: 4px;
            background: red;
            top: 50%;
            left: 50%;
            transform-origin: left center;
            transform: rotate(0deg);
            transition: transform 1s;
        }

 

        .scale {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }

 

        .label {
            position: absolute;
            bottom: 10px;
            width: 50px;
            text-align: center;
            transform: translateX(-50%);
        }
    </style>
</head>
<body>
    <div class=”barometer”>
        <div class=”needle” id=”needle”></div>
        <div class=”scale”>
            <div class=”label” style=”left: 0%”>Stormy</div>
            <div class=”label” style=”left: 25%”>Rainy</div>
            <div class=”label” style=”left: 50%”>Change</div>
            <div class=”label” style=”left: 75%”>Fair</div>
            <div class=”label” style=”left: 100%”>Dry</div>
        </div>
    </div>
    <script>
        document.addEventListener(“DOMContentLoaded”, function() {
            const needle = document.getElementById(“needle”);
            const apiKey = ‘a637924bcef24a25deb6d4fc2f9dd1ef’;
            const city = ‘Kathmandu’;

 

            function updateBarometer(pressure) {
                const minPressure = 950;
                const maxPressure = 1050;
                const minAngle = -90;
                const maxAngle = 90;

 

                const angle = ((pressure – minPressure) / (maxPressure – minPressure)) * (maxAngle – minAngle) + minAngle;
                needle.style.transform = `rotate(${angle}deg)`;
            }

 

            async function fetchWeatherData() {
                try {
                    const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`);
                    const data = await response.json();
                    const pressure = data.main.pressure;
                    updateBarometer(pressure);
                } catch (error) {
                    console.error(“Error fetching weather data:”, error);
                }
            }

 

            fetchWeatherData();
        });
    </script>
</body>
</html>

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top