# Position a mesh to follow DOM element's position

**URL:** <https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127>\
**Category:** Questions\
**Tags:** mesh, vector3, position\
**Created:** [September 14, 2023, 2:15pm UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127 "2023-09-14T14:15:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antony](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/antony/32/34501_2.png) [@Antony](https://forum.babylonjs.com/u/Antony)\
**Post date:** [September 14, 2023, 2:15pm UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127/1 "2023-09-14T14:15:35Z")

</div>

Hello everyone, I’m trying to write a program to fit a mesh into a DOM element’s bounds. I firstly use the orthographic camera mode so its perspective is closer to a 2D one.

I searched the calculation of how to calculate bounds from 3D space to 2D pixels, so I make use of this to work out how to calculate the mesh position, like this:

- Find current mesh bounds in pixels
- Find current mesh bounds in vector3
- Find out the x/y differences in pixels the mesh need to move
- New mesh position x = current mesh position x + x difference \* (mesh width in vector3) / (mesh width in vector3)

Here is the playground link:

[Place mesh within bounds of DOM element | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#FR87YF#21)

I can get the x position calculated correctly, but for y, it seems to be negative so I negate it, but the result is still incorrect. Can anyone advice on how to correctly calculate the new position y of the mesh? Thank you very much!

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [September 15, 2023, 12:38am UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127/2 "2023-09-15T00:38:48Z")

</div>

Welcome to the forums!

Hmm, I’m not sure how easy this will be. Maybe @Evgeni_Popov has some ideas?

---

<div class="post-metadata">

**Author:** ![Antony](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/antony/32/34501_2.png) [@Antony](https://forum.babylonjs.com/u/Antony)\
**Post date:** [September 15, 2023, 1:36am UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127/3 "2023-09-15T01:36:47Z")

</div>

Thanks Gary! I managed to find out this solution:

> **[Babylon.js Playground](https://playground.babylonjs.com/#FR87YF%2323)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

The idea is move a sample distance in 3d space and see how much moved in 2d:

var sampleFactor = 0.1;

```
    var oldX = meshBounds.x;
    var oldY = meshBounds.y;

    sphere.position.x += sampleFactor;
    sphere.position.y += sampleFactor;

    scene.render();

    var meshBoundsAfterMovedSampleFactor = getBoundsForMesh(sphere);

    var newX = meshBoundsAfterMovedSampleFactor.x;
    var newY = meshBoundsAfterMovedSampleFactor.y;

    var xRatio = sampleFactor / (newX - oldX);
    var yRatio = sampleFactor / (newY - oldY);

    sphere.position.x += xDiff * xRatio;
    sphere.position.y += yDiff * yRatio;

```

It kind of works but I also hoping for a more formal way to achieve the same goal. Thanks. 😃

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [September 15, 2023, 10:59am UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127/4 "2023-09-15T10:59:30Z")

</div>

You can do it by unprojecting the center of your rectangle and use this point as the sphere position:

> **[Babylon.js Playground](https://playground.babylonjs.com/#FR87YF%2324)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Antony](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/antony/32/34501_2.png) [@Antony](https://forum.babylonjs.com/u/Antony)\
**Post date:** [September 16, 2023, 1:15am UTC](https://forum.babylonjs.com/t/position-a-mesh-to-follow-dom-elements-position/44127/5 "2023-09-16T01:15:06Z")

</div>

Wow Evgeni, thank you very much, and so happy that I learn something new! 😍
