In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Pixel Particle Effect

PixelParticleEffect
* ...
* @author sph62
Get Adobe Flash player
by sph62 01 Mar 2009
    Embed
/**
 * PixelParticleEffect
 * ...
 * @author sph62
 */
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.events.MouseEvent;
	import flash.text.TextField;

        [SWF(backgroundColor="0xFFFFFF")] 
	public class PixelParticleEffect extends Sprite {
                
		private var loader:Loader;
		
		private var canvas:BitmapData;
		private var bitmap:Bitmap;
		private var particleList:Array = [];
		private var particleNum:int;
		private var num:int = 1;
		private const MAX_DIST:Number = 300;
		private const ADD_PARTICLEnum:int = 50;
		private var isEND:Boolean;
		
		public function PixelParticleEffect() {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var info:TextField = new TextField();
			info.appendText("Click to Reset");
			addChild(info);
			
			loader = new Loader();
			loader.load(new URLRequest("http://www.sph62.net/wonderfl/get_adobe_flash_player.png"), new LoaderContext(true));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSorceLoadComplate);
                        
			stage.addEventListener(MouseEvent.CLICK, reset);
		}
		
		private function reset(e:MouseEvent):void {
			if (hasEventListener(Event.ENTER_FRAME)) {
				removeEventListener(Event.ENTER_FRAME, showParticleEffect);
			}
			particleList = [];
			num = 1;
			removeChild(bitmap);
			onSorceLoadComplate();
		}
                
		private function onSorceLoadComplate(e:Event = null):void{
			var sorce:DisplayObject = loader;
			
			var offsetX:Number = (stage.stageWidth -sorce.width) / 2;
			var offsetY:Number = (stage.stageHeight - sorce.height) / 2;
			
			var mat:Matrix = new Matrix();
			mat.translate(offsetX, offsetY);
			
			var canvasWidth:Number = Math.min(offsetX + sorce.width +  MAX_DIST, stage.stageWidth);
			var canvasHeight:Number = Math.min(offsetY + sorce.height +  MAX_DIST, stage.stageHeight);
			
			canvas = new BitmapData(canvasWidth,canvasHeight,true,0x00000000);
			canvas.draw(sorce,mat);
			
			createParticle();
			
			canvas.fillRect(canvas.rect, 0x00000000);
			bitmap = new Bitmap(canvas, "auto", true);
			addChild(bitmap);
            
			addEventListener(Event.ENTER_FRAME, showParticleEffect);
			
		}
		
		private function createParticle():void {
			var i:int, j:int, c:uint;
			var p:PixcelParticle;
			for (i=0; i < canvas.width; i++) {
				for (j=0; j < canvas.height; j++) {
					c = canvas.getPixel32(i, j);
					if ( ( c >> 24 & 0xFF) != 0x00 &&( c >> 16 & 0xFF) != 0x00) {
						p = new PixcelParticle(i, j, c);
						setPosition(p);
						particleList.push(p);
					}
				}
			}
			
			particleNum = particleList.length;
			
		}
		
		private function setPosition(pp:PixcelParticle):void {
			var rad:Number = Math.random() * (Math.PI*2);
			var len:Number = Math.random() * MAX_DIST;
			pp.x = pp.lx + len * Math.cos(rad);
			pp.y = pp.ly + len * Math.sin(rad);
		}
		
		private function showParticleEffect(e:Event):void {
			canvas.lock();
			canvas.fillRect(canvas.rect, 0x00FFFFFF);
			var i:int;
			var p:PixcelParticle;
			var dx:Number, dy:Number;
			isEND = true;
			for ( i = 0; i < num; i++) {
				p = particleList[i];
				dx = Math.abs(p.lx - p.x);
				dy = Math.abs(p.ly - p.y);
				if (dx < 0.5 && dy < 0.5) {
					p.x = p.lx;
					p.y = p.ly;
				}else {
					p.x += (p.lx -p.x) * 0.250;
					p.y += (p.ly -p.y) * 0.250;
					isEND = false;
				}
				canvas.setPixel32(p.x, p.y, p.c);
			}
			
			canvas.unlock();
			
			if (isEND) {
				removeEventListener(Event.ENTER_FRAME, showParticleEffect);
			}else {
				num = (num + ADD_PARTICLEnum < particleNum)? num + ADD_PARTICLEnum : particleNum;
			}
			
		}
		
	}
	
}

class PixcelParticle {
	public var x:Number;
	public var y:Number;
	public var lx:Number;
	public var ly:Number;
	public var c:uint;
	
	public function PixcelParticle(sx:Number,sy:Number,color:uint) {
		x = 0;
		y = 0;
		lx = sx;
		ly = sy
		c = color;
	}
}