简体   繁体   English

动画缩放旋转 svg 元素

[英]Animate scale rotate svg element

Assuming I have an svg made with inkscape.假设我有一个用inkscape制作的svg。 In this SVG set with a viewbox, I want to animate each element inside the SVG.在这个带有视图框的 SVG 集中,我想为 SVG 内的每个元素设置动画。 There is no problem for translate or opacity ... but when I need to rotate or scale a single element, it acting weird.平移或不透明度没有问题……但是当我需要旋转或缩放单个元素时,它表现得很奇怪。

I try to correctly understand the concept of the viewbox but I need some help.我尝试正确理解 viewbox 的概念,但我需要一些帮助。

I understand that I have only one origin point when I have only one viewbox should I set multiple viewbox ?我知道当我只有一个视框时我只有一个原点,我应该设置多个视框吗?

 <?xml version="1.0" encoding="UTF-8"?> <svg id="SVGRoot" version="1.1" viewBox="0 0 700 500" xmlns="http://www.w3.org/2000/svg"> // rotate or scale acting weird <ellipse id="path9238" cx="332.91" cy="143.85" rx="64.941" ry="67.676" style="fill-rule:evenodd;fill:#f00;stroke:#000"/> // rotate or scale acting weird <rect id="rect9240" x="400.59" y="270.31" width="173.63" height="177.73" style="fill-rule:evenodd;fill:#f00;paint-order:normal"/> // rotate or scale acting weird <path id="path9242" d="m233.79 453.52-153.64-138.25 196.55-63.937z" style="fill-rule:evenodd;fill:#f00;paint-order:normal"/> </svg>

I'm using anime.js 3.0 or CSS or I can try anything else我正在使用anime.js 3.0 或CSS 或者我可以尝试其他任何东西

In Svg, the coordinates of any figure always have an absolute value that is calculated from the upper left corner of the SVG canvas.在 Svg 中,任何图形的坐标始终具有从 SVG 画布左上角计算的绝对值。
Therefore, when applying the scale (2) command, the coordinates of the center of the figure will also be doubled and the figure will shift to the right and down.因此,当应用 scale(2) 命令时,图形中心的坐标也会加倍,图形会向右和向下移动。

 <svg id="SVGRoot" version="1.1" width="500" height="500" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg" style="border:1px solid grey;"> <rect id="rect9240" transform="scale(2)" x="100" y="100" width="100" height="100" style="fill-rule:evenodd;fill:#f00;> stroke:#000; paint-order:normal"> <animateTransform xlink:href="#rect9240" attributeName="transform" type="scale" values="1;2;2;1;1" dur="8s" fill="freeze" repeatcount="indefinite" /> </rect> <circle cx="150" cy="150" r="3" fill="black" /> <circle cx="300" cy="300" r="3" fill="dodgerblue" /> <text x="150" y="140" font-size="16px" text-anchor="middle" > Center (150,150) </text> <text x="300" y="290" font-size="16px" text-anchor="middle" > Center (300,300) </text> </svg>

To return the enlarged figure to its original position, you must use the command translate (X, Y)要将放大的图形返回到其原始位置,您必须使用命令translate (X, Y)

There is a great post here by @Paul LeBeau where this is explained in detail. @Paul LeBeau 在这里发表了一篇很棒的文章,详细解释了这一点。

CSS solution CSS 解决方案

In order not to calculate the position of the center of the figure, you can use the CSS rule transform-box: fill-box为了不计算图形中心的位置,可以使用CSS规则transform-b​​ox:fill-box

When the value of the fill-box attribute is selectedfill-box属性的值被选中时

The object bounding box is used as the reference box.对象边界框用作参考框。

