基于canvas画布的星空效果

canvas是HTML5的新标签,其画布功能尤为强大。当然了canvas在IE10以下浏览器是不兼容的,所以呢为了特效肯定是牺牲一定的兼容性。这里呢,分享一个基于canvas开发的浩瀚星河插件,其实这个源代码是网上下载的,我把它整合了一下,重新封装一些参数提供更多的可改项。

首先引入两个JavaScript脚本,一个是jQuery插件,另一个是封装好的cosmos_canvas.js

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>  
  2. <script src="canvas/cosmos_canvas.js" type="text/javascript" charset="utf-8"></script>  

HTML,当然如果浏览器不支持canvas的话,运行程序没效果,并显示“该浏览器不支持canvas”

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <canvas id="starts">该浏览器不支持canvas</canvas>  

cosmos_canvas.js脚本

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. function canvas(id,starscolor,starsamount,starsradius,movrange,speed,trailing){  
  2.     //宇宙特效  
  3. "use strict";  
  4. var canvas = document.getElementById(id),  
  5.   ctx = canvas.getContext('2d'),  
  6.   w = canvas.width = window.innerWidth,  
  7.   h = canvas.height = window.innerHeight,  
  8.   
  9.   hue = starscolor,//230  
  10.   stars = [],  
  11.   count = 0,  
  12.   maxStars = starsamount;//星星数量  
  13.   
  14. var canvas2 = document.createElement('canvas'),  
  15.   ctx2 = canvas2.getContext('2d');  
  16. canvas2.width = 100;  
  17. canvas2.height = 100;  
  18. var half = canvas2.width / 2,  
  19.   gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);  
  20. gradient2.addColorStop(0.025, '#CCC');  
  21. gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');  
  22. gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');  
  23. gradient2.addColorStop(1, 'transparent');  
  24.   
  25. ctx2.fillStyle = gradient2;  
  26. ctx2.beginPath();  
  27. ctx2.arc(half, half, half, 0, Math.PI * 2);  
  28. ctx2.fill();  
  29.   
  30. // End cache  
  31.   
  32. function random(min, max) {  
  33.   if (arguments.length < 2) {  
  34.     max = min;  
  35.     min = 0;  
  36.   }  
  37.   
  38.   if (min > max) {  
  39.     var hold = max;  
  40.     max = min;  
  41.     min = hold;  
  42.   }  
  43.   
  44.   return Math.floor(Math.random() * (max - min + 1)) + min;  
  45. }  
  46.   
  47. function maxOrbit(x, y) {  
  48.   var max = Math.max(x, y),  
  49.     diameter = Math.round(Math.sqrt(max * max + max * max));  
  50.   return diameter / movrange;  
  51.   //星星移动范围,值越大范围越小,  
  52. }  
  53.   
  54. var Star = function() {  
  55.   
  56.   this.orbitRadius = random(maxOrbit(w, h));  
  57.   this.radius = random(starsradius, this.orbitRadius) / 8;   
  58.   //星星半径大小  
  59.   this.orbitX = w / 2;  
  60.   this.orbitY = h / 2;  
  61.   this.timePassed = random(0, maxStars);  
  62.   this.speed = random(this.orbitRadius) / speed;   
  63.   //星星移动速度  
  64.   this.alpha = random(2, 10) / 10;  
  65.   
  66.   count++;  
  67.   stars[count] = this;  
  68. }  
  69.   
  70. Star.prototype.draw = function() {  
  71.   var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,  
  72.     y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,  
  73.     twinkle = random(10);  
  74.   
  75.   if (twinkle === 1 && this.alpha > 0) {  
  76.     this.alpha -= 0.05;  
  77.   } else if (twinkle === 2 && this.alpha < 1) {  
  78.     this.alpha += 0.05;  
  79.   }  
  80.   
  81.   ctx.globalAlpha = this.alpha;  
  82.   ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);  
  83.   this.timePassed += this.speed;  
  84. }  
  85.   
  86. for (var i = 0; i < maxStars; i++) {  
  87.   new Star();  
  88. }  
  89.   
  90. function animation() {  
  91.   ctx.globalCompositeOperation = 'source-over';  
  92.   ctx.globalAlpha = trailing; //尾巴  
  93.   ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';  
  94.   ctx.fillRect(0, 0, w, h)  
  95.   
  96.   ctx.globalCompositeOperation = 'lighter';  
  97.   for (var i = 1, l = stars.length; i < l; i++) {  
  98.     stars[i].draw();  
  99.   };  
  100.   
  101.   window.requestAnimationFrame(animation);  
  102. }  
  103.   
  104. animation();  
  105. }  
调用方法:这里没写任何样式所以默认情况下是全屏效果

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $(function(){  
  2. //canvas的id名,星星颜色(hsla的hue色调),星星数量,星星半径比,星星移动范围(值越大范围越小),星星移动速度(值越大速度越慢),星星拖尾效果(0~1值越小拖尾越明显)  
  3. canvas("starts",230,1000,60,2,50000,0.5);  
  4. });  
效果:



然后我再加3个canvas,改些参数做效果对比吧

HTML

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <canvas id="starts">该浏览器不支持canvas</canvas>  
  2. <canvas id="starts1">该浏览器不支持canvas</canvas>  
  3. <canvas id="starts2">该浏览器不支持canvas</canvas>  
  4. <canvas id="starts3">该浏览器不支持canvas</canvas>  
javascript

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $(function(){  
  2.     //canvas的id名,星星颜色(hsla的hue色调),星星数量,星星半径比,星星移动范围(值越大范围越小),星星移动速度(值越大速度越慢),星星拖尾效果(0~1值越小拖尾越明显)  
  3. <span style="white-space:pre">  </span>canvas("starts",230,1000,60,2,50000,0.5);  
  4. <span style="white-space:pre">  </span>canvas("starts1",160,800,70,2.5,55000,0.4);  
  5. <span style="white-space:pre">  </span>canvas("starts2",80,600,80,3,60000,0.3);  
  6. <span style="white-space:pre">  </span>canvas("starts3",0,400,90,3.5,65000,0.2);  
  7. <span style="white-space:pre">  </span>$("canvas").width($(window).width()/2);  
  8. <span style="white-space:pre">  </span>$("canvas").height($(window).height()/2);  
  9. <span style="white-space:pre">  </span>$("canvas").css("float","left");  
  10. });  
就这样,四个浩瀚的宇宙出来了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值