如何添加单击并拖动来缩放 SVG 中的图像?

发布于 2024-12-03 01:33:19 字数 427 浏览 1 评论 0原文

我想让访问者通过单击并拖动来缩放图像:

<html>
    <body>
        <svg>
            <image>

是否有 JavaScript 库可以帮助我?

方法,但我仍然需要编写大量 JS 来:

  1. 我注意到 Raphael 有点击和拖动 图像的角落
  2. 使角落附近的区域可选择
  3. 为角落绘制图标,以便很明显它们是可选择的
  4. 添加单击、拖放至角落
  5. 更新图像的尺寸

我可以做到所有这些,但听起来很耗时。我应该使用开源脚本吗?其他建议?

I want to let visitors scale the image using click-and-drag:

<html>
    <body>
        <svg>
            <image>

Is there a JavaScript library that will help me?

I notice Raphael has click and drag methods but I will still have to write a lot of JS to:

  1. Find the corners of the image
  2. Make the area near the corners selectable
  3. Draw icons for the corners so it's obvious they are selectable
  4. Add click, drag and drop to the corners
  5. Update the dimensions of the image

I could do all that but it sounds time-consuming. Is there an open-source script I should use? Other suggestions?

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

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

发布评论

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

评论(3

千柳 2024-12-10 01:33:19

我在这篇文章中找到了这个解决方案:
SVG 中的可拖动和可调整大小

在答案中,您可以找到一个jsfiddle链接:http://jsfiddle.net/tmkfs/。您可以通过单击并拖动框的一角来调整框的大小。希望这段代码可以帮助你。

I found this solution at this post:
Draggables and Resizables in SVG.

In the answer, you can find a jsfiddle link: http://jsfiddle.net/tmkfs/. You can resize the box by clicking and dragging the corner of the box. Hope this code could help you.

清秋悲枫 2024-12-10 01:33:19

据我所知没有。我需要做同样的事情,但很惊讶我还没有找到插件,甚至还没有找到如何使用 Rahpeal.js 执行此操作的示例。因为这似乎是很多人想要用 SVG 或 Rapheal.js 做的事情,我很惊讶没有内置的方法来做到这一点(我本以为缩放 + 拖动可以做到这一点,但事实并非如此)就像看起来一样简单)。

我需要这个功能,所以我自己构建了它。我遇到了一个小障碍(此处记录:Raphael.js 拖动缩放会导致奇怪的跳跃行为),但刚刚开始工作。完成后,如果您愿意,我可以在 jsfiddle 或 github 上发布我的解决方案。

Not that I know of. I need to do the same thing and am surprised I haven't found a plugin or even an example of how to do this with Rahpeal.js yet. Since this seems like something a lot of people would want to do with SVG or Rapheal.js I'm suprised there isn't a built in way to do it (I would have thought scale + drag would do this but it wasn't as easy as it would have seemed).

I need this functionality so I've been building it out myself. I ran into a little snag (documented here: Raphael.js drag with scale causes weird jumping behavior) but just got it working. Once I'm finished, I can post my solution in a jsfiddle or on github when I'm done if you would like.

就是爱搞怪 2024-12-10 01:33:19

你应该看看 SvgKit 它提供了很好的操作库 - 生成 svg,包括使用 javascript 进行事件处理。

来自官方文档:

添加了 SVGKit 对象以进行事件处理。在 SVG 中可以是
从鼠标坐标到本地坐标很棘手。 SVG事件
具有启用可拖动项目、可旋转项目以及
防止平移和缩放项目。

事件演示在这里

You should have a look to SvgKit It provides nice library to manipulate - generate svg's including event handling using javascript.

From official documentation:

Additions to the SVGKit object for event handling. In SVG it can be
tricky to get from mouse coordinates to local coordinates. SVGEvent
has routines to enable draggable items, rotatable items, and to
prevent pan and zoom on items.

Event demos is here.

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