# Change Image object size with mouse

**URL:** https://discourse.processing.org/t/change-image-object-size-with-mouse/27077
**Category:** Coding Questions
**Created:** [January 15, 2021, 8:18am UTC](https://discourse.processing.org/t/change-image-object-size-with-mouse/27077 "2021-01-15T08:18:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 15, 2021, 8:18am UTC](https://discourse.processing.org/t/change-image-object-size-with-mouse/27077/1 "2021-01-15T08:18:23Z")

</div>

Hi! I’m here again with the question.  
I have an image object that I would like to display firstly small, but I want that when the user clicks and holds mouse pressed on the image and than starts to move mouse, the image should become bigger or smaller(depends on the direction of mouse). What I’ve firstly tried:  
in mousePressed() function I added

```auto
if(mousePressed == true){
 img.width = stoneSize+mouseX;
 img.height = stoneSize+mouseY;}

```

but…well…I understand it is not right as I need the left down corner of the picture be at the position of mouseX,mouseY. And what do I do when the image is not square, but round?

---

<div class="post-metadata">

### Author: ![ReyzArtz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reyzartz/32/11750_2.png) [@ReyzArtz](https://discourse.processing.org/u/ReyzArtz)
#### Post date: [January 15, 2021, 9:39am UTC](https://discourse.processing.org/t/change-image-object-size-with-mouse/27077/2 "2021-01-15T09:39:52Z")

</div>

I’m assuming that when u press and move the mouse left the image should increase and vice-versa  
this might help

```auto
if(mousePressed == true){
  if((pmouseX-mouseX)>0){
    img.width = stoneSize+mouseX;
    img.height = stoneSize+mouseY;
   }
  else{
    img.width = stoneSize-mouseX;
    img.height = stoneSize-mouseY;
  }
 }

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 15, 2021, 1:02pm UTC](https://discourse.processing.org/t/change-image-object-size-with-mouse/27077/3 "2021-01-15T13:02:12Z")

</div>

this version uses a copy of the initial image every time to avoid the distortion of the image over time.

It doesn’t preserve aspect ratio.

(EDITED: new version)

```auto

// https://discourse.processing.org/t/change-image-object-size-with-mouse/27077

PImage img, imgOriginal; 

boolean drag=false; 
int startX, startY;

//------------------------------------------------------
// Two Core Funcs 

void setup() {
  size(1800, 800);

  img=loadImage("c1.png");
  img.resize(460, 0);
  imgOriginal=(PImage)img.copy();
}//func

void draw() {

  background(220);
  fill(0);
  text("Click, hold and drag mouse to change the image size.", 
    12, 12); 

  img=resizeWithMouse(imgOriginal.copy(), img); 
  image(img, 90, 90);
}//func

//------------------------------------------------------
// Input funcs

void mousePressed() {
  drag=true;
  startX=mouseX; 
  startY=mouseY;
}//func

void mouseReleased() {
  drag=false;
}//func

//-------------------------------------------------------
// Other funcs

PImage resizeWithMouse( PImage imgLocal, PImage imgLocal2) {
  int newW=0; 
  int newH=0;

  if (drag) {
    // It is drag 

    // ------
    if (mouseX>startX) {
      newW=imgLocal2.width+(mouseX-startX);
      startX=mouseX;
    } else {
      newW=imgLocal2.width-abs(mouseX-startX);
      startX=mouseX;
    }
    if (newW<=0) 
      newW=3; 

    // ------
    if (mouseY>startY) {
      newH=imgLocal2.height+(mouseY-startY);
      startY=mouseY;
    } else {
      newH=imgLocal2.height-abs(mouseY-startY);
      startY=mouseY;
    }
    if (newH<=0) 
      newH=3; 

    // --------------------------
    if (newW>0 && newH>0)
      imgLocal.resize(newW, newH);

    return imgLocal;
  }//if

  return imgLocal2;
}//func
//

```

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 16, 2021, 1:42pm UTC](https://discourse.processing.org/t/change-image-object-size-with-mouse/27077/4 "2021-01-16T13:42:55Z")

</div>

Thanks! Tried to use it in my code, wokrs good, just needs a little bit of polishing to fit in my program properly and I managed to keep ratio! I am very grateful!
