I would like to take a step back, and have a look at the problem from a little distance.
In a Mercator projection, you have an imaginary point light source at the center of the globe.
You wrap someting like a flexible cylindrical screen around the globe, lets assume it touches the equator. This results in something like the following:
You see a cross-section of the globe, north and south pole shown, equator displayed as a fat blue line.
I’ve only shown one half of the problem, because it’s perfectly symmetrical WRT the globe’s N-S axis.
Note how a perfectly regular angular subdivision of the latitudes (here: once every 15°) translates to a hugely distorted image on the projection screen - the vertical magenta line. (This is the “tangent behaviour” I was speaking of).That’s the hallmark of the Mercator projection, btw… Which the British liked so much, because it subtly supported their claim of supremacy over the Indian sub-continent, because it made their tiny island appear much more significant, in comparison to the huge Indian landmass. I’m digressing …
It’s also important to note, that a Mercator projected map inherently can cover only a limited latitude range , because going all the way from north pole to south pole would require an infinite-height (tan(90°)) map, which is mathematically not possible. The example I’ve shown covers the range from 75° southern latitude up to 75° northern latitude.
Whatever the range: each map covers mapping coordinates ranging from (0,0) at their lower-left corner to (1, 1) at their upper-right corner, so they are normalized to whatever physical dimensions [pix] the bitmap may have.
The process of mapping is the inverse of projection.
WRT the top illustration, you’ll have to invert the direction of the arrows. Which will inevitably involve an Math.atan() behaviour.
What you’ll need to know:
what is the latitude range covered by your Mercator projected map? The map can’t possibly provide information beyond its range of coverage.
Points on the lowest covered latitude will have a “v”-component of u-v-mapping coordinates of 0.0.
Points on the highest covered latitude will have a “v”-component of u-v-mapping coordinates of 1.0.
Points at intermediate latitudes will have intermediate mapping coordinates, derived from the Math.atan() function.
For the “u”-value of mapping coordinates keep in mind, that the flat 2D Mercator-projected map has been unrolled from a seamless cylinder enclosing the globe and touching it at the equator. So the width of the unrolled map covers the range of 2 * Math.PI
The “u”-values of the u-v-mapping coordinates correspond to their fraction of a full circle. In your imagination, don’t go 180° east or west, but rather from 0° to 360° in a suitable direction.
Example:
Points on the 90° (East) longitude would all have a “u”-value of 0.25 (one fourth of the full circumference).