forked from: Unknown Pleasures
// forked from borealkiss's Unknown Pleasures
package {
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.BitmapData;
import flash.events.MouseEvent;
[SWF(width="600",height="600",backgroundColor="0x0")]
public class Fl025_The25Line extends Sprite{
private var _bitmapData:BitmapData = new BitmapData(600,100,false,0x0);
private var _yCrossSection:Number = Math.random()*100;
public function Fl025_The25Line(){
var container:Sprite = new Sprite()
this.addChild(container);
for (var i:int=0; i<60; i++){
_bitmapData.perlinNoise(600/5,100/5,5,Math.floor(10*Math.random()+1),true,false,7,true);
var shape:Shape = new Shape();
this.draw(shape.graphics,_bitmapData);
container.addChild(shape);
shape.y = 600*i/60 + 100;//Tune
}
container.scaleX = 0.2;
container.scaleY = 0.25
container.x = container.y = 600/2 - container.width/2;
this.addMask(container);
}
protected function draw(g:Graphics,sourceBitmapData:BitmapData):void{
g.lineStyle(0,0xbbbbbb);
//var y:int = _yCrossSection;
var y:int = 37;
g.beginFill(0x0);
g.moveTo(0,0)
for (var x:int=0; x<sourceBitmapData.width; x++) g.lineTo( x,-1*getHeight(x,y,sourceBitmapData) );
g.lineTo(x,0);
g.endFill();
}
protected function addMask(target:DisplayObjectContainer):void{
var margin:Number = 5;//Tune
var mask:Sprite = new Sprite();
this.addChild(mask);
mask.graphics.beginFill(0x00FFFF,0.5);
mask.graphics.drawRect(0,0,target.width - margin,target.height - margin);
mask.graphics.endFill();
mask.x = mask.y = 600/2 - mask.width/2;
target.mask = mask;
}
//will not be counted as a line
protected function getHeight(x:int,y:int,sourceBitmapData:BitmapData):Number{
//These must be tuned.
var offsetY:Number = 50;//Tune
var amplitude:Number = 10;//Tune
var decay:Number = 0.4;//Tune
var color:uint = sourceBitmapData.getPixel(x,y);
var r:Number = color >> 16;
var numerator:Number = amplitude*(Math.exp(r/(decay*255)) - 1);
var dumper:Number = 1e-4;//Tune
var denominator:Number = dumper*Math.pow(x-(sourceBitmapData.width/2),2) + 1;
return numerator / denominator + offsetY;
}
}
}