Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用RaphaelJS约束正方形中的文本元素?_Javascript_Drawing_Raphael - Fatal编程技术网

Javascript 如何用RaphaelJS约束正方形中的文本元素?

Javascript 如何用RaphaelJS约束正方形中的文本元素?,javascript,drawing,raphael,Javascript,Drawing,Raphael,我试图用自定义字体将文本元素约束在正方形中。我很难让约束发生 对于move函数,我的代码如下所示: if (this.attr("y") > offsetY || this.attr("x") > offsetX) { // keep dragging & storing original x and y this.attr({ x : this.ox + dx, y : this.oy + dy }); } else {

我试图用自定义字体将文本元素约束在正方形中。我很难让约束发生

对于move函数,我的代码如下所示:

if (this.attr("y") > offsetY || this.attr("x") > offsetX) { // keep dragging & storing original x and y
    this.attr({
        x : this.ox + dx,
        y : this.oy + dy
    });
} else {
    nowX = Math.min(offsetX, this.ox + dx);
    nowY = Math.min(offsetY, this.oy + dy);
    nowX = Math.max(0, nowX);
    nowY = Math.max(0, nowY);
    this.attr({
        x : nowX,
        y : nowY
    });
}
约束永远不会发生。但是,如果我在代码中使用两个正方形,它就可以工作了。我在这里俯瞰什么


感谢您的回答:)

如果在调用
paper.text()
时使用默认的文本定位值'middle',则x和y属性将返回文本跨度中心的坐标,而不是其左上角的坐标,就像使用矩形时一样


您不应该使用
x
y
属性,而应该通过获取坐标,然后从结果对象中使用
x
y
。这将使您现有的逻辑能够畅通无阻地工作。

谢谢,我使用了您的建议,并通过bbox对象解决了它!