使用 javascript/jQuery 查找距离鼠标位置最近的网格坐标

发布于 2024-08-30 03:09:42 字数 510 浏览 5 评论 0原文

我想做的是在页面上等距地创建一个不可见坐标网格。然后,我希望在触发 onclick 时将

放置在最接近指针的网格坐标处。大概的想法是:

替代文字

我对鼠标坐标的跟踪和

的放置效果很好。我所困扰的是如何解决坐标网格的问题。

首先,我是否应该将所有坐标放在一个数组中,然后将其与 onclick 坐标进行比较?

或者看到我的网格坐标遵循规则,我可以做一些事情,例如找出哪个坐标是无论我的间距的倍数最接近onclick坐标吗?

然后,我从哪里开始计算哪个网格点坐标最接近?最好的方法是什么?

谢谢!

What I'm trying to do is make a grid of invisible coordinates on the page equally spaced. I then want a <div> to be placed at whatever grid coordinate is closest to the pointer when onclick is triggered. Here's the rough idea:

alt text

I have the tracking of the mouse coordinates and the placing of the <div> worked out fine. What I'm stuck with is how to approach the problem of the grid of coordinates.

First of all, should I have all my coordinates in an array which I then compare my onclick coordinate to?

Or seeing as my grid coordinates follow a rule, could I do something like finding out which coordinate that is a multiple of whatever my spacing is is closest to the onclick coordinate?

And then, where do I start with working out which grid point coordinate is closest? What's the best way of going about it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为人所爱 2024-09-06 03:09:42

在计算哪个网格点最接近时 - 例如,每个块都是 10x10 像素 - 要获得网格索引,您只需将它们分开 -

  1. 单击 [ 237 ; 112]
  2. 10x10 网格索引块
  3. = [ 237/10 ; 112/10]=[23.7; 11.2 ]
  4. 将它们四舍五入以获得“最接近的”
  5. 块索引为 24;11

如果您需要存储数据,您可以在单击时将网格坐标推送到数组,以供稍后参考。

In terms of working out which grid point is closest - say, for example, each block is 10x10 pixels - to get the grid index you would just divide them out -

  1. Click at [ 237 ; 112 ]
  2. Blocks of 10x10
  3. Grid index = [ 237/10 ; 112/10 ] = [ 23.7 ; 11.2 ]
  4. Round them to get the "closest"
  5. Block indices are 24;11

If you need to store the data, you could push the grid co-ordinates to an array on click, to reference later.

那一片橙海, 2024-09-06 03:09:42

我可以做一些事情,比如找出哪个坐标是我的间距的倍数,最接近 onclick 坐标吗?

当然。网格的全部要点在于它可以通过简单的算术进行计算,而不是必须围绕一大堆任意点进行计算。

我从哪里开始计算哪个网格点坐标最接近?

这是一个简单的除法,每个轴都进行舍入。

#canvas { position: relative; width: 100px; height: 100px; border: solid red 1px; }
#nearest { position: absolute; width: 10px; height: 10px; background: yellow; }

<div id="canvas"><div id="nearest"></div></div>

var gridspacing= 10;
$('#canvas').mousemove(function(event) {
    var pos= $(this).offset();
    var gridx= Math.round((event.pageX-pos.left)/gridspacing);
    var gridy= Math.round((event.pageY-pos.top)/gridspacing);
    $('#nearest').css('left', (gridx-0.5)*gridspacing+'px').css('top', (gridy-0.5)*gridspacing+'px');
});

could I do something like finding out which coordinate that is a multiple of whatever my spacing is is closest to the onclick coordinate?

Sure. The whole point of a grid it's it's calculable with simple arithmetic, rather than having to cart around a big array of arbitrary points.

where do I start with working out which grid point coordinate is closest?

It's a simple division with rounding for each axis.

#canvas { position: relative; width: 100px; height: 100px; border: solid red 1px; }
#nearest { position: absolute; width: 10px; height: 10px; background: yellow; }

<div id="canvas"><div id="nearest"></div></div>

