免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
歡迎蒞臨,跑跑卡丁車台灣伺服器「NO1嫩咖」俱樂部的論壇站點,提醒您,在本站發言請勿涉及不當言論、灌水、政治、辱罵..等不雅字眼,違者刪除警告,如發文有不法交易、詐欺...等實際違法行為,本站將協助蒐證,並通報網絡警察單位進行辦理,論壇良好環境需你我共同遵守原則,再次感謝您的蒞臨。
帖子
返回列表 發帖

canvas動態代碼集中區

本文存放canvas代碼

星空特效
DEMO:http://no1.imotor.com/templates/default/ban3f.htm
  1. <script>
  2. //宇宙特效
  3. "use strict";
  4. var canvas = document.getElementById('canvas'),
  5.   ctx = canvas.getContext('2d'),
  6.   w = canvas.width = window.innerWidth,
  7.   h = canvas.height = window.innerHeight,

  8.   hue = 217,
  9.   stars = [],
  10.   count = 0,
  11.   maxStars = 1300;//星星数量

  12. var canvas2 = document.createElement('canvas'),
  13.   ctx2 = canvas2.getContext('2d');
  14. canvas2.width = 100;
  15. canvas2.height = 100;
  16. var half = canvas2.width / 2,
  17.   gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
  18. gradient2.addColorStop(0.025, '#CCC');
  19. gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');
  20. gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');
  21. gradient2.addColorStop(1, 'transparent');

  22. ctx2.fillStyle = gradient2;
  23. ctx2.beginPath();
  24. ctx2.arc(half, half, half, 0, Math.PI * 2);
  25. ctx2.fill();

  26. // End cache

  27. function random(min, max) {
  28.   if (arguments.length < 2) {
  29.     max = min;
  30.     min = 0;
  31.   }

  32.   if (min > max) {
  33.     var hold = max;
  34.     max = min;
  35.     min = hold;
  36.   }

  37.   return Math.floor(Math.random() * (max - min + 1)) + min;
  38. }

  39. function maxOrbit(x, y) {
  40.   var max = Math.max(x, y),
  41.     diameter = Math.round(Math.sqrt(max * max + max * max));
  42.   return diameter / 2;
  43.   //星星移动范围,值越大范围越小,
  44. }

  45. var Star = function() {

  46.   this.orbitRadius = random(maxOrbit(w, h));
  47.   this.radius = random(60, this.orbitRadius) / 8;
  48.   //星星大小
  49.   this.orbitX = w / 2;
  50.   this.orbitY = h / 2;
  51.   this.timePassed = random(0, maxStars);
  52.   this.speed = random(this.orbitRadius) / 50000;
  53.   //星星移动速度
  54.   this.alpha = random(2, 10) / 10;

  55.   count++;
  56.   stars[count] = this;
  57. }

  58. Star.prototype.draw = function() {
  59.   var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
  60.     y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
  61.     twinkle = random(10);

  62.   if (twinkle === 1 && this.alpha > 0) {
  63.     this.alpha -= 0.05;
  64.   } else if (twinkle === 2 && this.alpha < 1) {
  65.     this.alpha += 0.05;
  66.   }

  67.   ctx.globalAlpha = this.alpha;
  68.   ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);
  69.   this.timePassed += this.speed;
  70. }

  71. for (var i = 0; i < maxStars; i++) {
  72.   new Star();
  73. }

  74. function animation() {
  75.   ctx.globalCompositeOperation = 'source-over';
  76.   ctx.globalAlpha = 0.5; //尾巴
  77.   ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
  78.   ctx.fillRect(0, 0, w, h)

  79.   ctx.globalCompositeOperation = 'lighter';
  80.   for (var i = 1, l = stars.length; i < l; i++) {
  81.     stars[i].draw();
  82.   };

  83.   window.requestAnimationFrame(animation);
  84. }

  85. animation();
  86. </script>
複製代碼

TOP

1
返回列表
 

 

B Color Image Link Quote Code Smilies
高级模式 | 發新話題

 
你需要登入後才可以回帖 登入|立即註冊