/**
* Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5uj5
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.BlurFilter;
import flash.geom.*;
public class TrickMetaball extends Sprite
{
private var _balls:Vector.<Ball>;
private var _canvasSha:Shape;
private var _canvasBmp:BitmapData;
private var _output:BitmapData;
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()
{
createBalls( 10 );
//createBalls(25);
createOutput();
addEventListener(Event.ENTER_FRAME, execute);
}
private function createOutput():void
{
var bg:Sprite = new Sprite();
bg.graphics.beginFill(0xFFFFFF);
bg.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
addChild(bg);
var clrs:Array = new Array[ 0x666666,0x999999 ];
var alph:Array = new Array[ 0.7,0.7 ];
var spMd:Array = new Array[ 'pad' ];
bg.graphics.beginGradientFill( 'radial',clrs,alph,spMd );
_canvasSha = new Shape();
_canvasBmp = new BitmapData( _bound.width, _bound.height, true );
_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 < 0) b.x = _bound.width;
else if (b.x > _bound.width) b.x = 0;
if (b.y < 0) b.y = _bound.height;
else if (b.y > _bound.height) b.y = 0;
_canvasSha.graphics.beginFill(0x000000, 1.0);
_canvasSha.graphics.drawEllipse(b.x, b.y, b.radius, b.radius);
_canvasSha.graphics.endFill();
}
_canvasBmp.fillRect(_bound, 0x000000);
_canvasBmp.draw( _canvasSha );
_canvasBmp.applyFilter( _canvasBmp, _bound, _origin, _blurFilter );
_output.copyPixels( _canvasBmp, _bound, _origin );
_output.threshold( _canvasBmp, _bound, _origin, '>', 0x999999, 0xFF0000, 0xFF0000, true );
//_output.threshold( _output, _bound, _origin, '<', 0xee, 0xFF000000, 0xFF, true );
//_output.applyFilter( _output, _bound, _origin, _blurFilter );
}
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 = 60.0 * Math.random();
b.vx = 4.0 - Math.random() * 4.0;
b.vy = 4.0 - Math.random() * 4.0;
}
}
}
}
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;
}