在不放错图像的情况下旋转SVG

时间:2017-09-25 14:19:44

标签: svg

我有这个svg“代码”

<svg xmlns="http://www.w3.org/2000/svg" width="20" height="34">

<path transform="rotate(0 10 17)" fill="#6699FF" d="M 0.06,25.03 C 0.06,25.03 4.06,20.97 4.06,20.97 4.06,20.97 3.97,5.06 3.97,5.06 3.97,5.06 9.94,0.03 9.94,0.03 9.94,0.03 15.78,5.09 15.78,5.09 15.78,5.09 16.03,21.06 16.03,21.06 16.03,21.06 19.94,25.00 19.94,25.00 19.94,25.00 19.97,30.09 19.97,30.09 19.97,30.09 10.06,20.97 10.06,20.97 10.06,20.97 10.05,23.00 10.05,23.00 10.05,23.00 19.91,32.03 19.91,32.03 19.91,32.03 19.94,33.97 19.94,33.97 19.94,33.97 17.97,33.97 17.97,33.97 17.97,33.97 10.03,26.94 10.03,26.94 10.03,26.94 10.05,29.01 10.05,29.01 10.05,29.01 15.94,34.00 15.94,34.00 15.94,34.00 12.00,33.94 12.00,33.94 12.00,33.94 10.00,32.00 10.00,32.00 10.00,32.00 8.00,34.03 8.00,34.03 8.00,34.03 4.00,34.00 4.00,34.00 4.00,34.00 9.97,29.01 9.97,29.01 9.97,29.01 9.97,26.94 9.97,26.94 9.97,26.94 2.00,33.96 2.00,33.96 2.00,33.96 0.00,33.98 0.00,33.98 0.00,33.98 0.02,32.00 0.02,32.00 0.02,32.00 9.96,23.00 9.96,23.00 9.96,23.00 9.95,20.98 9.95,20.98 9.95,20.98 0.00,30.00 0.00,30.00 0.00,30.00 0.06,25.03 0.06,25.03 Z" />

<path transform="rotate(0 10 17) translate(0 0)" fill="white" d="M 8.04,17.04 C 8.04,17.04 7.98,11.04 7.98,11.04 7.98,11.04 5.00,11.00 5.00,11.00 5.00,11.00 10.00,6.04 10.00,6.04 10.00,6.04 14.96,11.02 14.96,11.02 14.96,11.02 12.02,11.04 12.02,11.04 12.02,11.04 12.02,17.04 12.02,17.04 12.02,17.04 8.04,17.04 8.04,17.04 Z" />

</svg>
它是一个里面有箭头的船。

我正在尝试旋转这张“图片”,(我需要将其旋转360度)。

当我将rotate(0 10 17)更改为rotate(90 10 17)时,它会被删除。那是因为我没有从图像中心旋转它。

我尝试使用这个公式计算中心,但我无法自己设法

x = 34; y = 20; o = 4.71238898 //(degrees to radiants) ;
a = Math.abs(x * Math.sin(o)) + Math.abs(y * Math.cos(o));
b = Math.abs(x * Math.cos(o)) + Math.abs(y * Math.sin(o)); 

我对那些数学/ svg问题非常不满意,我希望有人可以帮助我。

由于

1 个答案:

答案 0 :(得分:2)

旋转中心是正确的。但麻烦的是现在轮换了,你的图形不再是20x34,它是34x20。

所以你要做的第一件事就是更新宽度和高度。

<svg xmlns="http://www.w3.org/2000/svg" width="34" height="20">

当然,这不是最终的解决方案,因为这个新的34x20 SVG的中心位于旧的20x34中心的不同位置。一种解决方案是设计出一个可以工作的不同旋转中心,使图形旋转到新矩形的正确位置。

这有点棘手。幸运的是,有一种更简单的方法。我们可以在SVG中添加一个viewBox来告诉浏览器旋转符号所占区域的大小。浏览器会为我们重新定位。

viewBox属性中的四个值为:

<leftX> <topY> <width> <height>

我们已经知道了宽度和高度(34和20),所以我们只需要计算左和顶部的坐标。这些显然只是旋转中心的宽度和高度的一半。

leftX = 10 - (newWidth / 2)
      = 10 - 17
      = -7
 topY = 17 - (newHeight / 2)
      = 17 - 10
      = 7

因此viewBox属性必须为"-7 7 34 20"

&#13;
&#13;
<svg xmlns="http://www.w3.org/2000/svg" width="34" height="20" viewBox="-7 7 34 20">

