notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FORKED
  1. // forked from fladdict's FITC Cool Japan side A / Particle simple 40000
  2. /**
  3. * Sample code for FITC
  4. * Basic 40000 particle.
  5. * What is interesting is that, actually this one is 4 times more amount of particles than that of smokey one.
  6. * However people feel this one use less particle.
  7. */
  8. package {
  9.     import flash.display.Sprite;    
  10.     import flash.display.Bitmap;
  11.     import flash.display.BitmapData;
  12.     import flash.display.BlendMode;
  13.     import flash.events.Event;
  14.     import flash.filters.BlurFilter;
  15.     import flash.geom.ColorTransform;
  16.     import flash.geom.Matrix;
  17.     import flash.geom.Point;
  18.     import flash.utils.ByteArray;
  19.     
  20.     public class FlashTest extends Sprite {
  21.        //View
  22.         protected var canvas:BitmapData;
  23.         protected var canvasBitmap:Bitmap;
  24.         
  25.         //BitmapData that contains the actual froce that is applied to particles.
  26.         protected var forceMap:BitmapData;
  27.         
  28.         //BitmapData that contains the force of the storm.
  29.         protected var stormMap0:BitmapData;
  30.         protected var stormMap1:BitmapData;
  31.         
  32.         //BitmapData that contains the mouse repulsion force of the stage.
  33.         protected var repulsionMap:BitmapData;
  34.         protected var repulsionHistoryMap:BitmapData;
  35.         protected var repulsionFadeMap:BitmapData;
  36.         
  37.         //the number of particles. 5000-200000
  38.         protected var particleNum:int = 10000;
  39.         protected var particles:Vector.<Particle>
  40.         
  41.         //
  42.         protected var stormScaleRatioX:Number;
  43.         protected var stormScaleRatioY:Number;
  44.         protected var stormCycle:Number = 0;
  45.         
  46.         //color transform that guradually changes canvas.
  47.         protected var fadeCanvasColt:ColorTransform;
  48.         protected var canvasBlurFilter:BlurFilter;
  49.         
  50.         //temporaly variables
  51.         protected var tempColt:ColorTransform = new ColorTransform();
  52.         protected var tempPt:Point = new Point();
  53.         protected var tempMat:Matrix = new Matrix();
  54.         
  55.         public function FlashTest() {
  56.             init();
  57.             reset();
  58.         }
  59.         
  60.         protected function init():void
  61.         {
  62.             //build View
  63.             canvasBitmap = new Bitmap(null"auto"false);
  64.             addChild(canvasBitmap);
  65.             
  66.             //initialize the map that contains force of the storm;
  67.             stormMap0 = new BitmapData(256,256,false,0x000000);
  68.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  69.             stormMap1 = new BitmapData(256,256,false,0x000000);
  70.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  71.             
  72.             //
  73.             forceMap = new BitmapData(256,256,false,0x000000);
  74.             
  75.             //initialize repulsionMap
  76.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  77.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  78.             repulsionMap = new BitmapData(256,256,false,0);
  79.             
  80.             /**
  81.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  82.             * Red channel of bitmap data contains horizontal force.
  83.             * Green channel of bitmap data contains vertical force. 
  84.             */
  85.             var dx:int, dy:int;
  86.             var dist:Number, fx:Number, fy:Number;
  87.             var col:int;
  88.             for(var yy:int=0; yy<256; yy++)
  89.             {
  90.                 for(var xx:int=0; xx<256; xx++){
  91.                     dx = xx-128;
  92.                     dy = yy-128;
  93.                     dist = Math.sqrt(dx*dx+dy*dy);
  94.                     if(dist<1){
  95.                         fx = 0;
  96.                         fy = 0;
  97.                     }else{
  98.                         fx = dx / dist / dist / dist * 10000;
  99.                         fy = dy / dist / dist / dist * 10000;
  100.                     }
  101.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  102.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  103.                     fx += 128;
  104.                     fy += 128;
  105.                     col = (fx<<16) + (fy<<8)
  106.                     repulsionMap.setPixel(xx,yy,col);
  107.                 }
  108.             }
  109.             //You can see 
  110.             //canvasBitmap.bitmapData = repulsionMap;
  111.             
  112.             //initialize particle
  113.             particles = new Vector.<Particle>(particleNum);
  114.             
  115.             var particle:Particle;
  116.             var rr:int;
  117.             for(var i:int=0; i<particleNum; i++)
  118.             {
  119.                 particle = new Particle();
  120.                 particle.x = Math.random() * stage.stageWidth;
  121.                 particle.y = Math.random() * stage.stageHeight;
  122.                 
  123.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  124.                 particle.r = particle.g = particle.b = Math.min(Math.random()*32+16,255);  
  125.                 
  126.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  127.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.0009;
  128.                 particles[i] = particle;
  129.             }
  130.             
  131.             fadeCanvasColt = new ColorTransform(1,1,1,1,-6,-8,-12,0);
  132.             canvasBlurFilter = new BlurFilter(2,2,3);
  133.             
  134.             //set event listeners
  135.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  136.             stage.addEventListener(Event.RESIZE, resizeHandler);
  137.         }
  138.         
  139.         /**
  140.         *
  141.         * Called when you need reset the view. E.G. resizing screen.
  142.         */
  143.         protected function reset():void
  144.         {
  145.             if(canvas)
  146.                 canvas.dispose();
  147.                 
  148.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  149.             canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
  150.             canvasBitmap.bitmapData = canvas;
  151.             
  152.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  153.             stormScaleRatioX = 1 / stage.stageWidth*255;
  154.             stormScaleRatioY = 1 / stage.stageHeight*255;
  155.             
  156.            //canvasBitmap.bitmapData = stormMap0;  //if you want to see stormMap;
  157.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  158.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  159.         }
  160.         
  161.         
  162.         //We rebuild screen when swf is reseized.
  163.         //It is not required if you only run it on wonderfl.
  164.         protected function resizeHandler(e:Event):void
  165.         {
  166.             reset();
  167.         }
  168.         
  169.         //Update storm and particle every frame.
  170.         protected function enterFrameHandler(e:Event):void
  171.         { 
  172.            updateStormMap();
  173.            updateRepulsionMap();
  174.            updateForceMap();
  175.            updateParticles();
  176.         }
  177.         
  178.         /**
  179.         * We calculate current forcefielde's state by blending two stormMap.
  180.         * Therefore force field gradually changes every frame.
  181.         */
  182.         protected function updateStormMap():void
  183.         {
  184.             stormCycle++;
  185.             if(stormCycle == 180){
  186.                 stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  187.             }else if(stormCycle==360){
  188.                 stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  189.                 stormCycle = 0;
  190.             }
  191.         }
  192.         
  193.         /**
  194.         * We update repulsionMap according to the current mouse position.
  195.         */
  196.         protected function updateRepulsionMap():void
  197.         {
  198.             tempMat.a = 1;
  199.             tempMat.b = 0;
  200.             tempMat.c = 0;
  201.             tempMat.d = 1;
  202.             tempMat.tx = 0;
  203.             tempMat.ty = 0;
  204.             
  205.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  206.             
  207.             tempMat.translate(-128,-128);
  208.             
  209.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  210.             tempMat.translate(mouseX*stormScaleRatioX, mouseY*stormScaleRatioY);
  211.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  212.         }
  213.         
  214.         protected function updateForceMap():void
  215.         {
  216.             //generate current force fieldes state with blending two stormMaps.
  217.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  218.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  219.             forceMap.draw(stormMap0, null, tempColt);
  220.             
  221.             //add mouse repulsion force
  222.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  223.         }
  224.         
  225.         protected function updateParticles():void
  226.         {
  227.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  228.             
  229.             var stageW:int = canvas.width;
  230.             var stageH:int = canvas.height;
  231.             var loopW:int = stageW-1;
  232.             var loopH:int = stageH-1;
  233.             var byteIndex:int;
  234.             
  235.             canvas.lock();
  236.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  237.             
  238.             //Update Paritcle position and draw.
  239.             var col:int, r:int, g:int, b:int;
  240.             for(var i:int=0; i<particleNum; i++)
  241.             {
  242.                 var prt:Particle = particles[i];
  243.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  244.                 prt.vx = prt.vx * 0.99 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  245.                 prt.vy = prt.vy * 0.99 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  246.                 prt.x += prt.vx;
  247.                 prt.y += prt.vy;
  248.                 if(prt.x<0){
  249.                     prt.x = loopW;
  250.                 }else if(prt.x > loopW){
  251.                     prt.x = 1;
  252.                 }
  253.                 if(prt.y<0){
  254.                     prt.y = loopH;
  255.                 }else if(prt.y > loopH){
  256.                     prt.y = 1;
  257.                 }
  258.                 
  259.                 //Self implimentation of addtive color blend mode.
  260.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  261.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  262.                 r = (col>>16&0xff) + prt.r;
  263.                 g = (col>>8&0xff) + prt.g;
  264.                 b = (col&0xff) + prt.b;
  265.                 r = (r<0xff)? r : 0xff;
  266.                 g = (g<0xff)? g : 0xff;
  267.                 b = (b<0xff)? b : 0xff;
  268.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  269.             }
  270.             //canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  271.             canvas.unlock();
  272.         }
  273.     }
  274.     
  275. }
  276. class Particle
  277. {
  278.     //particle position
  279.     public var x:Number = 0;
  280.     public var y:Number = 0;
  281.     public var vx:Number = 0;
  282.     public var vy:Number = 0;
  283.     
  284.     //particle color
  285.     public var r:int;
  286.     public var g:int;
  287.     public var b:int;
  288.     
  289.     //scale factor that affects the force applied to the particle.
  290.     public var scaleFactor:Number;
  291. }
