【问题标题】:find top right corner of rotated textfield找到旋转文本字段的右上角
【发布时间】:2011-03-09 12:08:47
【问题描述】:

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

           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 中编写了下面的测试代码(天哪,这很痛苦……),显然库中有一个名为 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;

【问题讨论】:

  • 你需要更具体一些,你需要“以前是右上角的当前位置”吗?您是否总是将盒子转动 -45°,或者您需要任何旋转的通用解决方案?
  • 好吧,抱歉,无论旋转角度是多少,我都想始终获取 txt TextField 的右上角,我需要为此使用 trig 吗?

标签: actionscript-3 matrix transform


【解决方案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 类,但它应该能够与任何矩阵一起使用。

尝试复制并粘贴此代码并摆弄它,直到获得所需的结果。

【讨论】:

  • 感谢您的回复,这似乎在旋转版本上不起作用,似乎指向边界框的右下角而不是旋转文本字段的右上角
  • @Neil,你确定第一个 sn-p 不起作用吗?无论当前的旋转或位置是什么,它都应该为您提供过去的右上角的全局位置。您确定您将实际的文本字段实例本身用作 txt.localToGlobal 的“txt”部分而不是持有人 DisplayObject?
  • EyeSeeEm,我在上面添加了一个测试,除非我遗漏了一些我无法让它工作的东西。
  • @Neil 我刚刚编辑了我的答案,试试新代码。它似乎非常脆弱,所以1:1复制它。例如,如果省略 local3DtoGlobal 行,整个 TextField 就会消失...
  • 嘿,干得好老兄,我发现只需调整 Vector3D 中的值,我就可以定位我想要的任何部分。谢谢你的坚持。
猜你喜欢
  • 1970-01-01
  • 2021-09-10
  • 2012-08-11
  • 2016-01-12
  • 2013-02-13
  • 1970-01-01
  • 2011-12-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多