PDA

View Full Version : pixelate effect using AS 3.0



ty2008
September 29th, 2007, 08:31 AM
i want to build this http://www.senocular.com/flash/source.php?id=0.189
i tryed to convert senocular's pixelate effect from AS 2 to AS 3 but it's not working.

this is senocular's code


// create a movie clip to contain pixelated verson
// of the image in original_mc
this.createEmptyMovieClip( "pixelated_mc", 1 );
// place it below original_mc
pixelated_mc._y = original_mc._y + original_mc._height;

// create a variable to represent the current pixel size
var pixelSize = 1;

// every frame, increase pixelation
onEnterFrame = function(){
// create a new bitmapData object based on original
// movie clip size and current pixel size. This will
// be used to pixelate the original
var bitmapData = new flash.display.BitmapData( original_mc._width/pixelSize, original_mc._height/pixelSize, false );
// attach the bitmap to pixelated_mc
pixelated_mc.attachBitmap(bitmapData, 1);

// create a matrix object used to scale the image
// which will be drawn from the original to bitmapData
var scaleMatrix = new flash.geom.Matrix();
// scale based on pixelSize
scaleMatrix.scale(1/pixelSize, 1/pixelSize);

// draw the original in BitmapData object at its reduced size
bitmapData.draw( original_mc, scaleMatrix );

// assure pixelated_mc matches the size of original_mc
pixelated_mc._width = original_mc._width;
pixelated_mc._height = original_mc._height;

// increase pixelation
pixelSize *= 1.1;

// if pixel size is larger enough, return to 1
if (pixelSize > 80){
pixelSize = 1;
}
}and this is what i tryed


import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.*;
import flash.display.Bitmap;
import flash.display.BitmapData;


var pixelated_mc:MovieClip = new MovieClip();
addChild(pixelated_mc);
pixelated_mc.y = original_mc.y + original_mc.height;

var pixelSize = 1;

this.addEventListener(Event.ENTER_FRAME, pixelate);

function pixelate(event:Event){
var bitmapData:BitmapData = new BitmapData( original_mc.width/pixelSize, original_mc.height/pixelSize, false );
var bitmap:Bitmap = new Bitmap(bitmapData);
pixelated_mc.addChild(bitmap);
var scaleMatrix:Matrix = new Matrix();
scaleMatrix.scale(1/pixelSize, 1/pixelSize);
bitmapData.draw( original_mc, scaleMatrix );
pixelated_mc.width = original_mc.width;
pixelated_mc.height = original_mc.height;
pixelSize *= 1.1;
if (pixelSize > 80){
pixelSize = 1;
}
}could somebody please tell me what is not right ?
thanks

ty2008
October 2nd, 2007, 11:05 AM
i've found the problem

it should be


bitmap.width = original_mc.width;
bitmap.height = original_mc.height;

not


pixelated_mc.width = original_mc.width;
pixelated_mc.height = original_mc.height;

slopps
November 3rd, 2009, 09:46 PM
for the life of me I cant figure out what is going on in this line:
scaleMatrix.scale(1/pixelSize, 1/pixelSize);

Is it essentially averaging all the pixels in a "pixelSize" area into one color, or is it scaling up the pixel at the top left corner of a "pixelSize" square?
If you dont know what im talking about look at the example link posted up top.
Matrices blow my mind a little...