找到旋转文本字段的右上角

时间:2011-03-09 12:08:47

标签: actionscript-3 matrix transform

我正在使用以下内容从右上角旋转文本字段,我想知道找到右上角点的最佳方法。

           var txt:TextField = new TextField();
            txt.autoSize = TextFieldAutoSize.LEFT;
            txt.htmlText = "Some text here>";


            mtx = txt.transform.matrix.clone();
            MatrixTransformer.rotateAroundInternalPoint(mtx, txt.width, 0, -45);
            txt.transform.matrix = mtx;

编辑:

我刚刚在flash IDE中编写了下面的测试代码(omg,这很痛......),显然在库中有一个名为Arial的嵌入式字体。我无法让圆圈正确定位在文本字段的右上角。

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.geom.Matrix;
import flash.display.Shape;
import fl.motion.MatrixTransformer;
import flash.geom.Point;
import flash.text.TextFormat;
import flash.text.Font;

Font.registerFont(Arial);

var angle:Number = -45

var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.embedFonts = true;
txt.defaultTextFormat = new TextFormat("Arial", 20, 0x0000ff);
txt.text = "Here is some text";
txt.border = true;
txt.x = 100;
txt.y = 100;
addChild(txt);

var circle:Shape = new Shape();
circle.graphics.beginFill(0x00ff00);
circle.graphics.drawCircle(-5, -5, 10);
circle.graphics.endFill();
addChild(circle);

var mtx:Matrix = txt.transform.matrix.clone();
MatrixTransformer.rotateAroundInternalPoint(mtx, txt.width, 0, angle);
txt.transform.matrix = mtx;

// The top right after being turned -45°
var localPoint:Point = new Point( txt.width, txt.height );
var globalPosition:Point = txt.localToGlobal( localPoint );

circle.x = globalPosition.x;
circle.y = globalPosition.y;

1 个答案:

答案 0 :(得分:1)

如果你的意思是转右前的右上角:

// The field's original top right corner:
var localPoint:Point = new Point( txt.width, 0 );
var globalPosition:Point = txt.localToGlobal( localPoint );

如果你的意思是,在转过场后:

// The top right after being turned -45°
var localPoint:Point = new Point( txt.width, txt.height );
var globalPosition:Point = txt.localToGlobal( localPoint );

编辑:

好的,看到你的测试后,我尝试了很多东西,我想我发现了一些可行的方法:

var angle:Number = -45
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.defaultTextFormat = new TextFormat("Arial", 20, 0x0000ff);
txt.text = "Here is some text";
txt.border = true;
txt.x = 100;
txt.y = 250;
stage.addChild(txt);

var local3d:Vector3D = new Vector3D( txt.width, txt.height );

var circle:Shape = new Shape();
circle.graphics.beginFill(0x00ff00);
circle.graphics.drawCircle(-5, -5, 10);
circle.graphics.endFill();
stage.addChild(circle);

var mtx:Matrix = txt.transform.matrix.clone();
mtx.rotate( -45 / 180 * Math.PI );
txt.transform.matrix = mtx;

var globalPosition:Point = txt.local3DToGlobal( local3d );

circle.x = globalPosition.x;
circle.y = globalPosition.y;

如果我运行这个,我会得到一个TextField,转向-45度,绿色球粘在什么用作右下角。我不知道您的MatrixTransformer类,但它应该能够使用任何矩阵。

尝试复制并粘贴此代码并随意使用,直至获得所需的结果。