Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesnt appear to be working #1

Open
s2353553 opened this issue May 4, 2022 · 5 comments
Open

Doesnt appear to be working #1

s2353553 opened this issue May 4, 2022 · 5 comments

Comments

@s2353553
Copy link

s2353553 commented May 4, 2022

I dont understand how I cant get this to work for another svg.. I change the filename and id to the id set inside the svg.. and export the svg as programmatic ..but for some reason it wont work?

@s2353553
Copy link
Author

s2353553 commented May 4, 2022

I literally cannot get any file generated from svgator to work with this example but the svgator logo example works fine.. please help someone! I have no idea what the issue is, as I say surely its a matter of changing the filename and svg ID in index.html

@alex-lucaci
Copy link

I'm sorry it wasn't explicitly stated, but the current example only works if you do an export with "Animation type: JavaScript" and "Animation start: On click".

You can also check out pages about programmatic animation:
https://www.svgator.com/help/getting-started/animate-programmatically
https://www.svgator.com/help/getting-started/svgator-player-js-api

@alex-lucaci
Copy link

alex-lucaci commented May 4, 2022

Also, please note that the examples you see on the programmatic animation pages are referring to inline SVGs.

If you still want to include the SVG in an tag, ideally you would do something like this:

<!DOCTYPE html>
<html>
   <head>
      <title>Testing SVG</title>
      <script>
         //Wait for the document to load
            document.addEventListener("DOMContentLoaded", function() {
                //grab the <object> element
                let object1 = document.getElementById('animated-svg');

                //Wait for the content of the <object> to load
                object1.addEventListener("load", function() {
                    //grab the <svg> element
                    let svg = object1.contentDocument.getElementById('e5478wvxh25l1');

                    //Wait for the SVGtor player to be ready for use
                    svg.svgatorPlayer.ready(function(player) {
                        //inset your code here; i.e.
                        player.play();
                    });
                });
            });
      </script>
   </head>
   <body>
      <object id="animated-svg" type="image/svg+xml" data="animated.svg" style="height: 857px;"></object>
   </body>
</html>

Also it's good to know that this will not work if the SVG is on a different (sub)domain.

@s2353553
Copy link
Author

s2353553 commented May 4, 2022

Ok thanks, I eventually have the onclick working (by adjusting the settings as you said although it states to use programmatic rather than onclick on this page https://www.svgator.com/help/getting-started/animate-programmatically which is a contradiction.. no?) but I actually need the animation to trigger on mouseover of a div and also (for a different animation) when a div comes into view.... your advice on how I can achieve this would be much appreciated, thanks in advance!

Also is there really no real support available? I havent received any replies to my emails, once again many thanks in advance!

@alex-lucaci
Copy link

alex-lucaci commented May 4, 2022

which is a contradiction.. no?

As I previously mentioned, the onclick solution is (years) old and the programmatic one is only 2 months old.
Everyone is encouraged to move towards the programmatic solution, which is way more technical.

If you want to trigger on mouseover AND scroll into view, you would need to handle those events manually and decide when to do what. Here's a working JavaScript example:

//Wait for the document to load
document.addEventListener("DOMContentLoaded", function() {
    let object1 = document.getElementById('animated-svg');

    //Wait for the content of the "object" to load
    object1.addEventListener("load", function() {
        let svg = object1.contentDocument.getElementById('e5478wvxh25l1');

        //Wait for the SVGtor player to be ready for use
        svg.svgatorPlayer.ready(function(player) {
            mapDocumentEvents(player);
        });
    });
});

function mapDocumentEvents(player) {
    let object1 = document.getElementById('animated-svg');
    let svg = object1.contentDocument.getElementById('e5478wvxh25l1');

    checkIfElementIsInView(object1, player);

    //listen for the "scroll" event on the document
    document.addEventListener('scroll', () => {
        checkIfElementIsInView(object1, player);
    });

    //Listen for the mouseenter event on the SVG
    svg.addEventListener('mouseenter', () => {
        player.play();
    });

    //Listen for the mouseleave event on the SVG
    svg.addEventListener('mouseleave', () => {
        player.pause();
    });
}

function checkIfElementIsInView(obj, pl) {
    let rect = obj.getBoundingClientRect();

    /*
	Check if the element is visible completely.

	Use other values instead of 0 and "window.innerHeight"
	if you want to start/pause the animation when the element
	is only partially visible
	*/
    if (rect.top >= 0 && rect.bottom <= window.innerHeight) {
        pl.play();
    } else {
        pl.pause();
    }
}
  • You should also know that all new exports support programmatic animation, even if you select "Animation start: On click" for example, as long as the "Animation type" is set to JavaScript. As a matter of fact, the programmatic animation is always available as long as you export with JavaScript, no matter what "Animation start" you set.
  • Also, if you export with "Animation start: On scroll into view" it only works without additional coding if you include the SVG directly into the page, without using an <object> tag. The JavaScript solution provided here works even if you're using an <object> tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants