开发者

ActionScript - getPixel() Coords on BitmapData with Transformation Matrix?

开发者 https://www.devze.com 2023-02-20 03:33 出处:网络
i\'ve filled a circle shape with the bitmap data of a bitmap asset. i need to rotate the circle from the center, so i added the bitmap to the center of the circle and used a matrix transformation to

i've filled a circle shape with the bitmap data of a bitmap asset.

i need to rotate the circle from the center, so i added the bitmap to the center of the circle and used a matrix transformation to shift the bitmapdata upward and leftward so it appears centered.

now i'm trying to read the bitmap data with getPixel() but the return value is off since it is returning the untransformed position of the bitmap data instead of the visible transformed bitmapdata.

Bitmap(Sprite(evt.currentTarget).getChildAt(0)).bitmapData.getPixel(evt.localX, evt.localY)

how can i get an accurate reading of the pixel data from a bitmap with a matrix transformation?


updated with code. assume the redCircleData in my example is more colorful and something worth retrieving pixel data.

var redCircleData:开发者_StackOverflowSprite = new Sprite();
redCircleData.graphics.beginFill(0xFF0000, 1.0);
redCircleData.graphics.drawCircle(0, 0, 100);
redCircleData.graphics.endFill();

var matrix:Matrix = new Matrix();
matrix.tx = redCircleData.width / 2;
matrix.ty = redCircleData.height / 2;

var myBitmapData:BitmapData = new BitmapData(redCircleData.width, redCircleData.height, true, 0x00FFFFFF);
myBitmapData.draw(redCircleData, matrix);

var theBitmap:Bitmap = new Bitmap(myBitmapData, PixelSnapping.AUTO, true);
theBitmap.x -= matrix.tx;
theBitmap.y -= matrix.ty;

addChild(theBitmap);


The Matrix class gives you some very handy functions to deal with your problem:

Matrix.invert();

Matrix.transformPoint();

Get the inverse of your matrix (invert a copy of it) and then transform your Point(e.localX,e.localY) with it. Use the transformed point coords as your arguments to getPixel(). BitmapData is just a block of RGB or RGBA pixels in memory, where (0,0) is the upper left. You need the to work backward through the transform you use to display it to find the pixel of the bitmap that is under your mouse. That's what the inverted matrix does for you.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/geom/Matrix.html


I'm not sure if what you're trying to do is possible (I've never tried it, at least, and I'm under the impression that you can't really apply a transformation to a BitmapData object, but I might be wrong). However, you could create a new BitmapData with a snapshot of the region you're interested in and read the pixels from this new object. This way you could easily achieve what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消