Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the mouse to control a game in HTML canvas

I am trying to find out if it is possible for JavaScript and HTML's to take control of the mouse and introduce relative movement control - the type of mouse control you get in first person games (where the mouse can be moved in one direction and the player will infinitely turn until the mouse stops).

The only way I can think this could be achieved is by taking control of the mouse via JavaScript, which I'm sure is not possible. Does anyone have any ideas on this?

like image 629
Greg Avatar asked Dec 23 '10 15:12

Greg


People also ask

Is HTML Canvas good for games?

The <canvas> element is perfect for making games in HTML. The <canvas> element offers all the functionality you need for making games. Use JavaScript to draw, write, insert images, and more, onto the <canvas> .

How do you get the mouse position on canvas?

The dimension of the canvas is found using the getBoundingClientRect() function. This method returns the size of an element and its position relative to the viewport. The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position.

How do you draw a line on a mouse with a canvas?

To draw a line on the canvas, you should create a path using beginPath() method in context API. After that, you can use moveTo(x,y) method to move the drawing cursor based on x and y parameters. // To draw a static line.

What is HTML Canvas good for?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.


1 Answers

Good news old question, with the introduction of Mouse Lock, https://developer.mozilla.org/en/API/Mouse_Lock_API , you can capture relative movement.

like image 168
eighteyes Avatar answered Sep 27 '22 17:09

eighteyes