Jump to content
Search Community

Simiantics

Premium
  • Posts

    4
  • Joined

  • Last visited

Posts posted by Simiantics

  1. I can get the hand to "stick" to the end of the arm when the animation starts, but the minute update Paths starts, the coordinate system gets wonky. I'm trying to use the motion path plugin relative path function, but just can't get it right. 

     

    Also would like to rotate the hand from 6 to 12 o clock while it's moving, but that's a dream right now.

     

    See the Pen YzbWZmo by robws (@robws) on CodePen

  2.  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>
×
×
  • Create New...