mausuit: Pixels motion on 2011-7-10
/**
* Copyright mausuit ( http://wonderfl.net/user/mausuit )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/s7mo
*/
// forked from mausuit's flash on 2011-7-10
package
{
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.filters.GlowFilter;
import flash.utils.Timer;
import flash.geom.ColorTransform;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
public class FlashTest extends Sprite
{
private var _pixelsData:BitmapData = new BitmapData(465, 465, true, 0xff000000);
private var _pixelsArea:Bitmap = new Bitmap(_pixelsData, "auto", true);
private var _areaCTransform:ColorTransform = new ColorTransform();
private var _renderTimer:Timer = new Timer(30);
private var _pixelsCollection:Array = [];
// add random colors to particles
private var _colorsCollection:Array = [0xff88ff00, 0xffaaaa00, 0xffaacc44, 0xffbb4466];
private var _numOfPixels:uint = 5000;
public function FlashTest()
{
addEventListener(Event.ADDED_TO_STAGE, setup);
}
private function setup(e:Event):void
{
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
_areaCTransform.alphaMultiplier = 0.95;
// add pixel area to stage
addChild(_pixelsArea);
_renderTimer.addEventListener(TimerEvent.TIMER, render);
createPixels();
// initializing
initApp();
}
private function createPixels():void
{
var currentPixel:Pixel;
var initx:uint;
var inity:uint;
for(var i:uint = 0; i < _numOfPixels; i++)
{
initx = Math.round(Math.random() * 465);
inity = Math.round(Math.random() * 465);
currentPixel = new Pixel(initx, inity);
_pixelsCollection.push(currentPixel);
}
}
private function render(e:Event):void
{
var currentPixel:Pixel;
for(var i:uint = 0; i < _pixelsCollection.length; i++)
{
currentPixel = _pixelsCollection[i];
currentPixel.updatePosition();
if(currentPixel.x > 465)
{
currentPixel.x = 0
}
else if(currentPixel.x < 0)
{
currentPixel.x = 465;
}
if(currentPixel.y > 465)
{
currentPixel.y = 0
}
else if(currentPixel.y < 0)
{
currentPixel.y = 465;
}
_pixelsData.setPixel32(currentPixel.x, currentPixel.y, _colorsCollection[i % _colorsCollection.length]);
}
_pixelsData.colorTransform(_pixelsData.rect, _areaCTransform);
_pixelsArea.bitmapData = _pixelsData;
}
private function initApp():void
{
_renderTimer.start();
}
private function finishApp():void
{
_renderTimer.stop();
}
}
}
internal class Pixel
{
public var x:int;
public var y:int;
public function Pixel(posx:int, posy:int)
{
x = posx;
y = posy;
}
public function updatePosition():void
{
// Crazy movement in x to the right
x += Math.round(Math.random() * 2 - 1);
// normal UP movement
y -= 1;
}
}