<!DOCTYPE html>
<html>
<head>
<style>
body{background:#000;margin:0;}
canvas{cursor:crosshair;display:block;}
</style>
</head>
<body>
<canvas id="canvas">Canvas is not supported in your browser.</canvas>
<script>
    window.requestAnimFrame=(function(){
        return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){
                window.setTimeout(callback,1000/60)
            }
    })();
    
    var canvas=document.getElementById("canvas"),ctx=canvas.getContext("2d"),cw=window.innerWidth,ch=window.innerHeight,fireworks=[],particles=[],hue=120,limiterTotal=5,limiterTick=0,timerTotal=80,timerTick=0,mousedown=false,mx,my;canvas.width=cw;canvas.height=ch;
    
    function random(min,max){return Math.random()*(max-min)+min}
    
    function calculateDistance(p1x,p1y,p2x,p2y){
        var xDistance=p1x-p2x,yDistance=p1y-p2y;
        return Math.sqrt(Math.pow(xDistance,2)+Math.pow(yDistance,2))
    }
    
    function Firework(sx,sy,tx,ty){
        this.x=sx;
        this.y=sy;
        this.sx=sx;
        this.sy=sy;
        this.tx=tx;
        this.ty=ty;
        this.distanceToTarget=calculateDistance(sx,sy,tx,ty);
        this.distanceTraveled=0;
        this.coordinates=[];
        this.coordinateCount=3;
        while(this.coordinateCount--)
            {
                this.coordinates.push([this.x,this.y])
            }
        this.angle=Math.atan2(ty-sy,tx-sx);
        this.speed=2;
        this.acceleration=1.05;
        this.brightness=random(50,70);
        this.targetRadius=1
    }
    
    Firework.prototype.update=function(index){
        this.coordinates.pop();
        this.coordinates.unshift([this.x,this.y]);
        if(this.targetRadius<8){
            this.targetRadius+=0.3
        }
            else{
                this.targetRadius=1
            }
            this.speed*=this.acceleration;
            var vx=Math.cos(this.angle)*this.speed,vy=Math.sin(this.angle)*this.speed;
            this.distanceTraveled=calculateDistance(this.sx,this.sy,this.x+vx,this.y+vy);
            if(this.distanceTraveled>=this.distanceToTarget){
                createParticles(this.tx,this.ty);
                fireworks.splice(index,1)
            }else{
                this.x+=vx;this.y+=vy
            }
    };
    
    Firework.prototype.draw=function(){
        ctx.beginPath();
        ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
        ctx.lineTo(this.x,this.y);
        ctx.strokeStyle="hsl("+hue+", 100%, "+this.brightness+"%)";
        ctx.stroke();
        ctx.beginPath();
        ctx.arc(this.tx,this.ty,this.targetRadius,0,Math.PI*2);
        ctx.stroke()
    };
    
    function Particle(x,y){
        this.x=x;this.y=y;
        this.coordinates=[];
        this.coordinateCount=5;
        while(this.coordinateCount--){
            this.coordinates.push([this.x,this.y])
        }
        this.angle=random(0,Math.PI*2);
        this.speed=random(1,10);
        this.friction=0.95;
        this.gravity=1;
        this.hue=random(hue-20,hue+20);
        this.brightness=random(50,80);
        this.alpha=1;
        this.decay=random(0.015,0.03)
    }
    
    Particle.prototype.update=function(index){
        this.coordinates.pop();
        this.coordinates.unshift([this.x,this.y]);
        this.speed*=this.friction;
        this.x+=Math.cos(this.angle)*this.speed;this.y+=Math.sin(this.angle)*this.speed+this.gravity;this.alpha-=this.decay;
        if(this.alpha<=this.decay){
            particles.splice(index,1)
        }
    };
    
    Particle.prototype.draw=function(){
        ctx.beginPath();
        ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
        ctx.lineTo(this.x,this.y);
        ctx.strokeStyle="hsla("+this.hue+", 100%, "+this.brightness+"%, "+this.alpha+")";
        ctx.stroke()
    };
    
    function createParticles(x,y){
        var particleCount=30;
        while(particleCount--){
            particles.push(new Particle(x,y))
        }
    }
    
    function loop(){
        requestAnimFrame(loop);
        hue+=0.5;
        ctx.globalCompositeOperation="destination-out";
        ctx.fillStyle="rgba(0, 0, 0, 0.5)";
        ctx.fillRect(0,0,cw,ch);
        ctx.globalCompositeOperation="lighter";
        var i=fireworks.length;
        while(i--){
            fireworks[i].draw();fireworks[i].update(i)
        }
        var i=particles.length;
        while(i--){
            particles[i].draw();particles[i].update(i)
        }
        if(timerTick>=timerTotal){
            if(!mousedown){
                fireworks.push(new Firework(cw/2,ch,random(0,cw),random(0,ch/2)));
                timerTick=0
            }
        }
        else{ timerTick++}
        
        if(limiterTick>=limiterTotal){
                if(mousedown){
                    fireworks.push(new Firework(cw/2,ch,mx,my));
                    limiterTick=0
                }
        }
        else{ limiterTick++}
    }
    
    canvas.addEventListener("mousemove",function(e){
        mx=e.pageX-canvas.offsetLeft;
        my=e.pageY-canvas.offsetTop
    });
    canvas.addEventListener("mousedown",function(e){
        e.preventDefault();mousedown=true
    });
    canvas.addEventListener("mouseup",function(e){
        e.preventDefault();mousedown=false
    });
    window.onload=loop;
