flash on 2011-1-14

by yama3
♥0 | Line 129 | Modified 2011-01-14 16:57:55 | MIT License
play

ActionScript3 source code

/**
 * Copyright yama3 ( http://wonderfl.net/user/yama3 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fEq2
 */

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.easing.*;
    import org.libspark.betweenas3.tweens.ITween;
    import com.bit101.components.*;
    
    [SWF(frameRate = 60)]
        
    public class FlashTest extends Sprite {
        private const IMAGE_URL:String = "http://farm4.static.flickr.com/3190/2662752839_249c6642b1.jpg";
        private const IMG_W:int = 500;
        private const IMG_H:int = 375;
        private const SEGMENT:int = 20;
        private var loader:Loader;
        private var vertexs:Array;
        private var sprite:Sprite;
        private var isHide:Boolean = false;
        private var isShift:Boolean = false;
        
        public function FlashTest():void {
            stage.quality = StageQuality.MEDIUM;   
            graphics.beginFill(0x0);
            graphics.drawRect(0, 0, 465, 465);
            new Label(this, 360, 440, "PLEASE CLICK STAGE");
            
            var context:LoaderContext = new LoaderContext(true);
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, compHandler);
            loader.load(new URLRequest(IMAGE_URL), context);             
        }
        
        private function compHandler(e:Event):void {
            sprite = new Sprite();
            addChild(sprite);
            vertexs = [];
            for(var xx:int = 0; xx < SEGMENT; xx++) {
                vertexs[xx] = [];
                for(var yy:int = 0; yy < SEGMENT; yy++) {
                    vertexs[xx][yy] = new Point(xx * IMG_W / SEGMENT, yy * IMG_H/SEGMENT);
                }
            }
            draw();
            addEventListener(Event.ENTER_FRAME, draw);
            stage.addEventListener(MouseEvent.CLICK, clickHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        }
        
        private function clickHandler(e:MouseEvent):void {
            var tweens:Array = [];
            var xx:int, yy:int, delay:Number;
            var px:Number = SEGMENT * (mouseX / IMG_W);
            var py:Number = SEGMENT * (mouseY / IMG_H);
            
            if(!isHide) {
                for(xx = 0; xx < SEGMENT; xx++) {
                    for(yy = 0; yy < SEGMENT; yy++) {
                        vertexs[xx][yy] = new Point(xx * IMG_W / SEGMENT, yy * IMG_H / SEGMENT);
                        delay = Math.sqrt((xx-px)*(xx-px)+(yy-py)*(yy-py))/40;
                        tweens.push(
                            BetweenAS3.delay(
                                BetweenAS3.tween(vertexs[xx][yy], {
                                    x:px * IMG_W/SEGMENT,
                                    y:py * IMG_H/SEGMENT
                                }, null, delay, Cubic.easeIn),
                                delay / 2
                            )
                        )
                    }
                }
            } else {
                var max:Number = 0;
                for(xx = 0; xx < SEGMENT; x++) {
                    for(yy = 0; yy < SEGMENT; yy++) {
                        vertexs[xx][yy] = new Point(px*IMG_W / SEGMENT, py * IMG_H / SEGMENT);
                        delay = Math.sqrt((xx-px)*(xx-px)+(yy-py)*(yy-py));
                        max = Math.max(max, delay);
                    }
                }
                for(xx = 0; xx < SEGMENT; xx++) {
                    for(yy = 0; yy < SEGMENT; yy++) {
                        delay = (max - Math.sqrt((xx-px)*(xx-px)+(yy-py)*(yy-py)))/40;
                        tweens.push(
                            BetweenAS3.delay(
                                BetweenAS3.tween(vertexs[xx][yy], {
                                    x:xx*IMG_W/SEGMENT,
                                    y:yy*IMG_H/SEGMENT
                                }, null, delay + 0.05, Quad.easeOut),
                                delay/2
                            )
                        )
                    }
                }
            }
            var itw:ITween = BetweenAS3.parallelTweens(tweens);
            if(isShift) itw = BetweenAS3.scale(itw, 5);
            itw.play();
            
            isHide = !isHide;
        }
        
        private function draw(e:Event = null):void {
            var vertices:Vector.<Number> = new Vector.<Number>();
            var indices:Vector.<int> = new Vector.<int>();
            var uvtData:Vector.<Number> = new Vector.<Number>();
            
            for(var xx:int=0; xx<SEGMENT; xx++) {
                for(var yy:int = 0; yy < SEGMENT; yy++) {
                    vertices[vertices.length] = vertices[xx][yy];
                    vertices[vertices.length] = vertices[xx][yy];
                    uvtData[uvtData.length] = xx / SEGMENT;
                    uvtData[uvtData.length] = yy / SEGMENT;
                }
            }
            
            for(var i:int = 0; i < SEGMENT; i++) {
                for(var j:int = 0; j < SEGMENT - 1; j++) {
                    indices.push(i*SEGMENT+j, i*SEGMENT+j+1, (i+1) * SEGMENT + j);
                    indices.push(i*SEGMENT+j+1, (i+1)*SEGMENT+1+j, (i+1)*SEGMENT+j);
                }
            }
            var g:Graphics = sprite.graphics;
            g.clear();
            g.beginBitmapFill(Bitmap(loader.content).bitmapData);
            g.drawTriangles(vertices, indices, uvtData);
            g.endFill();
        }
        
        private function keyUpHandler(e:KeyboardEvent):void {
            if(e.keyCode == 16) isShift = false;
        }
        
        private function keyDownHandler(e:KeyboardEvent):void {
            if(e.keyCode == 16) isShift = true;
        }
    }
}