Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate SVG polygon points?

I have created a line chart with SVG (polygon points) that I would like animated.

I would like the points to all start animating from the X axis, and when finished animating, the result to look like the following image.

It seems there is no simple way to achieve this, with the googling I have done. Any tips would be most appreciated, thanks.

enter image description here

like image 242
Keith Donegan Avatar asked Mar 29 '17 10:03

Keith Donegan


People also ask

How do I make an SVG animation?

How it create SVG Animations: Select the Frame you want to animate and click on Enable SVG Export. Select a node within that Frame to set up animations such as X Position, Y Position, Scale, Rotation and Opacity. Use the built-in live-preview to tweak your animations until you're happy with the result.

Can an SVG file be animated?

SVG supports the ability to change vector graphics over time, to create animated effects. SVG content can be animated in the following ways: Using SVG's animation elements [svg-animation]. SVG document fragments can describe time-based modifications to the document's elements.

Why is my SVG not animating?

One of the most common reasons why the SVG animation doesn't work is the use of <img> tags to add the SVG. The SVG might be visible on the website, but the animation will not start. You can easily fix this by replacing all <img> tags with an <object> tag.


3 Answers

What do you mean by "animating from the X axis" exactly? Do you mean start flat and grow upwards to this shape?

If so, it is actually very easy.

<svg viewBox="0 0 2040 352">
  <defs>
  </defs>
  
  <polygon points="" fill="red">
     <animate attributeName="points" dur="1s" fill="freeze"
              from="0,352, 550,352, 1240,352, 1592,352, 1880,352, 2040,352,
                   2040,352,0,352"
              to="0,292, 550,232, 1240,258, 1592,158, 1880,168, 2040,0,
                   2040,352,0,352"/>
  </polygon>
</svg>

This example uses vanilla SVG SMIL animations. You can also use one of a number of SVG graphing or animation libraries.

like image 142
Paul LeBeau Avatar answered Sep 19 '22 23:09

Paul LeBeau


This effect can be easily achieved with transform:scaley keyframe animation:

(javascript code in followind snippet is only for generating random svg polygon)

let n = 5, w = innerWidth - 20, h = innerHeight - 20;
let pts = Array(n).fill(0).map((_, i) => [(1+i)*(w/(n+1)), -Math.random()* h]);

document.body.innerHTML += `<svg width=${w} height=${h} viewbox=0,${-h},${w},${h} >
  <polygon points=0,0,${pts},${w},0 /></svg>`;
  
@keyframes grow {
  0%   {transform: scaley(0)}
  100% {transform: scaley(1)}
}

polygon {
  fill: #e45;
  animation: grow 0.5s forwards;
}
like image 31
Stranger in the Q Avatar answered Sep 18 '22 23:09

Stranger in the Q


I have done something in the past, this was however with a single control point of a SVG Bezier curve. However I think you can apply the same principle. I think you need to do the following steps

  1. Create an array with y values of the curves
  2. Create a jQuery animate function with a step method for each y value [jquery animate1

animate example

$({ n: 0 }).animate({ n: 40}, {
        duration: 1000,
        step: function(calculatedYValue, fx) {
            //update graphs with calculatedYValue
            console.log(parseInt(calculatedYValue), 10);   
        }
    });    
like image 31
jeroenoliemans Avatar answered Sep 17 '22 23:09

jeroenoliemans