拉斐尔累积与绝对缩放/旋转/平移?

发布于 2024-10-20 13:05:41 字数 633 浏览 8 评论 0原文

我正在尝试用 Javascript 绘制交互式地图,使用 Raphael 来完成繁重的工作。

地图背景是一个相当复杂的东西,包含网格、地图元素、标签等。在此之上,我将绘制用户实际使用的内容。由于背景很复杂,我不想每帧都重新渲染它。因此,在绘制之后,我想重用这些绘图元素,仅在用户平移、缩放等时更改背景的平移、旋转、缩放。

不幸的是,我对 Raphael 的变换基元感到相当困惑:它们的行为不正确正如我所期望的。如果我调用 scale(),缩放似乎会应用于绘图元素的原始大小;但 translate() 是累积的,因此它适用于之前的翻译。 rotate() 可以是其中之一,因为它有一个我可以设置的选项...

是否可以进行绝对翻译?也就是说,能够指定我的对象(通常是路径)的新中心的绝对坐标?如果做不到这一点,那么跟踪旧位置以便当我想将其移动到新位置时可以应用增量是一种合理的方法吗?

或者我最好只是在每一帧重新渲染整个事情? (我看到有人认为 Raphael 不擅长复杂绘图的转换,因为大部分都是用 Javascript 完成的;查看正在生成的 SVG,我发现翻译似乎已返回到路径数据中,这将证明这一点...)

(顺便说一句,FWIW 我正在使用 GWT Raphael 界面来完成这一切。)

I'm trying to draw an interactive map in Javascript, using Raphael to do the heavy lifting.

The map background is a fairly complicated thing containing a grid, the map elements, labels, etc. On top of this I'm then going to draw the stuff the user is actually working with. Because the background is complex, I don't want to have to rerender it every frame. So, after drawing it I would like to reuse those drawing elements, merely changing the translation, rotation, scaling of the background as the user pans, zooms, etc.

Unfortunately I'm rather confused by Raphael's transformation primitives: they're not behaving as I would expect. If I call scale(), the scaling appears to apply to the original size of the drawing element; but translate() is cumulative, so it applies to the previous translation. rotate() can be either, as it has an option I can set...

Is it possible to do absolute translation? That is, to be able to specify the absolute coordinates of the new center of my objects (which are usually paths)? Failing that, is keeping track of the old location so I can apply a delta when I want to move it to the new location a reasonable way of doing this?

Or would I be better off simply rerendering the whole thing every frame? (I see suggestions that Raphael isn't good at transformations of complex drawings, as most of it is done in Javascript; looking at the SVG that's being produced, I see that the translation appears to be getting backed into the path data, which would bear this out...)

(BTW, FWIW I'm using the GWT Raphael interface for all this.)

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

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

发布评论

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

评论(2

提赋 2024-10-27 13:05:41

您可以使用 Element.attr 设置绝对位置。只需更改 x 和 y 属性:

myElement.attr("x", myX);
myElement.attr("y", myY);

You can use Element.attr to set absolute positions. Just change x and y properties:

myElement.attr("x", myX);
myElement.attr("y", myY);
痴梦一场 2024-10-27 13:05:41

我已经成功使用 raphael-zpd 插件。我不确定这是否会插入 GWT - 您可以查看他们的源代码并使其适应您的用例。

I've used the raphael-zpd plugin with success. I'm not sure if that will plug into GWT - you could check out their source code and adapt it to your use case.

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