本文实例讲述了JavaScript实现的简单烟花特效代码。分享给大家供大家参考,具体如下:

这是一款JavaScript烟花特效,过年的时候放到你的网页上祝贺大家牛年大吉吧,是不是很不错?

运行效果截图如下:

在线演示地址如下:

具体代码如下:

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

礼花特效

html,body{background:#000; height:100%; margin:0px; padding:0px;color:#FFF;}

.ball{color:#FF0000; position:absolute; font-size:16px;}

.star{color:#FF0000; position:absolute; font-size:4px;}

function Fireworks(sky, loop){

this.sky = sky;

this.skyWidth = document.body.clientWidth || document.documentElement.clientWidth;

this.skyHeight= document.body.clientHeight || document.documentElement.clientHeight;

this.x = this.y = 0;

this.step = 20;

this.delay = 30;

this.stars = [];

this.r = 10;

this.step2 = 7;

this.radius = 90;

this.angle = 45;

this.num = 16;

this.loop = loop;

this.degree = 2;

this.t = 0;

this.delt = 0;

this.max = 30;

this.cur = 1;

this.points = null;

}

Fireworks.prototype = {

init : function(){

this.x = parseInt(this.skyWidth/1.3 * Math.random()) + this.skyWidth / 8;

this.y = this.skyHeight;

this._y = parseInt((this.skyHeight / 4) * Math.random()) + this.skyHeight / 5;

},

setOpacity : function(obj, p){

if(p > 85){

var opacity = 100 - (p - 85) * 4;

if(document.all){

obj.style.filter = "alpha(opacity=" + opacity + ")";

}else{

obj.style.MozOpacity = opacity / 100;

}

}

},

getNextPoint : function(degree, coeff, t){

var tt = 1.0 - t;

for(var rr = 1; rr <= degree; rr++){

for(var i=0; i <= degree-rr; i++){

coeff[i] = tt * coeff[i] + t * coeff[i+1];

}

}

return coeff[0];

},

showBall : function(){

this.ball = document.createElement("div");

this.ball.innerHTML = "●";

this.ball.className = "ball";

this.ball.style.left = this.x + "px";

this.ball.style.top = this.y + "px";

this.sky.appendChild(this.ball);

},

moveBall : function(){

var self = this;

if(this.y > this._y){

var p = parseInt((this.skyHeight - this.y) / (this.skyHeight - this._y)*10);

this.y -= (this.step - p * 1.6);

this.ball.style.fontSize = 16 - p + "px";

this.ball.style.top = this.y + "px";

setTimeout(function(){self.moveBall();}, this.delay);

}else{

this.fire();

}

},

hideBall : function(){

this.sky.removeChild(this.ball);

this.ball = null;

},

showStars : function(){

var colors = ['#FF0000','#FF00FF','#00FF00','#00FFFF','#FFFF00','#FF0000','#FF00FF','#00FF00','#00FFFF','#FFFF00'];

var n = cs = parseInt(Math.random() * colors.length / 2);

var cc = parseInt(Math.random() * colors.length / 2);

var colorMode = parseInt(Math.random() * 2);

var star = Math.round(Math.random()) == 1 ? "★" : "☆";

this.r = 10;

this.radius = Math.round(Math.random() * 30) + 60;

this.num = Math.round(Math.random() * 5 + 5) * 2;

this.angle = 180 / this.num * 2;

for(var i=1; i<=this.num; i++){

this.stars[i] = document.createElement("div");

this.stars[i].innerHTML = star;

this.stars[i].className = "star";

if(colorMode == 1){

this.stars[i].style.color = colors[n];

if(++n > cs + cc)

n = cs;

}else{

this.stars[i].style.color = colors[parseInt(Math.random() * colors.length)];

}

this.sky.appendChild(this.stars[i]);

}

},

moveStars : function(){

var self = this;

if(this.r < this.radius){

var p = this.step2 - parseInt(this.r / this.radius * 5);

p = p < 1 ? 1 : p;

this.r += p;

p = parseInt(this.r / this.radius * 100);

for(var i=1; i<=this.num; i++){

this.stars[i].style.left = this.x - Math.round(this.r * Math.sin(Math.PI - (Math.PI / 180 * this.angle * i))) + "px";

this.stars[i].style.top = this.y - Math.round(this.r * Math.cos(Math.PI - (Math.PI / 180 * this.angle * i))) + "px";

this.stars[i].style.fontSize = 4 + p/10 + "px";

this.setOpacity(this.stars[i], p);

}

setTimeout(function(){self.moveStars();}, this.delay);

}else{

setTimeout(function(){self.hideStars();}, 200 * Math.random());

}

},

initBezier : function(){

var coeff_x = [];

var coeff_y = [];

this.points = [];

this.t = 0;

this.delt = 1.0 / this.max;

this.cur = 1;

var a = parseInt(Math.random() * 5) * 90;

coeff_x[0] = this.x;

coeff_y[0] = this.y;

for(var i=1; i<=this.num; i++){

coeff_x[1] = this.x + Math.sin(Math.PI - (Math.PI / 180 * this.angle * i)) * this.radius/2;

coeff_y[1] = this.y + Math.cos(Math.PI - (Math.PI / 180 * this.angle * i)) * this.radius/2;

coeff_x[2] = this.x + Math.sin(Math.PI - (Math.PI / 180 * (a-this.angle * i))) * this.radius;

coeff_y[2] = this.y + Math.cos(Math.PI - (Math.PI / 180 * (a-this.angle * i))) * this.radius;

this.points[(i-1)*2] = coeff_x.slice(0);

this.points[(i-1)*2+1] = coeff_y.slice(0);

}

},

moveStars2 : function(){

var self = this;

if(this.cur < this.max){

this.t += this.delt;

this.cur++;

p = parseInt(this.cur / this.max * 100);

for(var i=1; i<=this.num; i++){

this.stars[i].style.left = this.getNextPoint(this.degree, this.points[(i-1)*2], this.t) + "px";

this.stars[i].style.top = this.getNextPoint(this.degree, this.points[(i-1)*2+1], this.t) + "px";

this.stars[i].style.fontSize = 4 + p/10 + "px";

this.setOpacity(this.stars[i], p);

}

setTimeout(function(){self.moveStars2();}, this.delay);

}else{

setTimeout(function(){self.hideStars();}, 200 * Math.random());

}

},

hideStars : function(){

for(var i=1; i<=this.num; i++){

this.sky.removeChild(this.stars[i]);

this.stars[i] = null;

if(this.points != null){

delete this.points[(i-1)*2];

delete this.points[(i-1)*2+1];

}

}

if(this.points){

delete this.points;

}

this.points = null;

if(this.loop){

this.play();

}

},

fire : function(){

this.hideBall();

this.showStars();

var effect = parseInt(Math.random() * 2) + 1;

switch(effect){

case 1:

this.moveStars();

break;

case 2:

this.initBezier();

this.moveStars2();

break;

}

},

play : function(){

this.init();

this.showBall();

this.moveBall();

}

};

window.onload = function(){

for(var i=0; i<5; i++)

new Fireworks(document.body, true).play();

};

希望本文所述对大家JavaScript程序设计有所帮助。

java简单烟花代码_JavaScript实现的简单烟花特效代码相关推荐

  1. html图片鼠标动态效果代码,CSS3鼠标hover图片动画特效代码

    这是一款CSS3鼠标hover图片动画特效代码.该鼠标hover动画使用简单的CSS transition技术,配合元素的宽度变化,制作出炫酷的鼠标hover图片遮罩层动画效果. 使用方法 在页面中引 ...

  2. html特效代码是怎么实现的,html特效代码大全

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 23. 永远都会带着框架 if (window == top)top.location.href = "frames.htm"; // ...

  3. 怎么用html做出星空页面,用HTML5+canvas代码绘制的星空月亮图案特效代码

    脚本代码(For Alixixi.com)如下: 用HTML5绘制的一个星空特效图 canvas{ display: block;border:1px dotted skyblue; } body{ ...

  4. java获取redis中各种数据类型key对应的value代码简单封装

    来源:http://blog.csdn.net/russ44/article/details/52121180 目前在做自动化测试时,设计到需要获取存储在redis中的值,总结了操作代码如下: 需要j ...

  5. java判断回文字符串几种简单的实现

    11年it研发经验,从一个会计转行为算法工程师,学过C#,c++,java,android,php,go,js,python,CNN神经网络,四千多篇博文,三千多篇原创,只为与你分享,共同成长,一起进 ...

  6. JAVA Bean和XML之间的相互转换 - XStream简单入门

    JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...

  7. Java设计模式-工厂模式(1)简单工厂模式

    Java设计模式-工厂模式(1)简单工厂模式 一.前言 1)例子 2)类图关系 3)代码实现 二.简单工厂模式 2.1.概述: 2.2.类图关系: 2.3.代码修改: 2.4.优缺点 2.5.扩展-简 ...

  8. Java Tread多线程(0)一个简单的多线程实例

    作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39341887 本文演示,一个简单的多线程实例,并简单分析一下线程. 编程多 ...

  9. java的简单工厂模式_java设计模式之简单工厂模式

    简单工厂模式的概念 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例. ...

  10. JAVA 文件编译执行与虚拟机(JVM)简单介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytpo3 java程序的内存分配 JAVA 文件编译执行与虚拟机(JVM)介绍 ...

最新文章

  1. 使用YII2 构建一个定时任务
  2. python抓取头条文章
  3. AIRAVATA:量化机器学习中的参数泄露
  4. 天津大学计算机学院院长及副院长,李晓红 教授
  5. 判断文件夹存在_Excel VBA之FSO-2.3文件夹的移动
  6. python---基础知识回顾(十)进程和线程(协程gevent:线程在I/O请求上的优化)...
  7. es java 创建索引_Elasticsearch(ES) 创建索引
  8. 修改 ubnt 路由器固件
  9. OpenCV~捕获摄像头 帧率fps和waitkey函数 问题
  10. CTP 4097错误根源 / CTP程序运行没有反应/CTP版本说明
  11. Pycharm社区版下载与安装教程
  12. 海康练习设备网络抓图错误代码29
  13. 使用codesense的GJB 8114模板对c++源代码规则检测示例
  14. linux下libxml2库使用说明
  15. SAP MM 物料主数据-物料版次是配置及使用
  16. 雨流计数法的matlab实现
  17. Android模仿360动态悬浮窗,像360悬浮窗那样,用WindowManager实现炫酷的悬浮迷你音乐盒(下)...
  18. SQL数据库临时表创建和临时表拼接查询
  19. UserWarning: image file could not be identified because WEBP support not install
  20. ssm毕设项目创梦宝大学生创业众筹平台cds88(java+VUE+Mybatis+Maven+Mysql+sprnig)

热门文章

  1. 制作U盘安装UBUNTU
  2. android内存占用分析,Android内存优化————虚引用与弱引用的使用及内存分析工具...
  3. CentOS 6.6上搭建C++运行环境
  4. 使用内存映射文件在进程间共享数据
  5. 苹果偷学微信代码,程序员小哥:天知道微信为什么会引用这个变量
  6. Java实现简易的文本编辑器
  7. Android系统服务分析与Native Service实例
  8. java编程思想之多态理解
  9. App 抓包-Fiddler简单使用教程
  10. AX2012/D365 批处理如何创建