Jump to content
Search Community

Animate SVG ViewBox along path?

dfreerider test
Moderator Tag

Recommended Posts

Hi,

 

I'm quite new to Gsap and for my first project I am creating an animated map (think Indiana Jones's plane flying across the continents ). I have the map and the flight path all working, but I would like to be able to draw a second path and have that control the "camera".

 

I have been experimenting with the Svg ViewBox as a way to pan and zoom on the svg map and it works quite well. I can create a tween to set the viewBox attribute and the camera animates.

 

But I would like update the viewBox attribute such that it follows a path. 

 

I don't have a codepen since this is such a generalised question. But I was hoping for some ideas on how to best achieve having a svg viewBox animate along a path.

 

Best Regards

 

Espen 

Link to comment
Share on other sites

Now try it with a motion path.

 

const viewBox = document.querySelector("#svg").viewBox.baseVal;

gsap.to(viewBox, {
  motionPath: {
    path: [
      { x: 0, y: 150, width: 600, height: 2000 },
      { x: 0, y: 450, width: 600, height: 2000 },
      { x: 200, y: 770, width: 200, height: 2000 }
    ]
  }
})

 

  • Like 2
Link to comment
Share on other sites

Just put the width and height in a separate animation.

 

var scaleIt = gsap.timeline({paused: true,reversed: true})
.to(viewBox, {
  motionPath: {
    path: [
      { x: 100, y: 220,  },
    ]
  },
  duration: 1, ease: "none"
})
.to(viewBox, {
  ease: "none",
  duration: 1,
  width: 500, 
  height: 200
}, 0)

 

  • Like 2
Link to comment
Share on other sites

Wow! This is truly of great help! 

 

Blake's tip of using MotionPath to control the viewBox is fantastic! I thought it might be possible but just couldn't figure out how.

 

And Mikels examples show some really cool ideas. From them I think I should be able to accomplish exactly what I need. 

 

Thanks guys!

 

-Espen

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...