PageRenderTime 78ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Onyx-VJ 4.5.0/Onyx-VJ-Patches/src/ColorfulSpring.as

http://onyx-vj.googlecode.com/
ActionScript | 125 lines | 89 code | 15 blank | 21 comment | 4 complexity | 9a69df0f3305ee0d0844b8f325a5f9c9 MD5 | raw file
  1. /**
  2. * Copyright TIAGODJF ( http://wonderfl.net/user/TIAGODJF )
  3. * MIT License ( http://www.opensource.org/licenses/mit-license.php )
  4. * Downloaded from: http://wonderfl.net/c/psQk
  5. */
  6. // forked from _ueueueueue's forked from: Colorful Spring Particle
  7. /**
  8. * Copyright matsu4512 ( http://wonderfl.net/user/matsu4512 )
  9. * MIT License ( http://www.opensource.org/licenses/mit-license.php )
  10. * Downloaded from: http://wonderfl.net/c/9nuR
  11. */
  12. package {
  13. import flash.display.*;
  14. import flash.events.*;
  15. import flash.geom.ColorTransform;
  16. import flash.geom.Matrix;
  17. import onyx.core.*;
  18. import onyx.parameter.*;
  19. import onyx.plugin.*;
  20. public class ColorfulSpring extends Patch {
  21. private var array:Array = [];
  22. private const N:int = 10, minDist:int = 5, springAmount:Number = 0.0075;
  23. private var canvas:Bitmap, bmpData:BitmapData, sp:Sprite;
  24. private var tr:ColorTransform = new ColorTransform(0.97,0.97,0.995,1);
  25. public function ColorfulSpring() {
  26. /*graphics.beginFill(0);
  27. graphics.drawRect(0x990000,0,465,465);
  28. graphics.endFill();
  29. */
  30. sp = new Sprite();
  31. for(var i:int = 0; i < N; i++){
  32. //var ball:Ball = new Ball(Math.random() * 10 - 5, Math.random() * 10 - 5, Math.random()*20+10, Math.random() * 0xFFFFFF);
  33. var ball:Ball = new Ball(
  34. Math.random()*2-1,
  35. Math.random()*2-1,
  36. i * 18 + 30,
  37. 0x990000
  38. );
  39. array.push(ball);
  40. ball.x = DISPLAY_WIDTH / 2 + (Math.random() * 10 - 5);
  41. ball.y = DISPLAY_HEIGHT / 2 + (Math.random() * 10 - 5);
  42. ball.rotationX = Math.random() * 360;
  43. ball.rotationY = Math.random() * 360;
  44. sp.addChild(ball);
  45. }
  46. bmpData = new BitmapData(DISPLAY_WIDTH, DISPLAY_HEIGHT, true, 0xFF0FFFF);
  47. canvas = new Bitmap(bmpData);
  48. //addChild(canvas);
  49. }
  50. override public function render(info:RenderInfo):void {
  51. bmpData.colorTransform(bmpData.rect, tr);
  52. bmpData.draw(sp);
  53. var len:uint = N;
  54. while(len--) {
  55. var ball:Ball = array[len];
  56. ball.rotationX += ball.vx;
  57. ball.rotationY += ball.vy;
  58. ball.alpha += (ball.toAlpha-ball.alpha)/4;
  59. ball.toAlpha = 0;
  60. //if(ball.x < -20) ball.x = stage.stageWidth+20;
  61. //else if(ball.x > stage.stageWidth+20) ball.x = -20;
  62. //if(ball.y < -20) ball.y = stage.stageHeight+20;
  63. //else if(ball.y > stage.stageHeight+20) ball.y = -20;
  64. }
  65. sp.graphics.clear();
  66. for(var i:int = 0; i < N - 1; i++){
  67. var partA:Ball = array[i];
  68. for(var j:uint = i + 1; j < N; j++){
  69. var partB:Ball = array[j];
  70. spring(partA, partB);
  71. }
  72. }
  73. info.render(canvas);
  74. }
  75. private function spring(b1:Ball, b2:Ball):void{
  76. var dx:Number = b2.x - b1.x;
  77. var dy:Number = b2.y - b1.y;
  78. var dist:Number = Math.sqrt(dx * dx + dy * dy);
  79. if(dist < minDist){
  80. sp.graphics.lineStyle(1);
  81. var m:Matrix = new Matrix;
  82. m.createGradientBox(Math.abs(dx), Math.abs(dy), Math.atan2(dy,dx), Math.min(b1.x, b2.x), Math.min(b1.y, b2.y));
  83. sp.graphics.lineGradientStyle(GradientType.LINEAR, [b1.color, b2.color], [b1.alpha, b2.alpha], [0,255], m);
  84. sp.graphics.moveTo(b1.x, b1.y);
  85. sp.graphics.lineTo(b2.x, b2.y);
  86. b1.toAlpha += 0.1;
  87. b2.toAlpha += 0.1;
  88. var ax:Number = dx * springAmount;
  89. var ay:Number = dy * springAmount;
  90. b1.vx += ax / b1.r;
  91. b1.vy += ay / b1.r;
  92. b2.vx -= ax / b2.r;
  93. b2.vy -= ay / b2.r;
  94. }
  95. }
  96. }
  97. }
  98. import flash.display.*;
  99. import flash.filters.*;
  100. class Ball extends Sprite {
  101. public var vx:Number, vy:Number, r:Number, toAlpha:Number, color:uint;
  102. public function Ball(vx:Number, vy:Number, r:Number, color:uint) {
  103. this.vx = vx; this.vy = vy; this.r = r; this.color = color;
  104. toAlpha = 0;
  105. graphics.lineStyle(1,0xFFFFFF);
  106. graphics.drawCircle(0, 0, r);
  107. filters = [new GlowFilter(color, 1, 6, 6 ,2), new BlurFilter(4,4)];
  108. blendMode = BlendMode.ADD;
  109. }
  110. }