<path transform="rotate(90 10 17)" fill="#6699FF" d="M 0.06,25.03 C 0.06,25.03 4.06,20.97 4.06,20.97 4.06,20.97 3.97,5.06 3.97,5.06 3.97,5.06 9.94,0.03 9.94,0.03 9.94,0.03 15.78,5.09 15.78,5.09 15.78,5.09 16.03,21.06 16.03,21.06 16.03,21.06 19.94,25.00 19.94,25.00 19.94,25.00 19.97,30.09 19.97,30.09 19.97,30.09 10.06,20.97 10.06,20.97 10.06,20.97 10.05,23.00 10.05,23.00 10.05,23.00 19.91,32.03 19.91,32.03 19.91,32.03 19.94,33.97 19.94,33.97 19.94,33.97 17.97,33.97 17.97,33.97 17.97,33.97 10.03,26.94 10.03,26.94 10.03,26.94 10.05,29.01 10.05,29.01 10.05,29.01 15.94,34.00 15.94,34.00 15.94,34.00 12.00,33.94 12.00,33.94 12.00,33.94 10.00,32.00 10.00,32.00 10.00,32.00 8.00,34.03 8.00,34.03 8.00,34.03 4.00,34.00 4.00,34.00 4.00,34.00 9.97,29.01 9.97,29.01 9.97,29.01 9.97,26.94 9.97,26.94 9.97,26.94 2.00,33.96 2.00,33.96 2.00,33.96 0.00,33.98 0.00,33.98 0.00,33.98 0.02,32.00 0.02,32.00 0.02,32.00 9.96,23.00 9.96,23.00 9.96,23.00 9.95,20.98 9.95,20.98 9.95,20.98 0.00,30.00 0.00,30.00 0.00,30.00 0.06,25.03 0.06,25.03 Z" />

<path transform="rotate(90 10 17) translate(0 0)" fill="white" d="M 8.04,17.04 C 8.04,17.04 7.98,11.04 7.98,11.04 7.98,11.04 5.00,11.00 5.00,11.00 5.00,11.00 10.00,6.04 10.00,6.04 10.00,6.04 14.96,11.02 14.96,11.02 14.96,11.02 12.02,11.04 12.02,11.04 12.02,11.04 12.02,17.04 12.02,17.04 12.02,17.04 8.04,17.04 8.04,17.04 Z" />

</svg>
&#13;
&#13;
&#13;

<强>更新

如果你需要做任意角度,那么有一个更好的方法,使用Javascript。

  1. 将转换应用于路径
  2. 在SVG上调用getBBox()以获取其内容的尺寸。
  3. 使用这些值更新viewBoxwidth以及&#39; height`
  4. &#13;
    &#13;
    var angle = 145;  // degrees
    
    var mysvg = document.getElementById("mysvg");
    var paths = mysvg.getElementsByTagName("path");
    
    // Apply a transform attribute to each path
    for (var i=0; i<paths.length; i++) {
      paths[i].setAttribute("transform", "rotate("+angle+",10,17)");
    }
    
    // Now that the paths have been rotated, get the bounding box
    // of the SVG contents
    var bbox = mysvg.getBBox();
    
    // Update the viewBox from the bounds
    mysvg.setAttribute("viewBox", bbox.x + " " + bbox.y + " " +
                                  bbox.width +" "+ bbox.height);
    // Update the width and height
    mysvg.setAttribute("width", bbox.width + "px");
    mysvg.setAttribute("height", bbox.height + "px");
    &#13;
    <svg id="mysvg" xmlns="http://www.w3.org/2000/svg" width="20" height="34">
    
    <path fill="#6699FF" d="M 0.06,25.03 C 0.06,25.03 4.06,20.97 4.06,20.97 4.06,20.97 3.97,5.06 3.97,5.06 3.97,5.06 9.94,0.03 9.94,0.03 9.94,0.03 15.78,5.09 15.78,5.09 15.78,5.09 16.03,21.06 16.03,21.06 16.03,21.06 19.94,25.00 19.94,25.00 19.94,25.00 19.97,30.09 19.97,30.09 19.97,30.09 10.06,20.97 10.06,20.97 10.06,20.97 10.05,23.00 10.05,23.00 10.05,23.00 19.91,32.03 19.91,32.03 19.91,32.03 19.94,33.97 19.94,33.97 19.94,33.97 17.97,33.97 17.97,33.97 17.97,33.97 10.03,26.94 10.03,26.94 10.03,26.94 10.05,29.01 10.05,29.01 10.05,29.01 15.94,34.00 15.94,34.00 15.94,34.00 12.00,33.94 12.00,33.94 12.00,33.94 10.00,32.00 10.00,32.00 10.00,32.00 8.00,34.03 8.00,34.03 8.00,34.03 4.00,34.00 4.00,34.00 4.00,34.00 9.97,29.01 9.97,29.01 9.97,29.01 9.97,26.94 9.97,26.94 9.97,26.94 2.00,33.96 2.00,33.96 2.00,33.96 0.00,33.98 0.00,33.98 0.00,33.98 0.02,32.00 0.02,32.00 0.02,32.00 9.96,23.00 9.96,23.00 9.96,23.00 9.95,20.98 9.95,20.98 9.95,20.98 0.00,30.00 0.00,30.00 0.00,30.00 0.06,25.03 0.06,25.03 Z" />
    
    <path fill="white" d="M 8.04,17.04 C 8.04,17.04 7.98,11.04 7.98,11.04 7.98,11.04 5.00,11.00 5.00,11.00 5.00,11.00 10.00,6.04 10.00,6.04 10.00,6.04 14.96,11.02 14.96,11.02 14.96,11.02 12.02,11.04 12.02,11.04 12.02,11.04 12.02,17.04 12.02,17.04 12.02,17.04 8.04,17.04 8.04,17.04 Z" />
    
    </svg>
    &#13;
    &#13;
    &#13;