forked from: Line Wave
// forked from Saqoosha's Line Wave
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.events.Event;
import flash.geom.Point;
[SWF(width=465, height=465, backgroundColor=0x0, frameRate=60)]
public class Wave extends Sprite {
private static const ZERO_POINT:Point = new Point();
private static const K:Number = 10.0; // faster!
private static const M:Number = 0.5; // slower!
private static const SCROLL_SPEED:Number = 1.2 * M;
private static const NOISE_BASE_X:Number = 150;
private static const NOISE_BASE_Y:Number = 50;
private static const WAVE_HEIGHT:Number = 40;
private static const LINE_STEP_X:int = 16;
private static const LINE_STEP_Y:int = 4;
private var _noise:BitmapData;
private var _wave:Shape;
private var _scroll:Point = new Point();
public function Wave() {
this.addEventListener(Event.ADDED_TO_STAGE, this._onAddedToStage);
}
private function _onAddedToStage(e:Event):void {
this.stage.quality = StageQuality.LOW;
this._noise = new BitmapData(this.stage.stageWidth/K + 1, this.stage.stageHeight/K + 1, false, 0x0);
this._wave = this.addChild(new Shape()) as Shape;
this.addEventListener(Event.ENTER_FRAME, this._update);
}
private function _update(e:Event):void {
this._scroll.x += SCROLL_SPEED;
this._scroll.y += SCROLL_SPEED;
this._noise.perlinNoise(NOISE_BASE_X / (K * M), NOISE_BASE_Y / (K * M), 2, 123456, true, true, 1, true, [this._scroll]);
var g:Graphics = this._wave.graphics;
g.clear();
g.lineStyle(2, 0x1f5f7f, 0.5);
var px:Number = 0;
var py:Number = 0;
g.moveTo(px, py);
var last:Number = ((this._noise.getPixel(px/K, py/K) & 0xff) / 0x80 - 1) * WAVE_HEIGHT / M;
while (1) {
var ly:Number = py + ((this._noise.getPixel(px/K, py/K) & 0xff) / 0x80 - 1) * WAVE_HEIGHT / M
* 0.2 + last * 0.8; last = ly - py;
if (px == 0) {
g.moveTo(px, ly);
} else {
g.lineTo(px, ly);
}
px += LINE_STEP_X;
if (px >= this._noise.width * K) {
px = 0;
py += LINE_STEP_Y;
if (py >= this._noise.height * K) {
break;
}
}
}
}
}
}