I've got a very annoying Problem using rotations in my SVG Animation. The problem is: I have an object, which I want to move along a specific path. So far it works well. But if I try to rotate this object at the end of the path it doesn't work. Sometimes my Object disappears because it's outside the viewbox and I have to try many values for the x and y values of the rotate statement until it works. And then it only works if I set the duration to 0.001s so it rotates instant, because actually my object switches to a much higher point in the viewbox and begins to rotate and only the final position is right but you see it rotating down or up around any point so it doesn't really work. I have no idea how to get the x and y values which I need to rotate the object around its own axis. Here is the code example, I hope you know how to solve this problem:
<defs>
<g id="Karte" visibility="hidden">
<!--Karte-->
<rect x="0" y="0" width="980" height="550" style="stroke: none; fill: rgb(189,248,137)" />
<!--Straßen-->
<path id="Straße1" d="M 510 0 L 510 120 0 120 0 150 980 150 980 120 540 120 540 0" style="stroke: none; fill: lightgrey" />
<rect id="Straße2" x="0" y ="320" width="980" height="20" style="stroke:none; fill: lightgrey" />
<rect id="Straße3" x="0" y="500" width="980" height="30" style="stroke:none; fill: lightgrey" />
<rect id="Straße4" x="25" y="340" width="30" height="160" style="stroke:none; fill: lightgrey" />
<rect id="Straße5" x="930" y="340" width="30" height="160" style="stroke:none; fill: lightgrey" />
<path id="campus" d="M 515 150 L 515 170 510 170 510 300 525 300 525 320 650 320 650 300 665 300 665 170 545 170 545 150" style="stroke:none; fill: lightgrey" />
<polyline id="rampe1" points="570,300 570,307 635,317 635,320" style="stroke-width: 1; fill:none" />
<polyline id="rampe2" points="580,300 580,303 645,313 645,320" style="stroke-width: 1; fill:none" />
<g id="Treppe1">
<line x1="515" y1="153" x2="545" y2="153" stroke-width="0.5" />
<line x1="515" y1="157" x2="545" y2="157" stroke-width="0.5" />
<line x1="515" y1="161" x2="545" y2="161" stroke-width="0.5" />
<line x1="515" y1="165" x2="545" y2="165" stroke-width="0.5" />
</g>
<g id="Treppe2">
<line x1="525" y1="303" x2="565" y2="303" stroke-width="0.5" />
<line x1="525" y1="307" x2="565" y2="307" stroke-width="0.5" />
<line x1="525" y1="311" x2="565" y2="311" stroke-width="0.5" />
<line x1="525" y1="315" x2="565" y2="315" stroke-width="0.5" />
</g>
<set attributeName="visibility" to="visible" begin="0s" />
</g>
</defs>
<!--Figuren-->
<defs>
<g id="John" visibility="hidden">
<circle cx="520" cy="100" r="5" />
<line x1="520" y1="104" x2="520" y2="115" stroke-width="2" />
<line x1="520" y1="104" x2="520" y2="117" stroke-width="1" />
<polyline points="520,100 523.5,108 520,112" style="fill:none" />
<polyline points="520,100 514,104 520,108" style="fill:none" />
<set attributeName="visibility" to="visible" begin="0s" />
</g>
</defs>
<!--Animation-->
<use xlink:href="#Karte" />
<use x="420" y="360" transform="rotate(90 910 490)" xlink:href="#John" >
<animateMotion path="M 0 0 L -900 -10" begin="0s" dur="6s" fill="freeze" />
<animateTransform attributeName="transform" type="rotate" from="90 940 490" to="180 940 490" begin="6s" dur="0.001" fill="freeze"/>
<animateMotion path="M-900 -10 L -900 -190" begin="6.01s" dur="3.98s" fill="freeze" />
<animateTransform attributeName="transform" type="rotate" from="180 970 490" to="270 970 490" begin="10s" dur="0.001" fill="freeze"/>
<animateMotion path="M -900 -190 L -450 -190" begin="10.01" dur="4.98" fill="freeze" />
<animateTransform attributeName="transform" type="rotate" from="270 940 490" to="180 940 490" begin="15s" dur="0.001" fill="freeze"/>
<animateMotion path="M -450 -190 L -390 -190" begin="15" dur="1" fill="freeze" />
</use>
</svg>
Related
<g id = "doc">
/*Doc*/
<path d = "M 100 100 L 150 100 L 170 120 L 170 190 L 100 190 L 100 100 Z M 150 100 L 150 120
L 170 120 M 120 140 L 150 140 M 120 160 L 150 160" stroke = "black" stroke-width = "8px" fill = "white" transform = "translate(350, 110)">
</path>
</g>
<animate xlink:href = "#doc" attributeName = "x" from = "450" to = "1000" dur = "4" repeatCount = "indefinite" />
If I try to animate the with the id = "doc", which is located inside a tag, the animation is not working. Thanks for help!
Your SVG was a bloated file icon:
The only animation I can think of is:
<svg viewBox="0 0 90 110" style="background:pink;height:180px">
<path d="m10 10 50 0 20 20 0 70-70 0 0-90zm50 0 0 20 20 0m-50 20 30 0m-30 20 30 0"
stroke="black" stroke-width="8px" fill="white">
<animatemotion path="m0 0h100" dur="4s" repeatCount="indefinite"/>
</path>
</svg>
Update - animationMotion doesn't work in Chrome IMG and OBJECT tags
animationMotion does work inside an IMG tag in FireFox
To make it work in Chrome, change it to an animateTransform animation
Since my favorite pastime on a lazy Sunday is to clean SVGs, here is your code:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1320 520" style="background:pink" fill="black" stroke="black">
<g id="deviceleft">
<rect x="355" y="80" width="12" height="85" rx="5" />
<rect x="355" y="185" width="12" height="40" rx="5" />
<rect x="10" y="10" width="350" height="500" stroke-width="5" rx="10" fill="lightblue" />
<circle cx="335" cy="35" r="8" fill="grey" stroke-width="7" />
</g>
<g id="deviceright">
<rect x="1305" y="110" width="12" height="85" rx="5" />
<rect x="1305" y="210" width="12" height="40" rx="5" />
<rect x="1010" y="60" width="300" height="400" fill="grey" stroke-width="5" rx="10" />
<rect x="1105" y="60" width="110" height="25" rx="10" />
<circle cx="1185" cy="72" r="6" stroke="grey" stroke-width="4" />
<rect x="1135" y="68" width="35" height="8" fill="darkgrey" rx="5" />
</g>
<g transform="translate(0 175)">
<path d="m435 60 500 0m-1 0-19-10m19 10-19 10" stroke-width="6" />
<path d="m385 20 50 0 20 20 0 70-70 0 0-90zm50 0 0 20 20 0m-50 20 30 0m-30 20 30 0" stroke-width="8"
fill="white">
<animateTransform attributeName="transform" type="translate" dur="4s" values="50 0;450 0"
repeatCount="indefinite" />
</path>
</g>
</svg>
I want to have repeating alternate direction for the animation
<animateMotion keyPoints="0;1;0" keyTimes="0;0.5;1" dur="6s" repeatCount="indefinite" rotate="auto" >
<mpath href="#path1"/>
</animateMotion>
Keypoints and KeyTimes are not working
Full code:
<svg width="200" height="200" viewBox="0 0 700 700">
<g>
<path id="path1" d="M200,350 C 200,440 400,150 400,250 M200,350"
fill="none" stroke="blue" stroke-width="7.06" />
<path d="M-25,-12.5 L25,-12.5 L 0,-87.5 z"
fill="yellow" stroke="red" stroke-width="7.06" />
<animateMotion keyPoints="0;1;0" keyTimes="0;0.5;1" dur="6s" repeatCount="indefinite" rotate="auto" >
<mpath href="#path1"/>
</animateMotion>
</g>
</svg>
First there is an issue with your code. You are animating a group of shapes and you are using one of those shapes as mpath.
In the next examples I'm animating the triangle over the integral like shape. In order to animate the triangle in both ways I'm rewriting the mpath so that the path will reverse to the starting point.
svg{width:90vh}
<svg viewBox="0 0 700 700">
<path id="path1" d="M200,350 C 200,440 400,150 400,250 C400,150 200,440 200,350" fill="none" stroke="blue" stroke-width="7.06" />
<path d="M-25,-12.5 L25,-12.5 L 0,-87.5 z" fill="yellow" stroke="red" stroke-width="7.06" >
<animateMotion keyPoints="0;1;0" keyTimes="0;0.5;1" dur="6s" repeatCount="indefinite" rotate="auto" >
<mpath href="#path1"/>
</animateMotion>
</path>
</svg>
However if you want the triangle to stay the same side of the curve when reversing, in this case I'm using 2 animations, each one begining when the previous one ends. For the second animation I'm using rotate="auto-reverse"
svg{width:90vh}
<svg viewBox="0 0 700 700">
<path id="path1" d="M200,350 C 200,440 400,150 400,250" fill="none" stroke="blue" stroke-width="7.06" />
<path id="path2" d="M400,250 C400,150 200,440 200,350" fill="none" stroke="blue" stroke-width="7.06" />
<path d="M-25,-12.5 L25,-12.5 L 0,-87.5 z" fill="yellow" stroke="red" stroke-width="7.06">
<animateMotion id="a1" dur="3s" rotate="auto" begin="0;a2.end">
<mpath href="#path1" />
</animateMotion>
<animateMotion id="a2" dur="3s" rotate="auto-reverse" begin="a1.end">
<mpath href="#path2" />
</animateMotion>
</path>
</svg>
Please observe that the path for the second animation is the path for the first animation reversed.
is it possible to animate a path with marker elements? the path id in question is #orbit1. the reason i created the markers is that they will be used multiple times.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1" viewBox="0 0 1500 1000">
<defs>
<marker id="arrowRight" viewBox="0 0 7.1 11.5" refX="5" refY="5.75"
markerUnits="userSpaceOnUse" orient="auto-start-reverse"
markerWidth="7.1" markerHeight="11.5">
<polygon points="1,11.5 0,10.4 5.1,5.7 0,1 1,0 7.1,5.7" fill="#00897b"/>
</marker>
<marker id="circle" viewBox="0 0 6 6" refX="1" refY="3"
markerUnits="userSpaceOnUse" orient="auto"
markerWidth="6" markerHeight="6">
<circle cx="3" cy="3" r="3" fill="#4caf50"/>
</marker>
</defs>
<!-- arrowhead symbol -->
<symbol>
<path id="d_arrow1" fill="red" d="M-10-10L10 0l-20 10 2-10-2-10z" />
</symbol>
<!-- animateMotion defines the motion path animation -->
<animateMotion xlink:href="#a1" begin="0s" dur="3s" rotate="auto" repeatCount="indefinite">
<mpath xlink:href="#orbit1" />
<!-- mpath = sub-element for the <animateMotion> element provides the ability to reference an external <path> element as the definition of a motion path -->
</animateMotion>
<animateMotion xlink:href="#a2" begin="0s" dur="3s" rotate="auto-reverse" keyPoints="1;0" keyTimes="0;1" calcMode="linear" repeatCount="indefinite">
<mpath xlink:href="#orbit2" />
</animateMotion>
<!-- arrow paths -->
<path id="orbit2" d='M 365 400 A 370 200 0 0 1 1100 400' fill="none" stroke-width="3" style="stroke: green; stroke-dasharray: 50 818; stroke-dashoffset: 50"/>
<!-- marker arrows -->
<!-- orbit1 to be animated using mpath -->
<path id="orbit1" d="M308.7 34.9C381.3 37.4 444.3 78 478.7 137.5" stroke="#4caf50" fill="none" stroke-width="2" stroke-miterlimit="10" stroke-dasharray="50 1000" stroke-dashoffset="50"
marker-start="url(#circle)"
marker-end="url(#arrowRight)"/>
<!-- <animate> svg element used to animate an attribute or property of an element over time. it's normally inserted inside the element or referenced by the href attribute of the target element -->
<animate id="anim1" xlink:href="#orbit1" attributeName="stroke-dashoffset" from="50" to="-960" dur="3s" begin="0s" fill="freeze" repeatCount="indefinite" />
<!-- animate using the animateMotion definition above -->
<animate id="anim2" xlink:href="#orbit2" attributeName="stroke-dashoffset" from="-810" to="50" dur="3s" begin="0s" fill="freeze" repeatCount="indefinite" />
<!-- <use id="a1" xlink:href="#d_arrow1" x="0" y="0" width="100" height="50" />-->
<use id="a2" xlink:href="#d_arrow1" x="0" y="0" width="100" height="50" />
</svg>
Im new at coding and am trying to make a simple line rotate around its center using svg.
I have got the line rotating but everytime I add begin="mouseover" the animation no longer starts.
If anyone could help me get this working with a hover event I will be greatly appreciated.
<svg>
<line id="line_01" x1="20" y1="100"
x2="100" y2="20"
stroke="black"
stroke-width="2" stroke-linecap="round" />
<animateTransform
xlink:href="#line_01"
attributeName="transform"
pointer-events="all"
attributeType="XML"
type="rotate"
from="0 60 60"
to="360 60 60"
dur="3s"
/>
</svg>
You need to precede mouseover with the id of an element e.g.
<svg width="100%" height="100%">
<line id="line_01" x1="20" y1="100"
x2="100" y2="20"
stroke="black"
stroke-width="2" stroke-linecap="round" />
<animateTransform
xlink:href="#line_01"
begin="line_01.mouseover"
attributeName="transform"
pointer-events="all"
attributeType="XML"
type="rotate"
from="0 60 60"
to="360 60 60"
dur="3s"
/>
</svg>
Greeting to all,
I recently found myself reading the following article (http://apike.ca/prog_svg_smil.html) and more specifically the "Animation Element - animateMotion".
Is there a way of "telling" the moving "marker" to accelerate or decelerate on specific portions (segments) of the path, or is its speed always defined by the "dur" attribute (in seconds)?
Thanks in advance.
Controlling the animation timing is provided by the calcMode, keyTimes, keySplines and keyPoints attributes of svg animation elements such as animateMotion. Basically you specify points on a normalized timeline and tell svg how to map them onto the progress measured on a normalized timeline as well. you also specify how to interpolate between the support points given. for smooth animations you would chose calcMode="spline".
the relevant references are:
svg(animateMotion)
svg(keyPoints)
smil(CalcModes)
smil(keyTimes)
for demo purposes, have a look at an animated line tracking demo (online here, click on the dark bar; example taken from here, timing control added):
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"
width="200" height="200"
viewBox="0 0 200 200" >
<!-- Matthew Bystedt http://apike.ca 2012 -->
<!-- Pattern Definition -->
<defs>
<pattern id="rulerPattern" patternUnits="userSpaceOnUse"
x="0" y="0" width="10" height="10"
viewBox="0 0 10 10" >
<line x1="0" y1="0" x2="10" y2="0" stroke="lightblue" fill="none" stroke-dasharray="2,2" />
<line x1="0" y1="0" x2="0" y2="10" stroke="lightblue" fill="none" stroke-dasharray="2,2" />
</pattern>
<marker id="marker2" viewBox="0 0 10 10" refX="1" refY="5"
markerUnits="strokeWidth" orient="auto"
markerWidth="4" markerHeight="3">
<polyline points="0,0 10,5 0,10 1,5" fill="darkgreen" />
</marker>
</defs>
<!-- Background -->
<rect x="0" y="0" width="100%" height="100%" fill="url(#rulerPattern)" stroke="black" />
<!-- Path Line Example -->
<path id="myAniPath" d="M10,150 A15 15 180 0 1 70 140 A15 25 180 0 0 130 130 A15 55 180 0 1 190 120 A15 10 170 0 1 10 150" stroke="lightgreen" stroke-width="2" fill="none" marker-mid="url(#marker2)" />
<rect x="-10" y="-5" width="20" height="10" fill="none" stroke="black" >
<animateMotion begin="startButton.click" dur="10s" calcMode="spline" keyTimes="0; 1" keySplines=".75 0 .25 1" repeatCount="1" rotate="auto" fill="freeze">
<mpath xlink:href="#myAniPath" />
</animateMotion>
</rect>
<rect id="startButton" x="20" y="20" width="60" height="20" fill="green" />
<line stroke="black" stroke-width="2" x1="20" y1="20" x2="20" y2="40" >
<animate begin="startButton.click" attributeName="x1" from="20" to="80" dur="10s" repeatCount="1" fill="freeze" />
<animate begin="startButton.click" attributeName="x2" from="20" to="80" dur="10s" repeatCount="1" fill="freeze" />
</line>
</svg>