noswf
  1. // forked from fladdict's FITC Cool Japan side A / Particle simple 40000
  2. /**
  3. * Sample code for FITC
  4. * Basic 40000 particle.
  5. * What is interesting is that, actually this one is 4 times more amount of particles than that of smokey one.
  6. * However people feel this one use less particle.
  7. */
  8. package {
  9.     import flash.display.Sprite;    
  10.     import flash.display.Bitmap;
  11.     import flash.display.BitmapData;
  12.     import flash.display.BlendMode;
  13.     import flash.events.Event;
  14.     import flash.filters.BlurFilter;
  15.     import flash.geom.ColorTransform;
  16.     import flash.geom.Matrix;
  17.     import flash.geom.Point;
  18.     import flash.utils.ByteArray;
  19.     
  20.     public class FlashTest extends Sprite {
  21.        //View
  22.         protected var canvas:BitmapData;
  23.         protected var canvasBitmap:Bitmap;
  24.         
  25.         //BitmapData that contains the actual froce that is applied to particles.
  26.         protected var forceMap:BitmapData;
  27.         
  28.         //BitmapData that contains the force of the storm.
  29.         protected var stormMap0:BitmapData;
  30.         protected var stormMap1:BitmapData;
  31.         
  32.         //BitmapData that contains the mouse repulsion force of the stage.
  33.         protected var repulsionMap:BitmapData;
  34.         protected var repulsionHistoryMap:BitmapData;
  35.         protected var repulsionFadeMap:BitmapData;
  36.         
  37.         //the number of particles. 5000-200000
  38.         protected var particleNum:int = 40000;
  39.         protected var particles:Vector.<Particle>
  40.         
  41.         //
  42.         protected var stormScaleRatioX:Number;
  43.         protected var stormScaleRatioY:Number;
  44.         protected var stormCycle:Number = 0;
  45.         
  46.         //color transform that guradually changes canvas.
  47.         protected var fadeCanvasColt:ColorTransform;
  48.         protected var canvasBlurFilter:BlurFilter;
  49.         
  50.         //temporaly variables
  51.         protected var tempColt:ColorTransform = new ColorTransform();
  52.         protected var tempPt:Point = new Point();
  53.         protected var tempMat:Matrix = new Matrix();
  54.         
  55.         public function FlashTest() {
  56.             init();
  57.             reset();
  58.         }
  59.         
  60.         protected function init():void
  61.         {
  62.             //build View
  63.             canvasBitmap = new Bitmap(null"auto"false);
  64.             addChild(canvasBitmap);
  65.             
  66.             //initialize the map that contains force of the storm;
  67.             stormMap0 = new BitmapData(256,256,false,0x000000);
  68.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  69.             stormMap1 = new BitmapData(256,256,false,0x000000);
  70.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  71.             
  72.             //
  73.             forceMap = new BitmapData(256,256,false,0x000000);
  74.             
  75.             //initialize repulsionMap
  76.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  77.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  78.             repulsionMap = new BitmapData(256,256,false,0);
  79.             
  80.             /**
  81.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  82.             * Red channel of bitmap data contains horizontal force.
  83.             * Green channel of bitmap data contains vertical force. 
  84.             */
  85.             var dx:int, dy:int;
  86.             var dist:Number, fx:Number, fy:Number;
  87.             var col:int;
  88.             for(var yy:int=0; yy<256; yy++)
  89.             {
  90.                 for(var xx:int=0; xx<256; xx++){
  91.                     dx = xx-128;
  92.                     dy = yy-128;
  93.                     dist = Math.sqrt(dx*dx+dy*dy);
  94.                     if(dist<1){
  95.                         fx = 0;
  96.                         fy = 0;
  97.                     }else{
  98.                         fx = dx / dist / dist / dist * 10000;
  99.                         fy = dy / dist / dist / dist * 10000;
  100.                     }
  101.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  102.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  103.                     fx += 128;
  104.                     fy += 128;
  105.                     col = (fx<<16) + (fy<<8)
  106.                     repulsionMap.setPixel(xx,yy,col);
  107.                 }
  108.             }
  109.             //You can see 
  110.             canvasBitmap.bitmapData = repulsionMap;
  111.             
  112.             //initialize particle
  113.             particles = new Vector.<Particle>(particleNum);
  114.             
  115.             var particle:Particle;
  116.             var rr:int;
  117.             for(var i:int=0; i<particleNum; i++)
  118.             {
  119.                 particle = new Particle();
  120.                 particle.x = Math.random() * stage.stageWidth;
  121.                 particle.y = Math.random() * stage.stageHeight;
  122.                 
  123.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  124.                 particle.r = particle.g = particle.b = Math.min(Math.random()*16+16,255);  
  125.                 
  126.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  127.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.0009;
  128.                 particles[i] = particle;
  129.             }
  130.             
  131.             fadeCanvasColt = new ColorTransform(1,1,1,1,-16,-8,-8,0);
  132.             canvasBlurFilter = new BlurFilter(2,2,3);
  133.             
  134.             //set event listeners
  135.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  136.             stage.addEventListener(Event.RESIZE, resizeHandler);
  137.         }
  138.         
  139.         /**
  140.         *
  141.         * Called when you need reset the view. E.G. resizing screen.
  142.         */
  143.         protected function reset():void
  144.         {
  145.             if(canvas)
  146.                 canvas.dispose();
  147.                 
  148.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  149.             canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
  150.             canvasBitmap.bitmapData = canvas;
  151.             
  152.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  153.             stormScaleRatioX = 1 / stage.stageWidth*255;
  154.             stormScaleRatioY = 1 / stage.stageHeight*255;
  155.             
  156.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  157.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  158.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  159.         }
  160.         
  161.         
  162.         //We rebuild screen when swf is reseized.
  163.         //It is not required if you only run it on wonderfl.
  164.         protected function resizeHandler(e:Event):void
  165.         {
  166.             reset();
  167.         }
  168.         
  169.         //Update storm and particle every frame.
  170.         protected function enterFrameHandler(e:Event):void
  171.         { 
  172.            updateStormMap();
  173.            updateRepulsionMap();
  174.            updateForceMap();
  175.            updateParticles();
  176.         }
  177.         
  178.         /**
  179.         * We calculate current forcefielde's state by blending two stormMap.
  180.         * Therefore force field gradually changes every frame.
  181.         */
  182.         protected function updateStormMap():void
  183.         {
  184.             stormCycle++;
  185.             if(stormCycle == 180){
  186.                 stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  187.             }else if(stormCycle==360){
  188.                 stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  189.                 stormCycle = 0;
  190.             }
  191.         }
  192.         
  193.         /**
  194.         * We update repulsionMap according to the current mouse position.
  195.         */
  196.         protected function updateRepulsionMap():void
  197.         {
  198.             tempMat.a = 1;
  199.             tempMat.b = 0;
  200.             tempMat.c = 0;
  201.             tempMat.d = 1;
  202.             tempMat.tx = 0;
  203.             tempMat.ty = 0;
  204.             
  205.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  206.             
  207.             tempMat.translate(-128,-128);
  208.             
  209.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  210.             tempMat.translate(mouseX*stormScaleRatioX, mouseY*stormScaleRatioY);
  211.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  212.         }
  213.         
  214.         protected function updateForceMap():void
  215.         {
  216.             //generate current force fieldes state with blending two stormMaps.
  217.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  218.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  219.             forceMap.draw(stormMap0, null, tempColt);
  220.             
  221.             //add mouse repulsion force
  222.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  223.         }
  224.         
  225.         protected function updateParticles():void
  226.         {
  227.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  228.             
  229.             var stageW:int = canvas.width;
  230.             var stageH:int = canvas.height;
  231.             var loopW:int = stageW-1;
  232.             var loopH:int = stageH-1;
  233.             var byteIndex:int;
  234.             
  235.             canvas.lock();
  236.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  237.             
  238.             //Update Paritcle position and draw.
  239.             var col:int, r:int, g:int, b:int;
  240.             for(var i:int=0; i<particleNum; i++)
  241.             {
  242.                 var prt:Particle = particles[i];
  243.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  244.                 prt.vx = prt.vx * 0.99 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  245.                 prt.vy = prt.vy * 0.99 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  246.                 prt.x += prt.vx;
  247.                 prt.y += prt.vy;
  248.                 if(prt.x<0){
  249.                     prt.x = loopW;
  250.                 }else if(prt.x > loopW){
  251.                     prt.x = 1;
  252.                 }
  253.                 if(prt.y<0){
  254.                     prt.y = loopH;
  255.                 }else if(prt.y > loopH){
  256.                     prt.y = 1;
  257.                 }
  258.                 
  259.                 //Self implimentation of addtive color blend mode.
  260.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  261.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  262.                 r = (col>>16&0xff) + prt.r;
  263.                 g = (col>>8&0xff) + prt.g;
  264.                 b = (col&0xff) + prt.b;
  265.                 r = (r<0xff)? r : 0xff;
  266.                 g = (g<0xff)? g : 0xff;
  267.                 b = (b<0xff)? b : 0xff;
  268.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  269.             }
  270.             //canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  271.             canvas.unlock();
  272.         }
  273.     }
  274.     
  275. }
  276. class Particle
  277. {
  278.     //particle position
  279.     public var x:Number = 0;
  280.     public var y:Number = 0;
  281.     public var vx:Number = 0;
  282.     public var vy:Number = 0;
  283.     
  284.     //particle color
  285.     public var r:int;
  286.     public var g:int;
  287.     public var b:int;
  288.     
  289.     //scale factor that affects the force applied to the particle.
  290.     public var scaleFactor:Number;
  291. }
