Example | Constructor | Variables | Functions
Momentary button class.
let gui;
// Create variable for your Button
let b;
function setup() {
createCanvas(400, 400);
gui = createGui();
// Create Button
b = createButton("Button", 100, 50, 200, 50);
}
function draw() {
background(220);
drawGui();
if (b.isPressed) {
// Print a message when Button is pressed.
print(b.label + " pressed.");
}
if (b.isHeld) {
// Draw an ellipse when Button is held.
fill(255, 0, 0);
ellipse(200, 300, 100);
}
}
/// Add these lines below sketch to prevent scrolling on mobile
function touchMoved() {
// do some stuff
return false;
}let gui;
let b;
function setup() {
createCanvas(400, 400);
gui = createGui();
b = createButton("Button", 50, 50);
}Creates a new GuiButton object. This is a momentary button.
createButton(label, x, y, [w], [h])labelString: label for the GuiButtonxNumber: x-coordinate of the GuiButtonyNumber: y-coordinate of the GuiButtonwNumber: width of the GuiButton (default is 128)hNumber: height of the GuiButton (default is 32)
GuiButton
button.val = True;
print(button.val)Value of the GuiButton.
button.label = "Button";
print(button.label)Label of the GuiButton. Setting the label will automatically set labelOn and labelOff (see below).
button.labelOn = "Button On";
print(button.labelOn)Label of the GuiButton when pressed or "on".
button.labelOff = "Button Off";
print(button.labelOff)Label of the GuiButton when unpressed or "off".
button.setStyle("fillBg", color(255, 0, 0));button.setStyle({
fillBg: color("#FF0000"),
rounding: 10,
textSize: 40
});Individual GuiButton 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 GuiButton style properties are:
strokeWeightNumber: the weight (in pixels) of the strokeroundingNumber: radius of cornersfontObject|String: a font loaded via loadFont(), or a String representing a web safe font (a font that is generally available across all systems)textSizeNumber: the size of the letters in units of pixelsfillBgp5.Color: default background colorfillBgHoverp5.Color: hover background colorfillBgActivep5.Color: active background colorfillLabelp5.Color: default label colorfillLabelHoverp5.Color: hover label colorfillLabelActivep5.Color: active label colorstrokeBgp5.Color: default stroke colorstrokeBgHoverp5.Color: hover stroke colorstrokeBgActivep5.Color: active stroke color
