flash on 2009-9-9
/**
* Copyright yd_niku ( http://wonderfl.net/user/yd_niku )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3I9v
*/
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.net.*;
import flash.system.*;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.easing.*;
[SWF( backgroundColor=0x000000, frameRate=60)]
public class FlashTest extends Sprite {
private var _points:Vector.<Point> = new Vector.<Point>;
private var _vertex:Vector.<Number> = new Vector.<Number>;
private var _indices:Vector.<int> = new Vector.<int>;
private var _uv:Vector.<Number> = new Vector.<Number>;
private var _col:int = 24;
private var _row:int = 24;
private function init():void{
var edgeW:int = _col -1;
var edgeH:int = _row-1;
var pW:Number= 465 / edgeW;
var pH:Number= 465 /edgeH;
var p:Point;
for( var py:int=0; py<_row; ++py ) {
for( var px:int=0; px<_col; ++px ) {
p = new Point( px*pW, py*pH );
_points.push( p)
_vertex.push( p.x, p.y );
_uv.push( px/edgeW, py/edgeH);
}
}
var uvW:Number = 1/edgeW;
var uvH:Number = 1/edgeH;
for( var h:int=0; h<edgeH; ++h ) {
for( var w:int=0; w<edgeW; ++w ) {
var cell:int = h*edgeH + w;
var a:int = cell + h;
var b:int = a+1;
var c:int = a+_col;
var d:int = a+_col+1;
_indices.push( a, b, d, a, c, d );
}
}
}
private var _canvas:Sprite = new Sprite();
private function atReady (e:Event ):void {
addChild( _canvas );
drawImage();
stage.addEventListener( MouseEvent.CLICK, click );
}
private var _tween:ITween;
private var _isConvergencing:Boolean = false;
private function click(e:MouseEvent):void{
_isConvergencing = !_isConvergencing;
var clickPoint:Point = new Point( mouseX, mouseY );
if ( _isConvergencing ) {
var tweens:Array = [];
var p:Point;
for( var i :int =0; i<_points.length; ++i ) {
p = _points[i];
var dx:Number = clickPoint.x - p.x;
var dy:Number = clickPoint.y - p.y;
var length:Number = Math.sqrt( dx*dx + dy*dy );
var percent:Number = length / 650;
tweens.push( BetweenAS3.delay( BetweenAS3.to( p, { x:clickPoint.x, y:clickPoint.y }, percent*percent*0.2+0.5, Cubic.easeIn ), length* 0.001 ) )
}
_tween = BetweenAS3.parallelTweens( tweens );
}
else {
_tween.stop();
_tween = BetweenAS3.reverse( _tween );
}
_tween.play();
addEventListener( Event.ENTER_FRAME, loop );
}
private function loop( e:Event ):void {
for( var i :int =0; i<_points.length; ++i ) {
var p:Point = _points[i];
_vertex[ i*2 ] = p.x;
_vertex[ i*2 +1 ] = p.y;
}
_canvas.graphics.clear();
drawImage();
drawWireframe();
}
private function drawImage():void {
_canvas.graphics.beginBitmapFill(_contents.bitmapData);
_canvas.graphics.drawTriangles( _vertex, _indices, _uv );
_canvas.graphics.endFill();
}
private function drawWireframe():void {
var paths:Vector.<IGraphicsData> = new Vector.<IGraphicsData>();
var length:int = _indices.length;
for( var i:int = 0; i<length; i+=6 ) {
var p0:Point = _points[ _indices[i] ]; //a
var p1:Point = _points[ _indices[i+1] ];//b
var p2:Point = _points[ _indices[i+4] ];//c
var p3:Point = _points[ _indices[i+2] ];//d
var command:GraphicsPath = new GraphicsPath();
command.moveTo( p0.x, p0.y );
command.lineTo( p1.x, p1.y );
command.lineTo( p3.x, p3.y );
command.lineTo( p0.x, p0.y );
command.lineTo( p2.x, p2.y );
command.lineTo( p3.x, p3.y );
paths.push( command );
}
_canvas.graphics.lineStyle( 1, 0xFFFFFF );
_canvas.graphics.drawGraphicsData(paths);
}
public function FlashTest() {
init();
addEventListener( READY, atReady );
loadImage();
}
private static const READY :String = "ready";
private var _contents:Bitmap;
private function loadImage():void {
var req:URLRequest = new URLRequest( "http://assets.wonderfl.net/images/related_images/e/e7/e78d/e78d6d39d6aec526aa98e642f1abc6b5484dfea4" );
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
loader.load( req, new LoaderContext(true) );
}
private function onComplete( e:Event ):void {
var loader:Loader = LoaderInfo(e.target).loader;
_contents = loader.content as Bitmap;
//addChild( _contents );
dispatchEvent( new Event( READY ) )
}
}
}