</script>
</body>
</html>

js烟花特效的更多相关文章

  1. 未封装的js放大镜特效

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...

  2. 第五章 JS典型特效

    注意: 1.JS在HTML中从上到下依次执行,所以获取元素的结果与JS的位置有关 <!DOCTYPE html> <html> <head> <title&g ...

  3. CSS3实现烟花特效 --web前端

    烟花特效,比较简单,直接贴代码了…… <!DOCTYPE html><html lang="en"><head> <meta charse ...

  4. Three.js粒子特效,shader渲染初探(一篇非常详细的介绍)

    Three.js粒子特效,shader渲染初探 转载来源:https://juejin.im/post/5b0ace63f265da0db479270a 这大概是个序 关于Three.js,网上有不多 ...

  5. js 时钟特效

      时钟特效 CreateTime--2018年2月24日15:11:23 Author:Marydon 实现方式:都是基于HTML5的canvas标签实现的 款式一 借助jQuery插件实现 < ...

  6. 墙裂推荐4款js网页烟花特效

    以下是几款网页特效和一款软件: http://keleyi.com/keleyi/phtml/jstexiao/1.htm  http://keleyi.com/keleyi/phtml/jstexi ...

  7. 原生JS投票特效

    效果:http://hovertree.com/texiao/js/24/ 效果图: 代码如下: <!DOCTYPE html> <html lang="en"& ...

  8. Js文字特效—文字段逐个变色循环

    自己用来练习的,附上详细注释,如果有和我一样喜欢并想要学习Dom特效创作的朋友,推荐先系统了解Javascript中Html Dom Object部分的内容,包括常用方法及属性. <!DOCTY ...

  9. javascript js写特效日历

    <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. SQLServer怎样导入excel

    --从Excel文件里,导入数据到SQL数据库中,非常easy,直接用以下的语句: /*======================================================== ...

  2. careercup-链表 2.5

    2.5 给定两个用链表表示的整数,每个结点包含一个数位.这些数位是反向存放的,也就是个位排在链表首部.编写函数对这两个整数求和,并用链表形式返回结果. 示例: 输入: (7->1->6)+ ...

  3. C#绘制圆形时钟

    本文由作者参考部分案例后加以修改完成: 参考链接如下: http://blog.csdn.net/xuemoyao/article/details/8001113 http://wenku.baidu ...

  4. java 中能否使用 动态加载的类(Class.forName) 来做类型转换?

    今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...

  5. web前端:html

    一.理解表单的作用 1.web 应用程序不仅仅是给用户显示数据,还应该给用户提供一个可以输入数据的图形用户界面.表单的主要作用在于在网页上提供一个图形用户界面,已采集和提交用户输入的数据. 2.htm ...

  6. 《转载》CSS中的三种样式来源:创作人员、读者和用户代理

    CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式,很多第一次学习CSS的朋友,对这三种来源可能会存在一些困惑,下面我写一下自己的理解,若有错误的地方还请指正. ...

  7. C# 与 C++ 交互

    参考: http://www.cnblogs.com/liping13599168/archive/2011/03/31/2000320.html Platform Invoke Tutorial:h ...

  8. 对于百川SDK签名验证的问题

    SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...

  9. 域名解析-delphi 源码

    unit Unit1; interface uses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  10. jQuery easyUI框架中经常出现的问题

    相信开发者对于我们jquery来说都不会陌生吧,jquery为我们的开发提供了很多各式各样的库,满足各种开发的需求,其中我们知道的有轻量级的,但是也有一些基于富客服端的一些重量级库,顾名思义,当我们在 ...