前端小练习——大雪纷飞(JS没有上限!!!)

大家好,我是小黄。

具体效果:(大雪缓缓下落)

完整代码:

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>雪花形状的雪</title>
    <style>
        body, html {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
            background-color: #1c1c1c;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>

<canvas id="snowCanvas"></canvas>

<script>
    const canvas = document.getElementById('snowCanvas');
    const ctx = canvas.getContext('2d');

    // 设置画布的大小为窗口大小
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // 雪花的数量
    const snowflakesCount = 200;
    
    // 雪花数组
    const snowflakes = [];

    // 绘制六边形雪花
    function drawSnowflake(x, y, size, angle) {
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle);

        ctx.beginPath();
        for (let i = 0; i < 6; i++) {
            const currentAngle = (i * Math.PI) / 3;
            const nextAngle = ((i + 1) * Math.PI) / 3;
            ctx.moveTo(0, 0);
            ctx.lineTo(size * Math.cos(currentAngle), size * Math.sin(currentAngle));
            ctx.lineTo(size * Math.cos(nextAngle), size * Math.sin(nextAngle));
        }
        ctx.closePath();
        ctx.fillStyle = 'white';
        ctx.fill();
        ctx.restore();
    }

    // 雪花类
    class Snowflake {
        constructor() {
            // 随机初始位置
            this.x = Math.random() * canvas.width;
            this.y = Math.random() * canvas.height;
            this.size = Math.random() * 4 + 4; // 雪花的大小(4~8)
            this.speed = Math.random() * 3 + 1; // 雪花的下落速度
            this.wind = Math.random() * 0.5 - 0.25; // 随机的风力,控制雪花的左右漂动
            this.angle = Math.random() * Math.PI * 2; // 雪花的旋转角度
            this.rotationSpeed = Math.random() * 0.02 - 0.01; // 雪花的旋转速度
        }

        // 更新雪花位置
        update() {
            this.y += this.speed; // 下落
            this.x += this.wind; // 左右漂移
            this.angle += this.rotationSpeed; // 旋转

            // 如果雪花掉出画布,重新设置位置
            if (this.y > canvas.height) {
                this.y = 0;
                this.x = Math.random() * canvas.width;
            }
        }

        // 绘制雪花
        draw() {
            drawSnowflake(this.x, this.y, this.size, this.angle);
        }
    }

    // 初始化雪花
    for (let i = 0; i < snowflakesCount; i++) {
        snowflakes.push(new Snowflake());
    }

    // 绘制动画
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除之前的帧

        // 更新并绘制每一片雪花
        snowflakes.forEach(snowflake => {
            snowflake.update();
            snowflake.draw();
        });

        requestAnimationFrame(animate); // 请求下一帧
    }

    // 开始动画
    animate();
</script>
</body>
</html>

各位小伙伴还在BOSS直聘hr已读不回?!试试这个宝藏小程序!大家快看这里

创作不易,各位帅气漂亮的小伙伴点个关注再走呗!!

相关推荐
wytraining2 分钟前
SDD规范驱动开发
前端
枫叶丹45 分钟前
【HarmonyOS 6.0】AVCodec Kit 视频解码器平滑停用机制详解
开发语言·华为·音视频·harmonyos
深海鱼在掘金5 分钟前
Next.js从入门到实战保姆级教程(第十四章):性能优化深度实践
前端·typescript·next.js
tiger从容淡定是人生5 分钟前
Selenium与Playwright:两大Web自动化框架的深入对比
前端·selenium·测试工具·自动化·web测试·playwright·信息化战略
故事和你916 分钟前
洛谷-算法2-2-常见优化技巧1
开发语言·数据结构·c++·算法·动态规划·图论
酉鬼女又兒8 分钟前
JavaLeetCode 第一题「两数之和」:从暴力枚举到一遍哈希表的正确与错误实现,详解HashMap核心知识点及常见陷阱
java·开发语言·数据结构·算法·leetcode·职场和发展·散列表
好运的阿财11 分钟前
OpenClaw工具拆解之 web_fetch+image_generate
前端·python·机器学习·ai·ai编程·openclaw·openclaw工具
白夜111714 分钟前
C++(mixins 混入模式)
开发语言·c++·笔记
无风听海17 分钟前
Python 哨兵值模式(Sentinel Value Pattern)深度解析
开发语言·python·sentinel
清风玉骨19 分钟前
C++/Qt从零开始编译使用libxlsxwriter库
开发语言·qt