Beautifl - Flash Gallery

Preview

Water in Ball
ProjectNya 2010年5月9日 MIT License
?
//////////////////////////////////////////////////////////////////////////////  
[AS3.0] Waveクラスに挑戦! (2)  
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1133  
//////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////////////////////////////////////////
// [AS3.0] Waveクラスに挑戦! (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1133
////////////////////////////////////////////////////////////////////////////////


package {

    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.BlendMode;

    [SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]

    public class Main extends Sprite {
        private var water:Wave;
        private static var waves:Array = [190, 198, 206, 214];
        private static var segments:uint = 5;
        private static var xOffset:Number = 0.25;
        private static var yOffset:Number = 0.01;
        private var ball:Ball;

        public function Main() {
            init();
        }

        private function init():void {
            var frame:Shape = new Shape();
            frame.graphics.beginFill(0xFFFFFF);
            frame.graphics.drawCircle(0, 0, 116);
            frame.graphics.endFill();
            frame.x = 150;
            frame.y = 120;
            water = new Wave(300, 240, 0.2, 0.4);
            water.x = -150;
            water.y = -120;
            water.blendMode = BlendMode.MULTIPLY;
            water.addChild(frame);
            water.mask = frame;
            water.initialize(waves, segments, xOffset, yOffset);
            water.start();
            ball = new Ball(0, 120, 0xFFFFFF);
            addChild(ball);
            ball.x = 232;
            ball.y = 335;
            ball.contain(water);
        }

    }

}


import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import frocessing.math.PerlinNoise;
import frocessing.color.ColorHSV;
//import org.libspark.utils.GeomUtil;

class Wave extends Sprite {
    private var canvas:Sprite;
    private var perlin:PerlinNoise;
    private var color:ColorHSV;
    private var t:Number = 0;
    private var bWidth:uint;
    private var bHeight:uint;
    private var top:Number = 0.2;
    private var bottom:Number = 0.6;
    private var amplitude:Number = bottom - top;
    private var waves:Array = [190, 198, 206, 214];
    private var segments:uint = 5;
    private var ratio:Number = 1/segments;
    private var xOffset:Number = 0.25;
    private var yOffset:Number = 0.01;
    private static var tightness:uint = 40;

    public function Wave(w:uint, h:uint, t:Number = 0.2, b:Number = 0.6) {
        bWidth = w;
        bHeight = h;
        top = t;
        bottom = b;
        amplitude = bottom - top;
        init();
    }

    private function init():void {
        canvas = new Sprite();
        addChild(canvas);
        perlin = new PerlinNoise();
        color = new ColorHSV();
    }
    public function initialize(w:Array = null, s:uint = 5, xo:Number = 0.25, yo:Number = 0.01):void {
        waves = w;
        if (waves == null) waves = [190, 198, 206, 214];
        segments = s;
        xOffset = xo;
        yOffset = yo;
        ratio = 1/segments;
    }
    public function start():void {
        addEventListener(Event.ENTER_FRAME, draw, false, 0, true);
    }
    public function stop():void {
        removeEventListener(Event.ENTER_FRAME, draw);
    }
    private function draw(evt:Event):void {
        canvas.graphics.clear();
        for (var d:uint = 0; d < waves.length; d++) {
            var points:Array = new Array();
            points.push(new Point(-bWidth*ratio, bHeight*amplitude));
            for (var n:uint = 1; n <= segments+1; n++) {
                var xPos:Number = n*bWidth*ratio;
                var yPos:Number = perlin.noise(n*xOffset*d, t)*bHeight*amplitude + bHeight*top;
                points.push(new Point(xPos-bWidth*ratio, yPos));
            }
            t += yOffset;
            points.push(new Point(bWidth*(1+ratio), bHeight*amplitude));
            points.unshift(points[0]);
            points.push(points[points.length-1]);
            color.h = waves[d];
            canvas.graphics.beginFill(color.value, 0.2);
            canvas.graphics.moveTo(points[0].x, points[0].y);
            for (var p:uint = 0; p < points.length-3; p++) {
                var p0:Point = points[p];
                var p1:Point = points[p+1];
                var p2:Point = points[p+2];
                var p3:Point = points[p+3];
                for (var s:uint = 1; s < tightness+1; s++) {
                    //var px:Number = GeomUtil.spline(p0.x, p1.x, p2.x, p3.x, s/tightness);
                    //var py:Number = GeomUtil.spline(p0.y, p1.y, p2.y, p3.y, s/tightness);
                    var px:Number = spline(p0.x, p1.x, p2.x, p3.x, s/tightness);
                    var py:Number = spline(p0.y, p1.y, p2.y, p3.y, s/tightness);
                    canvas.graphics.lineTo(px, py);
                }
            }
            canvas.graphics.lineTo(bWidth*(1+ratio), bHeight);
            canvas.graphics.lineTo(-bWidth*ratio, bHeight);
            canvas.graphics.endFill();
        }
    }
    private function spline(p0:Number, p1:Number, p2:Number, p3:Number, t:Number):Number {
         var v0:Number = (p2 - p0) * 0.5;
        var v1:Number = (p3 - p1) * 0.5;
        var t2:Number = t * t;
        var t3:Number = t2 * t;
        return (2 * p1 - 2 * p2 + v0 + v1) * t3 + ( -3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
    }

}



import flash.display.Sprite;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
import frocessing.color.ColorHSV;

class Ball extends Sprite {
    public var id:uint;
    private var radius:uint;
    private var color:uint;
    private var hsv:ColorHSV;
    private var ball:Sprite;
    private var base:Shape;
    private var inside:Sprite;
    private var light:Shape;
    private var shade:Shape;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private var shaded:Boolean;
    private static var shadeHeight:Number;

    public function Ball(n:uint, r:uint, c:uint, a:Number = 1, s:Boolean = true) {
        id = n;
        radius = r;
        shaded = s;
        color = c;
        hsv = new ColorHSV(0, 1, 1);
        hsv.value = color;
        draw();
        ball.alpha = a;
        shadeHeight = radius*5;
    }

    public function init(hue:uint):void {
        hsv.h = hue;
        color = hsv.value;
        createBase();
    }
    public function contain(obj:DisplayObject):void {
        inside.addChild(obj);
    }
    public function set altitude(h:Number):void {
        ball.y = -radius-h;
        shade.alpha = 1 - h/shadeHeight;
    }
    private function draw():void {
        if (shaded) {
            shade = new Shape();
            addChild(shade);
            createShade();
        }
        ball = new Sprite();
        addChild(ball);
        ball.y = -radius;
        base = new Shape();
        ball.addChild(base);
        inside = new Sprite();
        ball.addChild(inside);
        light = new Shape();
        ball.addChild(light);
        createBase();
        createLight();
    }
    private function createBase():void {
        base.graphics.clear();
        base.graphics.beginFill(color);
        base.graphics.drawCircle(0, 0, radius);
        base.graphics.endFill();
        hsv.v = 0.8;
        var darken:uint = hsv.value;
        var glow:GlowFilter = new GlowFilter(darken, 1, radius*0.5, radius*0.5, 2, 3, true, false);
        base.filters = [glow];
    }
    private function createLight():void {
        var colors:Array = [bColor, bColor];
        var alphas:Array = [0.7, 0];
        var ratios:Array = [0, 191];
        var matrix:Matrix = new Matrix();
        var w:Number = radius*1.44;
        var h:Number = radius*1.35;
        var yOffset:Number = radius*0.95;
        matrix.createGradientBox(w, h, 0.5*Math.PI, -w*0.5, -yOffset);
        light.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
        light.graphics.drawEllipse(-w*0.5, -yOffset, w, h);
        light.graphics.endFill();
    }
    private function createShade():void {
        shade.graphics.beginFill(sColor);
        shade.graphics.drawEllipse(-radius*0.75, -radius*0.1775, radius*1.5, radius*0.375);
        shade.graphics.endFill();
        var shadow:DropShadowFilter = new DropShadowFilter(0, 90, sColor, 0.5, radius*0.15, radius*0.15, 1, 3, false, false, true);
        shade.filters = [shadow];
    }

}