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: ぺろーん(Flip Canvas)

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

// forked from mousepancyo's ぺろーん(Flip Canvas)
package  {
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.MouseEvent;
    import flash.display.Shape;
    import flash.display.Graphics;
    import flash.geom.Point;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import jp.progression.commands.*;
    import jp.progression.commands.lists.*;
    import jp.progression.commands.tweens.*;
    
    
    [SWF(width=465, height=465, backgroundColor=0xFFFFFF,frameRate=60)]
    
    public class Main extends Sprite{
        
        private var _canvas:BitmapData;
        private var _shape:Shape = new Shape();
        private var _g:Graphics = _shape.graphics;
        private var _mouseP:Point; 
        private var _timer:Timer;

        public function Main() {
             Wonderfl.capture_delay(20);
             //
            _canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
            addChild(new Bitmap(_canvas));
            //
            addChild(_shape);
            //
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
        }
        
        private function getBounse():Rectangle{
            var rect:Rectangle;
            rect = _canvas.getColorBoundsRect(0xFFFFFF, 0xFFFFFF, false);
            //
            return rect;
        }
        
        private function draw(e:MouseEvent):void{
            var p:Point = new Point(mouseX, mouseY);
            if(_mouseP == p) return;
            //
            _mouseP = p;
            //
            _g.lineTo(mouseX, mouseY);
            _canvas.draw(_shape);
        }
        
        private function onDown(e:MouseEvent):void{
            timerReset();
            stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
            _mouseP = new Point(mouseX, mouseY);
            //
            _g.clear();
            _g.lineStyle(4, 0);
            _g.moveTo(mouseX, mouseY);
        }
        
        private function onUp(e:MouseEvent):void{
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, draw);
            _g.clear();
            //
            startInderval();
        }
        
        private function startInderval():void{
            _timer = new Timer(500, 1);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, addCapture);
            _timer.start();
        }
        
        private function addCapture(e:TimerEvent):void{
            try{
                addChild(captureBmp());
            }catch(e:Error){
                //
            }
        }
        
        private function timerReset():void{
            if(!_timer) return;
            _timer.stop();
            _timer = null;
        }
        
        private function captureBmp():FripImage{
            var rect:Rectangle = getBounse();
            var bmd:BitmapData = new BitmapData(rect.width + 10, rect.height + 10);
            bmd.copyPixels(_canvas, rect, new Point(5, 5));
            //
            var flipImg:FripImage = new FripImage(bmd);
            flipImg.blendMode = "multiply"
            flipImg.x = rect.x - 5;
            flipImg.y = rect.y - 5;
            tweenOut(flipImg);
            flipImg.tweenOut();
            return flipImg;
        }
        
        private function tweenOut(target:Sprite):void{
            var list:SerialList = new SerialList();
            list.addCommand(
                            new Wait(2),
                            new DoTweener(target, {y:stage.stageHeight, alpha:0, time:2, transition:"easeOutCubic"})
                            )
            list.execute();
            //
            _canvas.fillRect(_canvas.rect, 0xFFFFFF);
        }

    }
    
}


//package  {
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.geom.Point;
    import jp.progression.commands.*;
    import jp.progression.commands.lists.*;
    import jp.progression.commands.tweens.*;
    
    internal class FripImage extends Sprite{
        
        private var _vertices:Vector.<Number> = new Vector.<Number>()
        private var _indices:Vector.<int> = new Vector.<int>()
        private var _uvData:Vector.<Number> = new Vector.<Number>()
        private var _pX:Vector.<Number> = new Vector.<Number>()
        private var _pY:Vector.<Number> = new Vector.<Number>()
        private var _pointList:Vector.<Point> = new Vector.<Point>()
        private var _bmd:BitmapData
        private var _bm:Bitmap
        private var _shape:Shape = new Shape()
        
        private var _xLen:int = 4    
        private var _yLen:int = 4

        public function FripImage(bmd:BitmapData) {
            _bmd = bmd;
            setup();
        }
        
        private function setup():void {
            addChild(_shape);
            for (var i:int=0; i < _xLen; i++){
                for (var j:int=0; j < _xLen; j++){
                    _uvData.push(j / (_xLen-1), i / (_yLen-1));
                    _vertices.push(_bmd.width * j / (_xLen-1), _bmd.height * i / (_yLen-1));
                    _pX.push(_bmd.width * j / (_xLen-1));
                    _pY.push(_bmd.height * i / (_yLen-1));

                    if (i != (_yLen - 1) && j != (_xLen - 1)){
                        _indices.push(_xLen * i + j, _xLen * i + j + 1, _xLen * (i + 1) + j);
                        _indices.push(_xLen * i + j + 1, _xLen * (i + 1) + j, _xLen * (i + 1) + j + 1);
                    }
                }
            }
            
            for (i=0; i < _xLen * _yLen; i++){
                var p:Point = new Point();
                p.x = _pX[i]
                p.y = _pY[i]
                _pointList.push(p);
            }
            
            createTriangles(_vertices, _indices, _uvData)    
        }
        
        public function tweenOut():void{
            var list:SerialList = new SerialList();
            list.addCommand(
                            new Wait(1),
                            new Func(startEnterFrame),
                            new Func(doTween),
                            new Wait(1.5),
                            new Func(stopEnterFrame)
                            )
            list.execute();
            //
            
        }
        
        private function startEnterFrame():void{
            this.addEventListener(Event.ENTER_FRAME, update);
        }
        private function stopEnterFrame():void{
            this.removeEventListener(Event.ENTER_FRAME, update);
        }
        
        private function doTween():void{
            var len:int = _pointList.length;
            var dist:Number = this.width * 2;
            for(var i:int = 0; i<len; i++){
                var tween:DoTweener = new DoTweener(_pointList[i], {x:dist, y:dist, time:1, delay:i * .05, transition:"easeInSine"});
                tween.execute();
            }
        }
        
        
        private function update(e:Event):void{
            var n:Number = 0.01
            var nX:int=0
            var nY:int=0
            
            for (var i:int=0; i < _vertices.length; i++){
                if(i%2 != 1){
                    if(nX<_pointList.length){
                        _vertices[i] = _pointList[nX].x
                        nX++
                    }
                }else{
                    if(nY<_pointList.length){
                        _vertices[i] = _pointList[nY].y
                        nY++
                    }
                }
            }
            
            createTriangles(_vertices, _indices, _uvData);
        }
        
        private function createTriangles(ver:Vector.<Number>, ind:Vector.<int>, uv:Vector.<Number>, cull:String = "none"):void{
            _shape.graphics.clear();
            _shape.graphics.beginBitmapFill(_bmd, null, true, true)
            _shape.graphics.drawTriangles(ver, ind, uv, cull)
            _shape.graphics.endFill()
        }

    }
//}