Skip to content

Instantly share code, notes, and snippets.

@denkspuren
Created November 12, 2014 13:15
Show Gist options
  • Save denkspuren/8431cc9e7269d1b5bfa5 to your computer and use it in GitHub Desktop.
Save denkspuren/8431cc9e7269d1b5bfa5 to your computer and use it in GitHub Desktop.
SliderControl in Processing
color THMGreen = color(128,186,36);
color THMGrey = color(74,92,102);
final float R = 10; // `final` declares R as a constant
class XSlider {
float x_min, x_max; // Slider range to capture mouse
float y_min, y_max; // Slider range to capture mouse
float x, y; // Position of slider
XSlider(float x_min, float x_max) {
this.x_min = x_min;
this.x_max = x_max;
y_min = 20-R;
y_max = 20+R;
x = x_min;
y = (y_min + y_max)/2;
}
void display() {
mouse();
stroke(0);
line(x_min,y,x_max,y);
noStroke();
ellipse(x,y,2*R,2*R);
}
void mouse() {
if (mouseX >= x_min && mouseX <= x_max &&
mouseY >= y_min && mouseY <= y_max) {
x = mouseX;
}
}
float getValue(float min, float max) {
return map(x,x_min,x_max,min,max);
}
}
XSlider xslider = new XSlider(50,350);
void showFunction(float phase) {
float y;
for(int x=0; x<width; x++) {
// y = sin(x+phase); // that's the idea
y = sin(map(x,0,width-1,0,TWO_PI)+phase);
stroke(0);
point(x,height/2-y*100);
}
}
void setup() {
size(400,300);
}
void draw() {
background(THMGreen);
fill(THMGrey);
xslider.display();
// Awesome: xslider doesn't know what it is used for!
showFunction(xslider.getValue(0,TWO_PI));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment