[AS3.0] ランダム波形 (2)

AS3.0ランダム波形を描いてみるよ。 :boy:

:caution: 要 Flash Player 9 以上

This movie requires Flash Player 9.0.0
Main.as
package {

  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;


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

  public class Main extends Sprite {
    // プロパティ
    private var canvas:Sprite;
    private static var bWidth:uint = 600;
    private static var bHeight:uint = 260;
    private static var segments:uint = 5;
    private static var ratio:Number = 1/segments;
    private static var waves:uint = 4;
    private var perlin:PerlinNoise;
    private var color:ColorHSV;
    private var t:Number = 0;
    private var xOffset:Number = 0.25;
    private var yOffset:Number = 0.01;
    private static var tightness:uint = 40;

    // コンストラクタ
    public function Main() {
      init();
    }

    // メソッド
    private function init():void {
      canvas = new Sprite();
      addChild(canvas);
      perlin = new PerlinNoise();
      color = new ColorHSV();
      addEventListener(Event.ENTER_FRAME, draw, false, 0, true);
    }
    private function draw(evt:Event):void {
      canvas.graphics.clear();
      for (var d:uint = 0; d < waves; d++) {
        var points:Array = new Array();
        points.push(new Point(-bWidth*ratio, bHeight*0.4));

        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*0.4 + bHeight*0.2;
          points.push(new Point(xPos-bWidth*ratio, yPos));
        }
        t += yOffset;
        points.push(new Point(bWidth*(1+ratio), bHeight*0.4));
        points.unshift(points[0]);
        points.push(points[points.length-1]);

        color.h = 190 + d*8;
        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);
            canvas.graphics.lineTo(px, py);
          }
        }

        canvas.graphics.lineTo(bWidth*(1+ratio), bHeight);
        canvas.graphics.lineTo(-bWidth*ratio, bHeight);
        canvas.graphics.endFill();
      }
    }

  }

}

Xcode + Flex 2 SDK + Terminal
$ mxmlc Main.as
とする。