• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    使用canvas实现雪花飘动效果的示例代码

    发布者: 怀卉 | 发布时间: 2025-6-16 12:24| 查看数: 126| 评论数: 0|帖子模式

    今天我们就使用canvas来实现雪花飘落的效果❄️
    一、canvas是什么?

    HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.
    <canvas> 标签只是图形容器,您必须使用脚本来绘制图形。
    你可以通过多种方法使用 canvas 绘制路径,盒、圆、字符以及添加图像。
    二、canvas的基本用法

    1.创建一个画布(Canvas)
    1. <canvas id="myCanvas" width="200" height="100"></canvas>
    复制代码
    2.使用JavaScript绘制图像
    1. //首先找到<canvas>元素
    2. var c=document.getElementById("myCanvas");
    3. //然后创建context对象
    4. var ctx=c.getContext("2d");
    5. //下面的两行代码绘制一个红色的矩形:
    6. ctx.fillStyle="#FF0000";
    7. ctx.fillRect(0,0,150,75);
    复制代码
    getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
    设置fillStyle属性可以是CSS颜色,渐变,或图案。fillStyle 默认设置是#000000。
    3.Canvas 坐标
    canvas 是一个二维网格。
    canvas 的左上角坐标为 (0,0)
    ctx.fillRect(0,0,150,75);
    上面的 fillRect 方法拥有参数 (0,0,150,75)。
    意思是:在画布上绘制 150x75 的矩形,从左上角开始 (0,0)。
    4.Canvas - 路径
    moveTo(x,y) 定义线条开始坐标
    lineTo(x,y) 定义线条结束坐标
    在canvas中绘制圆形, 我们将使用以下方法:
    1. arc(x,y,r,start,stop)
    复制代码
    使用arc() 画一个圆
    1. var c=document.getElementById("myCanvas");
    2. var ctx=c.getContext("2d");
    3. ctx.beginPath();
    4. ctx.arc(95,50,40,0,2*Math.PI);
    5. ctx.stroke();
    复制代码
    三、实现雪花飘动的思路

    1.创建一个画布(Canvas)
    1. var canvas =document.getElementById("canvas")
    2.     //参数 contextID 指定了您想要在画布上绘制的类型。
    3.     //当前唯一的合法值是 "2d",它指定了二维绘图,
    4.     //并且导致这个方法返回一个环境对象,该对象导出一个二维绘图 API。
    5.     var context = canvas.getContext("2d")
    6.     var w =window.innerWidth
    7.     var h =window.innerHeight
    8.     canvas.width = w;
    9.     canvas.height =h;
    复制代码
    2.创建雪花的对象数组
    1. var count =200 //雪花的个数
    2.     var snows=[] //雪花对象数组
    3.     for (var i=0 ; i< count;i++){
    4.         snows.push({
    5.             x:Math.random()*w,//Math.random()用于生成0~1的随机数
    6.             y:Math.random()*h,
    7.             r:Math.random()*5,
    8.         })
    9.     }
    复制代码
    3.绘制雪花样式
    1. function draw(){
    2.         context.clearRect(0,0,w,h)
    3.         context.beginPath()
    4.         for(var i=0; i<count;i++){
    5.             var snow = snows[i];//遍历每一片雪花
    6.             context.fillStyle ="rgb(255,255,255)" //设置雪花的样式
    7.             context.shadowBlur=10;
    8.             context.shadowColor="rgb(255,255,255)";
    9.             //moveTo 的方法是可以移动到指定的坐标
    10.             context.moveTo(snow.x,snow.y)
    11.             // 使用canvas arc()创建一个圆形
    12.              //x,y,r:圆的中心的x坐标和y坐标,r为半径
    13.             //0,Math.PI * 2起始弧度和结束弧度
    14.             
    15.             context.arc(snow.x,snow.y,snow.r,0,Math.PI * 2)
    16.             
    17.         }
    18.         //画布填充
    19.         context.fill()
    20.         move()
    21.     }
    复制代码
    4.实现雪花飘动
    1. function move(){
    2.         for (var i=0;i<count;i++){
    3.             var snow =snows[i];
    4.             snow.y +=(7-snow.r)/10 //从上往下飘落
    5.             snow.x+=((5-snow.r)/10)//从左到右飘落
    6.             if(snow.y>h){
    7.                 snows[i]={
    8.                     x:Math.random()*w,
    9.                     y:Math.random()*h,
    10.                     r:Math.random()*5,
    11.                 }
    12.             }
    13.         }
    14.     }
    复制代码
    5.设置刷新
    1.   draw()
    2.     //每毫秒刷新一次
    3. setInterval(draw,1)
    复制代码
    6.完整代码
    1. <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>雪花飘飘之使用canvas元素用于在网页上绘制图形。</title>  <style type="text/css">  *{            margin:0;            padding:0;            /* background-color: seagreen; */            background: url("雪人.jpg")  no-repeat;            background-size:100% 100%;        }  /* .can{            filter: blur(1px);        } */ </style></head><body> <canvas id="canvas" class="can"></canvas> <script type="text/javascript">    //canvas 元素用于在网页上绘制图形。 var canvas =document.getElementById("canvas")
    2.     //参数 contextID 指定了您想要在画布上绘制的类型。
    3.     //当前唯一的合法值是 "2d",它指定了二维绘图,
    4.     //并且导致这个方法返回一个环境对象,该对象导出一个二维绘图 API。
    5.     var context = canvas.getContext("2d")
    6.     var w =window.innerWidth
    7.     var h =window.innerHeight
    8.     canvas.width = w;
    9.     canvas.height =h;    var count =200 //雪花的个数
    10.     var snows=[] //雪花对象数组
    11.     for (var i=0 ; i< count;i++){
    12.         snows.push({
    13.             x:Math.random()*w,//Math.random()用于生成0~1的随机数
    14.             y:Math.random()*h,
    15.             r:Math.random()*5,
    16.         })
    17.     }    //绘制雪花    function draw(){        context.clearRect(0,0,w,h)        context.beginPath()        for(var i=0; i<count;i++){            var snow = snows[i];//遍历每一片雪花            context.fillStyle ="rgb(255,255,255)" //设置雪花的样式            context.shadowBlur=10;            context.shadowColor="rgb(255,255,255)";            //moveTo 的方法是可以移动到指定的坐标            context.moveTo(snow.x,snow.y)            // 使用canvas arc()创建一个圆形             //x,y,r:圆的中心的x坐标和y坐标,r为半径            //0,Math.PI * 2起始弧度和结束弧度                        context.arc(snow.x,snow.y,snow.r,0,Math.PI * 2)                               }        //画布填充        context.fill()        move()    }    //雪花飘动    function move(){
    18.         for (var i=0;i<count;i++){
    19.             var snow =snows[i];
    20.             snow.y +=(7-snow.r)/10 //从上往下飘落
    21.             snow.x+=((5-snow.r)/10)//从左到右飘落
    22.             if(snow.y>h){
    23.                 snows[i]={
    24.                     x:Math.random()*w,
    25.                     y:Math.random()*h,
    26.                     r:Math.random()*5,
    27.                 }
    28.             }
    29.         }
    30.     }    draw()
    31.     //每毫秒刷新一次
    32. setInterval(draw,1) </script></body></html>
    复制代码

    总结

    到此这篇关于使用canvas实现雪花飘动效果的示例代码的文章就介绍到这了,更多相关canvas雪花飘动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/768292.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表