Example | Constructor | Variables | Functions
Joystick class. This is similar to GuiSlider2d
except that the handle snaps back to the center point when input is released.
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;
}
let gui;
let j;
function setup() {
createCanvas(400, 400);
gui = createGui();
j = createJoystick("Joystick", 50, 50);
}
Creates a new GuiJoystick
object. This is a 2 dimensional slider, and the handle snaps back to the center.
createJoystick(label, x, y, [w], [h], [minX], [maxX], [maxX], [maxY])
label
String: label for the GuiJoystickx
Number: x-coordinate of the GuiJoysticky
Number: y-coordinate of the GuiJoystickw
Number: width of the GuiJoystick (default is 256)h
Number: height of the GuiJoystick (default is 256)minX
Number: lower bound of the value's x range (default value is -1)maxX
Number: upper bound of the value's x range (default value is 1)minY
Number: lower bound of the value's y range (default value is -1)maxY
Number: upper bound of the value's y range (default value is 1)
GuiJoystick
joystick.val = {x: 0.5, y: 0.5};
if (joystick.isChanged) {
print(joystick.val.x);
print(joystick.val.y);
}
Value of the GuiJoystick
returned as object {x: valX, y: valY}
.
joystick.valX = 0.5;
if (joystick.isChanged) {
print(joystick.valX);
}
X value of the GuiJoystick
.
joystick.valY = 0.5;
if (joystick.isChanged) {
print(joystick.valY);
}
Y value of the GuiJoystick
.
joystick.minX = -100;
Lower x bound of the GuiJoystick
range.
joystick.maxX = 100;
Upper x bound of the GuiJoystick
range.
joystick.minY = -100;
Lower y bound of the GuiJoystick
range.
joystick.maxY = 100;
Upper y bound of the GuiJoystick
range.
joystick.setStyle("fillBg", color(255, 0, 0));
joystick.setStyle({
fillBg: color("#FF0000"),
rounding: 10,
handleRadius: 30
});
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.
setStyle(property, value)
setStyle(Object)
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
None
The GuiJoystick
style properties are:
strokeWeight
Number: the weight (in pixels) of the strokerounding
Number: radius of cornershandleRadius
Number: radius of handle in pixelsfillBg
p5.Color: default background fill colorfillBgHover
p5.Color: hover background fill colorfillBgActive
p5.Color: active background fill colorfillTrack
p5.Color: default track fill colorfillTrackHover
p5.Color: hover track fill colorfillTrackActive
p5.Color: active track fill colorfillHandle
p5.Color: default handle fill colorfillHandleHover
p5.Color: hover handle fill colorfillHandleActive
p5.Color: active handle fill colorstrokeBg
p5.Color: default background stroke colorstrokeBgHover
p5.Color: hover background stroke colorstrokeBgActive
p5.Color: active background stroke colorstrokeTrack
p5.Color: default track stroke colorstrokeTrackHover
p5.Color: hover track stroke colorstrokeTrackActive
p5.Color: active track stroke colorstrokeHandle
p5.Color: default handle stroke colorstrokeHandleHover
p5.Color: hover handle stroke colorstrokeHandleActive
p5.Color: active handle stroke color