Example | Constructor | Variables | Functions
Slider class.
Note: vertical slider class is virtually the same, just with a different orientation.
let gui;
// Create a variable for your Slider
let s;
function setup() {
createCanvas(400, 400);
gui = createGui();
// Create Slider.
// The last two optional arguments define the min and max (minimum and maximum) values.
// The default min and max values are 0 and 1, respectively.
s = createSlider("Slider", 50, 50, 300, 32, 100, 300);
}
function draw() {
background(220);
drawGui();
if (s.isChanged) {
// Print a message when Slider is changed
// that displays its value.
print(s.label + " = " + s.val);
}
// Use Slider's value to determine where the ellipse is drawn.
fill(255, 0, 0);
ellipse(s.val, 300, 100);
}
/// Add these lines below sketch to prevent scrolling on mobile
function touchMoved() {
// do some stuff
return false;
}
let gui;
let s;
function setup() {
createCanvas(400, 400);
gui = createGui();
s = createSlider("Slider", 50, 50);
}
Creates a new GuiSlider
object. This is a horizontal slider.
Note: For a vertical slider, instead use createSliderV()
.
createSlider(label, x, y, [w], [h], [min], [max])
label
String: label for the GuiSliderx
Number: x-coordinate of the GuiSlidery
Number: y-coordinate of the GuiSliderw
Number: width of the GuiSlider (default is 128)h
Number: height of the GuiSlider (default is 32)min
Number: lower bound of the value's range (default value is 0)max
Number: upper bound of the value's range (default value is 1)
GuiSlider
slider.val = 0.5;
if (slider.isChanged) {
print(slider.val);
}
Value of the GuiSlider
.
slider.min = -100;
Lower bound of the GuiSlider
range.
slider.max = 100;
Upper bound of the GuiSlider
range.
slider.isInteger = True;
Sets the mode of the GuiSlider
so that all values are integers.
slider.setStyle("fillBg", color(255, 0, 0));
slider.setStyle({
fillBg: color("#FF0000"),
rounding: 10,
trackWidth: 0.1
});
Individual GuiSlider
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 GuiSlider
style properties are:
strokeWeight
Number: the weight (in pixels) of the strokerounding
Number: radius of cornerstrackWidth
Number: width of all slider tracks between 0 (line) and 1 (full)fillBg
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