bg.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //宇宙特效
  2. "use strict";
  3. var canvas = document.getElementById('canvas'),
  4. ctx = canvas.getContext('2d'),
  5. w = canvas.width = window.innerWidth,
  6. h = canvas.height = window.innerHeight,
  7. hue = 217,
  8. stars = [],
  9. count = 0,
  10. maxStars = 1300;//星星数量
  11. var canvas2 = document.createElement('canvas'),
  12. ctx2 = canvas2.getContext('2d');
  13. canvas2.width = 100;
  14. canvas2.height = 100;
  15. var half = canvas2.width / 2,
  16. gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
  17. gradient2.addColorStop(0.025, '#CCC');
  18. gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');
  19. gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');
  20. gradient2.addColorStop(1, 'transparent');
  21. ctx2.fillStyle = gradient2;
  22. ctx2.beginPath();
  23. ctx2.arc(half, half, half, 0, Math.PI * 2);
  24. ctx2.fill();
  25. // End cache
  26. function random(min, max) {
  27. if (arguments.length < 2) {
  28. max = min;
  29. min = 0;
  30. }
  31. if (min > max) {
  32. var hold = max;
  33. max = min;
  34. min = hold;
  35. }
  36. return Math.floor(Math.random() * (max - min + 1)) + min;
  37. }
  38. function maxOrbit(x, y) {
  39. var max = Math.max(x, y),
  40. diameter = Math.round(Math.sqrt(max * max + max * max));
  41. return diameter / 2;
  42. //星星移动范围,值越大范围越小,
  43. }
  44. var Star = function() {
  45. this.orbitRadius = random(maxOrbit(w, h));
  46. this.radius = random(60, this.orbitRadius) / 8;
  47. //星星大小
  48. this.orbitX = w / 2;
  49. this.orbitY = h / 2;
  50. this.timePassed = random(0, maxStars);
  51. this.speed = random(this.orbitRadius) / 1000000;
  52. //星星移动速度
  53. this.alpha = random(2, 10)/1000;
  54. count++;
  55. stars[count] = this;
  56. }
  57. Star.prototype.draw = function() {
  58. var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
  59. y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
  60. twinkle = random(10);
  61. if (twinkle === 1 && this.alpha > 0) {
  62. this.alpha -= 0.05;
  63. } else if (twinkle === 2 && this.alpha < 1) {
  64. this.alpha += 0.05;
  65. }
  66. ctx.globalAlpha = this.alpha;
  67. ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);
  68. this.timePassed += this.speed;
  69. }
  70. for (var i = 0; i < maxStars; i++) {
  71. new Star();
  72. }
  73. function animation() {
  74. ctx.globalCompositeOperation = 'source-over';
  75. ctx.globalAlpha = 0.5; //尾巴
  76. ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
  77. ctx.fillRect(0, 0, w, h)
  78. ctx.globalCompositeOperation = 'lighter';
  79. for (var i = 1, l = stars.length; i < l; i++) {
  80. stars[i].draw();
  81. };
  82. window.requestAnimationFrame(animation);
  83. }
  84. animation();