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

Box Shadows

Get Adobe Flash player
by abeall 05 Jul 2013
package  {
    import flash.filters.BlurFilter;
    
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.display.Shape;
    import flash.display.Graphics;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.display.BlendMode;
    
    
    public class BoxShadows extends MovieClip {
        
        private const NUM_BOXES:int = 50;
        private const MAX_BOX_SIZE:Number = 50;
        
        private const boxColor:uint = 0xcccccc;
        private const shadowColor:uint = 0x333333;
        private const shadowLength:Number = 10;
        
        private var boxes:Vector.<Box>;
        private var shadowCanvas:Shape;
        private var lightSource:Shape;
        
        public function BoxShadows() {
            shadowCanvas = new Shape();
            //shadowCanvas.cacheAsBitmap = true;
            //shadowCanvas.blendMode = BlendMode.LAYER;
            //shadowCanvas.alpha = .1;
            addChild(shadowCanvas);
            
            lightSource = new Shape();
            lightSource.graphics.beginFill(0xffffff);
            lightSource.graphics.drawCircle(0, 0, 5);
            addChild(lightSource);
            
            createBoxes();
            
            stage.addEventListener(MouseEvent.CLICK, stageClick);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMove);
        }
        
        private function stageClick(e:MouseEvent):void {
            createBoxes();
            drawBoxShadows();
        }
        
        private function stageMouseMove(e:MouseEvent):void {
            lightSource.x = mouseX;
            lightSource.y = mouseY;
            drawBoxShadows();
        }
        
        private function createBoxes():void {
            for each(var box:Box in boxes){
                removeChild(box);
            }
            
            boxes = new Vector.<Box>();
            
            var n:int = NUM_BOXES;
            while(n--){
                addBox();
            }
        }
        
        private function addBox():void {
            var box:Box = new Box(Math.random() * MAX_BOX_SIZE, Math.random() * MAX_BOX_SIZE, boxColor);
            box.x = Math.random() * stage.stageWidth - box.width;
            box.y = Math.random() * stage.stageHeight - box.height;
            box.rotation = Math.random() * 360;
            addChild(box);
            boxes.push(box);
        }
        
        private function drawBoxShadows():void {
            var g:Graphics = shadowCanvas.graphics;
            g.clear();
            
            var source:Point = new Point(lightSource.x, lightSource.y);
            
            for each(var box:Box in boxes){
                var points:Vector.<Point> = box.corners;
                for(var i:int = 0; i < points.length; i++){
                    var p1:Point = points[i];
                    var p2:Point = points[(i + 1) % points.length];
                    var ray1:Point = new Point(p1.x - source.x, p1.y - source.y);
                    ray1.x *= shadowLength;
                    ray1.y *= shadowLength;
                    var ray2:Point = new Point(p2.x - source.x, p2.y - source.y);
                    ray2.x *= shadowLength;
                    ray2.y *= shadowLength;
                    
                    g.beginFill(shadowColor);
                    g.moveTo(p1.x, p1.y);
                    g.lineTo(p1.x + ray1.x, p1.y + ray1.y);
                    g.lineTo(p2.x + ray2.x, p2.y + ray2.y);
                    g.lineTo(p2.x, p2.y);
                    g.endFill();
                }
            }
        }
    }
   

}

import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.display.Graphics;
import flash.geom.Point;

class Box extends Sprite {
    
    private var w:Number;
    private var h:Number;
    
    public function Box(w:Number, h:Number, color:uint){
        this.w = w;
        this.h = h;
        var g:Graphics = graphics;
        g.beginFill(color);
        g.drawRect(0, 0, w, h);
    }
    
    public function get topLeft():Point { return localToGlobal(new Point(0, 0)); }
    public function get topRight():Point { return localToGlobal(new Point(w, 0)); }
    public function get bottomLeft():Point { return localToGlobal(new Point(0, h)); }
    public function get bottomRight():Point { return localToGlobal(new Point(w, h)); }
    
    public function get corners():Vector.<Point> { return new <Point>[topLeft, topRight, bottomRight, bottomLeft]; }
}