오늘은 "Introduction to SMIL animation in SVG"에 나와 있는 원리를 간단히 실험해 보는 내용으로 채웠네요.
위 출처에 가시면 커브를 만드는 것과 커브에 따라 타원이 움직이는 코딩이 담겨져 있습니다. 저도 간단히 살펴보면서 배운 내용을 연습해 보았네요.
먼저 라인 커브를 만들어야 하는데 출처에 나온 소스에서 커브 값을 수정하여 다음과 같은 라인을 우선 만들었네요.
[커브 라인]
<svg xmlns="http://www.w3.org/2000/svg" width="500px" height="300px" style="background: yellow">
<path id="curve" stroke="black" fill="none" stroke-width="5"
d="M 50,50 C 300,130 400,350 450,150 500,-50 -90,200 50,50" />
</svg>
켄버스(svg)의 폭은 500이고 높이는 300으로 잡고 배경색은 노란색으로 세팅 한 후 path 태그에 선색은 black으로 선두깨는 5로 그리고 선 경로는 d로 잡아 놓으면 아래와 같은 결과가 나옵니다.
[결과]
[타원 이동]
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#ff0000" stroke="#00ff00"
stroke-width="2" opacity="0.9">
<animateMotion dur="2s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
타원은 ellipse태그로 기본 속성 세팅을 해놓고 빨강색으로 채우고 테두리는 녹색으로 두깨는 2입니다. 불투명도는 0.9로 세팅이 됩니다.
이 타원을 커브 라인을 따라 움직이게 하기 위해서 animateMotion 태그로 해서 세팅이 이루어 집니다. 출처의 소스를 보닌 2초동안 회전은 자동이고 repeatCount은 무한 반복하도록 세팅을 합니다. 여기서 이동 경로는 mpath 태그로 커브 라인의 path 태그의 id curve을 위와 같이 코딩을 하면 끝납니다.
기본이 되는 커브 라인은 한번 지정해 놓고 이 커브 라인을 따라 타원을 움직이게 하면 됩니다. 그러면 ellipse 태그를 3개 만들고 각각 mpath 경로를 커브라인으로 잡아 놓으면 3개의 타원이 커브라인 위에서 움직이게 됩니다.
3개의 타원을 만들어서 움직이게 하려면 다음과 같이 세팅합니다.
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#ff0000" stroke="#00ff00"
stroke-width="2" opacity="0.9">
<animateMotion dur="2s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#00ff00" stroke="#ff0000"
stroke-width="2" opacity="0.9">
<animateMotion dur="3s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#0000ff" stroke="#ffff00"
stroke-width="2" opacity="0.9">
<animateMotion dur="5s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
종합해 보면,
[전체소스]
<svg xmlns="http://www.w3.org/2000/svg" width="500px" height="300px" style="background: yellow">
<path id="curve" stroke="black" fill="none" stroke-width="5"
d="M 50,50 C 300,130 400,350 450,150 500,-50 -90,200 50,50" />
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#ff0000" stroke="#00ff00"
stroke-width="2" opacity="0.9">
<animateMotion dur="2s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#00ff00" stroke="#ff0000"
stroke-width="2" opacity="0.9">
<animateMotion dur="3s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
<ellipse cx="7" cy="-5" rx="20" ry="14" fill="#0000ff" stroke="#ffff00"
stroke-width="2" opacity="0.9">
<animateMotion dur="5s" rotate="auto" repeatCount="indefinite" >
<mpath xlink:href="#curve"/>
</animateMotion>
</ellipse>
</svg>
[결과]
코딩은 어려운 부분은 없습니다. 좀 불편했던 부분은 라인을 만드는 라인을 계산해내야 한다는 점에서 시간을 좀 많이 잡아 먹었네요. 사실 좀 재밌는 표현을 하려고 했는데 라인을 그리는 것 자체가 쉽지 않는 부분이였네요.
다른 라인 다른 물체를 이용하여 애니메이션 효과를 여러분들은 직접 실험해 보세요.