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

Starry Night

Click to zoom in / out
Get Adobe Flash player
by bongiovi015 02 May 2011
/**
 * Copyright bongiovi015 ( http://wonderfl.net/user/bongiovi015 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/z0wi
 */

package {
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Bitmap;   
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.geom.Matrix3D;
    import flash.geom.Point;
    import flash.geom.Vector3D;
    import caurina.transitions.Tweener;
    import caurina.transitions.properties.CurveModifiers;

    
    public class StarryNight extends Sprite {
        public static const MAX : int = 2000;
        public static const R : int = 500;
        
        public var bmpBg:Bitmap;
        public var canvas:Shape = new Shape;
        public var particles:Particle;
        public var scale:Number = 1;
        public var ctrl:Point = new Point;
        
        public function StarryNight() {
            CurveModifiers.init();
            initBg();
            initParticles();
            addChild(canvas);
            
            addEventListener(Event.ENTER_FRAME, render);
            stage.addEventListener(MouseEvent.CLICK,  onClickHandler);
        }
          
          
        public function render(e:Event=null) : void {
            var p : Particle = particles;
            var mtx : Matrix3D;
            var pos : Vector3D;
            canvas.graphics.clear();
            
            while(p!=null) {
                p.appendRotation((stage.mouseX / stage.stageWidth - .5) * 1, Vector3D.Y_AXIS);
                p.appendRotation(-(stage.mouseY / stage.stageHeight - .5) * .5, Vector3D.X_AXIS);
                p.appendRotation(.125, Vector3D.Z_AXIS);
                
                mtx = p.clone();
                mtx.appendScale(scale, scale, scale);
                mtx.appendTranslation(ctrl.x, ctrl.y, 0);
                pos = mtx.position;
                
                if(pos.z < 0) {
                    canvas.graphics.beginFill(p.color);
                    canvas.graphics.drawCircle(pos.x+232, pos.y+232, p.size*scale);
                    canvas.graphics.endFill();
                }
                p = p.next;
            }
        }
        
        
        public function onClickHandler(e:MouseEvent) : void {
            const D : Number = 2;
            Tweener.addTween(this, {time:D, transition:"easeInBack", scale: scale == 1 ? 1.5 : 1});
            Tweener.addTween(ctrl, {x:0, y:0, _bezier:[{x:scale == 1 ? 100 : -100, y:scale == 1 ? 100 : -100}], time:D, transition:"easeInSine"});
        }
        
        
        public function initParticles() : void {
            var i : int = 0;
            var pHead : Particle;
            
            for(i=0; i<MAX; i++) {
                var p : Particle = new Particle();
                p.appendTranslation(R-50 + Math.random() * 100, 0, 0);
                p.appendRotation(Math.random()*360, Vector3D.X_AXIS);
                p.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
                p.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
                p.size = 1 + Math.random() * 3;
                p.color = brightnessColor(0xFFFFFF, .2 + Math.random()*.8);
                
                if(particles == null) {
                    particles = p;
                    pHead = p;
                }else {
                    particles.next = p;
                    particles = p;
                }
            }
            
            particles = pHead;
        }


        public function initBg() : void {
            graphics.beginFill(0, 1);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            var bmpd:BitmapData = new BitmapData(465, 465);
            bmpd.perlinNoise(465, 465, 2, Math.random()*0xFFFF, false, true, 5);
            bmpBg = new Bitmap(bmpd);
            addChild(bmpBg).alpha = 0;
            Tweener.addTween(bmpBg, {time:2, transition:"easeOutSine", alpha:0.1});
        }
        
        
        public static function brightnessColor(color:uint, level:Number) : uint {
            var a:uint = color >> 24 & 0xFF; 
            var r:uint = color >> 16 & 0xFF; 
            var g:uint = color >> 8 & 0xFF; 
            var b:uint = color & 0xFF;
            
            r *= level;
            g *= level;
            b *= level;
            
            if(r > 0xFF) r = 0xFF;
            if(g > 0xFF) g = 0xFF;
            if(b > 0xFF) b = 0xFF;
            
            var newColor:uint = a << 24 | r << 16 | g << 8 | b;
            return newColor;
        }

    }
}


import flash.geom.Matrix3D;


class Particle extends Matrix3D {
    public var color:uint = 0xFFFFFF;
    public var size:Number = 1;
    public var next:Particle;
    
    public function Particle(v:Vector.<Number>=null) : void {
        super(v);
    }
}