JS使用canvas实现(下雨天)特效

this.ts = 0.96; //透明度的系数

this.l = random(h * 0.8, h * 0.9); //落地的高度

this.maxR = 50;

},

};




2.设置下落溅起圆的半径和颜色



draw: function () {

if (this.y > this.l) {

ctx.beginPath(); //开启一个路径

ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);

//  圆心 x ,y ,半径,起始角度,Π,false顺时针,true逆时针

ctx.strokeStyle = "rgba(0,255,255," + this.t + ")";

ctx.closePath(); //闭合一个路径

ctx.stroke(); //开始描边绘制

} else {

ctx.beginPath(); //开启一个路径

ctx.fillStyle = colorRandom();//随机颜色

ctx.fillRect(this.x, this.y, 2, 10);

//      矩形  x  y  w  h

ctx.closePath(); //闭合一个路径

}

this.update(); //坐标更新

},

update: function () {

if (this.y > this.l) {

//画圆

//   return;

// init.draw_arc();

if (this.t > 0.03) {

  this.r += this.rSpeed; //半径不断增大

  if (this.r > this.maxR) {

    this.t *= this.ts; //透明度变小

    // console.log(init.t);

  }

} else {

    

//   return;//雨停了

this.init(); //重新来过,初始化

}

} else {

this.y += this.ySpeed;

// this.draw_rect();

}

},




3.设置随机数和运动方式



function move() {

// 添加一个大蒙层

ctx.beginPath(); //开启一个路径

ctx.fillStyle = “rgba(0,0,0,0.1)”;

ctx.fillRect(0, 0, w, h);

// 矩形 x y w h

ctx.closePath(); //闭合一个路径

for(var i = 0;i<RainArr.length;i++){

  RainArr[i].draw();

}

window.requestAnimationFrame(move);

}

move();




4.设置随机颜色



function colorRandom(){

var r = Math.floor(Math.random()*255);

var g = Math.floor(Math.random()*255);

var b = Math.floor(Math.random()*255);

return "rgba("+ r +","+ g +","+ b +")";

}




 ![](https://img-blog.csdnimg.cn/c016115f9d1348ce9722972afa6deeec.gif)



 原整代码



<meta charset="UTF-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Document</title>

<style>

  * {

    padding: 0;

    margin: 0;

  }

  #cavs {

    background: black;

  }

  body {

    overflow: hidden;

  }

</style>
<canvas id="cavs" width="500" height="500"></canvas>

<script>

  var cavs = document.getElementById("cavs"); //获得了这个画板

  var ctx = cavs.getContext("2d"); //画笔  ‘3d’   --- webgl  --- threejs



  var w = (cavs.width = window.innerWidth);

  var h = (cavs.height = window.innerHeight);



  window.onresize = function () {

    var w = (cavs.width = window.innerWidth);

    var h = (cavs.height = window.innerHeight);

  };

var cont = 30; //雨滴的最大数

var RainArr = []; // 装雨滴的数组

  //开始



  function Rain() {

    //构造函数

  }



  Rain.prototype = {

    init: function () {

      this.x = random(1, w); //随机坐标x

      this.y = 0; // 坐标y

      this.r = 1;

      this.ySpeed = random(4, 7);

      this.rSpeed = 1;

      this.t = 1; //透明度

      this.ts = 0.96; //透明度的系数

      this.l = random(h * 0.8, h * 0.9); //落地的高度

      this.maxR = 50;

    },

    draw: function () {

      if (this.y > this.l) {

        ctx.beginPath(); //开启一个路径

        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);

        //  圆心 x ,y ,半径,起始角度,Π,false顺时针,true逆时针

        ctx.strokeStyle = "rgba(0,255,255," + this.t + ")";

        ctx.closePath(); //闭合一个路径

        ctx.stroke(); //开始描边绘制

      } else {

        ctx.beginPath(); //开启一个路径

        ctx.fillStyle = colorRandom();//随机颜色

        ctx.fillRect(this.x, this.y, 2, 10);

        //      矩形  x  y  w  h

        ctx.closePath(); //闭合一个路径

      }



      this.update(); //坐标更新

    },

    update: function () {

      if (this.y > this.l) {

        //画圆

        //   return;

        // init.draw_arc();

        if (this.t > 0.03) {

          this.r += this.rSpeed; //半径不断增大

          if (this.r > this.maxR) {

            this.t *= this.ts; //透明度变小

            // console.log(init.t);

          }

        } else {

            

        //   return;//雨停了

        this.init(); //重新来过,初始化

        }

      } else {

        this.y += this.ySpeed;

        // this.draw_rect();

      }

    },

  };



  //随机数的函数

  function random(min, max) {

    return Math.floor(Math.random() * (max - min) + min);

  }



  var one = new Rain();

  one.init(); //初始化一下

  // 产生多个雨滴

  function createRain() {

基础学习:

前端最基础的就是 HTML , CSS 和 JavaScript 。

网页设计:HTML和CSS基础知识的学习

HTML是网页内容的载体。内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字、图片、视频等。

CSS样式是表现。就像网页的外衣。比如,标题字体、颜色变化,或为标题加入背景图片、边框等。所有这些用来改变内容外观的东西称之为表现。

动态交互:JavaScript基础的学习

JavaScript是用来实现网页上的特效效果。如:鼠标滑过弹出下拉菜单。或鼠标滑过表格的背景颜色改变。还有焦点新闻(新闻图片)的轮换。可以这么理解,有动画的,有交互的一般都是用JavaScript来实现的。

和 JavaScript 。

网页设计:HTML和CSS基础知识的学习

HTML是网页内容的载体。内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字、图片、视频等。

[外链图片转存中…(img-P1CulBOH-1726010900947)]

CSS样式是表现。就像网页的外衣。比如,标题字体、颜色变化,或为标题加入背景图片、边框等。所有这些用来改变内容外观的东西称之为表现。

[外链图片转存中…(img-zfwAGcpC-1726010900949)]

动态交互:JavaScript基础的学习

JavaScript是用来实现网页上的特效效果。如:鼠标滑过弹出下拉菜单。或鼠标滑过表格的背景颜色改变。还有焦点新闻(新闻图片)的轮换。可以这么理解,有动画的,有交互的一般都是用JavaScript来实现的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值