canvs 实现进度条效果(烟火效果)

新手试一下canvas:先贴出效果(自己运行试一下)


贴出源码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        body{padding:0px;margin:0px;overflow: hidden;}
        #cas{display: block;border:1px solid;margin:auto;}
    </style>
    <title>progressBar</title>
</head>
<body>
<div class="game">
    <canvas id='cas' width="700" height="750" >您的浏览器不支持canvas,请更新浏览器</canvas>
</div>
<script>
    var canvas = document.getElementById("cas");
    var ctx = canvas.getContext("2d");
    var progress = 0;
    var flag = true;
    var booms = [];


    Array.prototype.foreach = function(callback){
        for(var i=0;i<this.length;i++){
            if(this[i]!==null) callback.apply(this[i] , [i])
        }
    }


    window.RAF = (function(){
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {window.setTimeout(callback, 1000 / 60); };
    })();


    function getRandom(a , b){
        return Math.random()*(b-a)+a;
    }


    var Frag = function(centerX , centerY , radius , color ,tx , ty){   //烟火碎屑对象
        this.tx = tx;
        this.ty = ty;
        this.x = centerX;
        this.y = centerY;
        this.dead = false;
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.color = color;
    }


    Frag.prototype = {
        paint:function(){
            ctx.save();
            ctx.beginPath();
            ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
            ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)";
            ctx.fill()
            ctx.restore();
        },
        moveTo:function(index){
            this.ty = this.ty+30;
            var dx = this.tx - this.x , dy = this.ty - this.y;
            this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1);
            this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1);
            if(dx===0 && Math.abs(dy)<=80){
                this.dead = true;
            }
            this.paint();
        }
    }


    var stage = {


        update:function(){


                if(progress<=600){
                    ctx.save();
                    ctx.fillStyle = "rgba(0,5,24,0.1)";
                    ctx.fillRect(0,0,canvas.width,canvas.height);//实现烟火尾巴效果
                    ctx.restore();
                    ctx.save();
                    ctx.fillStyle="blue";
                    ctx.fillRect(50,300,600,50);
                    ctx.restore();


                    ctx.save();
                    var gradient=ctx.createLinearGradient(50,300,600,50);
                    gradient.addColorStop("0","grey");
                    gradient.addColorStop("0.5","black");
                    gradient.addColorStop("1.0","red");//进度条渐变
                    ctx.fillStyle=gradient;
                    ctx.fillRect(50,300,progress,50);
                    progress += 2;
                    var  percentage= parseInt(progress/600*100)+"%";
                    ctx.font="30px Verdana";
                    ctx.fillText(percentage,200,200);
                    ctx.restore();


                    booms = [];
                    for(var i = 0;i<5;i++){
                        var  color = {
                            a:parseInt(getRandom(128,255)),
                            b:parseInt(getRandom(128,255)),
                            c:parseInt(getRandom(128,255))
                                        }    //end color
                        var fanwei = parseInt(getRandom(300, 400));
                        var a = getRandom(-Math.PI, -Math.PI*3/4);
                        var x = getRandom(0, fanwei) * Math.cos(a) + progress+50;
                        var y = getRandom(0, fanwei) * Math.sin(a) + 300;


                        var frag = new Frag( progress+50 , 300 , 2 , color , x , y );
                       booms.push(frag);
                    }//end for


                    booms.foreach(function(){
                        this.moveTo();
                    })
                }else{
                    flag =false;
                }


        },


        loop:function(){
            var _this = this;
            if(flag){
            this.update();
            }
            RAF(function(){
                _this.loop();
            });
        },


        start:function(){
            this.loop();
        }
    }
    stage.start();
</script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现水流效果主要需要以下步骤: 1. 绘制水流路径 使用 Canvas 绘制一条水流路径,可以通过 bezierCurveTo() 方法绘制贝塞尔曲线来实现。 2. 绘制水流 使用 Canvas 绘制水流,可以通过绘制多个圆形来实现,圆形的位置和大小可以根据时间和水流路径计算得到。 3. 实现动画效果 在每一帧中,重新计算水流位置和大小,然后清空 Canvas 并重新绘制水流。 以下是一个简单的 Canvas 水流效果实现示例代码: ```javascript var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var waterPath = []; // 水流路径 var waterDrops = []; // 水滴 var waterSpeed = 0.01; // 水速 var waterRadius = 10; // 水滴半径 var waterColor = '#00A0E9'; // 水颜色 var lastTime = 0; // 上一帧时间戳 // 添加水流路径点 function addWaterPath(x, y) { waterPath.push({x: x, y: y}); } // 绘制水流路径 function drawWaterPath() { ctx.beginPath(); ctx.moveTo(waterPath[0].x, waterPath[0].y); for (var i = 1; i < waterPath.length - 2; i++) { var c = (waterPath[i].x + waterPath[i + 1].x) / 2; var d = (waterPath[i].y + waterPath[i + 1].y) / 2; ctx.quadraticCurveTo(waterPath[i].x, waterPath[i].y, c, d); } ctx.quadraticCurveTo(waterPath[i].x, waterPath[i].y, waterPath[i + 1].x, waterPath[i + 1].y); ctx.strokeStyle = waterColor; ctx.lineWidth = 2; ctx.stroke(); } // 添加水滴 function addWaterDrop() { var t = Date.now(); var deltaTime = t - lastTime; lastTime = t; var speed = waterSpeed * deltaTime; for (var i = 0; i < waterPath.length - 1; i++) { var distance = Math.sqrt(Math.pow(waterPath[i + 1].x - waterPath[i].x, 2) + Math.pow(waterPath[i + 1].y - waterPath[i].y, 2)); // 水流路径长度 var count = Math.floor(distance / speed); // 水滴数量 var x = waterPath[i].x, y = waterPath[i].y; for (var j = 0; j < count; j++) { x += (waterPath[i + 1].x - waterPath[i].x) / count; y += (waterPath[i + 1].y - waterPath[i].y) / count; var r = waterRadius * (1 - j / count); waterDrops.push({x: x, y: y, r: r}); } } } // 绘制水滴 function drawWaterDrops() { for (var i = 0; i < waterDrops.length; i++) { ctx.beginPath(); ctx.arc(waterDrops[i].x, waterDrops[i].y, waterDrops[i].r, 0, 2 * Math.PI); ctx.fillStyle = waterColor; ctx.fill(); } } // 实现动画效果 function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, canvas.width, canvas.height); addWaterDrop(); drawWaterDrops(); drawWaterPath(); } // 初始化水流路径 addWaterPath(100, 300); addWaterPath(200, 200); addWaterPath(300, 400); addWaterPath(400, 200); addWaterPath(500, 300); // 开始动画 animate(); ``` 需要注意的是,这个示例代码中使用了 requestAnimationFrame() 方法来实现动画效果,这是一种更加高效和流畅的动画实现方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值