forked from: flash on 2011-7-15
@author shujita
/**
* Copyright shihu ( http://wonderfl.net/user/shihu )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6rJ7
*/
// forked from shihu's flash on 2011-7-15
/**
* @author shujita
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.getTimer;
public class TileClone extends Sprite{
//------- CONST ------------------------------------------------------------
//------- MEMBER -----------------------------------------------------------
private var _sourceBMD:BitmapData;
private var _targetBMD:BitmapData;
private var _applyBMD:BitmapData;
private var _bmWidth:int;
private var _bmHeight:int;
//------- PUBLIC -----------------------------------------------------------
public function TileClone() {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _onLoader1Complete, false, 0, true );
loader.load( new URLRequest( "http://farm6.static.flickr.com/5028/5589057286_717c49292f_b.jpg" ), new LoaderContext( true ) );
}
//--------------------------------------
//
//--------------------------------------
//------- PRIVATE ----------------------------------------------------------
private function _onLoader1Complete( event:Event ):void {
var bm:Bitmap = Bitmap( LoaderInfo( event.currentTarget ).content );
_bmWidth = bm.width;
_bmHeight = bm.height;
_targetBMD = new BitmapData( _bmWidth, _bmHeight );
_targetBMD.draw( bm );
_sourceBMD = _targetBMD.clone();
_applyBMD = _sourceBMD;
addChild( new Bitmap( _targetBMD ) );
stage.addEventListener( MouseEvent.CLICK, _onClick );
addEventListener( Event.ENTER_FRAME, _onEnterFrame );
}
private function _onEnterFrame( event:Event ):void {
_targetBMD.lock();
const LENGTH:uint = Math.pow( 2, 8 );
var gaussianX:Gaussian = new Gaussian( mouseX, stage.stageWidth >> 3 );
var gaussianY:Gaussian = new Gaussian( mouseY, stage.stageHeight >> 3 );
var bmd:BitmapData;
if ( _applyBMD == _targetBMD ) {
bmd = _targetBMD.clone();
} else {
bmd = _sourceBMD;
}
for ( var idx:int = 0; idx < LENGTH; idx++ ) {
var sizeX:int = 4 + 10 * Math.random();
var sizeY:int = 4 + 10 * Math.random();
var diff:int = 2 + 40 * Math.random();
var targetX:int = gaussianX.integer;
var targetY:int = gaussianY.integer;
var nextX:int = targetX + Math.random() * diff - ( diff / 2 );
var nextY:int = targetY + Math.random() * diff - ( diff / 2 );
_targetBMD.copyPixels( bmd, new Rectangle( targetX, targetY, sizeX, sizeY ), new Point( nextX, nextY ) );
}
_targetBMD.unlock();
}
private function _onClick( event:MouseEvent ):void {
if ( _applyBMD == _sourceBMD ) {
_applyBMD = _targetBMD;
} else {
_applyBMD = _sourceBMD;
}
}
//------- PROTECTED --------------------------------------------------------
//------- INTERNAL ---------------------------------------------------------
}
}
class Gaussian {
private static const _2pi:Number = Math.PI;
private static const _hwhm:Number = 2.35 / 2;
private var _half:Number = _hwhm;
private var _peak:Number = 0;
public function Gaussian(expectation:Number,hwhm:Number) {
_half = hwhm / _hwhm;
_peak = expectation;
}
private function normalDistribution():Number {
var u1:Number = Math.random();
var u2:Number = Math.random();
return _half * Math.sqrt(-2 * Math.log(u1)) * Math.cos(_2pi * u2);
}
public function get float():Number {
return normalDistribution() + _peak;
}
public function get integer():int {
var g:Number = normalDistribution();
return int(g+((g > 0)? .5 : -.5))+_peak;
}
}