Below is an example of increasing and decreasing the size of figures:以下是增加和减少数字大小的示例:

 svg { width:50%; } .ellipse1, .rect1, .path1 { transform-box: fill-box; animation: scale 3s linear infinite alternate; animation-direction: alternate; transform-origin:50% 50%; } @keyframes scale { 0% { transform: scale(0.5); } 100% { transform: scale(1); } }
 <svg id="SVGRoot" version="1.1" viewBox="0 0 700 500" xmlns="http://www.w3.org/2000/svg"> <ellipse class="ellipse1" id="path9238" cx="332.91" cy="143.85" rx="64.941" ry="67.676" style="fill-rule:evenodd;fill:#f00;stroke:#000"/> <rect class="rect1" id="rect9240" x="400.59" y="270.31" width="173.63" height="177.73" style="fill-rule:evenodd;fill:#f00; stroke:#000; paint-order:normal"/> <path class="path1" id="path9242" d="m233.79 453.52-153.64-138.25 196.55-63.937z" style="fill-rule:evenodd;fill:#f00;stroke:#000; paint-order:normal"/> </svg>

  • Rotation example旋转示例

 svg { width:50%; } .ellipse1, .rect1, .path1 { transform-box: fill-box; animation: spin 4s linear infinite alternate; transform-origin:50% 50%; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(359deg); } } <path class="path1" id="path9242" d="m233.79 453.52-153.64-138.25 196.55-63.937z" style="fill-rule:evenodd;fill:#f00;stroke:#000; paint-order:normal"/> </svg>
 <svg id="SVGRoot" version="1.1" viewBox="0 0 700 500" xmlns="http://www.w3.org/2000/svg"> <ellipse class="ellipse1" id="path9238" cx="332.91" cy="143.85" rx="64.941" ry="67.676" style="fill-rule:evenodd;fill:#f00;stroke:#000"/> <rect class="rect1" id="rect9240" x="400.59" y="270.31" width="173.63" height="177.73" style="fill-rule:evenodd;fill:#f00; stroke:#000; paint-order:normal"/> <path class="path1" id="path9242" d="m233.79 453.52-153.64-138.25 196.55-63.937z" style="fill-rule:evenodd;fill:#f00;stroke:#000; paint-order:normal"/> </svg>

  • Increase and rotation增加和旋转

 svg { width:50%; } .ellipse1, .rect1, .path1 { transform-box: fill-box; animation: scale1 4s linear, spin 4s linear 4s infinite alternate; transform-origin:50% 50%; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes scale1 { 0% { transform: scale(0.5);} 100% { transform: scale(1);} }
 <svg id="SVGRoot" version="1.1" viewBox="0 0 700 500" xmlns="http://www.w3.org/2000/svg"> <ellipse class="ellipse1" id="path9238" cx="332.91" cy="143.85" rx="64.941" ry="67.676" style="fill-rule:evenodd;fill:#f00;stroke:#000"/> <rect class="rect1" id="rect9240" x="400.59" y="270.31" width="173.63" height="177.73" style="fill-rule:evenodd;fill:#f00; stroke:#000; paint-order:normal"/> <path class="path1" id="path9242" d="m233.79 453.52-153.64-138.25 196.55-63.937z" style="fill-rule:evenodd;fill:#f00;stroke:#000; paint-order:normal"/> </svg>

I guess you mean the transform-origin CSS propierty.我猜你的意思是转换源CSS 属性。 It is related to the center point of the svg file.它与 svg 文件的中心点有关。 Then, you need to calculate the center point of the element to animate related to document's center point.然后,您需要计算元素的中心点以与文档的中心点相关联。
For the animations you can use CSS animations .对于动画,您可以使用CSS 动画

The basics of viewBox is that the 4 numbers are " xy width height ”. Generally the x/y coordinates are 0 which keeps your origin in the top left corner. Something people often do is place the origin in the middle. viewBox 的基础是这 4 个数字是“ xy width height ”。一般 x/y 坐标是 0,这样你的原点就在左上角。人们经常做的事情是将原点放在中间。

In order to do that you move your viewBox top left by half of the width and half the height.为此,您将 viewBox 左上角移动一半宽度和一半高度。 In your case " -350 -250 700 500 ".在你的情况下“ -350 -250 700 500 ”。

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

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