08.02.2019 Views

Web_Designer_UK__May_2018

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tutorials<br />

Code a dynamic SVG radial menu<br />

GSAP animations<br />

The GSAP animation library is simple<br />

yet extremely powerful. The library<br />

gives us a really simple syntax and<br />

handles all of the complex calculations<br />

under the hood for us. In its simplest<br />

form the library allows us to create<br />

timelines and then add animations to<br />

these timelines. This concept will be<br />

familiar if you’ve used Flash or any<br />

other animation GUI.<br />

var tl = new timelineLite();<br />

Once we have created a timeline we<br />

can create an animation to add to it.<br />

The example below will animate the<br />

‘circle’ element from a scale of ‘0’ to<br />

its original scale, over the duration of<br />

one second. We’re also providing the<br />

delay and start times of ‘0’ although<br />

this isn’t required as they are the<br />

default values.<br />

tl.from(<br />

‘circle’, 1, {<br />

scale:’0’<br />

}, 0, 0<br />

);<br />

The library also has a number of more<br />

complicated functions we can use<br />

like the staggerTo and staggerFrom<br />

functions, which allow us to animate a<br />

number of elements one after another,<br />

as well as a number of paid plugins for<br />

really specific uses.<br />

The library is performant and<br />

handles much of the browser<br />

compatibility issues you would run into<br />

making it ideal for production.<br />

14. Outside layer else logic<br />

In the last step we created the logic for when the home<br />

menu was not already open, so here we create the<br />

opposite logic. We need to make sure to update the<br />

variables to ensure we can keep track of the menu’s state.<br />

We also play the ‘animate_outside’ timeline in reverse<br />

when clicking the main menu button.<br />

else {<br />

homeOpen = false;<br />

outsideCircleOpen = false;<br />

animate_outside.reverse();<br />

}<br />

http://srt.lt/zDcYw<br />

15. Animate the<br />

home menu buttons<br />

We now need to create a new timeline for our home<br />

menu buttons and change the initial play state to paused.<br />

We can utilise GSAP’s ‘staggerFrom’ function to apply the<br />

same animation to the buttons, one at a time, with a given<br />

delay between each iteration.<br />

var animate_home = new TimelineLite({paused:<br />

true});<br />

animate_home<br />

.staggerFrom(“.home-menu g”, 0.5, {<br />

transformOrigin:”50% 50%”,<br />

opacity: “0”,<br />

scale: “0”<br />

}, 0.125, 0.25)<br />

;<br />

http://srt.lt/q1SyG<br />

16. Add animations to<br />

click functions<br />

We’ve created our home button’s timeline so we now<br />

need to update our click function to include playing and<br />

reversing the animation when the home button is clicked.<br />

$(‘.home-button’).click(function() {<br />

if(homeOpen == false) {<br />

...<br />

animate_home.play();<br />

} else {<br />

...<br />

animate_home.reverse();<br />

}<br />

});<br />

http://srt.lt/qC2B5s<br />

17. More menu<br />

We now need to do exactly the same for the More menu<br />

by creating a click function and staggered animation<br />

timeline for the menu and its buttons. We also need<br />

to update the menu state variables within each of our<br />

click functions.<br />

http://srt.lt/i3rT<br />

18. Settings and FAQ menu<br />

As you may have guessed we need to repeat the same<br />

thing for both the Settings and FAQ menus. Remember to<br />

add the menu state variables to each of the layers and<br />

play the animation timelines in reverse.<br />

http://srt.lt/x5wTx<br />

19. Almost finished<br />

We now have all of the animations being triggered and<br />

played, or reversed given the appropriate click on the<br />

middle layer. We need to make sure all of the animations<br />

are reversed and the state variables updated when the<br />

main menu is clicked and is already open.<br />

$(‘.main-menu’).click(function() {<br />

animate_middle.reverse();<br />

animate_outside.reverse();<br />

...<br />

menuOpen = false;<br />

outsideCircleOpen = false;<br />

homeOpen = false;<br />

...<br />

});<br />

http://srt.lt/Bk2Hz4<br />

20. Active states — middle layer<br />

That’s all of our animations and click functions created –<br />

the only thing left to do is to add the ‘is-active’ class to the<br />

button that has been clicked. jQuery allows us to use<br />

advanced CSS selectors to first listen for any click to<br />

buttons in the middle layer and then to remove the class<br />

from all the other buttons.<br />

$(‘.middle-layer [class*=”-button”]’).<br />

click(function(){<br />

$(‘.middle-layer [class*=”-button”]’).<br />

removeClass(‘is-active’);<br />

$(‘.outside-layer [class*=”-button”]’).<br />

removeClass(‘is-active’);<br />

$(this).addClass(‘is-active’);<br />

});<br />

http://srt.lt/T8Z3f5<br />

21. Active states — outside layer<br />

We now do the same thing for the buttons on the outside<br />

layer, targeting, removing and adding our active class to<br />

the clicked button. That’s it, our radial menu, created with<br />

SVG, animated with GSAP and coded with jQuery is<br />

finished. The animations are simple yet effective and the<br />

active states and hover interactions allow the user to keep<br />

track of their place in the menu.<br />

$(‘.outside-layer [class*=”-button”]’).<br />

click(function(){<br />

$(‘.outside-layer [class*=”-button”]’).<br />

removeClass(‘is-active’);<br />

$(this).addClass(‘is-active’);<br />

});<br />

http://srt.lt/k9Gj1x<br />

Collection of tutorial pens - http://srt.lt/AmTp<br />

tutorial _________________________________________________55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!