forked from: minimal snow
click to toggle perlin
/**
* Copyright ysle ( http://wonderfl.net/user/ysle )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/spb7
*/
// forked from yshu's minimal snow
package
{
//click to toggle perlin
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
public class Main extends Sprite
{
private var particles:Array = []
private var bmd:BitmapData
private var noise:BitmapData
private var offset:Array = [new Point(), new Point()]
private var noiseBm:Bitmap;
public function Main():void
{
stage.scaleMode = 'noScale'
stage.align = 'TL'
graphics.beginFill(0)
graphics.drawRect(0,0,465,465)
addChild(noiseBm=new Bitmap(noise = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0)))
addChild(new Bitmap(bmd = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0)))
for (var i:int = 0; i < 1000; i++) particles.push( initParticle( { }, Math.random() * bmd.height ))
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame)
stage.addEventListener(MouseEvent.CLICK,onClick)
}
private function initParticle(o:Object, y:Number = 0):Object
{
o.x = Math.floor( Math.random() * bmd.width )
o.y = y
o.sx = 2+Math.random()*2
o.sy = 4+Math.random()*4
o.c = ((o.sx / 3 * 0xff) << 24 ) + 0xffffff
return o
}
private function onEnterFrame(e:Event):void
{
bmd.fillRect(bmd.rect, 0x00000000)
noise.perlinNoise(300, 300, 2, 2, true, true, 1 | 4 , false, offset)
offset[0].x += 6
offset[0].y += 4
offset[1].x += 5
offset[1].y += 2
var color:Number
for each (var p:Object in particles)
{
color = noise.getPixel(p.x, p.y)
p.x += ( color >> 16) / 0xff * 2 * p.sx
p.y += ( color & 0xFF) / 0xff * 2 * p.sy
if ( p.x > bmd.width) p.x = p.x - bmd.width
if ( p.y > bmd.height) initParticle(p)
bmd.setPixel32(p.x, p.y, p.c)
}
}
private function onClick(e:MouseEvent):void { noiseBm.visible = !noiseBm.visible }
}
}