0

Am very new to canvas and javascript. I found a snippet for a starfield animation effect but it loops indefinitely. How do I get the animation to stop after say, 30 seconds? I believe it has something to do with clearInterval or setTimeout but I have no idea where in the code this should be implemented. Any help would be greatly appreciated.

window.onload = function() {

        var starfieldCanvasId     = "starfieldCanvas", 
            framerate             = 60,         
            numberOfStarsModifier = 1.0,      
            flightSpeed           = 0.02;             

        var canvas        = document.getElementById(starfieldCanvasId),

            context       = canvas.getContext("2d"),
            width         = canvas.width,
            height        = canvas.height,
            numberOfStars = width * height / 1000 * numberOfStarsModifier,
            dirX          = width / 2,
            dirY          = height / 4,
            stars         = [],
            TWO_PI        = Math.PI * 2;

        for(var x = 0; x < numberOfStars; x++) {
            stars[x] = {
                x: range(0, width),
                y: range(0, height),
                size: range(0, 1)
            };
        }

        window.setInterval(tick, Math.floor(1000 / framerate));            

        function tick() {
            var oldX,
                oldY;

            context.clearRect(0, 0, width, height);

            for(var x = 0; x < numberOfStars; x++) {
                oldX = stars[x].x;
                oldY = stars[x].y;

                stars[x].x += (stars[x].x - dirX) * stars[x].size * flightSpeed ;
                stars[x].y += (stars[x].y - dirY) * stars[x].size * flightSpeed ;
                stars[x].size += flightSpeed;

                if(stars[x].x < 0 || stars[x].x > width || stars[x].y < 0 || stars[x].y > height) {
                    stars[x] = {
                        x: range(0, width),
                        y: range(0, height),
                        size: 0
                    };
                }

                context.strokeStyle = "rgba(160, 160, 230, " + Math.min(stars[x].size, 2) + ")";
                context.lineWidth = stars[x].size;
                context.beginPath();
                context.moveTo(oldX, oldY);
                context.lineTo(stars[x].x, stars[x].y);
                context.stroke();
            }

        }

        function range(start, end) {
            return Math.random() * (end - start) + start;
        }

    };
2
  • what have you tried? because there are several means to stop the animation from appearing to run, and it doesn't seem like you've attempted anything here. Commented Sep 4, 2018 at 2:46
  • let me rephrase, there will be a function loop related to rendering (currently, that's your tick function) that is set on an interval; if your objective is to stop all animation (in it's present form), you would clear the timer; but irregardless to whether your objective to stop all animation or simply stop updating this animation, you would most certainly need to account for time stamps (i.e. initial timestamp that is compared to a timestamp on each loop... Commented Sep 4, 2018 at 3:10

1 Answer 1

1

It appears that tick() is the animation loop, so change the line;

window.setInterval(tick, Math.floor(1000 / framerate)); 

to

window.animLoop = window.setInterval(tick, Math.floor(1000 / framerate));
window.setTimeout( function() { window.clearInterval( window.animLoop ) }, 30000 );

where 30000 is the time to end in milliseconds.

This will stop the animation from repeating by ending the interval from looping.

Sign up to request clarification or add additional context in comments.

1 Comment

ah.. proved my comment in the question wrong: running a second loop without regards to timestamps.. though, this is definitely not a graceful implementation, but it is an answer. Having said that, window.setInterval returns a int representing the interval Id, so I think you ought to reflect that instead of naming it animLoop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.