BitmapData - scale and select area in one matrix?

人盡茶涼 提交于 2019-12-08 13:29:48

问题


I'm using a transform matrix as part of a bitmap draw to select an area of my target rather than drawing from 0,0:

var bmd:BitmapData = new BitmapData(target.width,target.height,true,0);
var mat:Matrix = new Matrix(1,0,0,1,-target.x,-target.y);
bmd.draw(this,mat);

This works perfectly, drawing the contents of this using target as a boundary. I can also use a matrix to scale as I draw like this:

var scale:Number = .32;
var bmd:BitmapData = new BitmapData(target.width/scale,target.height/scale,true,0);
var mat:Matrix = new Matrix(scale,0,0,scale);
bmd.draw(this,mat);

The problem comes when I try to combine the two into one operation:

var scale:Number = .32;
var bmd:BitmapData = new BitmapData(target.width/scale,target.height/scale,true,0);
var mat:Matrix = new Matrix(scale,0,0,scale,-target.x,-target.y);
bmd.draw(this,mat);

I'm not sure what's going wrong here, but when this is added to the stage as a bitmap nothing shows up, but if I only do one operation or the other they both work as expected. Any ideas?


回答1:


Doh, second question today that I'm answering myself. The tx and ty properties need to be multiplied by the scale factor in order to preserve the correct offset values. Presumably this is something to do with the order in which the matrix is translated?

Solution:

var scale:Number = .32;
var bmd:BitmapData = new BitmapData(target.width/scale,target.height/scale,true,0);
var mat:Matrix = new Matrix(scale,0,0,scale,-(target.x*scale),-(target.y*scale));
bmd.draw(this,mat);


来源:https://stackoverflow.com/questions/6520157/bitmapdata-scale-and-select-area-in-one-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!