How to change scale of ellipse using mouseDragged

etcetc
edited October 2014 in Questions about Code

Hello!

I'm totally new in the forum and am a bit petrified of asking my question in the wrong place and pissing everyone off, so if I should be posting elsewhere, please let me know. I also did my best to check if my question has already been addressed but I've found nothing.

I'm a bit new to the interaction side of Processing. Currently, my code does the following: with each click of the mouse, (using mousePressed();) ellipses are drawn in various different colors.

What I'm having trouble with is adding code that will allow for the mouseDragged(); command to control how big/small/oval/round each ellipse will be. I've tried using scale();, but no changes appear _at all _ when I run the code.

Here is my code so far (without scale(); since it wasn't effecting any changes) What should I be including in my mouseDragged() command? Any hints or tips would be so appreciated. I want to keep my code as simple as possible. :

//global varialbes
float x = 20;
int value = 0;

//setup
void setup() {
 size(800, 800);
 background(255,255,255); 
}

//draw
void draw() {
}

// Whenever a user clicks the mouse, the code written inside keyPressed() is executed.
void mousePressed(){
 noFill();
 stroke(random(255), random(255), random(255), random(255));
 strokeWeight(x-15);
 color(40, 50, 100);
 ellipseMode(CENTER);
 ellipse(mouseX,mouseY,100,100);  
}

// Whenever a user drags the mouse the code written inside keyPressed() is executed.
void mouseDragged(){

}

Answers

  • _vk_vk
    edited October 2014 Answer ✓

    draw() is a loop that, by default, goes continuously at 60fps.

    mousePressed() and mouseDragged() are "listening" to mouse. And are called every time mouse is pressed or dragged respectvily.

    So things drawn in those functions will only exist during the event. If you need something to be draw continuously it should be done inside draw() function. Read the reference on draw() setup() and those functions.

    So usually you would draw in draw(), and have this draw being affected by a variable modified in mouseWhatever().

    Those can be booleans to act like flags:

    global ->

    boolean black;
    

    in draw ->

        if(black){
        fill(0);
        }else{
        fill(255);
        }
      //draw here
    

    and in mousePressed() ->

     black = !black; // will toogle the value...
    

    or just number vars

    global ->

    float sz = 10;
    

    in draw ->

    ellipse(100, 100, sz, sz);
    

    in mouseDragged ->

    sz++; //will increment sz
    

    The same principle to use scale(), call it with a var in draw, modify the var in mouse function.

  • Answer ✓

    If future when posting code on to the forum make sure there is a blank line before and after it then select all the code and press Ctrl+K to format it for the forum. In this case I have done it for you.

    Also I have moved the topic to Questions about Code.

    And before you say anything I am not pissed off :D

  • Thanks _vk!!! I understand exactly what you're saying :)) Thanks for putting it so simply!

    And thank you quark for not being pissed off ;) I appreciate it haha

Sign In or Register to comment.