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

flash on 2010-8-23

TODO
初回ローディング ok
パフォーマンス改善
背景色いじり
クリックしたらサムネ表示←サムネロード ▲
タグ検索窓 ok
BGM?
@see http://www.nicovideo.jp/watch/sm11464969
@see http://www.youtube.com/watch?v=x08R89_zRSs
Get Adobe Flash player
by uwi 28 Aug 2010
    Embed
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cxbE
 */

package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.geom.*;
    import flash.ui.*;
    import flash.system.LoaderContext;
    import flash.text.TextField;
    import flash.filters.*;
    import flash.utils.*;
    import org.libspark.betweenas3.*;
    import org.libspark.betweenas3.easing.*;
    import org.libspark.betweenas3.tweens.*;
    import com.bit101.components.*;
    
    // TODO
    // 初回ローディング ok
    // パフォーマンス改善
    // 背景色いじり
    // クリックしたらサムネ表示←サムネロード ▲
    // タグ検索窓 ok
    // BGM?
    // @see http://www.nicovideo.jp/watch/sm11464969
    // @see http://www.youtube.com/watch?v=x08R89_zRSs
    [SWF(backgroundColor="#f3f3f3")]
    public class FlashTest extends Sprite {
        private const FEEDURL : String = "http://api.flickr.com/services/feeds/photos_public.gne?format=rss_200&tags=";
        private const media : Namespace = new Namespace("http://search.yahoo.com/mrss/");
        private var _thumburls : Array;
        private var _ul : URLLoader;
        private var _tf : TextField;
        private var SW : Number;
        private var SH : Number;
        private var _msg : Label;
        private var _searchBox : Text;
        private var _searchBtn : PushButton;
        
        public function FlashTest() {
            _tf = new TextField();
            _tf.width = 465;
            _tf.height = 465;
            _tf.y = 250;
//            addChild(_tf);
            
            SW = stage.stageWidth;
            SH = stage.stageHeight;
            
            _parts = [];
            _loadingQueue = [];
            _blurs = [[]];
            for(var i : uint = 1;i < 100;i++){
                _blurs.push([new BlurFilter(i, i)]);
            }
            
            _ul = null;
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            
            _searchBox = new Text(this, 10, SH - 20);
            _searchBox.editable = true;
            
            // 日本語打てないぢゃん
            var flagEnter : Boolean = false;
            _searchBox.textField.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent) : void {if(e.keyCode == Keyboard.ENTER){flagEnter = true; }});
            _searchBox.textField.addEventListener(TextEvent.TEXT_INPUT, function(e:TextEvent) : void {if(flagEnter){e.preventDefault();}});
            _searchBox.textField.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent) : void {if(e.keyCode == Keyboard.ENTER && flagEnter){onTagSearch(null);} flagEnter = false; });
            _searchBtn = new PushButton(this, 220, SH - 20, "TagSearch!", onTagSearch);
            
            _msg = new Label(this, 10, SH - 40);
            _msg.autoSize = true;
            _loadingTween = null;
        }
        
        private var _blurs : Array; // ピンぼけ用blur
        private var _parts : Array;
        private var _step : Number = -9999999;
 
        private function onTagSearch(e : MouseEvent) : void
        {
            if(_ul != null){
                _ul.close();
                _ul = null;
            }
            _ul = new URLLoader();
            _ul.addEventListener(Event.COMPLETE, onFeedLoadComplete);
            _ul.load(new URLRequest(FEEDURL + encodeURIComponent(_searchBox.text)));
            _msg.text = "Loading feed..";
            if(_loadingTween != null){
                _loadingTween.stop();
                _loadingTween = BetweenAS3.to(_msg, {x:10}, 1.0);
                _loadingTween.play();
            }
        }
        
        private function onEnterFrame(e : Event) : void
        {
            if(_loadingQueue.length == 0){
                if(_ul == null){
                    _ul = new URLLoader();
                    _ul.addEventListener(Event.COMPLETE, onFeedLoadComplete);
                    _ul.load(new URLRequest(FEEDURL + encodeURIComponent(_searchBox.text)));
                    _msg.text = "Loading feed..";
                }
            }else{
                if(_step % 180 == 0 && _parts.length <= 20){
                    var l : Loader = new Loader();
                    l.load(new URLRequest(_loadingQueue.pop()), new LoaderContext(true));
                    l.contentLoaderInfo.addEventListener(Event.COMPLETE, onPictureLoadComplete);
                 }
            }

            stepParts();
            _step++;
        }
        
        private function stepParts() : void
        {
            for(var i : int = 0;i < _parts.length;i++){
                var sp : MovieClip = _parts[i];
                if(sp.y >= SH){
                    removeChild(sp);
                    sp.removeEventListener(MouseEvent.CLICK, onPartClick);
                    sp.bmp.bitmapData.dispose();
                    if(i == _parts.length - 1){
                        _parts.pop();
                    }else{
                        _parts[i] = _parts.pop();
                        i--;
                    }
                    continue;
                }
                sp.x += sp.v[0];
                sp.y += sp.v[1];
                var oaz : int = Math.abs(sp.z) / 10;
                sp.z += sp.v[2];
                var az : int = Math.abs(sp.z) / 10;
                if(oaz != az){
                    sp.bmp.filters = _blurs[az];
                }
                sp.rotationX += sp.omega[0];
                sp.rotationY += sp.omega[1];
                sp.rotationZ += sp.omega[2];
                if(sp.alpha < 1)sp.alpha += 0.05;
            }
        }
        
        private var _loadingQueue : Array;
        private var _loadingTween : ITween;
        
        public function onFeedLoadComplete(e : Event) : void
        {
            _ul.removeEventListener(Event.COMPLETE, onFeedLoadComplete);
            var xl : XMLList = XML(_ul.data)..media::content;
            var len : uint = xl.length();
            _msg.text = len == 0 ? "Contents not found." : "Loading contents...";
            if(len > 0){
                if(_loadingTween != null)_loadingTween.stop();
                 _loadingTween = BetweenAS3.repeat(
                    BetweenAS3.serial(
                        BetweenAS3.to(_msg, {x:110}, 0.5, Cubic.easeInOut),
                        BetweenAS3.to(_msg, {x:10}, 0.5, Cubic.easeInOut)
                        ), 9999);
                 _loadingTween.play();
            }
            for(var i : uint = 0;i < len;i++){
                if(xl[i].@height * xl[i].@width <= 1200 * 1200){
                    _urlInfoMap[xl[i].@url.toXMLString()] = {
                        link : XML(_ul.data)..link[i+2].text().toXMLString(),
                        thumbnail : XML(_ul.data)..media::thumbnail[i].@url.toXMLString()
                    }
//                    tr(XML(_ul.data)..link[i+1].text().toXMLString());
//                    tr(XML(_ul.data)..media::thumbnail[i].@url.toXMLString());
  
                    _loadingQueue.push(xl[i].@url.toXMLString());
                }
            }
            _ul = null;
        }
        
        private var _urlInfoMap : Object = {};

        public function onPictureLoadComplete(e : Event) : void
        {
            e.currentTarget.removeEventListener(Event.COMPLETE, onPictureLoadComplete);
            _msg.text = "Enjoy!";
            if(_loadingTween != null){
                _loadingTween.stop();
                _loadingTween = BetweenAS3.to(_msg, {x:10}, 1.0);
                _loadingTween.play();
            }
            
            var l : Loader = e.currentTarget.loader as Loader; 
            
            var shrink : Number = 200 / Math.sqrt(l.width * l.height);
            if(shrink > 1.0)shrink = 1.0;
            var base : BitmapData = new BitmapData(l.width * shrink, l.height * shrink, false, 0x000000);
            base.draw(l, new Matrix(shrink, 0, 0, shrink));
            tr(l.width, l.height, shrink);
            
            var map : BitmapData = new BitmapData(base.width, base.height, false, 0);
            
            var i : uint;
            var N : uint = 7;
            
            var xsums : Vector.<Number> = new Vector.<Number>(N);
            var ysums : Vector.<Number> = new Vector.<Number>(N);
            var nums : Vector.<uint> = new Vector.<uint>(N);
            for(i = 0;i < N;i++){
                xsums[i] = 0;
                ysums[i] = 0;
                nums[i] = 0;
            }

            var x : uint, y : uint;
            map.lock();
            var q : Vector.<uint> = new Vector.<uint>();
            for(i = 0;i < N;i++){
                x = Math.random() * base.width;
                y = Math.random() * base.height;
                map.setPixel(x, y, i + 1);
                q.push(x, y, 0);
            }
            
            var sss : Number = getTimer();
            var p : uint = 0; 
            while(p < q.length){
                x = q[p++];
                y = q[p++];
                var left : uint = q[p++];
                if(left >= 1){
                    q.push(x, y, left - 1);
                }else{
                    var c : uint = map.getPixel(x, y);
                    xsums[c-1] += x;
                    ysums[c-1] += y;
                    nums[c-1]++;
                    var oc : uint = base.getPixel(x, y);
                    for(var u : int = -1;u <= 1;u++){ 
                        for(var v : int = -1;v <= 1;v++){
                            if(u == 0 && v == 0)continue;
                            var nx : int = x + u;
                            var ny : int = y + v; 
                            if(nx >= 0 && nx < map.width && ny >= 0 && ny < map.height && map.getPixel(nx, ny) == 0){
                                var dc : Number = deltaColor(base.getPixel(nx, ny), oc);
                                map.setPixel(nx, ny, c);
                                q.push(nx, ny, Math.min(dc / 7, 20));
                            }
                        }
                    }
                }
            }
            map.unlock();
            var ggg : Number = getTimer();
            tr(ggg - sss);
            var targX : Number = SW / 2 + Math.random() * SW / 2.5 - SW / 5;
            var targY : Number = SH / 2 + Math.random() * SH / 2.5 - SH / 5;
            var T : Number = targY + SW / 3;
            for(i = 0;i < N;i++){
                var bb : BitmapData = new BitmapData(base.width, base.height, true, 0);
                bb.copyPixels(base, base.rect, new Point());
                bb.threshold(map, map.rect, new Point(), "!=", i+1, 0, 0xffffff);
                
                var sp : MovieClip = new MovieClip();
                var g : Array = [xsums[i] / nums[i], ysums[i] / nums[i]];
                var bmp : Bitmap = new Bitmap(bb);
                bmp.x = -g[0];
                bmp.y = -g[1];
                sp.addChild(bmp);
                _parts.push(sp);
                addChildAt(sp, 0);
//                sp.mouseEnabled = false;
                sp.mouseChildren = false;
                
                sp.v = [
                    (Math.random() - 0.5) * 1.5,
                    (Math.random() + 0.5) * 1,
                    (Math.random() + 0.5) * -1
                    ];
                sp.omega = [
                    (Math.random() - 0.5) * 2,
                    (Math.random() - 0.5) * 2,
                    (Math.random() - 0.5) * 0
                    ];
                sp.bmp = bmp;
                sp.alpha = 0.0;
                sp.x = targX - base.width / 2 + g[0] - sp.v[0] * T;
                sp.y = targY - base.height / 2 + g[1] - sp.v[1] * T;
                sp.z = 0 - sp.v[2] * T;
                sp.rotationX = -sp.omega[0] * T;
                sp.rotationY = -sp.omega[1] * T;
                sp.rotationZ = -sp.omega[2] * T;

                sp.info = _urlInfoMap[l.contentLoaderInfo.url];
                sp.addEventListener(MouseEvent.CLICK, onPartClick);
            }
            _urlInfoMap[l.loaderInfo.url] = null;
            
            l.unload();
            base.dispose();
        }
        
        private function onPartClick(e : MouseEvent) : void
        {
             navigateToURL(new URLRequest(e.currentTarget.info.link), "_blank");
        }

        
        public function deltaColor(a : uint, b : uint) : Number
        {
            var ra : uint = (a >> 16) & 0xff;
            var ga : uint = (a >> 8) & 0xff;
            var ba : uint = (a >> 0) & 0xff;
            var rb : uint = (b >> 16) & 0xff;
            var gb : uint = (b >> 8) & 0xff;
            var bb : uint = (b >> 0) & 0xff;
            return Math.abs(ra - rb) + Math.abs(ga - gb) + Math.abs(ba - bb);
        }
        
        
        private function tr(...o : Array) : void
        {
            _tf.appendText(o + "\n");
            _tf.scrollV = _tf.maxScrollV;
        }
    }
}