20多行js实现canvas雪夜下雪效果

前言

<canvas>是HTML 5 新增的元素,可用于通过使用JavaScript中的脚本来绘制图形

实现canvas雪夜下雪效果

1、设置canvas标签的id为cvs,设置背景颜色为黑色

<canvas id="cvs" style="background-color: #000;"></canvas>

2、设置body外边距为0

<body style="margin: 0;">

3、通过获取DOM元素获得画板

// 获取面版
 const cvs = document.querySelector("#cvs");

4、指定二维绘图

getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性
getContext(‘2d’):它指定了二维绘图,并且导致这个方法返回一个环境对象,该对象导出一个二维绘图 API。

const ctx = cvs.getContext('2d');

5、设置宽高填满页面

// 取出屏幕的宽高
const {clientWidth:width,clientHeight:height} = document.documentElement;
// 重设canvas的宽高
cvs.width =width;
cvs.height =height;

6、设置雪点的颜色

ctx.fillStyle='#ffffff';

7、定义一个雪点数据

设置雪点个数为500个

const bgColors = Array.from(new Array(500)).map(v=>{
    return {
        x:Math.random()*width,  // 在宽度范围内随机
        y:Math.random()*height, // 在高度范围内随机
        step:Math.random()*2.5+0.5, // 运动速度随机
    }
})

8、定义一个下雪函数

函数内部:requestAnimationFrame()高频执行 → clearRect()清除画布 → beginPath()重新绘图的准备 → forEach遍历雪点 → fill()重设矩形 → fill()渲染 → 往下飘

const render = ()=>{
     // 2.清空
     ctx.clearRect(0,0,width,height);
     // 3.重新绘图的标识
     ctx.beginPath();
     // 4.遍历
     bgColors.forEach(v=>{
         // 7.往下飘的效果
         v.y = v.y > height?0:(v.y+v.step)
         // 5.重设一个矩形
         ctx.rect(v.x,v.y,3,3)
     })
     // 6.渲染
     ctx.fill();
     //  1.requestAnimationFrame制作高频执行
     requestAnimationFrame(render);
 }

9、调用下雨函数

render();

二、完整代码及运行效果

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
</head>
<body style="margin: 0;">
    <canvas id="cvs" style="background-color: #000;"></canvas>
</body>
<script>
    const cvs = document.querySelector("#cvs");
    const ctx = cvs.getContext('2d');
    // 取出屏幕的宽高
    const {clientWidth:width,clientHeight:height} = document.documentElement;
    // 重设canvas的宽高
    cvs.width =width;
    cvs.height =height;
    // 设置雪点的颜色
    ctx.fillStyle='#ffffff';

    // 定义雪点的数组
    // 设置雪点个数为400个
    const bgColors = Array.from(new Array(400)).map(v=>{
        return {
            x:Math.random()*width,  // 在宽度范围内随机
            y:Math.random()*height, // 在高度范围内随机
            step:Math.random()*2.5+0.5, // 运动速度随机
        }
    })

    // 定义render函数
    const render = ()=>{
        // 清空
        ctx.clearRect(0,0,width,height);
        // 重新开始
        ctx.beginPath();
        // 遍历
        bgColors.forEach(v=>{
            // 往下飘的效果
            v.y = v.y>height?0:(v.y+v.step)
            // 重设
            ctx.rect(v.x,v.y,3,3)
        })
        // 渲染
        ctx.fill();
        //  requestAnimationFrame制作高频执行
        requestAnimationFrame(render);
    }

    // 调用render()函数
    render()
</script>
</html>

运行结果在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值