Jump to content

  • Log In with Google      Sign In   
  • Create Account


HTML5: Broken lines...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 blueshogun96   Crossbones+   -  Reputation: 808

Like
1Likes
Like

Posted 15 March 2014 - 04:55 PM

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.

 

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.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png


Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 2956

Like
1Likes
Like

Posted 17 March 2014 - 01:40 AM

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).



#3 blueshogun96   Crossbones+   -  Reputation: 808

Like
1Likes
Like

Posted 19 March 2014 - 01:10 AM

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.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS