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

ColorCarousel

Get Adobe Flash player
by szbzs2004 24 Sep 2011
/**
 * Copyright szbzs2004 ( http://wonderfl.net/user/szbzs2004 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3ayK
 */

package  {

    import flash.display.Shape;    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    
    import frocessing.color.ColorHSV;
    
    [SWF (frameRate="50", backgroundColor="0x000000")]
    
    public class ColorCarousel extends Sprite {

        private static const CARDS_COUNT:uint = 11;
        private static const CARD_WIDTH:Number = 100;
        private static const CARD_HEIGHT:Number = 100;
        private static const MAX_SPEED:Number = 5;
        private static const MAX_PITCH:Number = 40;
        
        private var cards:Array;
        private var speed:Number;
        
        public function ColorCarousel() {
            x = stage.stageWidth / 2;
            y = stage.stageHeight / 2;
            speed = 0;
            cards = new Array();
            for (var i:uint = 0; i < CARDS_COUNT; ++i) {
                var card:Shape = new Shape();
                var hue:int = (i * 360) / CARDS_COUNT;
                var color:uint = (new ColorHSV(hue)).toRGB().value;
                card.graphics.beginFill(color);
                card.graphics.drawRect(0, CARD_HEIGHT/2, CARD_WIDTH, -CARD_HEIGHT);
                card.rotationY = 360 - hue;
                addChild(card);
                cards.push(card);
            }
            sortCards();
            stage.addEventListener(MouseEvent.MOUSE_MOVE, pitch);
            addEventListener(Event.ENTER_FRAME, animation);
        }

        private function pitch(e:MouseEvent):void {
            var tX:Number = 1 - 2 * e.stageX / stage.stageWidth;
            speed = tX * MAX_SPEED;
            var tY:Number = 1 - 2 * e.stageY / stage.stageHeight;
            rotationX = tY * MAX_PITCH;
            e.updateAfterEvent();
        }
        
        private function animation(e:Event):void {
            if (speed != 0) {
                cards.forEach(function(c:Shape, i:int, a:Array):void {
                    c.rotationY += speed;
                });
                sortCards();
            }
        }
        
        private function sortCards():void {
            function calcZ(a:Number):Number {
                return (-Point.polar(1, Math.PI * (2 - a / 180)).y);
            }
            cards.sort(function(c1:Shape, c2:Shape):Number {
                var z1:Number = calcZ(c1.rotationY);
                var z2:Number = calcZ(c2.rotationY);
                if (z1 < z2) {
                    return -1;
                } else if (z1 > z2) {
                    return 1;
                } else {
                    return 0;
                }
            });
            cards.forEach(function(c:Shape, i:int, a:Array):void {
                setChildIndex(c, i);
            });
        }
    }
}