noswf

FITC Cool Japan side A / Particle simple 40000 10000 Particle Wave at 1920x1200 [diff(77)]

  1. // forked from fladdict's FITC Cool Japan side A / Particle simple 40000
  2. /**
  3. * Modified for 1920x1200 screensaver resolution
  4. * Added varying colours and colorTransforms
  5. * Fixed looping problem with missing pixels
  6. * Increased force pattern complexity
  7. */
  8. package {
  9.     import flash.display.Sprite;    
  10.     import flash.display.Bitmap;
  11.     import flash.display.BitmapData;
  12.     import flash.display.BlendMode;
  13.     import flash.events.Event;
  14.     import flash.filters.BlurFilter;
  15.     import flash.geom.ColorTransform;
  16.     import flash.geom.Matrix;
  17.     import flash.geom.Point;
  18.     import flash.utils.ByteArray;
  19.     import flash.display.StageDisplayState;
  20.     import flash.events.MouseEvent;
  21.     
  22.     [SWF(width="1920", height="1200", frameRate="30", backgroundColor="#000000")]
  23.     
  24.     public class particle_wave extends Sprite {
  25.        //View
  26.         protected var canvas:BitmapData;
  27.         protected var canvasBitmap:Bitmap;
  28.         
  29.         //BitmapData that contains the actual froce that is applied to particles.
  30.         protected var forceMap:BitmapData;
  31.         
  32.         //BitmapData that contains the force of the storm.
  33.         protected var stormMap0:BitmapData;
  34.         protected var stormMap1:BitmapData;
  35.         
  36.         //BitmapData that contains the mouse repulsion force of the stage.
  37.         protected var repulsionMap:BitmapData;
  38.         protected var repulsionHistoryMap:BitmapData;
  39.         protected var repulsionFadeMap:BitmapData;
  40.         
  41.         //the number of particles. 5000-200000
  42.         protected var particleNum:int = 10000;
  43.         protected var particles:Vector.<Particle>
  44.         protected var particleForceScale:Number = 0.0025;
  45.         protected var particleColorScale:Number = 95;
  46.         //
  47.         protected var stormScaleRatioX:Number;
  48.         protected var stormScaleRatioY:Number;
  49.         protected var stormCycle:Number = 0;
  50.         
  51.         //color transform that guradually changes canvas.
  52.         protected var fadeCanvasColt:ColorTransform;
  53.         protected var canvasBlurFilter:BlurFilter;
  54.         
  55.         //temporaly variables
  56.         protected var tempColt:ColorTransform = new ColorTransform();
  57.         protected var tempPt:Point = new Point();
  58.         protected var tempMat:Matrix = new Matrix();
  59.         
  60.         //
  61.         protected var colTransR:Number;
  62.         protected var colTransG:Number;
  63.         protected var colTransB:Number;
  64.         protected var colTransTargetR:Number;
  65.         protected var colTransTargetG:Number;
  66.         protected var colTransTargetB:Number;
  67.         
  68.         public function particle_wave() {
  69.             Wonderfl.capture_delay(5);
  70.             init();
  71.             reset();
  72.         }
  73.         
  74.         protected function init():void
  75.         {
  76.             //build View
  77.             canvasBitmap = new Bitmap(null"auto"false);
  78.             addChild(canvasBitmap);
  79.             
  80.             //initialize the map that contains force of the storm;
  81.             stormMap0 = new BitmapData(256,256,false,0x000000);
  82.             stormMap0.perlinNoise(128,128,5,Math.random()*100,true,true,3,false);
  83.             stormMap1 = new BitmapData(256,256,false,0x000000);
  84.             stormMap1.perlinNoise(128,128,5,Math.random()*100,true,true,3,false);
  85.             
  86.             //
  87.             forceMap = new BitmapData(256,256,false,0x000000);
  88.             
  89.             //initialize repulsionMap
  90.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  91.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  92.             repulsionMap = new BitmapData(256,256,false,0);
  93.             
  94.             //
  95.             colTransR = 0;
  96.         colTransG = 0;
  97.         colTransB = 0;
  98.         colTransTargetR = 0.5+Math.random()*1.5;
  99.         colTransTargetG = 0.5+Math.random()*1.5;
  100.         colTransTargetB = 0.5+Math.random()*1.5;
  101.             /**
  102.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  103.             * Red channel of bitmap data contains horizontal force.
  104.             * Green channel of bitmap data contains vertical force. 
  105.             */
  106.             var dx:int, dy:int;
  107.             var dist:Number, fx:Number, fy:Number;
  108.             var col:int;
  109.             for(var yy:int=0; yy<256; yy++)
  110.             {
  111.                 for(var xx:int=0; xx<256; xx++){
  112.                     dx = xx-128;
  113.                     dy = yy-128;
  114.                     dist = Math.sqrt(dx*dx+dy*dy);
  115.                     if(dist<1){
  116.                         fx = 0;
  117.                         fy = 0;
  118.                     }else{
  119.                         fx = dx / dist / dist / dist * 100;
  120.                         fy = dy / dist / dist / dist * 100;
  121.                     }
  122.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  123.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  124.                     fx += 128;
  125.                     fy += 128;
  126.                     col = (fx<<16) + (fy<<8)
  127.                     trace(fx<<16);
  128.                     repulsionMap.setPixel(xx,yy,col);
  129.                 }
  130.             }
  131.             //You can see 
  132.             canvasBitmap.bitmapData = repulsionMap;
  133.             
  134.             //initialize particle
  135.             particles = new Vector.<Particle>(particleNum);
  136.             
  137.             var particle:Particle;
  138.             var rr:int;
  139.             for(var i:int=0; i<particleNum; i++)
  140.             {
  141.                 particle = new Particle();
  142.                 particle.x = Math.random() * stage.stageWidth;
  143.                 particle.y = Math.random() * stage.stageHeight;
  144.                 
  145.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  146.                 particles[i] = particle;
  147.                 
  148.                 particle.vfri = 0.98+Math.random()*0.01;
  149.             }
  150.             
  151.             fadeCanvasColt = new ColorTransform(1,1,1,1,-8-Math.random()*8,-8-Math.random()*8,-8-Math.random()*8,0);
  152.             canvasBlurFilter = new BlurFilter(1.3,1.3,1);
  153.             
  154.             //
  155.             stage.addEventListener(MouseEvent.CLICK,function(e:Event):void{stage.displayState=(StageDisplayState.FULL_SCREEN==stage.displayState)?StageDisplayState.NORMAL:StageDisplayState.FULL_SCREEN});
  156.             //set event listeners
  157.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  158.             stage.addEventListener(Event.RESIZE, resizeHandler);
  159.         }
  160.         
  161.         /**
  162.         *
  163.         * Called when you need reset the view. E.G. resizing screen.
  164.         */
  165.         protected function reset():void
  166.         {
  167.             if(canvas)
  168.                 canvas.dispose();
  169.                 
  170.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  171.             canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
  172.             canvasBitmap.bitmapData = canvas;
  173.             
  174.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  175.             stormScaleRatioX = 1 / stage.stageWidth*255;
  176.             stormScaleRatioY = 1 / stage.stageHeight*255;
  177.             
  178.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  179.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  180.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  181.         }
  182.         
  183.         
  184.         //We rebuild screen when swf is reseized.
  185.         //It is not required if you only run it on wonderfl.
  186.         protected function resizeHandler(e:Event):void
  187.         {
  188.             reset();
  189.         }
  190.         
  191.         //Update storm and particle every frame.
  192.         protected function enterFrameHandler(e:Event):void
  193.         { 
  194.            updateStormMap();
  195.            updateRepulsionMap();
  196.            updateForceMap();
  197.            updateParticles();
  198.         }
  199.         
  200.         /**
  201.         * We calculate current forcefielde's state by blending two stormMap.
  202.         * Therefore force field gradually changes every frame.
  203.         */
  204.         protected function updateStormMap():void
  205.         {
  206.             stormCycle++;
  207.             if(stormCycle == 180){
  208.                 stormMap0.perlinNoise(128,128,5,Math.random()*100,true,true,3,false);
  209.             }else if(stormCycle==360){
  210.                 stormMap1.perlinNoise(128,128,5,Math.random()*100,true,true,3,false);
  211.                 
  212.                 colTransTargetR = 0.5+Math.random()*1.5;
  213.         colTransTargetG = 0.5+Math.random()*1.5;
  214.         colTransTargetB = 0.5+Math.random()*1.5;
  215.                 
  216.                 fadeCanvasColt = new ColorTransform(1,1,1,1,-8-Math.random()*8,-8-Math.random()*8,-8-Math.random()*8,0);
  217.                 
  218.                 stormCycle = 0;
  219.             }
  220.         }
  221.         
  222.         /**
  223.         * We update repulsionMap according to the current mouse position.
  224.         */
  225.         protected function updateRepulsionMap():void
  226.         {
  227.             tempMat.a = 1;
  228.             tempMat.b = 0;
  229.             tempMat.c = 0;
  230.             tempMat.d = 1;
  231.             tempMat.tx = 0;
  232.             tempMat.ty = 0;
  233.             
  234.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  235.             
  236.             tempMat.translate(-128,-128);
  237.             
  238.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  239.             tempMat.translate(mouseX*stormScaleRatioX, mouseY*stormScaleRatioY);
  240.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  241.         }
  242.         
  243.         protected function updateForceMap():void
  244.         {
  245.             //generate current force fieldes state with blending two stormMaps.
  246.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  247.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  248.             forceMap.draw(stormMap0, null, tempColt);
  249.             
  250.             //add mouse repulsion force
  251.             //forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  252.             
  253.             colTransR += (colTransTargetR-colTransR)/50;
  254.             colTransG += (colTransTargetG-colTransG)/50;
  255.             colTransB += (colTransTargetB-colTransB)/50;
  256.         }
  257.         
  258.         protected function updateParticles():void
  259.         {
  260.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  261.             
  262.             var stageW:int = canvas.width;
  263.             var stageH:int = canvas.height;
  264.             var loopW:int = stageW-1;
  265.             var loopH:int = stageH-1;
  266.             var byteIndex:int;
  267.             
  268.             canvas.lock();
  269.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  270.             
  271.             //Update Paritcle position and draw.
  272.             var col:int, r:int, g:int, b:int;
  273.             for(var i:int=0; i<particleNum; i++)
  274.             {
  275.                 var prt:Particle = particles[i];
  276.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  277.                 prt.vx = prt.vx * prt.vfri + (forceBytes[byteIndex+1]-128)*particleForceScale;
  278.                 prt.vy = prt.vy * prt.vfri + (forceBytes[byteIndex+2]-128)*particleForceScale;
  279.                 prt.x += prt.vx;
  280.                 prt.y += prt.vy;
  281.                 if(prt.x<0){
  282.                     prt.x += loopW;
  283.                 }else if(prt.x > loopW){
  284.                     prt.x -= loopW;
  285.                 }
  286.                 if(prt.y<0){
  287.                     prt.y += loopH;
  288.                 }else if(prt.y > loopH){
  289.                     prt.y -= loopH;
  290.                 }
  291.                 
  292.                 //Self implimentation of addtive color blend mode.
  293.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  294.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  295.                 r = (col>>16&0xff) + particleColorScale * colTransR;
  296.                 g = (col>>8&0xff) + particleColorScale * colTransG;
  297.                 b = (col&0xff) + particleColorScale * colTransB;
  298.                 r = (r<0xff)? r : 0xff;
  299.                 g = (g<0xff)? g : 0xff;
  300.                 b = (b<0xff)? b : 0xff;
  301.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  302.             }
  303.             //canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  304.             canvas.unlock();
  305.         }
  306.     }
  307.     
  308. }
  309. class Particle
  310. {
  311.     //particle position
  312.     public var x:Number = 0;
  313.     public var y:Number = 0;
  314.     public var vx:Number = 0;
  315.     public var vy:Number = 0;
  316.     
  317.     //friction
  318.     public var vfri:Number = 1
  319. }
