首页 > 语言 > JavaScript > 正文

JavaScript实现Flash炫光波动特效

2024-05-06 16:20:13
字体:大 中 小
来源:转载
供稿:网友

JavaScript写的炫光波动效果,看到一些Flash效果不错,用JS也模拟一下,还有很多不完善的地方,给各位参考参考。

看到flash的实现这类的动画非常的便捷,于是试图胡搞一下。全部是用dom模拟的像素点,锯齿是难免的……

这个要避免锯齿恐怕要再加一次滤镜了吧,或者用图片。

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <title>炫光波动效果</title> 
  5. <script> 
  6. var lightWave = function(T,left,thick,sharp,speed,vibration,amplitude,opacity){ 
  7. this.cont = T;//炫光容器 
  8. this.left = left;//炫光向右偏移量 
  9. this.thick = thick;//粗细 
  10. this.sharp = sharp;//尖锐度 
  11. this.speed = speed;//波动速度 
  12. this.vibration = vibration;//单位时间内的振动频率 
  13. this.amplitude = amplitude;//振幅 
  14. this.opacity = opacity;//透明度 
  15. this.cont.style.position = 'relative'; 
  16. this.move(); 
  17. } 
  18. lightWave.prototype = { 
  19. point:function(n,l,t,c,color){ 
  20. var p = document.createElement('p'); 
  21. p.innerHTML = ''; 
  22. p.style.top = t + 'px'; 
  23. p.style.left = l + 'px'; 
  24. p.style.width = 1 + 'px'; 
  25. p.style.height = n + 'px'; 
  26. p.style.filter = 'alpha(opacity='+this.opacity+')'; 
  27. p.style.lineHeight = 0; 
  28. p.style.position = 'absolute'; 
  29. p.style.background = color; 
  30. c.appendChild(p); 
  31. return this; 
  32. }, 
  33. color:function(){ 
  34. var c = ['0','3','6','9','c','f']; 
  35. var t = [c[Math.floor(Math.random()*100)%6],'0','f']; 
  36. t.sort(function(){return Math.random()>0.5?-1:1;}); 
  37. return '#'+t.join(''); 
  38. }, 
  39. wave:function(){ 
  40. var l = this.left,t = this.wavelength,color = this.color(); 
  41. var c = document.createElement('div'); 
  42. c.style.top = this.amplitude+20+'px'; 
  43. c.style.position = 'absolute'; 
  44. c.style.opacity = this.opacity/100; 
  45. for(var i=1;i<this.thick;i++){ 
  46. for(var j=0;j<this.thick*this.sharp-i*i;j++,l++){ 
  47. this.point(i,l,-9999,c,color); 
  48. } 
  49. } 
  50. for(var i=this.thick;i>0;i--){ 
  51. for(var j=this.thick*this.sharp-i*i;j>0;j--,l++){ 
  52. this.point(i,l,-9999,c,color); 
  53. } 
  54. } 
  55. this.cont.appendChild(c); 
  56. return c; 
  57. }, 
  58. move:function(){ 
  59. var wl = this.amplitude; 
  60. var vibration = this.vibration; 
  61. var w = this.wave().getElementsByTagName('p'); 
  62. for(var i=0;i<w.length;i++){ 
  63. w[i].i = i; 
  64. } 
  65. var m = function(){ 
  66. for(var i=0,len=w.length;i<len;i++){ 
  67. if(w[i].ori == true){ 
  68. w[i].i-=vibration; 
  69. var top = w[i].i%180==90?0:wl*Math.cos(w[i].i*Math.PI/180); 
  70. w[i].style.top = top+'px'; 
  71. if(parseFloat(w[i].style.top)<=-wl){ 
  72. w[i].ori = false; 
  73. } 
  74. }else{ 
  75. w[i].i+=vibration; 
  76. var top = w[i].i%180==90?0:wl*Math.cos(w[i].i*Math.PI/180); 
  77. w[i].style.top = top+'px'; 
  78. if(parseFloat(w[i].style.top)>=wl){ 
  79. w[i].ori = true; 
  80. } 
  81. } 
  82. } 
  83. } 
  84. setInterval(m,this.speed); 
  85. } 
  86. } 
  87. window.onload = function(){ 
  88. var targetDom = document.body; 
  89. new lightWave(targetDom,0,3,36,120,6,20,40); 
  90. new lightWave(targetDom,50,2,70,120,10,30,30); 
  91. } 
  92. </script> 
  93. </head> 
  94. <body style="background:#000;margin-top:100px"> 
  95. </body> 
  96. </html> 

参数:

 

 
  1. var lightWave = function (T,left,thick,sharp,speed,vibration,amplitude,opacity){ 
  2. this .cont = T; //需要添加炫光的容器 
  3. this .left = left; //炫光出生时的向右偏移量 
  4. this .thick = thick; //粗细程度 
  5. this .sharp = sharp; //尖锐程度 
  6. this .speed = speed; //波动速度 
  7. this.vibration = vibration; //单位时间内的振动频率 
  8. this .amplitude = amplitude; //振幅 
  9. this .opacity = opacity; //透明度 
  10. this .cont.style.position = 'relative'; 
  11. this .move(); 
  12. } 

大家感兴趣可以来讨论一下。

另外,还遇到个问题,上面代码中ie下面的透明度滤镜不起作用,经研究得知,改变父容器的定位会影响子节点的透明滤镜的继承。

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选