【问题标题】:SVG synchronization of two animations involving scale and translate simultaneously两个涉及缩放和平移的动画的 SVG 同步
【发布时间】:2018-05-13 10:12:27
【问题描述】:

我无法同步涉及缩放和翻译的 SVG 转换。三角形的顶点从点 (967, 491) 平移到 (900, 100),同时 png 图像也应随顶点平移,但也应在移动时按比例缩小或放大。

为了得到 png 图像的原始位置,我正确使用了公式 -centerX*(scale factor - 1), -centerY*(scaleFactor - 1)。这是动画之前的原始位置。但是随着运动的开始,我无法计算它的最终位置。

谁能帮我解决这个问题。

<svg viewBox="0 0 1200 800">
	<defs>
		<g id="triangle">
			<desc>Triangle</desc>
			<polygon id="triangle_" points="967,491 738,449 724,603">
				<animate attributeName="points" attributeType="XML"
				begin="0s" dur="5s" from="967,491 738,449 724,603" to="900,100 738,449 724,603" fill="freeze" />
			</polygon>
			<g transform="translate(483,245)">
				<image xlink:href="http://www.alegralabs.com/human.png" x="940" y="442" width="53" height="98" transform="scale(0.5)" style="fill:#FFF">
					<animateTransform attributeName="transform" type="scale" attributeType="XML"
					begin="0s" dur="5s" from="0.5" to="0.2" fill="freeze" />
				</image>
				<animateTransform attributeName="transform" type="translate" attributeType="XML"
					begin="0s" dur="5s" to="773,80" fill="freeze" />
					<!--<animateMotion from="0,0" to="241,122" dur="5s" fill="freeze" /> -->
			</g>
			<g>
				<text x="940" y="442">1234</text>
				<animateMotion from="0,0" to="-67,-391" dur="5s" fill="freeze" />
			<g>
		</g>
		
	</defs>
	<use xlink:href="#triangle" />
</svg>

【问题讨论】:

    标签: animation svg synchronization scale translate


    【解决方案1】:

    首先,让您自己的生活更轻松,并给图像一个 x/y 位置,使其中心位于 0,即。 e. x="-26.5" y="-48"。那么scale 不会导致图像定位发生任何变化。将&lt;text&gt; 也移动到&lt;g&gt; 元素内,使图像和文本的相对位置保持不变。

    将该组的坐标原点视为移动的单个点,它可以与三角形的移动顶点相同:transform="translate(967,491)"

    之后,一个动画可以缩放图像,另一个将&lt;g&gt; 元素移动到动画位置。您可以使用与移动顶点相同的坐标。

    <svg viewBox="0 0 1200 800">
    	<defs>
    		<g id="triangle">
    			<desc>Triangle</desc>
    			<polygon id="triangle_" points="967,491 738,449 724,603">
    				<animate attributeName="points" attributeType="XML"
    				begin="0s" dur="5s" from="967,491 738,449 724,603" to="900,100 738,449 724,603" fill="freeze" />
    			</polygon>
    			<g transform="translate(967,491)">
    				<animateTransform attributeName="transform" type="translate" attributeType="XML"
    					begin="0s" dur="5s" from="967,491" to="900,100" fill="freeze" />
    				<image xlink:href="http://www.alegralabs.com/human.png" x="-26.5" y="-48" width="53" height="98" transform="scale(0.5)" style="fill:#FFF">
    					<animateTransform attributeName="transform" type="scale" attributeType="XML"
    					begin="0s" dur="5s" from="0.5" to="0.2" fill="freeze" />
    				</image>
    				<text x="-27" y="-49">1234</text>
    			<g>
    		</g>
    		
    	</defs>
    	<use xlink:href="#triangle" />
    </svg>

    【讨论】:

    • 太棒了。只是想知道你为什么在图片上方写
    • 因为我在 之后编写了 并且它没有按预期工作。但无论如何,非常感谢。
    • 位置没有区别。动画总是应用于它们的直接父元素。这只是一个可读性的问题——这样动画就在它所应用的属性的下方。
    • 谢谢您,先生,您很棒。扩展它,我正在创建一个小动画。您看到的数字是递增的,即它们将是一个递增的计数器。所以,我使用了setInterval,实际上我尝试了多种方法,但是SVG屏幕变得不稳定。您能否在这方面指导我解决问题所在。我在 SO 中为此创建了一个新帖子,如果您可以查看它 - 非常感谢 stackoverflow.com/questions/50333150/…
    • 我觉得这里也有类似的问题,但是没有解决办法stackoverflow.com/questions/39765481/…
    猜你喜欢
    • 2011-07-04
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2015-12-30
    • 2013-12-17
    • 2019-08-07
    • 2017-08-30
    相关资源
    最近更新 更多