noswf
  1. // forked from fladdict's FITC Cool Japan side A / Particle simple 40000
  2. /**
  3. * Sample code for FITC
  4. * Basic 40000 particle.
  5. * What is interesting is that, actually this one is 4 times more amount of particles than that of smokey one.
  6. * However people feel this one use less particle.
  7. */
  8. package {
  9.     import flash.display.Sprite;    
  10.     import flash.display.Bitmap;
  11.     import flash.display.BitmapData;
  12.     import flash.display.BlendMode;
  13.     import flash.events.Event;
  14.     import flash.filters.BlurFilter;
  15.     import flash.geom.ColorTransform;
  16.     import flash.geom.Matrix;
  17.     import flash.geom.Point;
  18.     import flash.utils.ByteArray;
  19.     
  20.     public class FlashTest extends Sprite {
  21.        //View
  22.         protected var canvas:BitmapData;
  23.         protected var canvasBitmap:Bitmap;
  24.         
  25.         //BitmapData that contains the actual froce that is applied to particles.
  26.         protected var forceMap:BitmapData;
  27.         
  28.         //BitmapData that contains the force of the storm.
  29.         protected var stormMap0:BitmapData;
  30.         protected var stormMap1:BitmapData;
  31.         
  32.         //BitmapData that contains the mouse repulsion force of the stage.
  33.         protected var repulsionMap:BitmapData;
  34.         protected var repulsionHistoryMap:BitmapData;
  35.         protected var repulsionFadeMap:BitmapData;
  36.         
  37.         //the number of particles. 5000-200000
  38.         protected var particleNum:int = 40000;
  39.         protected var particles:Vector.<Particle>
  40.         
  41.         //
  42.         protected var stormScaleRatioX:Number;
  43.         protected var stormScaleRatioY:Number;
  44.         protected var stormCycle:Number = 0;
  45.         
  46.         //color transform that guradually changes canvas.
  47.         protected var fadeCanvasColt:ColorTransform;
  48.         protected var canvasBlurFilter:BlurFilter;
  49.         
  50.         //temporaly variables
  51.         protected var tempColt:ColorTransform = new ColorTransform();
  52.         protected var tempPt:Point = new Point();
  53.         protected var tempMat:Matrix = new Matrix();
  54.         
  55.         public function FlashTest() {
  56.             init();
  57.             reset();
  58.         }
  59.         
  60.         protected function init():void
  61.         {
  62.             //build View
  63.             canvasBitmap = new Bitmap(null"auto"false);
  64.             addChild(canvasBitmap);
  65.             
  66.             //initialize the map that contains force of the storm;
  67.             stormMap0 = new BitmapData(256,256,false,0x000000);
  68.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  69.             stormMap1 = new BitmapData(256,256,false,0x000000);
  70.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  71.             
  72.             //
  73.             forceMap = new BitmapData(256,256,false,0x000000);
  74.             
  75.             //initialize repulsionMap
  76.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  77.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  78.             repulsionMap = new BitmapData(256,256,false,0);
  79.             
  80.             /**
  81.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  82.             * Red channel of bitmap data contains horizontal force.
  83.             * Green channel of bitmap data contains vertical force. 
  84.             */
  85.             var dx:int, dy:int;
  86.             var dist:Number, fx:Number, fy:Number;
  87.             var col:int;
  88.             for(var yy:int=0; yy<256; yy++)
  89.             {
  90.                 for(var xx:int=0; xx<256; xx++){
  91.                     dx = xx-128;
  92.                     dy = yy-128;
  93.                     dist = Math.sqrt(dx*dx+dy*dy);
  94.                     if(dist<1){
  95.                         fx = 0;
  96.                         fy = 0;
  97.                     }else{
  98.                         fx = dx / dist / dist / dist * 10000;
  99.                         fy = dy / dist / dist / dist * 10000;
  100.                     }
  101.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  102.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  103.                     fx += 128;
  104.                     fy += 128;
  105.                     col = (fx<<16) + (fy<<8)
  106.                     repulsionMap.setPixel(xx,yy,col);
  107.                 }
  108.             }
  109.             //You can see 
  110.             canvasBitmap.bitmapData = repulsionMap;
  111.             
  112.             //initialize particle
  113.             particles = new Vector.<Particle>(particleNum);
  114.             
  115.             var particle:Particle;
  116.             var rr:int;
  117.             for(var i:int=0; i<particleNum; i++)
  118.             {
  119.                 particle = new Particle();
  120.                 particle.x = Math.random() * stage.stageWidth;
  121.                 particle.y = Math.random() * stage.stageHeight;
  122.                 
  123.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  124.                 particle.r = particle.g = particle.b = Math.min(Math.random()*16+16,255);  
  125.                 
  126.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  127.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.0009;
  128.                 particles[i] = particle;
  129.             }
  130.             
  131.             fadeCanvasColt = new ColorTransform(1,1,1,1,-16,-8,-8,0);
  132.             canvasBlurFilter = new BlurFilter(2,2,3);
  133.             
  134.             //set event listeners
  135.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  136.             stage.addEventListener(Event.RESIZE, resizeHandler);
  137.         }
  138.         
  139.         /**
  140.         *
  141.         * Called when you need reset the view. E.G. resizing screen.
  142.         */
  143.         protected function reset():void
  144.         {
  145.             if(canvas)
  146.                 canvas.dispose();
  147.                 
  148.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  149.             canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
  150.             canvasBitmap.bitmapData = canvas;
  151.             
  152.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  153.             stormScaleRatioX = 1 / stage.stageWidth*255;
  154.             stormScaleRatioY = 1 / stage.stageHeight*255;
  155.             
  156.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  157.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  158.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  159.         }
  160.         
  161.         
  162.         //We rebuild screen when swf is reseized.
  163.         //It is not required if you only run it on wonderfl.
  164.         protected function resizeHandler(e:Event):void
  165.         {
  166.             reset();
  167.         }
  168.         
  169.         //Update storm and particle every frame.
  170.         protected function enterFrameHandler(e:Event):void
  171.         { 
  172.            updateStormMap();
  173.            updateRepulsionMap();
  174.            updateForceMap();
  175.            updateParticles();
  176.         }
  177.         
  178.         /**
  179.         * We calculate current forcefielde's state by blending two stormMap.
  180.         * Therefore force field gradually changes every frame.
  181.         */
  182.         protected function updateStormMap():void
  183.         {
  184.             stormCycle++;
  185.             if(stormCycle == 180){
  186.                 stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  187.             }else if(stormCycle==360){
  188.                 stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  189.                 stormCycle = 0;
  190.             }
  191.         }
  192.         
  193.         /**
  194.         * We update repulsionMap according to the current mouse position.
  195.         */
  196.         protected function updateRepulsionMap():void
  197.         {
  198.             tempMat.a = 1;
  199.             tempMat.b = 0;
  200.             tempMat.c = 0;
  201.             tempMat.d = 1;
  202.             tempMat.tx = 0;
  203.             tempMat.ty = 0;
  204.             
  205.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  206.             
  207.             tempMat.translate(-128,-128);
  208.             
  209.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  210.             tempMat.translate(mouseX*stormScaleRatioX, mouseY*stormScaleRatioY);
  211.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  212.         }
  213.         
  214.         protected function updateForceMap():void
  215.         {
  216.             //generate current force fieldes state with blending two stormMaps.
  217.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  218.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  219.             forceMap.draw(stormMap0, null, tempColt);
  220.             
  221.             //add mouse repulsion force
  222.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  223.         }
  224.         
  225.         protected function updateParticles():void
  226.         {
  227.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  228.             
  229.             var stageW:int = canvas.width;
  230.             var stageH:int = canvas.height;
  231.             var loopW:int = stageW-1;
  232.             var loopH:int = stageH-1;
  233.             var byteIndex:int;
  234.             
  235.             canvas.lock();
  236.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  237.             
  238.             //Update Paritcle position and draw.
  239.             var col:int, r:int, g:int, b:int;
  240.             for(var i:int=0; i<particleNum; i++)
  241.             {
  242.                 var prt:Particle = particles[i];
  243.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  244.                 prt.vx = prt.vx * 0.99 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  245.                 prt.vy = prt.vy * 0.99 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  246.                 prt.x += prt.vx;
  247.                 prt.y += prt.vy;
  248.                 if(prt.x<0){
  249.                     prt.x = loopW;
  250.                 }else if(prt.x > loopW){
  251.                     prt.x = 1;
  252.                 }
  253.                 if(prt.y<0){
  254.                     prt.y = loopH;
  255.                 }else if(prt.y > loopH){
  256.                     prt.y = 1;
  257.                 }
  258.                 
  259.                 //Self implimentation of addtive color blend mode.
  260.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  261.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  262.                 r = (col>>16&0xff) + prt.r;
  263.                 g = (col>>8&0xff) + prt.g;
  264.                 b = (col&0xff) + prt.b;
  265.                 r = (r<0xff)? r : 0xff;
  266.                 g = (g<0xff)? g : 0xff;
  267.                 b = (b<0xff)? b : 0xff;
  268.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  269.             }
  270.             //canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  271.             canvas.unlock();
  272.         }
  273.     }
  274.     
  275. }
  276. class Particle
  277. {
  278.     //particle position
  279.     public var x:Number = 0;
  280.     public var y:Number = 0;
  281.     public var vx:Number = 0;
  282.     public var vy:Number = 0;
  283.     
  284.     //particle color
  285.     public var r:int;
  286.     public var g:int;
  287.     public var b:int;
  288.     
  289.     //scale factor that affects the force applied to the particle.
  290.     public var scaleFactor:Number;
  291. }
