【问题标题】:In createjs textAlign Center Causing hit area misalignment在createjs textAlign Center 中导致命中区域错位
【发布时间】:2015-09-26 22:32:39
【问题描述】:

当我将文本设置为链接、设置边界形状并将点击区域设置为边界形状时,如果 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";
});

【问题讨论】:

  • 顺便说一句,您不需要将光标设置在鼠标悬停上。这正是cursor 所做的(设置将在鼠标悬停时应用的光标)。

标签: text createjs


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我会调查的。我对使用 HTML5 Canvas 和 createJS 套件还是很陌生。如果你是 createjs 的创造者,感谢你所做的一切。这是一个非常有用的工具!
  • @gskinner,我也面临同样的问题。只有一个区别是我们允许用户在添加文本后更改对齐方式。你能看看这个stackoverflow.com/questions/64314986/…
猜你喜欢
  • 2016-01-22
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2015-11-08
相关资源
最近更新 更多