[COLOR=#000000][FONT=Arial]Hi![/FONT][/COLOR]
[FONT=arial][COLOR=#000000]I need some help. I’m trying to create a cropping behavior. For that I’ve done a custom sprite with following additional properties:[/COLOR][COLOR=#000000]** target_image** : Sprite[/COLOR][COLOR=#000000], selection_rectangle : Rectangle[/COLOR][COLOR=#000000], mask : Sprite[/COLOR][COLOR=#000000], where the rectangle fits the exact same position and size that the mask[/COLOR] ones. My custom sprite is never resized or moved directly. Only its child have their properties updated
[COLOR=#000000]My goal is following. User creates the rectangle through helper class (which created handles too). Two things can happen then:[/COLOR]
[COLOR=#000000]1) Moving the handles (middle-left, middle-right, top-middle, bottom-middle) user only resizes the rectangle and mask. This is the “crop” effect. For that I’m ok. [/COLOR]
[COLOR=#000000]2) Moving the “corner handles” should resize the rectangle + the mask + the target image at the same time (note that the target image may not have the same dimension as the mask and rectangle one because of cropping behavior used in 1)). Image dimension should update (keeping its ratio) proportionally according to the new rectangle/mask dimension. This is were I’m stuck.[/COLOR]
[COLOR=#000000]I made a formula where at the moment I succeeded in resizing proportionally the image and the rectangle but I can’t figure out of to preserve the image dimension. Currently the image resets back to the rectangle dimension.
Finally I would like the image position not to change, that is, if I move my rectangle from (top,left) handle, rectangle is moving (x/y) and resizing but how can I make the image’s x,y updating to the corresponding offset distance ?[/COLOR][/FONT]
public static function resize(target_image : DisplayObject, selection_rectangle : Rectangle) : void {
var matrix : Matrix = new Matrix();
var trueWidth : Number = target_image.width / target_image.scaleX;
var trueHeight : Number = target_image.height / target_image.scaleY;
var wRectangle : Number = selection_rectangle.width;
var hRectangle : Number = selection_rectangle.height;
var sX : Number = wRectangle / trueWidth;
var sY : Number = hRectangle / trueHeight;
var ratioImage : Number = trueWidth / trueHeight;
var ratioRectangle : Number = hRectangle / hRectangle;
var s : Number = ratioImage >= ratioRectangle ? sY : sX;
var w : Number = trueWidth * s;
var h : Number = trueHeight * s;
var tX : Number = (w - hRectangle);// tX = 0.0;// tX = .5 * (w - hRectangle);
var tY : Number = (h - hRectangle);// tY = 0.0;// tY = .5 * (h - hRectangle);
// dimension
matrix.scale(s, s);
// position
matrix.translate(selection_rectangle.left - tX, selection_rectangle.top - tY);
target_image.transform.matrix = matrix;
}
[COLOR=#000000][FONT=Arial]Based on this script could someone help me to fine tune the formula (matrix.scale, matrix.translate). Is my formula correct or should I use a completely different approach ?[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]Thanks in advanced to all :)[/FONT][/COLOR]