noswf
  1. // forked from fladdict's FITC Cool Japan side A / Particle simple 40000
  2. /**
  3. * Sample code for FITC
  4. * Basic 40000 particle.
  5. * What is interesting is that, actually this one is 4 times more amount of particles than that of smokey one.
  6. * However people feel this one use less particle.
  7. */
  8. package {
  9.     import flash.display.Sprite;    
  10.     import flash.display.Bitmap;
  11.     import flash.display.BitmapData;
  12.     import flash.display.BlendMode;
  13.     import flash.events.Event;
  14.     import flash.filters.BlurFilter;
  15.     import flash.geom.ColorTransform;
  16.     import flash.geom.Matrix;
  17.     import flash.geom.Point;
  18.     import flash.utils.ByteArray;
  19.     
  20.     public class FlashTest extends Sprite {
  21.        //View
  22.         protected var canvas:BitmapData;
  23.         protected var canvasBitmap:Bitmap;
  24.         
  25.         //BitmapData that contains the actual froce that is applied to particles.
  26.         protected var forceMap:BitmapData;
  27.         
  28.         //BitmapData that contains the force of the storm.
  29.         protected var stormMap0:BitmapData;
  30.         protected var stormMap1:BitmapData;
  31.         
  32.         //BitmapData that contains the mouse repulsion force of the stage.
  33.         protected var repulsionMap:BitmapData;
  34.         protected var repulsionHistoryMap:BitmapData;
  35.         protected var repulsionFadeMap:BitmapData;
  36.         
  37.         //the number of particles. 5000-200000
  38.         protected var particleNum:int = 40000;
  39.         protected var particles:Vector.<Particle>
  40.         
  41.         //
  42.         protected var stormScaleRatioX:Number;
  43.         protected var stormScaleRatioY:Number;
  44.         protected var stormCycle:Number = 0;
  45.         
  46.         //color transform that guradually changes canvas.
  47.         protected var fadeCanvasColt:ColorTransform;
  48.         protected var canvasBlurFilter:BlurFilter;
  49.         
  50.         //temporaly variables
  51.         protected var tempColt:ColorTransform = new ColorTransform();
  52.         protected var tempPt:Point = new Point();
  53.         protected var tempMat:Matrix = new Matrix();
  54.         
  55.         public function FlashTest() {
  56.             init();
  57.             reset();
  58.         }
  59.         
  60.         protected function init():void
  61.         {
  62.             //build View
  63.             canvasBitmap = new Bitmap(null"auto"false);
  64.             addChild(canvasBitmap);
  65.             
  66.             //initialize the map that contains force of the storm;
  67.             stormMap0 = new BitmapData(256,256,false,0x000000);
  68.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  69.             stormMap1 = new BitmapData(256,256,false,0x000000);
  70.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  71.             
  72.             //
  73.             forceMap = new BitmapData(256,256,false,0x000000);
  74.             
  75.             //initialize repulsionMap
  76.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  77.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  78.             repulsionMap = new BitmapData(256,256,false,0);
  79.             
  80.             /**
  81.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  82.             * Red channel of bitmap data contains horizontal force.
  83.             * Green channel of bitmap data contains vertical force. 
  84.             */
  85.             var dx:int, dy:int;
  86.             var dist:Number, fx:Number, fy:Number;
  87.             var col:int;
  88.             for(var yy:int=0; yy<256; yy++)
  89.             {
  90.                 for(var xx:int=0; xx<256; xx++){
  91.                     dx = xx-128;
  92.                     dy = yy-128;
  93.                     dist = Math.sqrt(dx*dx+dy*dy);
  94.                     if(dist<1){
  95.                         fx = 0;
  96.                         fy = 0;
  97.                     }else{
  98.                         fx = dx / dist / dist / dist * 10000;
  99.                         fy = dy / dist / dist / dist * 10000;
  100.                     }
  101.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  102.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  103.                     fx += 128;
  104.                     fy += 128;
  105.                     col = (fx<<16) + (fy<<8)
  106.                     repulsionMap.setPixel(xx,yy,col);
  107.                 }
  108.             }
  109.             //You can see 
  110.             canvasBitmap.bitmapData = repulsionMap;
  111.             
  112.             //initialize particle
  113.             particles = new Vector.<Particle>(particleNum);
  114.             
  115.             var particle:Particle;
  116.             var rr:int;
  117.             for(var i:int=0; i<particleNum; i++)
  118.             {
  119.                 particle = new Particle();
  120.                 particle.x = Math.random() * stage.stageWidth;
  121.                 particle.y = Math.random() * stage.stageHeight;
  122.                 
  123.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  124.                 particle.r = particle.g = particle.b = Math.min(Math.random()*16+16,255);  
  125.                 
  126.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  127.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.0009;
  128.                 particles[i] = particle;
  129.             }
  130.             
  131.             fadeCanvasColt = new ColorTransform(1,1,1,1,-16,-8,-8,0);
  132.             canvasBlurFilter = new BlurFilter(2,2,3);
  133.             
  134.             //set event listeners
  135.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  136.             stage.addEventListener(Event.RESIZE, resizeHandler);
  137.         }
  138.         
  139.         /**
  140.         *
  141.         * Called when you need reset the view. E.G. resizing screen.
  142.         */
  143.         protected function reset():void
  144.         {
  145.             if(canvas)
  146.                 canvas.dispose();
  147.                 
  148.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  149.             canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
  150.             canvasBitmap.bitmapData = canvas;
  151.             
  152.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  153.             stormScaleRatioX = 1 / stage.stageWidth*255;
  154.             stormScaleRatioY = 1 / stage.stageHeight*255;
  155.             
  156.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  157.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  158.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  159.         }
  160.         
  161.         
  162.         //We rebuild screen when swf is reseized.
  163.         //It is not required if you only run it on wonderfl.
  164.         protected function resizeHandler(e:Event):void
  165.         {
  166.             reset();
  167.         }
  168.         
  169.         //Update storm and particle every frame.
  170.         protected function enterFrameHandler(e:Event):void
  171.         { 
  172.            updateStormMap();
  173.            updateRepulsionMap();
  174.            updateForceMap();
  175.            updateParticles();
  176.         }
  177.         
  178.         /**
  179.         * We calculate current forcefielde's state by blending two stormMap.
  180.         * Therefore force field gradually changes every frame.
  181.         */
  182.         protected function updateStormMap():void
  183.         {
  184.             stormCycle++;
  185.             if(stormCycle == 180){
  186.                 stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  187.             }else if(stormCycle==360){
  188.                 stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  189.                 stormCycle = 0;
  190.             }
  191.         }
  192.         
  193.         /**
  194.         * We update repulsionMap according to the current mouse position.
  195.         */
  196.         protected function updateRepulsionMap():void
  197.         {
  198.             tempMat.a = 1;
  199.             tempMat.b = 0;
  200.             tempMat.c = 0;
  201.             tempMat.d = 1;
  202.             tempMat.tx = 0;
  203.             tempMat.ty = 0;
  204.             
  205.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  206.             
  207.             tempMat.translate(-128,-128);
  208.             
  209.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  210.             tempMat.translate(mouseX*stormScaleRatioX, mouseY*stormScaleRatioY);
  211.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  212.         }
  213.         
  214.         protected function updateForceMap():void
  215.         {
  216.             //generate current force fieldes state with blending two stormMaps.
  217.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  218.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  219.             forceMap.draw(stormMap0, null, tempColt);
  220.             
  221.             //add mouse repulsion force
  222.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  223.         }
  224.         
  225.         protected function updateParticles():void
  226.         {
  227.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  228.             
  229.             var stageW:int = canvas.width;
  230.             var stageH:int = canvas.height;
  231.             var loopW:int = stageW-1;
  232.             var loopH:int = stageH-1;
  233.             var byteIndex:int;
  234.             
  235.             canvas.lock();
  236.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  237.             
  238.             //Update Paritcle position and draw.
  239.             var col:int, r:int, g:int, b:int;
  240.             for(var i:int=0; i<particleNum; i++)
  241.             {
  242.                 var prt:Particle = particles[i];
  243.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  244.                 prt.vx = prt.vx * 0.99 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  245.                 prt.vy = prt.vy * 0.99 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  246.                 prt.x += prt.vx;
  247.                 prt.y += prt.vy;
  248.                 if(prt.x<0){
  249.                     prt.x = loopW;
  250.                 }else if(prt.x > loopW){
  251.                     prt.x = 1;
  252.                 }
  253.                 if(prt.y<0){
  254.                     prt.y = loopH;
  255.                 }else if(prt.y > loopH){
  256.                     prt.y = 1;
  257.                 }
  258.                 
  259.                 //Self implimentation of addtive color blend mode.
  260.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  261.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  262.                 r = (col>>16&0xff) + prt.r*2;
  263.                 g = (col>>8&0xff) + prt.g*1.5;
  264.                 b = (col&0xff) + prt.b;
  265.                 r = (r<0xff)? r : 0xff;
  266.                 g = (g<0xff)? g : 0xff;
  267.                 b = (b<0xff)? b : 0xff;
  268.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  269.             }
  270.             //canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  271.             canvas.unlock();
  272.         }
  273.     }
  274.     
  275. }
  276. class Particle
  277. {
  278.     //particle position
  279.     public var x:Number = 0;
  280.     public var y:Number = 0;
  281.     public var vx:Number = 0;
  282.     public var vy:Number = 0;
  283.     
  284.     //particle color
  285.     public var r:int;
  286.     public var g:int;
  287.     public var b:int;
  288.     
  289.     //scale factor that affects the force applied to the particle.
  290.     public var scaleFactor:Number;
  291. }
noswf
Get Adobe Flash Player