star.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. function getStar() {
  2. var canvas = document.createElement('canvas'),
  3. ctx = canvas.getContext('2d'),
  4. w = (canvas.width = window.innerWidth * 2),
  5. h = (canvas.height = window.innerHeight * 2),
  6. hue = 217,
  7. stars = [],
  8. count = 0,
  9. maxStars = 350; //星星数量
  10. //放背景图
  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) / 150000;
  52. //星星移动速度
  53. this.alpha = random(2, 10) / 10;
  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(
  68. canvas2,
  69. x - this.radius / 2,
  70. y - this.radius / 2,
  71. this.radius,
  72. this.radius
  73. );
  74. this.timePassed += this.speed;
  75. };
  76. for (var i = 0; i < maxStars; i++) {
  77. new Star();
  78. }
  79. function animation() {
  80. ctx.globalCompositeOperation = 'source-over';
  81. ctx.globalAlpha = 0.5; //尾巴
  82. ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
  83. ctx.fillRect(0, 0, w, h);
  84. ctx.globalCompositeOperation = 'lighter';
  85. for (var i = 1, l = stars.length; i < l; i++) {
  86. stars[i].draw();
  87. }
  88. window.requestAnimationFrame(animation);
  89. }
  90. var canvasBoxs = document.createElement('div');
  91. var images = document.createElement('img');
  92. images.src = './img/starBg.jpg';
  93. images.style.opacity = '0.4';
  94. images.style.width = '100%';
  95. images.style.height = '1280px';
  96. images.style.zIndex = '22';
  97. images.style.position = 'absolute';
  98. images.style.background = '#000';
  99. canvasBoxs.appendChild(images);
  100. canvas.style.zIndex = '23';
  101. canvasBoxs.appendChild(canvas);
  102. starBG = new THREE.CSS3DObject(canvasBoxs);
  103. starBG.position.x = 0;
  104. starBG.position.y = 0;
  105. starBG.position.z = -5000;
  106. var scal = (window.innerWidth + 10000) / window.innerWidth / 2;
  107. starBG.scale.set(scal, scal, scal);
  108. scene.add(starBG);
  109. animation();
  110. }