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

bug with 270° #646

Open
archilmede opened this issue Apr 11, 2021 · 10 comments
Open

bug with 270° #646

archilmede opened this issue Apr 11, 2021 · 10 comments

Comments

@archilmede
Copy link

archilmede commented Apr 11, 2021

Using the patch from version 0.5.2, I still found a bug for a return to 0 °. Herewith the problem:
`

<title>horloge</title> <style> html, body { margin: 0; padding: 0; background: #87CEEB; }
    #horloge {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 40%;
        height: auto;
        transform: translate(-50%, -50%);
    }
</style>
<script src="snap.svg.min.js"></script>
<script> var paper = Snap("#horloge"); var seconde, minute, heure; var s, mn, h;
    function now() {
        let date = new Date();
        s = date.getSeconds();
        mn = date.getMinutes();
        h = date.getHours() % 12;
    }

    function calage_h_min() {
        minute.transform(`r${6*mn+s/10},50,50`);
        heure.transform(`r${30*h+mn/2+s/120},50,50`);
    }

    function initHorloge() {
        let attribute = {
            'stroke-width': 2,
            stroke: 'white'
        };
        paper.circle(50, 50, 45).attr({
            'stroke-width': 3,
            stroke: 'white',
            fill: 'none'
        });

        minute = paper.line(50, 20, 50, 50).attr(attribute);
        heure = paper.line(50, 26, 50, 50).attr(attribute);
        now();
        calage_h_min();
        paper.circle(50, 50, 3).attr('fill', 'white');
        seconde = paper.line(50, 15, 50, 50).attr({
            'stroke-width': 1,
            stroke: 'red'
        }).transform(`r${6*s},50,50`);
        paper.circle(50, 50, 1.5).attr('fill', 'red');

        let grad = paper.g();
        for (let i = 0; i < 4; i++) {
            grad.add(paper.circle(50, 10, 2).transform(`r${90*i},50,50`));
        }

        for (let j = 0; j < 14; j++) {
            grad.add(paper.circle(50, 10, 1).transform(`r${30*j},50,50`));
        }
        grad.attr('fill', 'white');
        anime();
    }

    function anime() {
        now();
        calage_h_min();
        seconde.animate({
            transform: `r${6*s},50,50`
        }, 1000, mina.bounce, anime);
    }
    window.addEventListener('load', initHorloge);
</script>
`
@ibrierley
Copy link
Collaborator

I think you need to be a bit clearer what the bug is, and reduce the code to a minimal example.

@archilmede
Copy link
Author

I am French and very bad at English ... In fact, the example given is a clock whose second hand returns to 12 o'clock (0 °) which instead of continuing in the normal direction, makes a turn in the opposite direction to restart in the normal direction. Indeed the angle does not progress any more arriving at 12h (the seconds return to 0). I think the fix works with a continual increase in angle but if it goes back to 0 and increases again it has a bug ... the needle rolls back to at 0 ° angle and starts again in the normal direction. See code ...
I hope I was clear in my explanation ...

@ibrierley
Copy link
Collaborator

Can you put it on a jsfiddle ? Is it that it's animating to 0 rather than 360 or something ?

@archilmede
Copy link
Author

archilmede commented Apr 12, 2021 via email

@ibrierley
Copy link
Collaborator

Reading through that, it sounds like it's simply animating to 0, so this would be correct behaviour and not a bug (but I may be misunderstanding). You just want it to rotate to 360 and not 0 when the seconds are at 0.

@archilmede
Copy link
Author

archilmede commented Apr 13, 2021 via email

@ibrierley
Copy link
Collaborator

There is no bug as such (that has been explained so far), an animation from a higher to a lower number will go in the opposite direction. You just need to handle crossover points. I.e this should fix the clock problem...

function anime() {
        now();
        calage_h_min();
        if( s == 0 ) { s=60 }
        if( s == 1 ) { seconde.transform( `r0,50,50` ) }
        seconde.animate({
            transform: `r${6*s},50,50`
        }, 1000, mina.bounce, anime);
    }

@archilmede
Copy link
Author

archilmede commented Apr 13, 2021 via email

@ibrierley
Copy link
Collaborator

0.5.1 "works" (i.e it doesn't actually), but it works on 0 as it switches internally to represent -90 deg at 270, and animates from -90 to 0, which is "fine". All that's happened though, is it's broken at 270 instead of 0. It still has essentially the same problem, worse in fact.

If you look at older versions, eg 0.4.1 it has the same issue you are seeing in 0.5.2.

So the question is, what should happen when we animate anything from 359 to 0 (or 360 to 1) ? Should it go erm upwards, or downwards ? Of course it should go downwards. It would be completely incorrect for a tween between 359 and 0 to go to 359.1 or whatever.

It's down to you to make that happen. There's a few ways to skin a cat, but the most intuitive to me is to reset it, when it gets to 0, which is why I suggested that code.

@archilmede
Copy link
Author

archilmede commented Apr 13, 2021 via email

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