BIGEMPA Js API示例中心

天气效果源代码展示

代码编辑区 运行 下载 还原
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>demo</title>
    <link href="http://ua.bigemap.com:30081/bmsdk/bigemap.js/v2.1.0/bigemap.css" rel="stylesheet" />
    <script src="http://ua.bigemap.com:30081/bmsdk/bigemap.js/v2.1.0/bigemap.js"></script>
    <style>
        html,
        body {
            margin: 0;
            height: 100%;
            overflow: hidden;
            background: #000;
        }

        #map {
            width: 100%;
            height: 100%;
        }

        #weatherCanvas {
            position: absolute;
            top: 0;
            left: 0;
            pointer-events: none;
            z-index: 500;
        }

        #controls {
            position: absolute;
            top: 10px;
            left: 10px;
            z-index: 999;
            background: rgba(255, 255, 255, 0.85);
            padding: 8px 12px;
            border-radius: 10px;
            font-family: "Microsoft YaHei", sans-serif;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <canvas id="weatherCanvas"></canvas>
    <div id="controls">
        天气:
        <select id="weatherSelect">
            <option value="clear">晴天</option>
            <option value="cloud">阴天</option>
            <option value="rain">下雷雨</option>
            <option value="normalrain">下雨</option>
            <option value="snow">下雪</option>
        </select>
    </div>

    <script>
        // 初始化地图
        // 软件配置信息地址,软件安装完成之后使用本地地址,如:http://localhost:9000
        BM.Config.HTTP_URL = "http://ua.bigemap.com:30081/bmsdk/"
        // 在ID为map的元素中实例化一个地图,并设置地图的ID号,ID号程序自动生成,无需手动配置 ,中心点,默认的级别和显示级别控件
        var map = BM.map('map', 'bigemap.dc-tian-w-satellite', { center: [30,104], zoom: 8, zoomControl:false, attributionControl: false, preferCanvas: true, minZoom: 1 });

        let m1 = BM.marker(map.getCenter()).addTo(map);
        m1.on("click", (e) => {
            console.log(`eeeee`, e);
        })

        // Canvas 层
        const canvas = document.getElementById('weatherCanvas');
        const ctx = canvas.getContext('2d');
        resizeCanvas();
        window.addEventListener('resize', resizeCanvas);

        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }

        // 状态变量
        let particles = [];
        let weather = 'clear';
        let transitionAlpha = 1.0; // 用于渐变过渡
        let nextWeather = null;
        let lightningAlpha = 0; // 闪电亮度

        // 初始化粒子
        function createParticles(type) {
            // const count = type === 'rain' ? 250 : type === 'snow' ? 200 : 0;
            let count = null;
            if(type == "rain"){
                count = 250;
            }else if(type == `snow`){
                count = 200;
            }else if(type == "normalrain"){
                count = 240;
            }
            particles = [];
            for (let i = 0; i < count; i++) {
                particles.push({
                    x: Math.random() * canvas.width,
                    y: Math.random() * canvas.height,
                    size: type === 'snow' ? Math.random() * 3 + 1 : Math.random() * 2 + 1,
                    speedY: type === 'rain' || type == "normalrain"  ? Math.random() * 4 + 4 : Math.random() * 1 + 0.5,
                    speedX: type === 'rain'|| type == "normalrain" ? Math.random() * 0.5 - 0.25 : Math.random() * 0.5 - 0.25
                });
            }
        }

        // 切换天气
        document.getElementById('weatherSelect').addEventListener('change', e => {
            nextWeather = e.target.value;
            transitionAlpha = 0; // 触发渐变动画
            createParticles(nextWeather);
        });

        // 动画循环
        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 当前天气透明度渐变
            if (nextWeather && transitionAlpha < 1) {
                transitionAlpha += 0.02;
                if (transitionAlpha >= 1) {
                    weather = nextWeather;
                    nextWeather = null;
                }
            }

            const alpha = nextWeather ? transitionAlpha : 1;

            // 绘制天气效果
            ctx.save();
            ctx.globalAlpha = alpha;
            if (weather === 'rain'){
                drawRain();
                drawLightning()
            };
            if (weather === 'snow'){
                drawSnow()
            };
            if (weather === 'cloud'){
                 drawClouds()
            };
            if (weather === 'normalrain'){
                drawnormalRain()
            };
            ctx.restore();
            requestAnimationFrame(animate);
        }

        // 绘制雨
        function drawRain() {
            ctx.strokeStyle = 'rgba(174,194,224,0.8)';
            ctx.lineWidth = 1;
            for (let p of particles) {
                ctx.beginPath();
                ctx.moveTo(p.x, p.y);
                ctx.lineTo(p.x + p.speedX * 2, p.y + p.speedY * 10);
                ctx.stroke();

                p.x += p.speedX;
                p.y += p.speedY * 5;
                if (p.y > canvas.height) {
                    p.y = -10;
                    p.x = Math.random() * canvas.width;
                }
            }
        }

        // 绘制雪
        function drawSnow() {
            ctx.fillStyle = 'rgba(255,255,255,0.9)';
            for (let p of particles) {
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
                ctx.fill();

                p.x += p.speedX;
                p.y += p.speedY;
                if (p.y > canvas.height) {
                    p.y = -10;
                    p.x = Math.random() * canvas.width;
                }
            }
        }

        // 绘制阴天
        function drawClouds() {
            const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
            gradient.addColorStop(0, 'rgba(200,200,200,0.7)');
            gradient.addColorStop(1, 'rgba(100,100,100,0.3)');
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        }

        // 闪电逻辑
        function drawLightning() {
            if (Math.random() < 0.005) { // 随机闪电概率
                lightningAlpha = 1.0;
            }
            if (lightningAlpha > 0) {
                ctx.fillStyle = `rgba(255,255,255,${lightningAlpha})`;
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                lightningAlpha -= 0.08;
            }
        }

        //普通下雨   
        function drawnormalRain() {
          ctx.strokeStyle = 'rgba(174,194,224,0.8)';
            ctx.lineWidth = 1;
            for (let p of particles) {
                ctx.beginPath();
                ctx.moveTo(p.x, p.y);
                ctx.lineTo(p.x + p.speedX * 2, p.y + p.speedY * 10);
                ctx.stroke();

                p.x += p.speedX;
                p.y += p.speedY * 5;
                if (p.y > canvas.height) {
                    p.y = -10;
                    p.x = Math.random() * canvas.width;
                }
            }
        }

        // 启动
        createParticles(weather);
        animate();
    </script>
</body>

</html>