2

This program is an animated line that randomly walks around the screen. Ive refactored the code to be used in an object but am still running into issues. I would like to have the line "bounce" off the side of the screen but unfortunately when it does so it creates a much larger line that is not animated, and starts to grow out of control.

function animatedLine(startx, starty, colorStr){
    // these should be passed into the object.
    this.curpointX = startx,
    this.curpointY = starty,
    this.NORTH = 1,
    this.NORTHEAST = 2;
    this.EAST = 3;
    this.SOUTHEAST = 4;
    this.SOUTH = 5;
    this.SOUTHWEST = 6; 
    this.WEST = 7;
    this.NORTHWEST = 8;
    this.colorHex = colorStr;

    var self = this;
    // Lets get rid of one of these position variables.
    this.startpointx = this.curpointX;
    this.startpointy = this.curpointY;
    this.curposx = this.curpointX;
    this.curposy = this.curpointY;
    this.endpointx = this.curpointX;
    this.endpointy = this.curpointY;
    this.myinterval = {};

    this.init = function() {
        this.myinterval = setInterval( function() { self.animate(self.endpointx,self.endpointy);}, 10);
    }

    this.animate = function(endpointx, endpointy) {
        this.startpointy = this.curposy;
        this.startpointx = this.curposx;
        if (this.curposx == endpointx && this.curposy == endpointy){
            this.drawLine();
            return false;
        }
        else if(endpointx != this.curposx && endpointy != this.curposy){
            // this will screw up if we have half pixel somewhere. ( will always be diagnol)
            this.curposy += (endpointy > this.curposy ? 1 : -1);            
            this.curposx += (endpointx > this.curposx ? 1 : -1);
        }
        else if(endpointx != this.curposx){
            this.curposx += (endpointx > this.curposx ? 1 : -1);
        }
        else if(endpointy != this.curposy){
            this.curposy += (endpointy > this.curposy ? 1 : -1);
        }
        else{
            console.log("We have a problem");
        }
        this.drawShape(this.curposx, this.curposy, this.colorHex);
    }

    this.drawShape = function(tendpointx, tendpointy, clor){
        var canvas = document.getElementById('bg');
        var ctx = canvas.getContext('2d');

        ctx.strokeStyle = clor;
        ctx.globalAlpha = 0.2;
        ctx.beginPath();
        ctx.moveTo(this.startpointx ,this.startpointy );
        ctx.lineTo(tendpointx,tendpointy);
        ctx.stroke();
    } 

    this.drawLine = function(flagDirection){

        clearInterval(this.myinterval);

        // calculate the next point with direction and distance.
        var direction = Math.floor(Math.random() * 8) + 1;
        var distance = Math.floor(Math.random() * 10) + 1;

        var newPointY, newPointX;

        switch(direction){
            case this.NORTH:
                newPointX = this.endpointx;
                newPointY = this.endpointy - distance;
                this.setAnimationVariables(newPointX, newPointY);
                break; 
            case this.NORTHEAST:
                newPointX = this.endpointx + distance;
                newPointY = this.endpointy - distance;
                this.setAnimationVariables(newPointX, newPointY);
                break;
            case this.EAST:
                newPointX = this.endpointx + distance;
                newPointY = this.endpointy;
                this.setAnimationVariables(newPointX, newPointY);
                break; 
            case this.SOUTHEAST: 
                newPointX = this.endpointx + distance;
                newPointY = this.endpointy + distance;
                this.setAnimationVariables(newPointX, newPointY);
                break;
            case this.SOUTH:
                newPointX = this.endpointx;
                newPointY = this.endpointy + distance;
                this.setAnimationVariables(newPointX, newPointY);
                break;
            case this.SOUTHWEST:
                newPointX =  this.endpointx - distance;
                newPointY = this.endpointy + distance;
                this.setAnimationVariables(newPointX, newPointY);               
                break;
            case this.WEST:
                newPointX = this.endpointx - distance;
                newPointY = this.endpointy;
                this.setAnimationVariables(newPointX, newPointY);
                break;
            case this.NORTHWEST:
                newPointX = this.endpointx - distance;
                newPointY = this.endpointy - distance;
                this.setAnimationVariables(newPointX, newPointY);
                break;
            default:
                console.log("We have a problem");
        }
        this.init();
    }

    // Helper function to set variables for animation. 
    // TODO refactor to get rid of some of these variables.
    this.setAnimationVariables = function(newPointX, newPointY){

        // we can check this inside of here. 
        // check the newpoints. Verify its inside the canvas.
        if(newPointY > 0 && newPointX > 0 && newPointY < $(document).height() && newPointX < $(document).width()){
            this.startpointx = this.endpointx;
            this.startpointy = this.endpointy;
            this.curpointX = this.endpointx;
            this.curpointY = this.endpointy;
            this.endpointx = newPointX;
            this.endpointy = newPointY;         
        }
        else {
            // This is bugging out for some reason we are running the full programatic stack.
            this.drawLine();
        }
    }
}

You can see my jsfiddle here:http://jsfiddle.net/9mmc4eab/2/

As you can see the line begins to draw itself exponentially because everytime it hits the edge of the screen it creates a new "stack" for each of the new lines. How can I address this?

Eugene Scray
  • 263
  • 1
  • 3
  • 13
  • 3
    `this.animate(...)` isn't returning a function for the interval to execute. It's similar to `setInterval(alert('Hello World!'),1000)` which will only result in 1 alert. – Kevin B Dec 08 '14 at 20:41
  • Interesting, this function returns a bunch of errors: this.myinterval = setInterval("this.animate(" + this.endpointx+ "," + this.endpointy+")", 10); – Eugene Scray Dec 08 '14 at 20:49
  • see the accepted answer in the duplicate, not the question's attempt. – Kevin B Dec 08 '14 at 20:50
  • for some reason this is not working for me either. this.animate is not a function: this.myinterval = setInterval(function() {this.animate(this.endpointx,this.endpointy);}, 10); – Eugene Scray Dec 08 '14 at 20:54
  • because `this` becomes `window`, you will need to store a reference to `this` on the outside. `var self = this;` and then use `self.animate(...` – Kevin B Dec 08 '14 at 20:55
  • Awesome, its working now but Im still running into a recursion issue. – Eugene Scray Dec 11 '14 at 21:31

0 Answers0