IE 在拖动时平滑地图平移

发布于 2024-08-20 02:57:04 字数 955 浏览 3 评论 0原文

我试图使我使用 javascript 构建的地图应用程序的性能变得平滑。 实现了拖动平移,

  • 我最初使用onmousedown
  • onmousemove
  • onmouseup

但是在 IE 中感觉非常缓慢,而且当您快速移动光标时,地图不会更新其位置,直到您停止移动。

我将代码转换为使用本机 IE 事件

  • ondragstart
  • ondrag
  • ondragend

使用这些事件时性能要好得多,但似乎我无法使用标准 css 属性设置鼠标光标。我只能将光标设置到一些预定义的位置,这不是我想要的。

所以我的问题是。如何使用第一组事件平滑 IE 中的拖动,或者如何使用本机事件设置自定义光标。

编辑:代码示例

代码非常非常简单。即使当我删除加载新图块的逻辑(即仅移动容器)时,它仍然感觉很笨重。下面是平移函数:

// the "this" object refers to the div containing all the tile layers.
function movemap(e) 
{ 
   e = e || window.event;
   var dx = this.mouseX - e.clientX; 
   var dy = this.mouseY - e.clientY; 

   if (dx !== 0 || dy !== 0) {
      this.style.left = parseInt(this.style.left) + dx + 'px'; 
      this.style.top = parseInt(this.style.top) + dy + 'px'; 
      this.mouseX = e.clientX; 
      this.mouseY = e.clientY;
   }
} 

I've trying to smooth out the performance of a map application i've been building using javascript. I initially implemented a dragging pan using

  • onmousedown
  • onmousemove
  • onmouseup

However in IE it feels really sluggish and it appears that when you move the cursor really fast the map doesn't update it position until you stop moving.

I converted my code to use the native IE events

  • ondragstart
  • ondrag
  • ondragend

The performance was MUCH better when using these events BUT it seems that I can't set the mouse cursor using the standard css properties. I can only set the cursor to a few predefined ones which is not what I want.

So my question is. How can I either smooth the drag in IE using the first set of events or how can I set a custom cursor using the native events.

EDIT: Code sample

The code is very very simple. Even when I remove the logic to load new tiles (i.e only the container is getting moved) it still feels clunky. Below is the pan function:

// the "this" object refers to the div containing all the tile layers.
function movemap(e) 
{ 
   e = e || window.event;
   var dx = this.mouseX - e.clientX; 
   var dy = this.mouseY - e.clientY; 

   if (dx !== 0 || dy !== 0) {
      this.style.left = parseInt(this.style.left) + dx + 'px'; 
      this.style.top = parseInt(this.style.top) + dy + 'px'; 
      this.mouseX = e.clientX; 
      this.mouseY = e.clientY;
   }
} 

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

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

发布评论

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

评论(1

林空鹿饮溪 2024-08-27 02:57:04

如果没有代码示例,真的很难说是什么导致拖动事件运行缓慢。

效果缓慢可能是由于您在事件执行中运行的一些繁重操作造成的。浏览器中的事件系统非常有趣。这是同步的。这意味着,在当前执行完成之前,不会再次触发该事件。

这意味着,您有两种可能性:1. 优化事件内部的代码,以便占用更少的 CPU 时间,或者 2. 使事件异步并实现您自己的互斥/信号量逻辑。

您可以使用 setTimeout 功能来执行第二个操作。如果您执行 setTimeout,((code),1) 您的事件将异步继续,因此将调度下一个事件,而无需等待代码完成。好吧,提前考虑一下,在这种情况下你必须开始考虑锁定和排队。我所说的排队是指将所有已分派的事件排队以供将来使用。

很久以前,由于一些繁重的代码执行,我也做了一些异步事件调度。这对我有用。也许它也适合你。

It's really difficult to say, what makes your drag events to work slow, without code examples.

Sluggish effect can be due to some heavy operations you are running inside event execution. Event system in the browser is really interesting. It's synchronous. Which means, the event will be not triggered again, until the current execution is finished.

That means, you have two possibilities: 1. optimize your code inside event, so that it takes less CPU time or 2. make your event asynchronous and implement your own mutex/semaphor logic.

The second one you can do, by using setTimeout functionality. If you do setTimeout,((code),1) your event will continue asynchronously, so the next event will be dispatched without awaiting for your code to complete. Well, take in advance that in that case you have to start thinking about locking and queuing. By queuing I mean, to queue all dispatched events for future use.

Long time ago, I did also some asynchronous event dispatching, because of some heavy code execution. It worked for me. Maybe it will work for you too.

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