# Offscreen canvas and multithreading

**URL:** <https://forum.babylonjs.com/t/offscreen-canvas-and-multithreading/8873>\
**Category:** Questions\
**Created:** [February 28, 2020, 9:17pm UTC](https://forum.babylonjs.com/t/offscreen-canvas-and-multithreading/8873 "2020-02-28T21:17:17Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![semirel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/semirel/32/5871_2.png) [@semirel](https://forum.babylonjs.com/u/semirel)\
**Post date:** [February 29, 2020, 9:46pm UTC](https://forum.babylonjs.com/t/offscreen-canvas-and-multithreading/8873/12 "2020-02-29T21:46:26Z")

</div>

I saw significant improvements to the overall runtime (5-8 times faster) with using function `getClosestFacetAtCoordinates` instead of my own function. For each position in the discretized grid, I find the “anchor”, i.e. a point on the mesh from which the connector line emanates, by getting the closest point on the mesh. I stumbled upon FacetData and enabled it on the meshes. I encountered an issue with it (found in post: [getClosestFacetAtCoordinates returning null most of the time](https://forum.babylonjs.com/t/getclosestfacetatcoordinates-returning-null-most-of-the-time/8891)), but this looks very promising.

Including the previous function:

```auto
/**
 * Finds the point that is closest to the part's mesh. 
 * @param {Vector3} point
 * @return {Vector3} Closest point on the Part Mesh
 */
closestPointOnPartMeshTo(point) {
    var positions = this._partMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
    var minSquaredDistance = Math.pow(10, 9);
    var closestVertex = BABYLON.Vector3.Zero;
    var numberOfVertices = positions.length / 3;	

    for (var i = 0; i < numberOfVertices; i = i + 1) {
        var vertPosition = new BABYLON.Vector3(positions[i*3], positions[i*3+1], positions[i*3+2]);
        var dsq = BABYLON.Vector3.DistanceSquared(point, vertPosition);

        if (minSquaredDistance > dsq) {
            minSquaredDistance = dsq;
            closestVertex = vertPosition;
        }
    }    
    return closestVertex;
}

```

---

_[View the full topic](https://forum.babylonjs.com/t/offscreen-canvas-and-multithreading/8873)._
