Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
canvas { position: absolute; top: 0; left: 0; background-color: black; }
JavaScript
//==//==============// // canvas setup // //==//==============// var ctx = c.getContext( '2d' ), width = c.width = window.innerWidth, height = c.height = window.innerHeight, //==//==================// // a few parameters // //==//==================// particleCount = ( width / 2 ) |0, obstacleCount = ( ( width + height ) / 20 ) |0, obstacleRadius = 100, gravity = .1, //==//========================// // other needed variables // //==//========================// particles = [], obstacles = [], frame = 0, tau = Math.PI * 2, //==//=========================// // initialization function // //==//=========================// init = function( ) { // initial fade to cancel repaint leftover lines, pointed out by the great lemon ctx.fillStyle = '#222'; ctx.fillRect( 0, 0, width, height ); while( --particleCount ) particles.push( new Particle ); while( --obstacleCount ) obstacles.push( new Obstacle ); anim(); }, //==//===================================// // animation loop and main functions // //==//===================================// anim = function( ) { window.requestAnimationFrame( anim ); step( ); draw( ); }, step = function( ) { particles.map( function( particle ) { particle.step( ); } ); obstacles.map( function( obstacle ) { obstacle.step( ); } ); }, draw = function( ) { ctx.globalCompositeOperation = 'destination-out'; ctx.shadowBlur = 0; ctx.fillStyle = 'rgba(0, 0, 0, .1)'; ctx.fillRect( 0, 0, width, height ); ctx.globalCompositeOperation = 'lighter'; particles.map( function( particle ) { particle.draw( ); } ); ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = 'rgba(25, 25, 25, .2)'; // purposely didn't change ctx.shadowColor obstacles.map( function( obstacle ) { obstacle.draw( ); } ); }, spawnNew = function( x, y ) { var part = new Particle; part.x = x; part.y = y; particles.push( part ); }, //==//===================// // utility functions // //==//===================// checkDistance = function( par, obs ) { var x = par.x - obs.x, y = par.y - obs.y, d = obs.radius; return d*d >= x*x + y*y; }, rand = function( min, max ) { return Math.random( ) * ( max - min ) + min; }, validateObstacleResize = function( obs ) { obs.validateResize( ); }, //==//====================================// // Particle and Obstacle constructors // //==//====================================// Particle = function( ) { this.reset( ); }, Obstacle = function( ) { this.reset( ); }; Particle.prototype = { reset: function( ) { this.x = rand( 0, width ) |0; this.y = rand( -obstacleRadius, 0 ) |0; this.vx = this.vy = this.shine = 0; this.last = { x: this.x, y: this.y }; this.color = 'hsla(hue, 80%, 50%, alp)' .replace( 'hue', ( frame + 360 * this.x / width) |0 ); }, step: function( ) { if(this.shine) --this.shine; this.last.x = this.x; this.last.y = this.y; this.x += this.vx; this.y += this.vy += gravity; if( ( this.x < 0 || this.x > width ) && Math.random( ) < .4 ) this.vx *= -1; if( this.y < -obstacleRadius || this.y > height ) { frame += .3; this.reset( ); } var len = obstacles.length; while( --len + 1 ) { if( checkDistance( this, obstacles[ len ] ) ) { this.hit( obstacles[ len ] ); len = 0; } } }, hit: function( obs ) { this.vx = ( this.x - obs.x ) * rand( .02, .03 ); this.vy = ( this.y - obs.y ) * rand( .02, .03 ); this.shine = 2; ++obs.toRemove; }, draw: function( ) { ctx.strokeStyle = this.color.replace( 'alp', .4 ); ctx.beginPath( ); ctx.moveTo( this.last.x, this.last.y ); ctx.lineTo( this.x , this.y ); ctx.stroke( ); if( this.shine ) { ctx.shadowColor = ctx.strokeStyle; ctx.shadowBlur = this.shine * 3; ctx.fillStyle = this.color.replace( 'alp', .05 ); ctx.beginPath( ); ctx.arc( this.x |0, this.y |0, this.shine * 8, 0, tau ); ctx.fill( ); } else ctx.shadowBlur = 0; } } Obstacle.prototype = { reset: function( ) { this.x = rand( 0, width ) |0; this.y = rand( obstacleRadius * 2, height ) |0; this.maxRadius = rand( obstacleRadius / 2, obstacleRadius ) |0; this.radius = rand( this.maxRadius / 2, this.maxRadius ); this.new = 30; this.toRemove = 0; }, step: function( ) { if( this.new ) this.new -= 5; if( this.toRemove ) { --this.radius; --this.toRemove; if( this.radius < 0) this.reset( ); } else if ( this.radius < this.maxRadius ) ++this.radius; }, draw: function( ) { ctx.shadowBlur = this.new; ctx.beginPath( ); ctx.arc( this.x, this.y, this.radius, 0, tau ); ctx.fill( ); }, validateResize: function( ) { if( this.x > width || this.y > height ) this.reset( ); } } //==//=================// // and here we go! // //==//=================// init( ); //==//================// // resize handler // //==//================// window.addEventListener( 'resize', function( ) { width = c.width = window.innerWidth; height = c.height = window.innerHeight; ctx.fillStyle = '#222'; ctx.fillRect( 0, 0, width, height ); obstacles.map( validateObstacleResize ); } ); //==//==============// // add on click // //==//==============// window.addEventListener( 'click', function() { for( var i = 0; i < 10; ++i ) particles.push( new Particle ); } );
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>彩虹风暴-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号