jueves, 31 de enero de 2008

Pixelizer AS3

Hace un par de meses que he descubierto AS3 (actionscript 3) y el montón de cosas que permite hacer, bueno ya os contaré más adelante algunas de ellas de momento quería compartir una modesta clase que he adaptado a ActionScript 3 para hacer el efecto de pixelización de una imagen, la clase es muy fácil de utilizar. La he adaptado de actionscript 2 que he encontrado en el blog de sephorith.com

package {
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.geom.Rectangle;

public class Pixelizer extends MovieClip {
/** define the variables used **/
private var img:Bitmap;
private var cloned:Bitmap;
private var mc:MovieClip;
private var rect:Rectangle;
private var row:Number;
private var w:Number;
private var h:Number;
private var diff:Number;
private var pixelSize:Number;
private var iPixelSize:Number;
private var step:Number;
private var _timer:Timer;
public function Pixelizer(image_path:Class, pixelSz:Number = 30, _step:Number = 5) {
iPixelSize = pixelSz;
step = _step;
pixelSize = pixelSz;
// attach a bitmapData loading an image
// from the library
img = Bitmap(new image_path);

// and clone it
cloned = Bitmap(new image_path);
// getting the bitmapdata width and height
w = img.width;
h = img.height;

// create a new movieclip and attach the image
// to it
addChild(img);
rect = new Rectangle();
row = -1;
while(row < diff =" w" width =" diff" height =" pixelSize" number =" 0;" y =" c;" _timer =" new" rect =" new" row =" 0;" diff =" w" width =" diff" height =" pixelSize" number =" 0;" y =" c"> 1) {
pixelSize -= step;
}
else if (pixelSize == 1) {
_timer.stop();
}
else {
pixelSize = 1;
}
e.updateAfterEvent();
}
}
}

Para utilizarla solo debe importarse la clase e instanciarla pasandole como parametro una imagen cargada
ej:

[Embed(source = '../library/ojo.jpg')] private var FondoImage:Class;
...
var p:Pixelizer = new Pixelizer(FondoImage,20,2);
...

La clase extiende a la clase MovieClip así que una vez instanciada solo hay que añadirla mediante el método addChild al escenario o a otro elemento.

No hay comentarios: