Example | Constructor | Variables | Functions
Toggle button class.
let gui;
// Create a variable for your Checkbox
let cb1;
function setup() {
createCanvas(400, 400);
gui = createGui();
// Create Checkbox
cb1 = createCheckbox("Checkbox", 150, 50, 100, 100);
}
function draw() {
background(220);
drawGui();
if (cb1.isPressed) {
// Print a message when Checkbox is pressed
// that displays its value.
print(cb1.label + " is " + cb1.val);
}
if (cb1.val) {
// Draw an ellipse when Checkbox is true.
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 t;
function setup() {
createCanvas(400, 400);
gui = createGui();
t = createToggle("Toggle", 50, 50);
}
Creates a new GuiToggle
object. This is a toggle button.
createToggle(label, x, y, [w], [h], [defaultVal])
label
String: label for the GuiTogglex
Number: x-coordinate of the GuiToggley
Number: y-coordinate of the GuiTogglew
Number: width of the GuiToggle (default is 128)h
Number: height of the GuiToggle (default is 32)defaultVal
Number: default value for your GuiToggle (default isfalse
)
GuiToggle
toggle.val = True;
print(toggle.val)
Value of the GuiToggle
.
toggle.label = "Toggle";
print(toggle.label)
Label of the GuiToggle
. Setting the label
will automatically set labelOn
and labelOff
(see below).
toggle.labelOn = "Toggle On";
print(toggle.labelOn)
Label of the GuiToggle
when pressed or "on".
toggle.labelOff = "Toggle Off";
print(toggle.labelOff)
Label of the GuiToggle
when unpressed or "off".
toggle.setStyle("fillBgOn", color(255, 0, 0));
toggle.setStyle({
fillBgOn: color("#FF0000"),
rounding: 10,
textSize: 40
});
Individual GuiToggle
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 GuiToggle
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 pixelsfillBgOff
p5.Color: default background color when offfillBgOffHover
p5.Color: hover background color when offfillBgOffActive
p5.Color: active background color when offfillBgOn
p5.Color: default background color when onfillBgOnHover
p5.Color: hover background color when onfillBgOnActive
p5.Color: active background color when onfillLabelOff
p5.Color: default label color when offfillLabelOffHover
p5.Color: hover label color when offfillLabelOffActive
p5.Color: active label color when offfillLabelOn
p5.Color: default label color when onfillLabelOnHover
p5.Color: hover label color when onfillLabelOnActive
p5.Color: active label color when onstrokeBgOff
p5.Color: default stroke color when offstrokeBgOffHover
p5.Color: hover stroke color when offstrokeBgOffActive
p5.Color: active stroke color when offstrokeBgOn
p5.Color: default stroke color when onstrokeBgOnHover
p5.Color: hover stroke color when onstrokeBgOnActive
p5.Color: active stroke color when on