Slider that move with mouse over

edited August 2014 in How To...

Hi I'm new to processing and was wandering if anybody could help me out with this.

**I need to make a slider that moves when the mouse is over it. When the mouse is no longer over them they remain in their last position. The slider does not need to report any information, it is purely for visual purpose **

Here's what i've so far.

void setup() { size(640,1136); }

void draw() {

background(230);

if (mouseY<(1136/6))

{ fill(202,45,36); noStroke(); rectMode(CORNERS); rect(mouseX,(1136/6),0,0); fill(100); }

textSize(20);

fill(255);
text("Keep the slider in last position! ", 320, 1136/12); textAlign(CENTER);

}

Thank you very much for any advice

Answers

  • edited August 2014

    Thanks for any help

  • _vk_vk
    Answer ✓

    When you put all the code for drawing the slider inside the if block checking for mouse over it, it will only exist when the mouse is over it... Also you will need a var to store the position, this var should be updated when mouse is over the slider. So move the drawing outside the if block and put there the updating of var.

    something like this:

    int sliderVal;
    void setup() { 
      size(640, 1136);
    }
    
    void draw() {
    
      background(230);
    
      if (mouseY<(1136/6)) {
        sliderVal = mouseX;
      } 
      fill(202, 45, 36); 
      noStroke(); 
      rectMode(CORNERS); 
      rect(sliderVal, (1136/6), 0, 0); 
      fill(100); 
    
      textSize(20);
    
      fill(255);
      text("Keep the slider in last position! ", 320, 1136/12); 
      textAlign(CENTER);
    }
    
  • thank you so much, that makes perfect sense, a really good explanation.

    Thanks again!!!!

  • _vk_vk
    Answer ✓

    My pleasure. Some side notes: soon you will see that using "hard numbers" are not a good choice, I mean, for instance, instead of 1136/6 you would rather use a var to store that value, and test against it int sliderY = 1136/6; if ( mouseY < sliderY){//blah;} so if you need to change those numbers later it will be really easy. Also try to choose one:

    if(blah){
    //stuff
    }
    

    or

    if (blah)
    {
    //stuff
    } 
    

    avoid using both in same code, as it gets harder to read.

  • _ vk, could you explain to me how to make a scrolling background? I have a question out there. But the answers I am getting kind of confused me. It's the one that asks about a scrolling background. Could you also do it with a 25 width by 50 height rectangle? Thanks :)

  • Thanks for the extra tips _VK

  • _vk_vk
    edited September 2014

    @TechWiz777 I think my best advice for you is: learn how to learn and get help, or hire someone to make a software to you.

    What is your point? Are you willing to learn code? If so, get all the advices you got from here put them together, understand them, and come back for more, with specific doubts when needed. Then you will be learning.

    But if you just need the code done for you, offer money for the job and you might get someone to hire.

    Since the confused cousin post, I don't feel that you are really trying to understand and learn from your's posts and it's answers.

    To be more clear, an example. If you ask "how to make something" and someone tells you "look at this and that examples". Don't come back saying "is this going to help me" go there read the examples, try to understand them and come back with something like: " I cant get what array[index%length] means or "why is the code using mousePressed() and not mousePressed". I'm sure people will be glad to help you then.

    :-c

Sign In or Register to comment.