Home

Awesome

JoyStick 2 - The new version

Codacy Badge VanillaJS GitHub release (latest by date) GitHub file size in bytes GitHub stars GitHub forks GitHub All Releases npm npm

About

Author: Roberto D'Amico

A simple JoyStick for web application that use HTML5, Canvas and JavaScript. You can simply add a JoyStick in your HTML5 page, base configuration is ready for use it. The JoyStick can be used either on touch devices, or on devices that use mouse, touchpads or similar pointing systems. Developed for Web Remote Control of Robot, the JoyStick can be used for all other scope.

Note: the code not use JQuery, or other framework, but only pure Vanilla JavaScript.

Actual release version is 2.0.0. The complete history of project is avaliable in the Releases page.

Note: the new version is fully compatible with the previous ones!

By using the new callback function, introduced with version 2.0.0 of the library, you can use the properties of the object, referenced in the callback function, to be able to access the same information that in previous versions of the library, was available only through calls to specific methods. The available properties are:

The old methods, the only ones available up to version 1.6.0, can still be used with the new version. Using them you can know the position of the Stick is located. The methods available are:

How to use it

You can simply look the joy.html file for an example of use the library.

First add the JS library in your page, follow the example code to use, if necessary you need to change te path of joy.js file.

<script src="joy.js"></script>

Next define the HTML object that will contain the control, in the following example it shows a div, whose dimensions are defined by in line CSS.

<div id="joyDiv" style="width:200px;height:200px;margin-bottom:20px;"></div>

At the end of page add the initialization of JoyStick object, the only configuration that must be done is to suggest as a parameter for the name of the object that will contain the JoyStick control, in this case the name of div defined in the previous step.

<script type="text/javascript">
// Create JoyStick object into the DIV 'joyDiv'
var joy = new JoyStick('joyDiv');
</script>

You can call one of method, explained before, that return status of Stick, for example in the joy.html file every 50 millisecond was called the method GetX() and showed the value in a textbox:

setInterval(function(){ x.value=joy.GetX(); }, 50);

Otherwise you can use the new callback function, but you need to initialize the JoyStick differently, as in the example below:

var Joy1 = new JoyStick('joy1Div', {}, function(stickData) {
    joy1IinputPosX.value = stickData.xPosition;
    joy1InputPosY.value = stickData.yPosition;
    joy1Direzione.value = stickData.cardinalDirection;
    joy1X.value = stickData.x;
    joy1Y.value = stickData.y;
});

The third parameter is the callback function, in whose signature, there is the object that exposes the properties of the JoyStick. You can see a complete example in the joy.html file in this repo.

Example

You can see an example of result in this picture

JoyStick in action!!!

But if you want see the JoyStick in action go to this link.

Advanced Options

All configuration parameters are optional, must be passed in JSON format, therefore it is sufficient to indicate only the parameters for which you want to provide a configuration other than the Default value.

Tips & Tricks

The title parameter, that have as default value 'joystick', is used to set the ID of Canvas elemente that contains the JoyStick, you can use this to define custom CSS style for the canvas. For example in the joy.html file the CSS style is used to set the border of Canvas with this row of code:

#joystick {
 border: 1px solid #9C9898;
}

Share your experience

If you have integrated JoyStick into your project, and you want to share your user experience, I will be happy to add it on a Use cases Wiki page of this project. If you want, send me an email with some information about the project.

Contribute

To report BUG or request new Features, you can use GitHub's ISSUE system related to this project.