简体   繁体   中英

Image X & Y axe

i have a image (1536x1536), it's a map.

And i want to have X and Y axe on image max x: 3000.0, max y: 3000.0, min x: -3000.0, min y: -3000.0.

I want to to convert my coordinates (X,Y) in position on map (top/bottom and left/right)

From comments I assume you do not resize image by CSS but keep it in it's natural state. As such you can use a translate object with ratio difference. If this is extremely resource dependent I recommend using array over object (as to code displayed below).

It give you point to pixel translation.

var translate = {
    x: 1536 / 6000,
    y: 1536 / 6000
};

function pt_translate(pt) {
    return {
        x: Math.round((pt.x + 3000) * translate.x),
        y: Math.round((pt.y + 3000) * translate.y)
    };
}

// Sample coordinate input:
pt = {
    x: -1029.3652,
    y:  369.122
};

var ptt = pt_translate(pt);

Result:

{x: 504, y: 862}

More samples:

Various console log samples

console.log(pt_translate({x:0,y:0}), 'vs', 1536 / 2, 1536 / 2);
console.log(pt_translate({x:3000,y:3000}), 'vs', 1536, 1536);
console.log(pt_translate({x:-3000,y:-3000}), 'vs', 0,0);
console.log(pt_translate({x:910,y:-1045}), 'vs', 1000,500);
console.log(ptt);

Yields:

{x: 768, y: 768} "vs" 768 768
{x: 1536, y: 1536} "vs" 1536 1536
{x: 0, y: 0} "vs" 0 0
{x: 1000, y: 500} "vs" 1000 500
{x: 504, y: 862} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM