HTML5 Mouse leaving canvas area in html5

Discussion in 'Programming' started by Uhfgood, Jan 6, 2017.

Tags:
  1. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    I've implemented some code that allows me to drag the paddle in my breakout game let or right sort of like a scroll bar. Only if I hold down the button drag it outside the canvas area, let go of the button while outside of the canvas area, I then move back into the canvas, and the mouse automatically moves as though I were still dragging it. Right now I'm detecting if the paddle has hit the wall objects, and then I act as though I released the left mouse button. But it still does this crazy thing...

    Code below:
    Code:
    var hit_wall = false;
    
    if( is_dragging == false )
    {
        if( mouse_check_button( mb_left ) &&
            collision_point( mouse_x, mouse_y, self, false, false ) )
        {
            is_dragging = true;
            prev_mouse_x = mouse_x;
        }
    }
    else
    {
        var mouse_travel = mouse_x - prev_mouse_x;
        if( !place_meeting(x + mouse_travel, y, obj_left_wall) &&
            !place_meeting(x + mouse_travel, y, obj_right_wall) )
            x += mouse_travel;
        else
        {
            hit_wall = true;
            if( mouse_travel < 0 )
            {
                // left
                var dis = distance_to_object( obj_left_wall );
                if( dis < abs( mouse_travel ) )
                {
                    x -= ( dis - 1 );
                }
            }
            else
            {
                // right
                var dis = distance_to_object( obj_right_wall );
                if( dis < abs( mouse_travel ) )
                {
                    x += ( dis - 1 );
                }
            }
        }
        
        prev_mouse_x = mouse_x;
    }
    if( !mouse_check_button( mb_left ) || hit_wall )
    {
        is_dragging = false;
        speed = 0;
        prev_mouse_x = mouse_x;
        //if( hit_wall ) show_message( "hit wall" );
    }
     
  2. Laurent57

    Laurent57 Member

    Joined:
    Nov 20, 2016
    Posts:
    154
    Can't you check the coordinates of the mouse and use mouse_clear if the pointer is outside?
     
  3. Uhfgood

    Uhfgood Member

    Joined:
    Sep 26, 2016
    Posts:
    87
    Tried device_mouse_raw_x but it only reports the position if it's over the canvas area -- the above code attempts to turn off mouse dragging when the paddle hits the wall objects, but this doesn't really work either.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice