GuiJoystick
August 26, 2019 ยท View on GitHub
GuiJoystick
Example | Constructor | Variables | Functions
Description
Joystick class. This is similar to GuiSlider2d except that the handle snaps back to the center point when input is released.

Example
let gui;
// Create a variable for your slider
let j;
// Create variables for tracking position and velocity
let x, y, velX, velY;
function setup() {
createCanvas(400, 400);
gui = createGui();
// Create Joystick.
// The last four optional arguments define minimum and maximum values
// for the x and y axes; minX, maxX, minY, maxY
// The default min and max values for all four are -1 and 1.
j = createJoystick("Joystick", 10, 210, 175, 175, -1, 1, 1, -1);
// Starting position and velocity
x = 300;
y = 100;
velX = 0;
velY = 0;
}
function draw() {
background("#242038");
drawGui();
if (j.isChanged) {
// Print a message when Slider 1 is changed
// that displays its value.
print(j.label + " = {" + j.valX + ", " + j.valY + "}");
}
// Use Joystick's output to change velocity
velX += j.valX;
velY += j.valY;
// Draw our ellipse
fill("#7AA0FF");
stroke("#FFFFFF")
ellipse(x + velX, y + velY, 100);
}
/// Add these lines below sketch to prevent scrolling on mobile
function touchMoved() {
// do some stuff
return false;
}
Constructor
createJoystick()
Example
let gui;
let j;
function setup() {
createCanvas(400, 400);
gui = createGui();
j = createJoystick("Joystick", 50, 50);
}
Description
Creates a new GuiJoystick object. This is a 2 dimensional slider, and the handle snaps back to the center.
Syntax
createJoystick(label, x, y, [w], [h], [minX], [maxX], [maxX], [maxY])
Parameters
labelString: label for the GuiJoystickxNumber: x-coordinate of the GuiJoystickyNumber: y-coordinate of the GuiJoystickwNumber: width of the GuiJoystick (default is 256)hNumber: height of the GuiJoystick (default is 256)minXNumber: lower bound of the value's x range (default value is -1)maxXNumber: upper bound of the value's x range (default value is 1)minYNumber: lower bound of the value's y range (default value is -1)maxYNumber: upper bound of the value's y range (default value is 1)
Returns
GuiJoystick
Variables
val
Example
joystick.val = {x: 0.5, y: 0.5};
if (joystick.isChanged) {
print(joystick.val.x);
print(joystick.val.y);
}
Description
Value of the GuiJoystick returned as object {x: valX, y: valY}.
valX
Example
joystick.valX = 0.5;
if (joystick.isChanged) {
print(joystick.valX);
}
Description
X value of the GuiJoystick.
valY
Example
joystick.valY = 0.5;
if (joystick.isChanged) {
print(joystick.valY);
}
Description
Y value of the GuiJoystick.
minX
Example
joystick.minX = -100;
Description
Lower x bound of the GuiJoystick range.
maxX
Example
joystick.maxX = 100;
Description
Upper x bound of the GuiJoystick range.
minY
Example
joystick.minY = -100;
Description
Lower y bound of the GuiJoystick range.
maxY
Example
joystick.maxY = 100;
Description
Upper y bound of the GuiJoystick range.
Functions
setStyle()
Example
joystick.setStyle("fillBg", color(255, 0, 0));
joystick.setStyle({
fillBg: color("#FF0000"),
rounding: 10,
handleRadius: 30
});
Description
Individual GuiJoystick objects can be styled using this method. There are two types of input it takes. Use an input string and value to set an individual property, and use an Object with key/value notation to set multiple properties at once.
Syntax
setStyle(property, value)
setStyle(Object)
Parameters
property String: name of property to be set
value Number|String|Object: value of property to be set
Object Object: multiple propertys and values to be set
Returns
None
Style
The GuiJoystick style properties are:
strokeWeightNumber: the weight (in pixels) of the strokeroundingNumber: radius of cornershandleRadiusNumber: radius of handle in pixelsfillBgp5.Color: default background fill colorfillBgHoverp5.Color: hover background fill colorfillBgActivep5.Color: active background fill colorfillTrackp5.Color: default track fill colorfillTrackHoverp5.Color: hover track fill colorfillTrackActivep5.Color: active track fill colorfillHandlep5.Color: default handle fill colorfillHandleHoverp5.Color: hover handle fill colorfillHandleActivep5.Color: active handle fill colorstrokeBgp5.Color: default background stroke colorstrokeBgHoverp5.Color: hover background stroke colorstrokeBgActivep5.Color: active background stroke colorstrokeTrackp5.Color: default track stroke colorstrokeTrackHoverp5.Color: hover track stroke colorstrokeTrackActivep5.Color: active track stroke colorstrokeHandlep5.Color: default handle stroke colorstrokeHandleHoverp5.Color: hover handle stroke colorstrokeHandleActivep5.Color: active handle stroke color