# How do I use the X and Y values from an object from touches in P5js?

**URL:** https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766
**Category:** Coding Questions
**Created:** [August 17, 2021, 6:42pm UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766 "2021-08-17T18:42:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nothingness](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nothingness](https://discourse.processing.org/u/nothingness)
#### Post date: [August 17, 2021, 6:42pm UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766/1 "2021-08-17T18:42:51Z")

</div>

This is my first week playing with P5js and Processing so **be gentle.**

I’ve got the following P5js code where I’m looking at touch values. By pressing multi-points on a touch screen brings up more circles anchored to a central position.

What I’d like to know is how do I call the x value for object 1 that’s printed in the console? or y value for object 0? how would i state them in the code?

What I’d like to do is use these values to change the dimensions of a shape in the middle of the screen, like something in object 0 driving the height of the shape or object 1 on the the x or y values driving strokeWeight or colour . However I’m in my first week and completely lost to how to use these values in the code.

```auto

function setup() {
  createCanvas(windowWidth, windowHeight);
  background(200);

}

function draw() {
  background(255);

  fill(255, 0, 0);
  strokeWeight(0)

  for (var i = 0; i < touches.length; i++) {
    fill(255);
   strokeWeight(3) 
    ellipse(touches[i].x, touches[i].y, 50, 50);
     line(touches[i].x, touches[i].y, windowWidth/2,windowHeight/2);

 
     
     fill(255);
    ellipse(windowWidth/2,windowHeight/2, 10, 10); 
    print (touches);
    
    }
             }     
              

// do this prevent default touch interaction
function mousePressed() {
  return false;
}

document.addEventListener('gesturestart', function(e) {
  e.preventDefault();
});

```

 ![Screenshot_20210817-185452_Chrome](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fbdbf3bf8bc418c3ffcd24d18552ba2db8093dde.jpeg)

[or Sketch file is here](https://editor.p5js.org/joshuaebutters/sketches/-b-tahz9r)]

**any help would be muchly muchly appreciated, i’m a newb trying to find my way**

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [August 17, 2021, 6:52pm UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766/2 "2021-08-17T18:52:34Z")

</div>

Hi! Welcome to the forum!

Don’t worry, people here are gentle 🙂 But I want to know if the code is something you wrote or you found somewhere. Because if you wrote it by yourself, perhaps you already know the answer 🙂

Basically, the 0th object is `touches[0]` and the 1st one is `touches[1]`. And `.x` and `.y` can access x and y values. It may be tricky that you need to make sure `touches` have at least 2 elements; otherwise you will get an error accessing an element.

If you feel lost - I suggest following [Dan’s tutorials](https://thecodingtrain.com/beginners/p5js/). I understand you want to achieve what you want, but there is no shortcut especially if you just started.

---

<div class="post-metadata">

### Author: ![nothingness](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nothingness](https://discourse.processing.org/u/nothingness)
#### Post date: [August 17, 2021, 7:33pm UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766/3 "2021-08-17T19:33:42Z")

</div>

Thankyou so much for the reply! It’s great to have another set of eyes on this…

Whenever i use `touch[1].x` for example to drive the size of the shape, it comes up with an error in the console saying ‘TypeError: Cannot read property of ‘x’ of undefined’. Any idea of why this happens?

Thankyou again for your time!

FYI - i’ve mixed projects together to form this so I haven’t directly wrtitten it but trying stuff out!

---

<div class="post-metadata">

### Author: ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)
#### Post date: [August 18, 2021, 12:05am UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766/4 "2021-08-18T00:05:39Z")

</div>

If you’re going to reference `touches[1]` you need to make sure that `touch.length` is greater than or equal to 2 before doing so, or that `touches[1]` is not `undefined`:

```auto
if (touches.length >= 2) {
  print(touches[1].x);
}

```

Also if you’re going to cross post here and on [stack overflow](https://stackoverflow.com/questions/68822093/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js) please mention that in both places.

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [August 18, 2021, 12:19am UTC](https://discourse.processing.org/t/how-do-i-use-the-x-and-y-values-from-an-object-from-touches-in-p5js/31766/5 "2021-08-18T00:19:09Z")

</div>

> [@nothingness](#):
>
> Whenever i use `touch[1].x` for example to drive the size of the shape, it comes up with an error in the console saying ‘TypeError: Cannot read property of ‘x’ of undefined’.

Make sure to use `touches`, not `touch`.

Also note that, as @micuat and @KumuPaul suggested, you need to check `touches.length` to make sure its value is greater than `1` before you try to access `touches[1]`. Otherwise, you would be trying to access an element in `touches` that does not exist.

Are you familiar with the [`if-else`](https://p5js.org/reference/#/p5/if-else) topic in the [p5.js Reference](https://p5js.org/reference/)?

Check that out, then try this code as an example of using `touches` along with an `if` statement to draw a line, provided that there are at least two points:

```auto
function setup() {
  createCanvas(windowWidth, windowHeight);
  background(200);
}

function draw() {
  background(255);
  strokeWeight(4);
  
  // check touches.length; draw line if there are at least 2 points
  if (touches.length >= 2) {
    line(touches[0].x, touches[0].y, touches[1].x, touches[1].y);
  }
  
}

// do this to prevent default touch interaction
function mousePressed() {
  return false;
}

document.addEventListener("gesturestart", function (e) {
  e.preventDefault();
});

```

As it runs, touch the screen in two places to see a line connecting the two points.

Please ask questions if you do not fully understand the example. After that, you can try to write a script that uses the two points to draw a rectangle instead of a line. That is a little more challenging. The following, from the p5.js Reference, may help:

- [`rect()`](https://p5js.org/reference/#/p5/rect)
- [`min()`](https://p5js.org/reference/#/p5/min)
- [`max()`](https://p5js.org/reference/#/p5/max)

_EDITED on August 17, 2021 to simplify the example code._
