flash on 2012-4-18
How to interact with this?
What inspired you?
Core logic explanation?
or requests for viewers?
If this field is left blank, description will be auto extracted from code comments.
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
[SWF(backgroundColor=0x0, width=800, height=600, frameRate=24)]
public class NorthernLights extends Sprite {
private var bmpData:BitmapData;
private var bmpBack:Bitmap;
private var bmpDataBack:BitmapData;
private var bmpPerlin:BitmapData;
private var blur:BlurFilter;
private var _particles:Array;
private var numParticles:int = 5000;
public function NorthernLights():void {
bmpPerlin = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0xFFFFFF);
bmpPerlin.perlinNoise(100, 100, 13, Math.round(Math.random()*100), false, true);
addChildAt(new Bitmap(bmpPerlin), 0);
bmpDataBack = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
bmpBack = new Bitmap(bmpDataBack);
addChild(bmpBack);
bmpBack.blendMode = "normal";
bmpData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFD700);
_particles = [];
blur = new BlurFilter(10, 10, 1);
for(var i:int=0; i<numParticles; i++) {
var p:Particle = new Particle(stage.stageWidth / 2, stage.stageHeight / 2);
_particles.push(p);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(evt:MouseEvent):void {
bmpPerlin.perlinNoise(100, 100, 13, Math.round(Math.random()*100), false, true);
}
private function onEnterFrame(evt:Event):void {
bmpData.fillRect(bmpData.rect, 0x00000000);
var p:Particle;
var i:int = _particles.length;
while(--i > -1) {
p = _particles[i];
bmpData.setPixel32(p.x, p.y, p.color);
p.move(bmpPerlin.getPixel(p.x, p.y));
}
bmpDataBack.draw(bmpData, null, null, "add");
bmpDataBack.applyFilter(bmpDataBack, bmpData.rect, new Point(0, 0), blur);
}
}
}
class Particle {
private var w:uint=800;
private var h:uint=600;
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var color:uint = 0xFFD700;
public function Particle(_x:Number, _y:Number):void {
x = _x;
y = _y;
vx = 5*(Math.random()-Math.random());
vy = 5*(Math.random()-Math.random());
}
public function move(_value:uint):void {
var r:uint = _value >> 16;
var g:uint = _value >> 8 & 255;
var b:uint = _value & 255;
vx += (r-b)/100;
vy += (g-b)/100;
// clip
vx = Math.min(vx, 5);
vy = Math.min(vy, 5);
vx = Math.max(vx, -5);
vy = Math.max(vy, -5);
x += vx;
y += vy;
if(x < 0 || x > w) {
vx *= -1;
}
if(y < 0 || y > h) {
vy *= -1;
}
r = (x / w) * 255;
g = (y / h) * 255;
b = Math.abs(Math.round((vx+vy)))*1;
color = (255 << 24 | r << 16 | g << 8 | b);
}
}