Advertisement Jump to content
Sign in to follow this  
blueshogun96

HTML5: Broken lines...

This topic is 1771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!