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

forked from: Metaball

...
@author hbb
Get Adobe Flash player
by faseer 22 Jun 2010
    Embed
/**
 * Copyright faseer ( http://wonderfl.net/user/faseer )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/v0Og
 */

// forked from faseer's Metaball
package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author hbb
	 */
	public class TrickMetaball extends Sprite
	{
		private var _balls:Vector.<Ball>;
		private var _canvasSha:Shape;
		private var _canvasBmp:BitmapData;
		private var _output:BitmapData;
		
		private var _rArr:Array;
		private var _gArr:Array;
		private var _bArr:Array;
		
		private const _bound:Rectangle = new Rectangle(0.0, 0.0, 480.0, 480.0);
		private const _origin:Point = new Point(0.0, 0.0);
		private const _blurFilter:BlurFilter = new BlurFilter( 64.0, 64.0, 2);
		private const _blurFilter2:BlurFilter = new BlurFilter(8.0, 8.0, 1);
		
		public function TrickMetaball() 
		{
			createPalette();
			createBalls( 80);
			createOutput();
			addEventListener(Event.ENTER_FRAME, execute);
		}
		
		private function createPalette():void
		{
			_rArr = [];
			_gArr = [];
			_bArr = [];
			
			var n:int = 8;
			var s:int = 256 / n;
			for(var i:int=0; i<256; ++i)
			{
				var v:int = int(i/s) * s;
				if(v>255) v = 255;
				_rArr[i] = v << 16;
				_gArr[i] = v << 8;
				_bArr[i] = v;
			}
		}
		
		private function createOutput():void
		{
			_canvasSha = new Shape();
			_canvasBmp = new BitmapData( _bound.width, _bound.height, false );
			_output = _canvasBmp.clone();
			
			addChild( new Bitmap(_output) );
		}
		
		private function execute(e:Event):void 
		{
			_canvasSha.graphics.clear();
			
			var b:Ball;
			
			for (var i:int = _balls.length; i--; )
			{
				b = _balls[i];
				b.x += b.vx;
				b.y += b.vy;
				
				if (b.x + b.radius < 0) b.x = _bound.width + b.radius;
				else if (b.x - b.radius > _bound.width) b.x = - b.radius;
				
				if (b.y + b.radius < 0) b.y = _bound.height + b.radius;
				else if (b.y - b.radius > _bound.height) b.y = - b.radius;
				
				_canvasSha.graphics.beginFill( b.color, 1.0);
				_canvasSha.graphics.drawCircle( b.x, b.y, b.radius );
				_canvasSha.graphics.endFill();
			}
			
			
			_canvasBmp.fillRect(_bound, 0xFFFFFFFF);
			_canvasBmp.draw( _canvasSha );
			_canvasBmp.applyFilter( _canvasBmp, _bound, _origin, _blurFilter );
			
			//_output.threshold( _canvasBmp, _bound, _origin, '<', 0xf0, b.color, 0xFF, true );
			
			//_output.copyPixels( _canvasBmp, _bound, _origin );
			_output.paletteMap( _canvasBmp, _bound, _origin, _rArr, _gArr, _bArr, null );
			_output.applyFilter( _output, _bound, _origin, _blurFilter2 );
		}
		
		private function createBalls( num:int ):void
		{
			_balls = new Vector.<Ball>();
			var b:Ball;
			for (var i:int = 0; i < num; ++i)
			{
				b = _balls[i] = new Ball();
				b.x = _bound.width * Math.random(),
				b.y = _bound.height * Math.random(),
				b.radius = 20.0 + 20.0 * Math.random();
				b.vx = 4.0 - Math.random() * 8.0;
				b.vy = 4.0 - Math.random() * 8.0;
				
				var v:int = 0xff / i;
				
				var rr:int = (v & 0xff);
				var gg:int = (v & 0xff);
				var bb:int = (v & 0xff);
				
				rr += 0x33;
				gg += 0x99;
				bb += 0xdd;
				
				if(rr > 0xff) rr = 0xff;
				if(gg > 0xff) gg = 0xff;
				if(bb > 0xff) bb = 0xff;
				
				b.color = (rr << 16) | (gg << 8) | bb;
			}
			
		}
		
	}

}

class Ball
{
	public var x:Number = 0.0;
	public var y:Number = 0.0;
	public var vx:Number = 0.0;
	public var vy:Number = 0.0;
	public var radius:Number = 0.0;
	public var color:uint = 0;
}