【问题标题】:Rotate and move line in svg在 svg 中旋转和移动线条
【发布时间】:2015-09-14 13:07:11
【问题描述】:

我想在围绕自身中心旋转的同时左右移动一条线。

使用矩形非常容易,因为您有 x、y 和高度和宽度。 虽然有一条线我似乎无法弄清楚。

<line id='line' x1='20' y1='100' x2='180' y2='100' stroke-width='2' stroke='black' transform='rotate(410, 100, 100)'></line>

我可以通过编辑旋转的第一个值来轻松地旋转我的线

现在,只要我移动我的线,旋转就不再正常工作并且不会围绕其中心旋转。

同时移动我的对象有一种向下的冲动,虽然它应该是直线的。

this.lineObject.setAttribute("x2", parseInt(this.lineObject.getAttribute("x2")) + step);
this.lineObject.setAttribute("x1", (parseInt(this.lineObject.getAttribute("x2")) - 160)); //160 is the length of the line.
this.lineObject.setAttribute("y2", (parseInt(this.lineObject.getAttribute("y2")) - step));
this.lineObject.setAttribute("y1", (parseInt(this.lineObject.getAttribute("y1")) - step));

我怎样才能使我的线笔直移动并保持围绕其中心旋转的能力。

【问题讨论】:

    标签: css html svg rotation


    【解决方案1】:

    注意旋转变换的第二个参数恰好是 (x2 + x1) /2,第三个参数是 (y1 + y2) / 2,即直线的中心点。

    你所要做的就是在更新行位置时更新 transform 属性。

    【讨论】:

    • 你是说410后面的2? 100, 100 我试过了,但它飞出屏幕,我无法好好看看发生了什么 编辑:实际上,我认为我第一次尝试时可能犯了一个错误。我一会儿试试
    • 我的移动功能不准确,你的解决方案是正确的
    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    相关资源
    最近更新 更多