看了一个烟花的html作品 --引用:http://www.w3cfuns.com/blog-5444049-5404365.html
最近老大想把项目改成响应式,一直在学习没时间更新博客。今天看到一个原生的js烟花项目,感觉很好,把记下来,以后把妹用。
[run]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width"/>
<title>烟花</title>
<style type="text/css">
body{
background-color: black;
margin: 0;
}
#canvas{
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="text/javascript">
window.requestAnimFrame = (function () {
return window.requestAnimFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback,1000 / 60);
};
})();
Function.prototype.extend = function (oContext, bIsStatic) {
var oThis = (typeof bIsStatic != 'undefined' && bIsStatic)? this: this.prototype;
for ( var prop in oContext) {
oThis[prop] = oContext[prop];
}
}
var opt = {
canvas : document.getElementById('canvas'),
ctx: document.getElementById('canvas').getContext('2d'),
w: window.innerWidth,
h: window.innerHeight,
fireworks: [],//烟花
grains: [],//粒子
x: 0,
y: 0,
mousedown: false,
hue: 0,//色调
totalLimite: 5,
limite: 0,
totalTime: 80,
time: 0
};
opt.canvas.width = opt.w;
opt.canvas.height = opt.h;
function random(min,max){
return Math.random()*(max-min)+min;
}
function calcuDis(x1,y1,x2,y2){
return Math.sqrt(Math.pow(x1-x2,2)+Math.sqrt(y1-y2,2));
}
function Firework( x1, y1, x2, y2 ) {
this.x = this.x1 = x1;
this.y = this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.target = calcuDis( x1, y1, x2, y2 );
this.distance = 0;
this.position = [];
for(var i = 2; i >= 0; i--) {
this.position.push( [ this.x, this.y ] );
}
this.angle = Math.atan2( y2 - y1, x2 - x1 );
this.speed = 2;
this.acceleration = 1.05;//加速度
this.lightness = random( 50, 70 );
this.targetRadius = 1;
}
Firework.extend({
update: function(index) {
this.position.pop();
this.position.unshift( [ this.x, this.y ] );
if( this.targetRadius < 8 ) {
this.targetRadius += 0.3;
} else {
this.targetRadius = 1;
}
this.speed *= this.acceleration;
var x = Math.cos( this.angle ) * this.speed,
y = Math.sin( this.angle ) * this.speed;
this.distance = calcuDis( this.x1, this.y1, this.x + x, this.y + y );
if( this.distance >= this.target ) {
for(var i = 50; i >= 0; i--) {
opt.grains.push( new Grain( this.x2, this.y2 ) );
}
opt.fireworks.splice( index, 1 );
} else {
this.x += x;
this.y += y;
}
},
draw: function() {
opt.ctx.beginPath();
opt.ctx.moveTo( this.position[ this.position.length - 1][ 0 ], this.position[ this.position.length - 1][ 1 ] );
opt.ctx.lineTo( this.x, this.y );
opt.ctx.strokeStyle = 'hsl(' + opt.hue + ', 100%, ' + this.lightness + '%)';
opt.ctx.stroke();
opt.ctx.closePath();
opt.ctx.beginPath();
opt.ctx.arc( this.x2, this.y2, this.targetRadius, 0, Math.PI * 2 );
opt.ctx.stroke();
opt.ctx.closePath();
}
});
function Grain( x, y ) {
this.x = x;
this.y = y;
this.position = [];
for(var i = 4; i >= 0; i--) {
this.position.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( opt.hue - 10, opt.hue + 10 );
this.lightness = random( 50, 80 );
this.alpha = 1;
this.decay = random( 0.01, 0.03 );
}
Grain.extend({
update: function(index) {
this.position.pop();
this.position.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 ) {
opt.grains.splice( index, 1 );
}
},
draw: function() {
opt.ctx.beginPath();
opt.ctx.moveTo( this.position[ this.position.length - 1 ][ 0 ], this.position[ this.position.length - 1 ][ 1 ] );
opt.ctx.lineTo( this.x, this.y );
opt.ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.lightness + '%, ' + this.alpha + ')';
opt.ctx.stroke();
opt.ctx.closePath();
}
});
function loop(){
opt.ctx.globalCompositeOperation = 'destination-out';
opt.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
opt.ctx.fillRect( 0, 0, opt.w, opt.h );
opt.ctx.globalCompositeOperation = 'lighter';
opt.hue += 0.5;
var i = opt.fireworks.length;
while( i-- ) {
opt.fireworks[ i ].draw();
opt.fireworks[ i ].update( i );
}
i = opt.grains.length;
while( i-- ) {
opt.grains[ i ].draw();
opt.grains[ i ].update( i );
}
if( opt.time >= opt.totalTime ) {
if( !opt.mousedown ) {
opt.fireworks.push( new Firework( opt.w / 2, opt.h, random( 0, opt.w ), random( 0, opt.h / 2 ) ) );
opt.time= 0;
}
} else {
opt.time++;
}
if( opt.limite >= opt.totalLimite ) {
if( opt.mousedown ) {
opt.fireworks.push( new Firework( opt.w / 2, opt.h, opt.x, opt.y ) );
opt.limite = 0;
}
} else {
opt.limite++;
}
requestAnimFrame(loop);
}
opt.canvas.onmousemove = function(e){
opt.x = e.pageX - opt.canvas.offsetLeft;
opt.y = e.pageY - opt.canvas.offsetTop;
}
opt.canvas.onmousedown = function(e){
opt.mousedown = true;
}
opt.canvas.onmouseup = function(e){
opt.mousedown = false;
}
window.onload= function(){
loop();
};
</script>
</body>
</html>
看了一个烟花的html作品 --引用:http://www.w3cfuns.com/blog-5444049-5404365.html的更多相关文章
- 独立看第一个C++程序到最终结果log----2019-04-15
本文纯为本人记录,有网上诸多参考,请勿转发! 记录可能可能有点啰嗦,自己划重点吧!! (无论是生活还是工作,如果很困惑,千万不要消极一定要勇敢积极的面对它,不用说太多懂得人自然懂,一定要解决这个疑惑就 ...
- 今天我看了一个H5游戏EUI的例子,我都快分不清我到底是在用什么语言编译了代码了,作为刚刚学习H5游戏开发的菜鸟只能默默的收集知识
今天看了一个EUI的demo,也是接触H5游戏开发的第五天了,我想看看我能不能做点什么出来,哎,自己写果然还是有问题的.在看EUI哪一个demo的时候就遇见了一些摇摆不定的问题,我觉得提出来 1.to ...
- java中使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?
java中使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变? 是引用对象的地址值不能变,引用变量所指向的对象的内容是可以改变. final变量永远指向这个对象,是一个常量指针,而 ...
- 独立看第一个C++程序到最终结果log----2019-04-16
(如果一个人夸你,千万别相信,一个人真优秀是不需要说出来的,所以别人夸你的时候也是自己最松懈的时候,千万不能飘,只能说明自己不是很差而已,世界上优秀的人很多,一直优秀到最后的人却是凤毛菱角. 如果一个 ...
- Mybatis轻松入门(仅要求会用看着一个就够了,略过源码分析部分即可)
文章目录 ==一.概念== 二.快速入门 1.开发步骤 2.环境搭建 2.1.导入Mybatis的坐标和其他坐标 2.2.创建User表 2.3.编写实体 2.4.编写UserMapper配置文件 2 ...
- 一个C#序列化时循环引用的问题
以前一直没搞懂为什么C#在做对象序列化时(Json序列化,XML序列化等)有时候会出现循环引用的问题,下面写了个例子,类People有一个属性引用了类Child,而类Child也有一个属性引用了类Pe ...
- C#面试-关于const和readonly(看了一个点赞很多的帖子有感而发!)
前景提要: 最近大家都在面试,讨论最多.最基础的问题,莫过于“关于const和readonly常见的笔试题剖析”,等等的大牛解析.我就是一个小菜,只不过,有点不敢苟同大牛的意见.废话少说,进入重点. ...
- 一个注意事项:内部类引用的外部变量必须是final的
之前写过一个项目,好久没更新了,最近翻起以前的代码,发现在这里报了一个错.(现在转到Intellij了,从前在Eclipse luna中是可以编译通过的,Eclipse mars也会报错,JDK版本都 ...
- 用python做一个烟花show
烟花效果如图(截了几个时刻的静态图): 源代码如下: # -*- coding: utf-8 -*- # Nola import tkinter as tk from PIL import Image ...
随机推荐
- node.js 的事件机制
昨天到今天, 又看了一边node 的事件模块, 觉得很神奇~ 分享一下 - -> 首先, 补充下对node 的理解: nodeJs 是一个单进程单线程应用程序, 但是通过事件和回调支持并发 ...
- UI进阶 XML解析适配 引入GDataXML文件时候 'libxml/tree.h'file not found 错误解决办法
在工程的"Build Settings"页中找到"Header Search Path"项,添加"/usr/include/libxml2" ...
- java线程之多个生产者消费者
温故一下上一节所学习的生产者消费者代码: 两个线程时: 通过标志位flag的if判断和同步函数互斥较好解决两个线程,一个生产者.一个消费者交替执行的功能 类名:ProducterConsumerDem ...
- Java排序小算法(冒泡和选择)
package MyTest; import java.util.Scanner; public class BubbleSort { public void Init(int array[]) { ...
- BZOJ 2324: [ZJOI2011]营救皮卡丘(带上下限的最小费用最大流)
这道题么= =还是有些恶心的,第一次写带上下界的网络流,整个人都萌萌哒~~~ 首先先预处理得最短路后 直接用费用流做就行了。 第一次写,还是挺好写的= = CODE: #include<cstd ...
- [Selenium With C#学习笔记] Lesson-02 Web元素定位
使用Selenium来做自动化测试,一般的流程是:查找定位元素--->操作元素--->断言,那么第一步我们需要能够完成查找并定位元素,Selenium目前提供了8种基本定位方法,可根据实际 ...
- 连连看的原生JS实现V2
对上一次的连连看程序进行了一点修改: var llk = function () { this.ReStart(); } llk.prototype = { Init: function () { / ...
- ios常用资源网址链接
M了个J博客 http://www.cnblogs.com/mjios/tag/objective-c/ Cocoa China http://www.cocoachina.com git网 ht ...
- JavaScript中数组Array方法详解
ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...
- p1221网络布线(最小生成树 Prim(普里母)算法) p1222 Watering Hole
描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路 ...