How do I change mouseX, mouseY default origin positions

hey guys, this is a very small question.
and here is my code:
void setup() {
size(600, 800);
background(20, 40, 80);
}
void draw() {
stroke(30,200,100,40);
line(mouseX,mouseY,300,30);
}
void mousePressed(){background (20,190,220);}
//void keyPressed(){background(100,20,10);}

but everytime when I refresh the canvas it appears a line from the top left to where my mouse is. I met this situation when I try to draw the squares. I mean it must be a fundamental problem.
see as my attached
Screen Shot 2022-11-19 at 1.35.56 AM

1 Like

it’s not that simple

initially mouseX,mouseY is 0,0, so the initial line appears.

you can use the if-command to avoid this initial line:

void setup() {
  size(600, 800);
  background(20, 40, 80);
}

void draw() {
  stroke(30, 200, 100, 40);
  if (mouseX>0 && mouseY>0) {   // !!!!!!!!!!!!!!!!
    line(300, 30, mouseX, mouseY );
  }
}

void mousePressed() {
  if (mouseButton==RIGHT) {
    background (20, 190, 220);
  }
}

//void keyPressed(){background(100,20,10);}


====================================================================

OR

use if(mousePressed) instead, so you need to hold the mouse to draw.

  • then you want to use the RIGHT mouse button to clear the screen (see function mousePressed()).
void setup() {
  size(600, 800);
  background(20, 40, 80);
}

void draw() {
  stroke(30, 200, 100, 40);
  if (mousePressed) {   //  !!!!!!!!!!!!!!!!!!!!!!!!!!!!
    line(300, 30, mouseX, mouseY );
  }
}

void mousePressed() {
  if (mouseButton==RIGHT) {  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    background (20, 190, 220);
  }
}

//void keyPressed(){background(100,20,10);}

4 Likes

Hello @Siyue!
This is a great question!! :slightly_smiling_face:

And I hope you don’t mind, but I altered the title of your post to make it more searchable for others with the same question!
If you feel the title does not reflect your question, please of course feel free to revise.
However, please use descriptive keywords.
:nerd_face:

4 Likes

Hi

You can just draw lines with your code
Because

line(mouseX,mouseY,300,30);

You can Use point ()

https://funprogramming.org/20-The-smallest-drawing-program-ever.html

Hello @Siyue,

Take a look here:

I used LEFT for drawing and RIGHT for clearing.

There is a tutorial on Interactivity here:
https://processing.org/tutorials

:)

This does not solve the problem, but I think is usefull to note that one can set mouseX and mouseY:


void setup() {
  size(600, 800);
  background(20, 40, 80);
  mouseX = 100;
  mouseY = 100;
}
void draw() {
  stroke(30, 200, 100, 40);
  line(mouseX, mouseY, 300, 30);
}
void mousePressed() {
  background (20, 190, 220);
}
//void keyPressed(){background(100,20,10);}
4 Likes

Hi

void setup() {
 size(1000,800);
background (0,124,255);
}

void draw() {
  
  if (mousePressed) {
    strokeWeight(6); 
    stroke(204, 102, 0);
    line(pmouseX, pmouseY, mouseX, mouseY);
  }
}

2 Likes

hey Chris
both way works. thank you !

1 Like