GuiToggle
August 26, 2019 ยท View on GitHub
GuiToggle
Example | Constructor | Variables | Functions
Description
Toggle button class.

Example
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;
}
Constructor
createToggle()
Example
let gui;
let t;
function setup() {
createCanvas(400, 400);
gui = createGui();
t = createToggle("Toggle", 50, 50);
}
Description
Creates a new GuiToggle object. This is a toggle button.
Syntax
createToggle(label, x, y, [w], [h], [defaultVal])
Parameters
labelString: label for the GuiTogglexNumber: x-coordinate of the GuiToggleyNumber: y-coordinate of the GuiTogglewNumber: width of the GuiToggle (default is 128)hNumber: height of the GuiToggle (default is 32)defaultValNumber: default value for your GuiToggle (default isfalse)
Returns
GuiToggle
Variables
val
Example
toggle.val = True;
print(toggle.val)
Description
Value of the GuiToggle.
label
toggle.label = "Toggle";
print(toggle.label)
Description
Label of the GuiToggle. Setting the label will automatically set labelOn and labelOff (see below).
labelOn
toggle.labelOn = "Toggle On";
print(toggle.labelOn)
Description
Label of the GuiToggle when pressed or "on".
labelOff
toggle.labelOff = "Toggle Off";
print(toggle.labelOff)
Description
Label of the GuiToggle when unpressed or "off".
Functions
setStyle()
Example
toggle.setStyle("fillBgOn", color(255, 0, 0));
toggle.setStyle({
fillBgOn: color("#FF0000"),
rounding: 10,
textSize: 40
});
Description
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.
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 GuiToggle 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 pixelsfillBgOffp5.Color: default background color when offfillBgOffHoverp5.Color: hover background color when offfillBgOffActivep5.Color: active background color when offfillBgOnp5.Color: default background color when onfillBgOnHoverp5.Color: hover background color when onfillBgOnActivep5.Color: active background color when onfillLabelOffp5.Color: default label color when offfillLabelOffHoverp5.Color: hover label color when offfillLabelOffActivep5.Color: active label color when offfillLabelOnp5.Color: default label color when onfillLabelOnHoverp5.Color: hover label color when onfillLabelOnActivep5.Color: active label color when onstrokeBgOffp5.Color: default stroke color when offstrokeBgOffHoverp5.Color: hover stroke color when offstrokeBgOffActivep5.Color: active stroke color when offstrokeBgOnp5.Color: default stroke color when onstrokeBgOnHoverp5.Color: hover stroke color when onstrokeBgOnActivep5.Color: active stroke color when on