var gridspacing= 10;
$('#canvas').mousemove(function(event) {
    var pos= $(this).offset();
    var gridx= Math.round((event.pageX-pos.left)/gridspacing);
    var gridy= Math.round((event.pageY-pos.top)/gridspacing);
    $('#nearest').css('left', (gridx-0.5)*gridspacing+'px').css('top', (gridy-0.5)*gridspacing+'px');
});
够钟 2024-09-06 03:09:42

我最初写的是与鲍宾斯类似的答案,但他比我先到达那里。我喜欢这种方式,但他的版本有一些楼层(尽管它仍然是一个非常好的答案)。

我认为您想要的是一个无 HTML 的网格(即没有像表格这样的标记),bobince 提供了一个解决方案。在这种情况下,代码可能会针对跨浏览器兼容性、可读性、错误和速度进行显着优化。

所以,我建议代码应该更像这样:

#canvas { position: relative; width: 100px; height: 100px; border: solid red 1px; }
#nearest { position: absolute; width: 10px; height: 10px; background: yellow; }

<div id="canvas"><div id="nearest"></div></div>

var
    canvasOffset = $("div#canvas").offset(),
    // Assuming that the space between the points is 10 pixels. Correct this if necessary.
    cellSpacing = 10;

$("div#canvas").mousemove(function(event) {
    event = event || window.event;
    $("div#nearest").css({
        top: Math.round((mouseCoordinate(event, "X") - canvasOffset.left) / cellSpacing) * cellSpacing + "px",
        left: Math.round((mouseCoordinate(event, "Y") - canvasOffset.top) / cellSpacing) * cellSpacing + "px"
    });
});

// Returns the one half of the current mouse coordinates relative to the browser window.
// Assumes the axis parameter to be uppercase: Either "X" or "Y".
function mouseCoordinate(event, axis) {
    var property = (axis == "X") ? "scrollLeft" : "scrollTop";
    if (event.pageX) {
        return event["page"+axis];
    } else {
        return event["client"+axis] + (document.documentElement[property] ? document.documentElement[property] : document.body[property]);;
    }
};

mouseCooperative() 函数是这两个函数的简化版本:

function mouseAxisX(event) {
    if (event.pageX) {
        return event.pageX;
    } else if (event.clientX) {
        return event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
};

function mouseAxisY(event) {
    if (event.pageY) {
        return event.pageY;
    } else if (event.clientY) {
        return event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
};

我真的很喜欢你项目的想法,也许我会自己做一些类似的东西:D

I was initially writing an answer similar to bobince's, but he got there before me. I like that way of doing it, but his version has got some floors (though it's still a very good answer).

I presume that what you want is a HTML-less grid (that is, without markup like a table), which bobince supplies a solution for. In that case, the code may be optimised significantly for cross browser compatibility, readability, errors and speed.

So, I suggest the code should be more like this:

#canvas { position: relative; width: 100px; height: 100px; border: solid red 1px; }
#nearest { position: absolute; width: 10px; height: 10px; background: yellow; }

<div id="canvas"><div id="nearest"></div></div>

var
    canvasOffset = $("div#canvas").offset(),
    // Assuming that the space between the points is 10 pixels. Correct this if necessary.
    cellSpacing = 10;

$("div#canvas").mousemove(function(event) {
    event = event || window.event;
    $("div#nearest").css({
        top: Math.round((mouseCoordinate(event, "X") - canvasOffset.left) / cellSpacing) * cellSpacing + "px",
        left: Math.round((mouseCoordinate(event, "Y") - canvasOffset.top) / cellSpacing) * cellSpacing + "px"
    });
});

// Returns the one half of the current mouse coordinates relative to the browser window.
// Assumes the axis parameter to be uppercase: Either "X" or "Y".
function mouseCoordinate(event, axis) {
    var property = (axis == "X") ? "scrollLeft" : "scrollTop";
    if (event.pageX) {
        return event["page"+axis];
    } else {
        return event["client"+axis] + (document.documentElement[property] ? document.documentElement[property] : document.body[property]);;
    }
};

The mouseCoordinate() function is a boiled down version of these two functions:

function mouseAxisX(event) {
    if (event.pageX) {
        return event.pageX;
    } else if (event.clientX) {
        return event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
};

function mouseAxisY(event) {
    if (event.pageY) {
        return event.pageY;
    } else if (event.clientY) {
        return event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
};

I really like the idea of your project, perhaps I'll make something similar myself :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文