flash on 2012-11-16
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9tfv
*/
package {
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var pic:Bitmap;
public var vecLight:Vector.<Light>;
public function FlashTest()
{
pic = new Bitmap(new BitmapData(128,128,false,0));
pic.width = 460;
pic.height = 460;
addChild(pic);
vecLight = new Vector.<Light>;
var i:int;
var m:Light;
for (i = 0; i < 8; i++)
{
m = new Light();
m.cx = Math.random() * pic.bitmapData.width;
m.cy = Math.random() * pic.bitmapData.height;
m.rad = 8 + Math.random()*256;
m.red = Math.random() ;
m.green = Math.random() ;
m.blue = Math.random() ;
vecLight.push(m);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onEnter(e:Event):void
{
var cw:Number;
var ch:Number;
cw = pic.bitmapData.width / pic.width;
ch = pic.bitmapData.height / pic.height;
pic.bitmapData.setPixel(mouseX*cw, mouseY*ch, 0xFFffFF);
m = vecLight[0];
m.cx = mouseX * cw;
m.cy = mouseY * ch;
m.ity = (m.cx/cw)*0.07;
var v:int;
var num:int;
var m:Light;
var mx:Number;
var my:Number;
var mrad:Number;
var rad2:Number;
var i:int;
var k:int;
var ni:int;
var nk:int;
var t:Number;
var dx:Number;
var dy:Number;
var bm:BitmapData;
var bnum:Number;
var mag2:Number;
bm = pic.bitmapData;
ni = bm.height;
nk = bm.width;
mrad = 8;
rad2 = mrad*mrad;
var rad4:Number;
var cr:Number = 0;
var cg:Number = 0;
var cb:Number = 0;
var uc:uint;
mx = mouseX*cw;
my = mouseY*ch;
bm.lock();
num = vecLight.length;
for (i = 0; i < ni; i++)
{
for (k = 0; k < nk; k++)
{
bnum = 0;
cr = 0;
cg = 0;
cb = 0;
for (v = 0; v < num; v++)
{
m = vecLight[v];
rad2 = m.rad * m.rad;
dx = (m.cx) - k;
dy = (m.cy) - i;
mag2 = (dx*dx + dy*dy);
if (mag2 > (rad2*rad2)) { continue; }
bnum += 1;
//t = mag2 / rad2;
t = (m.rad) / mag2;
if (t > 1) { t = 1;}
if (t < 0) { t = 0;}
/*
if (bnum == 1)
{
cr = m.red * t;
cg = m.green * t;
cb = m.blue * t;
}
else
*/
{
cr += m.red * t *m.ity;
cg += m.green * t *m.ity;
cb += m.blue * t *m.ity;
}
/*
cr = Math.random();
cg = Math.random();
cb = Math.random();
*/
} //nextv
bnum *= 0.5;
/*
cr /= bnum;
cg /= bnum;
cb /= bnum;
*/
if (cr > 1) { cr = 1;}
if (cg > 1) { cg = 1;}
if (cb > 1) { cb = 1; }
uc = (255*cr) << 16 | (255*cg) << 8 | (255*cb);
bm.setPixel(k,i,uc);
//bm.setPixel(k,i, 255 *bnum);
/*
dx = mx - k;
dy = my - i;
t = rad2 / (dx*dx + dy*dy);
if (t > 1) { t = 1;}
if (t < 0) { t = 0; }
bm.setPixel(k, i, 255 * t);
*/
}//nextk
}//nexti
bm.unlock();
}//onenter
}//classend
}
internal class Light
{
public var red:Number = 1.0;
public var green:Number = 1.0;
public var blue:Number = 1.0;
public var color:uint = 0xFFffFF;
public var cx:Number = 0;
public var cy:Number = 0;
public var rad:Number = 8;
public var ity:Number = 1.0;
};//classend