# Constrain() function / mouse click on button

**URL:** <https://discourse.processing.org/t/constrain-function-mouse-click-on-button/24299>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [October 2, 2020, 11:27pm UTC](https://discourse.processing.org/t/constrain-function-mouse-click-on-button/24299 "2020-10-02T23:27:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigjoebob](https://avatars.discourse-cdn.com/v4/letter/b/82dd89/32.png) [@bigjoebob](https://discourse.processing.org/u/bigjoebob)\
**Post date:** [October 2, 2020, 11:27pm UTC](https://discourse.processing.org/t/constrain-function-mouse-click-on-button/24299/1 "2020-10-02T23:27:38Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi!! I need help in understanding how to use the constrain() function. I want to be able to click on a rectangle and have it change colors, however, I want it to change colors only when I click on the rectangle how do I do that?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 2, 2020, 11:31pm UTC](https://discourse.processing.org/t/constrain-function-mouse-click-on-button/24299/2 "2020-10-02T23:31:54Z")

</div>

> [@bigjoebob](#):
>
> … only when I click on the rectangle how do I do that?

- [Collision Detection](http://JeffreyThompson.org/collision-detection/point-rect.php)

---

<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:** [October 2, 2020, 11:32pm UTC](https://discourse.processing.org/t/constrain-function-mouse-click-on-button/24299/3 "2020-10-02T23:32:12Z")

</div>

constrain has not to do with it

```auto
void mousePressed() {

  if (mouseX >= x && 
      mouseX <= x+widthRect && 
      mouseY >= y && 
      mouseY <= y+heightRect) {
    // do something 
  } else {
    // nothing 
  }

}

```

see [https://www.processing.org/examples/button.html](https://www.processing.org/examples/button.html)
