Actionscript 2, Actionscript 2.0, Flash, Flash _, Photography, Web developer

Bitmapdata Copypixels Rectangle at any angle example

Here is a function which copies pixels from one bitmapdata to another based on rotated rectangle // something like copyPixels but it is doing work with rotated rectangle
it has also optimisation for angle =0 then it uses copyPixels function

function copyRectangle(origBitmap:BitmapData, destBitmap:BitmapData, recX:Number, recY:Number, recW:Number, recH:Number, angle:Number) {

if (angle != 0) {

// degrees > radians
angle = angle*Math.PI/180;
// sin and cos for angle
sinVal = Math.sin(angle);
cosVal = Math.cos(angle);
// going through rectangle pixels
for (i=recX; i<(recX+recW); i++) {

for (j=recY; j<(recY+recH); j++) {

// getting position of original pixel / calculating rotation
Nx = (i-recX)*cosVal-(j-recY)*sinVal;
Ny = (j-recY)*cosVal+(i-recX)*sinVal;
Nx = Math.round(recX+Nx);
Ny = Math.round(recY+Ny);
// getting pixel value with alpha
colVal = origBitmap.getPixel32(Nx, Ny);
// setting pixel value with alpha
destBitmap.setPixel32(i-recX, j-recY, colVal);



} else {

// optimisation for angle 0, no rotation
destBitmap.copyPixels(origBitmap, new Rectangle(recX, recY, recW, recH), new Point(0, 0));



for some reason FLA files are big, and I’ve had this problem earlier so I can’t upload 5mb FLA file, anyway I think using a function is easy just like
copyRectangle(myBitmap, myBitmap2, 100, 100, , 150, 200, angle);

examples are here
Angle Random // using my algo
Angle Zero // using copyPixles

you can see speed difference

EDIT : Links are updated, sorry for keep you waiting…


4 thoughts on “Bitmapdata Copypixels Rectangle at any angle example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s