Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wave animation for a character converted to svg

I have problems animating part of the character 'W' that is converted to svg. This character is styled out a bit, it has like small flag at the left side (the part that I want to animate).
Right now when the animation is going, that flag is stretched vertically at the top of page. It should stay at the same position where it was, also the top and bottom line of the flag should be in parallel( like in image sample below).


enter image description here
Code sample:

var pathData = "M253.477,175...";
var path = new paper.Path(pathData);
var flags = {
    collection:[]
}
var Flag = function(){
  var model = {
      startIndex:0, // start point in path.segments array
      middleIndex:0,// middle point in path.segments array
      endIndex:0, // end point in path.segments array
      height:20, // the wave animation height
      segments:[] // only flag segments
  }  
  return model;
};

var initializeFlag = function(){
    var segments = path.segments;
    //...   
    for(var i = flag.startIndex; i <= flag.endIndex; i++ ){
        flag.segments.push(segments[i]);
    }
    flags.collection.push(flag); //adds to flags collection
};

var doWaveAnimation = function(segment, counter, height, top, e){
    var sinus = Math.sin(e.time * 3 + counter);
    segment.point.y = sinus * height + top;  
};

var animateFlags = function(e){
   var collection = flags.collection;
   for(var i = 0; i < collection.length; i++){
      var flag = collection[i];

      for(var s = flag.startIndex, n = flag.endIndex -1;
         s < flag.middleIndex && n > flag.middleIndex -2;
         s++, n--){

          //top line
          doWaveAnimation(flag.segments[n], n, flag.height, 180, e);
          //bottom line
          doWaveAnimation(flag.segments[s], s, flag.height, 200, e);
       }
    }
};
//...

Full code sample -> flag animation

To get greater understanding what kind of "wave" animation I want, here is also one example(at the bottom of page) -> http://paperjs.org/

EDIT
Looks like the main reason why this animation is not working properly is that both lines are not positioned horizontally but diagonally..

like image 580
Mr. Blond Avatar asked Oct 20 '25 04:10

Mr. Blond


1 Answers

There's a few things you can do to make this easier:

  1. Make the 'flag' segments linear instead curved
  2. Create your flags so that they are N segments long and are at the end of a path. Then you can refer to them by segment index instead of matching coordinates
  3. Store each moving segment's initial coordinates in a property
  4. Move each segment by a ratio of it's distance from the letter form over the entire length of the flag.

Here's an example sketch

Try moving the X-coordinate of each segment with a different phase to create a more complex motion.

like image 144
Alex Blackwood Avatar answered Oct 21 '25 21:10

Alex Blackwood