简体   繁体   English

在SVG中旋转多边形

[英]Rotating a polygon in SVG

In the code below, I tried to move the polygon which is the arrow's head and place it at the position 100,100. 在下面的代码中,我尝试移动多边形,即箭头的头部,并将其放置在位置100,100处。 But polygon is placed at wrong direction. 但多边形放置在错误的方向。 The position of line and will change depending upon the input. 线的位置将根据输入而改变。 I need the output to be something like this: 我需要输出是这样的:

图

 <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve"> <polygon class="st0" points="2,7 0,0 11,7 0,14" transform="translate(100,100) rotate(45 0 0)"/ stroke="red" fill="red"/> <line x1="0" y1="0" x2="100" y2="100" stroke="green"/> </svg> 

The smallest fix is probably to add a translation that lets you treat the concave part of the arrow as the origin for both the rotation and other translation. 最小的修复可能是添加一个平移,使您可以将箭头的凹陷部分视为旋转和其他平移的原点。

<polygon … transform="translate(100 100) rotate(45 0 0) translate(-2 -7)" />

 <svg version="1.1" xmlns="http://www.w3.org/2000/svg"> <polygon points="2,7 0,0 11,7 0,14" transform="translate(100 100) rotate(45 0 0) translate(-2 -7)" stroke="red" fill="red" /> <line x1="0" y1="0" x2="100" y2="100" stroke="green" /> </svg> 

You are using 您正在使用

rotate(deg, cx, cy)

to rotate element. 旋转元素。 With this configuration, it means that rotate the element deg degree with respect to point (cx,cy) (of element) 根据该结构,则意味着相对于点(CX,CY)(元素的)旋转度的元件

So you should set cx, cy value as a center of your element. 因此,您应该将cx,cy值设置为元素的中心。

 <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve"> <polygon class="st0" points="2,7 0,0 11,7 0,14" transform="translate(100,100) rotate(45 10 0)"/ stroke="red" fill="red"/> <line x1="0" y1="0" x2="100" y2="100" stroke="green"/> </svg> 

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

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