Jump to content
Search Community

Two-buttons invoke "Zoom" and then "Rotate" not working (locking)

Simiantics test
Moderator Tag

Recommended Posts

 Given I'm using an SVG I load dynamically, I wasn't sure how to CodePen this, so hopefully it will be okay:

I want the Start Animation button to zoom in on the SVG, then the Bad Monkey animation to animate the monkey's arm in the zoomed position.

 

The zooming works right, but then pressing either button does nothing. If I press "Bad Monkey" first, it console.logs, but doesn't tie up the thread or whatever.

 

The reason I tried to use a global timeline was to make sure the Bad Monkey animation occurred in the zoomed context (that is, the coordinate space was shared between the zoomed image and the desire to rotate an internal component).


Any ideas?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image and SVG Animation with GSAP</title>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.2/dist/gsap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/MotionPathPlugin.min.js"></script>
    <style>
        #animation-container {
            position: relative;
            width: 500px;
            height: 500px;
            margin-top: 50px;
            display: block;
        }

        .svg-overlay {
            border: 1px solid gray;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .svg-overlay svg {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>

<div id="animation-container">
    <div id="svg-container1" class="svg-overlay"></div>
</div>

<div style="margin-top:50px">
    <button id="startAnimation" style='height:75px;width:150px'>Start Animation</button>
    <button id="badMonkey" style='height:75px;width:150px'>Bad Monkey</button>
</div>

<script>

let myTimeline = gsap.timeline({paused: true});

function loadSVG(url, target, x, y) {
    fetch(url)
        .then(response => response.text())
        .then(svg => {
            document.getElementById(target).innerHTML = svg;
            // Directly configure the timeline here if necessary, or elsewhere based on your app's logic
        })
        .catch(error => console.error('Error loading SVG:', error));
}

function startAnimation() {
    myTimeline.clear(); // Clear the timeline to reset or remove previous animations if needed
    myTimeline.to("#svg-container1 svg", {
        scale: 1.75,
        xPercent: 2,
        transformOrigin: "left center",
        duration: 1,
        onComplete: function() {
            console.log("Animation completed");
            // Place any code here that you want to execute after the timeline completes
            // For example, enabling a button, starting another animation, etc.
        }
    }).play();
}

function badMonkey() {
    console.log("bad Monkey run")
    // Check if the timeline has the zoom animation completed before adding the badMonkey animation
    if (myTimeline.progress() > 0 && !myTimeline.isActive()) {
        myTimeline.call(() => {
            gsap.to("#right-arm", {
                rotation: "+=25",
                transformOrigin: "top left",
                duration: 0.5
            });
        }, [], '+=0'); // Schedule immediately after the current end of the timeline
    }
}


document.getElementById('badMonkey').addEventListener('click', badMonkey);
document.getElementById('startAnimation').addEventListener('click', startAnimation);


loadSVG('svg/train canvas.svg', 'svg-container1', 0, 0);

</script>

</body>
</html>
Link to comment
Share on other sites

Hi @Simiantics and welcome to the GSAP Forums!

 

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

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...