zoukankan      html  css  js  c++  java
  • canvas特效:下雨效果

        canvas做出美爆了了下雨效果,本效果原创

        

       雨点大小可自己调节,页面代码如下:

      

    <body>
    <canvas id="canvas" width="2000px" height="1200px"></canvas>
    <script>
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");

    //构建构造函数 创建圆形
    function Circle()
    {
    this.x = Math.random()*canvas.width;
    this.y = -10;
    this.width=Math.random()*2;
    this.height = Math.random()*30;

    //画一个圆
    this.paint = function(){
    context.beginPath();

    context.fillStyle="rgba(255,255,255,0.3)";
    context.fillRect(this.x,this.y,this.width,this.height);
    context.fill();
    }

    //设置步数
    this.step = function(){
    this.y+=8;
    }
    }


    //定义一个全局数组,盛放圆片数组,初始化为0
    var circles = [];

    //将一个圆片放入圆的数组中
    function createCircles(){
    var circle = new Circle();
    circles[circles.length] = circle;
    }

    //将圆画出来
    function paintCircls(){
    for(var i=0;i<circles.length;i++){
    circles[i].paint();
    }
    }

    //将圆画出来
    function stepCircles(){
    for(var i=0;i<circles.length;i++){
    circles[i].step();
    }
    }

    var myimg = new Image();
    myimg.src = "demo-2-bg.jpg";

    var time = 0;
    setInterval(
    function(){
    context.drawImage(myimg,0,0); //刷新背景图片
    time++;
    if(time%3==0){
    createCircles(); //新生成一个圆
    }
    paintCircls(); //将当前页面所有圆绘出来
    stepCircles(); //圆向下走
    },10);

    </script>
    </body>

      that's all! 换上不同图片就OK了

  • 相关阅读:
    吴太银:华为消费者云服务Cassandra使用场景与最佳实践
    使用FileZilla连接Linux
    debug 与 release
    删除cocos2dx项目模版
    [转]C/C++控制台输出时设置字体及背景颜色
    iphone调试的一些问题
    [转]Refactoring Game Entities with Components
    使用QT + cocos2dx制作工具
    [转]printf输出字体颜色
    Error: No module named books
  • 原文地址:https://www.cnblogs.com/qinglingyue/p/5177007.html
Copyright © 2011-2022 走看看