Sign in to follow this  
blueshogun96

HTML5: Broken lines...

Recommended Posts

blueshogun96    2264

As of late, I've been creating an HTML5 version of my game (with basic functionality).  I'm new to HTML5 game dev, so I'm still learning the ropes here.  What I'm trying to do is leave a line/trail behind the user's movement.  In C/C++, everything works fine, but in HTML5/Javascript, the trail is broken and not every line gets drawn.

 

[attachment=20436:Screen Shot 2014-03-15 at 3.31.21 PM.png]

 

No idea why.  This is the first time I've run into this issue.  Windows, MacOSX, iOS, no problems on the native side.

 

The relevant bits of my code below:

/* Trails */
function trail_t( x1, y1, x2, y2 )
{
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.intersection = false;
    this.alpha = 255;
}

var trails = [];


function draw_user()
{
    var game_canvas = document.getElementById("game_canvas");
    var context = game_canvas.getContext("2d");
    
    /* Draw the asterisk */
    context.translate( user.x, user.y );
    context.rotate( user.rot );
    context.drawImage( small_asterisk_image, -8, -8, 16, 16 );
    context.rotate( -user.rot );
    context.translate( -user.x, -user.y );
    
    user.rot += 5.0/(360.0/(Math.PI*2));
    
    /* Add trails */
    add_trail();
}

function add_trail()
{
    /* Add a new line */
    var t = new trail_t( user.x, user.y, user.lx, user.ly );
    trails.push(t);
}

function draw_trails()
{
    var game_canvas = document.getElementById("game_canvas");
    var context = game_canvas.getContext("2d");
    
    /* Draw all trails */
    for( var i = trails.length; i--; )
    {
        context.beginPath();
        context.moveTo( trails[i].x1, trails[i].y1 );
        context.lineTo( trails[i].x2, trails[i].y2 );
        context.lineWidth = 2;
        context.stroke();
    }
}

function update_trails()
{
    /* Update all trails */
    for( var i = trails.length; i--; )
    {
        trails[i].alpha -= 5;
        if( trails[i].alpha < 5 )
        {
            trails.splice( i, 1 );
        }
    }
}

Everything goes smoothly, except for that.  No idea why.  Any ideas?  Thanks.

 

Shogun.

Share this post


Link to post
Share on other sites
Krohm    5030

You should be calling moveto only for the first point, all other lines are to be connected. You're currently terminating each path and starting a new subpath each loop.

You can - must - do this in a single loop, if you have constant line thickness then move out stroke for better perf (but I guess that's not really doable).

Share this post


Link to post
Share on other sites
blueshogun96    2264

Okay, fixed it!

 

I honestly forgot to take your last bit of advice.  Moving beginPath(), lineWidth and stroke() outside of the loop did it.  Now that I have better understand of how these functions work, hopefully I won't abuse it in the future.

 

The function now looks like this:

function draw_trails()
{
    var game_canvas = document.getElementById("game_canvas");
    var context = game_canvas.getContext("2d");
    
    /* Draw all trails */
    context.beginPath();
    for( var i = trails.length; i--; )
    {
        if( i === trails.length )
            context.moveTo( trails[i].x1, trails[i].y1 );
        else
            context.lineTo( trails[i].x1, trails[i].y1 );
        context.lineTo( trails[i].x2, trails[i].y2 );
    }
    context.lineWidth = 2;
    context.stroke();
}

Thanks again.

 

Shogun.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this