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])
label
String: label for the GuiButtonx
Number: x-coordinate of the GuiButtony
Number: y-coordinate of the GuiButtonw
Number: width of the GuiButton (default is 128)h
Number: 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:
strokeWeight
Number: the weight (in pixels) of the strokerounding
Number: radius of cornersfont
Object|String: a font loaded via loadFont(), or a String representing a web safe font (a font that is generally available across all systems)textSize
Number: the size of the letters in units of pixelsfillBg
p5.Color: default background colorfillBgHover
p5.Color: hover background colorfillBgActive
p5.Color: active background colorfillLabel
p5.Color: default label colorfillLabelHover
p5.Color: hover label colorfillLabelActive
p5.Color: active label colorstrokeBg
p5.Color: default stroke colorstrokeBgHover
p5.Color: hover stroke colorstrokeBgActive
p5.Color: active stroke color