简体   繁体   English

围绕中心旋转线元素

[英]Rotate line element around center

So I want to rotate my svg > line element in javascript but the rotation needs to be around its center + the length of the line has to remain the same 所以我想在javascript中旋转svg> line元素,但旋转必须围绕其中心+线的长度必须保持不变

Remaining the line length is the problem right now 剩下的行长是现在的问题

My element: 我的元素:

<svg xmlns="http://www.w3.org/2000/svg">
    <line id="line" x1="20" y1="100" x2="100" y2="100" stroke-width="2"
          stroke="black">
    </line>
</svg>

And this is my javascript: 这是我的javascript:

var rotateJump = 10;
var line = document.getElementById("line");
function rotate() {
    line.setAttribute("y1", parseInt(line.getAttribute("y1")) - rotateJump);
    line.setAttribute("y2", parseInt(line.getAttribute("y2")) + rotateJump);
}
rotate();

How to properly rotate maintaining line length? 如何正确旋转保持线长? It gets bigger when standing up right The length it has laying horizontal should be the max length it has. 正确站立时会变大。水平放置的长度应为最大长度。

Any tips in to the right direction on how I should approach this? 关于如何正确处理此问题的任何提示?

Setting the transform attribute with rotation() should be the way to go. 设置带有rotation()的transform属性应该是方法。
Rotate takes 3 arguments [degree] [originX] [originY] Rotate需要3个参数[degree] [originX] [originY]
In the example below i put it in a interval so the code repeats and you can see a live rotation. 在下面的示例中,我将其间隔了一段时间,以便代码重复执行,您可以看到实时旋转。

 var rotateJump = 10; var line = document.getElementById("line"); var i = 0; setInterval(function() { i += 2; line.setAttribute("transform", "rotate(" + i + ", 100, 100)"); }, 25); 
 <svg width="300px" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 200 200"> <circle cx="100" cy="100" r="80" fill="firebrick"></circle> <line id="line" x1="20" y1="100" x2="100" y2="100" stroke-width="2" stroke="black"></line> </svg> 

As stated in my comment using transform is the best option if the line doesn't need to change the original coordinates, but it may not always be the case (for example you'll need to calculate collision of another object with the rotated line later). 如我的评论中所述,如果线不需要更改原始坐标,则使用transform是最佳选择,但是并非总是如此(例如,稍后您需要计算另一个对象与旋转线的碰撞) )。

What you're really looking for is how to draw a diameter of a circle with a radius of half you line's length and the center point in the middle of the line, given an angle at which the diameter should be drawn. 您真正要寻找的是如何绘制半径为直线长度一半的圆的直径,并以直线的中心绘制中心点,并给出绘制直径的角度。 Or in other words: how to find a point on a circle's circumference given the radius and angle. 换句话说:在给定半径和角度的情况下,如何在圆的圆周上找到一个点。

So given your line you can get the center ( 60, 100 ) and radius ( 40 ). 因此,根据您的直线,您可以获得中心( 60, 100 )和半径( 40 )。

Then you can calculate x1, y1 and x2, y2 which are points on a circle: 然后,您可以计算x1, y1x2, y2 ,它们是圆上的点:

 var line = document.getElementById("line"); var centerX = 60, centerY = 100, radius = 40, angle = 0; var rotate = function(angle){ var x1, y1, x2, y2; x1 = centerX + radius * Math.cos( angle ); y1 = centerY + radius * Math.sin( angle ); x2 = centerX - radius * Math.cos( angle ); y2 = centerY - radius * Math.sin( angle ); line.setAttribute( 'x1', x1 ); line.setAttribute( 'y1', y1 ); line.setAttribute( 'x2', x2 ); line.setAttribute( 'y2', y2 ); } // animation setInterval( function(){ angle += .1; rotate( angle ); },100); 
 <svg xmlns="http://www.w3.org/2000/svg"> <line id="line" x1="20" y1="100" x2="100" y2="100" stroke-width="2" stroke="black"> </line> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM