Advertisement
orgicus

oldschool particle fun

Mar 31st, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //2017 - http://lifesine.eu
  2.  
  3. import flash.display.*;
  4. import flash.filters.BlurFilter;
  5.  
  6. var w = stage.stageWidth;
  7. var h = stage.stageHeight;
  8. var hw = w * .5;
  9. var hh = h * .5;
  10. var dsqr = hw*hw + hh*hh;
  11.  
  12. var np = 1000;
  13. var p = [];
  14. for(var i = 0 ; i < np; i++){
  15.     var d = 3 + Math.random() * 3;
  16.     p[i] = addChild(new MovieClip()) as MovieClip;
  17.     p[i].graphics.beginFill(0xFFFFFF);
  18.     //p[i].graphics.drawEllipse(-d * .5,-d * .5, d , d);
  19.     p[i].graphics.drawRect(-d * .5,-d * .5, d , d);
  20.     p[i].graphics.endFill();
  21.     p[i].filters = [new BlurFilter()];
  22.     resetParticle(p[i]);
  23. }
  24.  
  25. function resetParticle(particle:MovieClip):void{
  26.     var s = 3.0;
  27.     particle.vx = Math.random() * 2 * s - s;
  28.     particle.vy = Math.random() * 2 * s - s;
  29.     particle.x = hw;
  30.     particle.y = hh;
  31. }
  32.  
  33. function updateParticle(particle:MovieClip):void{
  34.     if((particle.x < 0 || particle.x > w) ||
  35.         (particle.y < 0 || particle.y > w)){
  36.         resetParticle(particle);
  37.     }
  38.    
  39.     var a = flash.utils.getTimer() * 0.001;
  40.     var r = 0.01;
  41.    
  42.     particle.vx += Math.cos(a) * r;
  43.     particle.vy += Math.sin(a) * r;
  44.    
  45.     particle.x += particle.vx;
  46.     particle.y += particle.vy;
  47.    
  48.     var dx = hw - particle.x;
  49.     var dy = hh - particle.y;
  50.     var d = (dx*dx + dy*dy);
  51.     var s = map(d,0,dsqr,1.0,10.0);
  52.     particle.scaleX = particle.scaleY = s;
  53.    
  54.     var dmx = particle.x - stage.mouseX;
  55.     var dmy = particle.y - stage.mouseY;
  56.     var dmsqr = (dmx*dmx + dmy * dmy);
  57.    
  58.     var t = s * 25.5;
  59.     var ct = particle.transform.colorTransform;
  60.     ct.redOffset = ct.greenOffset = ct.blueOffset = (-t);
  61.     ct.redMultiplier = map(dmsqr,0,dsqr * .25,0,255);
  62.     ct.greenMultiplier = map(dmsqr,0,dsqr,0,255);
  63.     //ct.blueMultiplier = map(dmsqr,0,dsqr * 2,-255,255);
  64.     ct.alphaMultiplier = map(dmsqr,0,dsqr * .25,0.0,1.0);
  65.     particle.transform.colorTransform = ct;
  66.    
  67.     particle.rotation += particle.vx+particle.vy;
  68.    
  69.     /*
  70.     var blur = particle.filters[0];
  71.     blur.blurX = blur.blurY = map(dmsqr,0,dsqr * .25,0.0,2.0);
  72.     particle.filters = [blur];
  73.     //*/
  74. }
  75.  
  76. function update(event:Event):void{
  77.     for(var i = 0 ; i < np; i++){
  78.         updateParticle(p[i]);
  79.     }
  80. }
  81.  
  82. this.addEventListener(Event.ENTER_FRAME,update,false,0,true);
  83.  
  84. function map(value:Number,
  85.                                 start1:Number, stop1:Number,
  86.                                 start2:Number, stop2:Number) {
  87.     var outgoing =
  88.       start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
  89.     var badness:String = null;
  90.     if (outgoing != outgoing) {
  91.       badness = "NaN (not a number)";
  92.  
  93.     } else if (outgoing == Number.NEGATIVE_INFINITY ||
  94.                outgoing == Number.POSITIVE_INFINITY) {
  95.       badness = "infinity";
  96.     }
  97.     if (badness != null) {
  98.         trace("map(%s, %s, %s, %s, %s) called, which returns %s",
  99.                       value, start1, stop1,
  100.                       start2, stop2, badness);
  101.     }
  102.     return outgoing;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement