在createjs textAlign Center中导致命中区域错位

时间:2015-09-25 15:58:45

标签: text createjs

当我将文本设置为链接时,设置边界形状,并将命中区域设置为边界形状,如果textAlign ='center',我的命中区域将关闭。有什么想法吗?

var linkColor = "#0000ff";
var linkFont = "bold 14px Times";

var presentationLink = new createjs.Text("View Presentation", linkFont, linkColor);
presentationLink.textAlign = "left";
presentationLink.maxWidth = 170;
presentationLink.x = 300;
presentationLink.y = 125;
stage.addChild(presentationLink);

var plBounds = presentationLink.getTransformedBounds();

var s = new createjs.Shape().set({ x: plBounds.x, y: plBounds.y + plBounds.height });
s.graphics.s(linkColor).moveTo(0, 0).lineTo(plBounds.width, 0);
stage.addChild(s);

var hitAreaForPLink = new createjs.Shape(new createjs.Graphics().beginFill("#ffffff").drawRect(-10, -10, plBounds.width + 20, plBounds.height + 10));
presentationLink.hitArea = hitAreaForPLink;

stage.enableMouseOver();

presentationLink.on("mouseover", function () {
    presentationLink.cursor = "pointer";
});

1 个答案:

答案 0 :(得分:1)

hitArea根据其所有者的坐标系定位。也就是说,所有者的所有转换都应用于hitArea。这样,如果您要为所有者设置动画,hitArea会按预期跟踪它。

由于已经应用了转换,因此您需要使用getBounds,而不是getTransformedBounds。请参阅此示例:http://jsfiddle.net/gskinner/uagv5t84/2/