如何为 raphael 添加 SVGPan 支持

发布于 2024-12-08 09:46:16 字数 1234 浏览 1 评论 0原文

我正在使用一个使用 raphael 库创建 svg 内容的库。我现在想添加缩放功能。我找到了以下库:

http:// /www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/

这就是svg 部分看起来:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
//...the image

问题是该库需要围绕整个图像的 script 标签和 ag 标签。操作后它应该看起来像这样:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<script xlink:href="SVGPan.js"/>
<g id="viewport" transform="translate(200,200)">
...//the image
</g>

我需要添加脚本标签,但真正的问题是将 id="viewport" 的 ag 元素放在图像的整个其余部分周围。 我如何使用 JQuery 或计划 JavaScript 来做到这一点?

编辑:

我有点超前了。事实证明,添加脚本标签似乎不起作用:

var svg = $('svg'); 
svg.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink'); // else error in firefox with links
var scriptElement = document.createElementNS('http://www.w3.org/2000/svg','script');
scriptElement.setAttribute('xlink:href','js/SVGPan.js');
svg.prepend(scriptElement);

我没有收到错误,并且脚本完全运行,但未添加标签。我已经验证 $('svg') 选择了所需的元素,我可以用它做一些事情,除了为我不理解的任何原因添加脚本标记。

i'm using a library that creates svg content using raphael library. I would now like to add zooming functionality. I found following library:

http://www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/

This is how the svg part looks:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
//...the image

The issue is that this library expects a script tag and a g tag surrounding the whole image. It should look like this after manipulation:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<script xlink:href="SVGPan.js"/>
<g id="viewport" transform="translate(200,200)">
...//the image
</g>

I would need to add the script tag but the real problem is to put a g element with id="viewport" around the whole rest of the image.
How can i do that with JQuery or plan JavaScript?

EDIT:

I got a bit ahead of myself. It turns out that adding the script tag does not seem to work:

var svg = $('svg'); 
svg.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink'); // else error in firefox with links
var scriptElement = document.createElementNS('http://www.w3.org/2000/svg','script');
scriptElement.setAttribute('xlink:href','js/SVGPan.js');
svg.prepend(scriptElement);

I don't get an error and the script runs completely but the tag is not added. I have verified that $('svg') selects the desired element and I can do stuff with it except prepending the script tag for whatever resaon i do not understand.

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

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

发布评论

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

评论(3

欢烬 2024-12-15 09:46:16

我认为 Raphael 不允许您操作 SVG 元素,但您可以尝试类似的 Raphael 特定库。这可能会提供您正在寻找的内容:https://github.com/semiaddict/raphael-zpd

编辑:实际上可以操作 SVG 元素:Raphael 公开了 Node 属性。因此,如果您能找到拉斐尔纸质物体,您可以尝试以下操作:

paper.node.setAttribute("id", "viewport")

I don't think Raphael allows you to manipulate the SVG element but you could try a similar Raphael-specific library. This one may provide what you are looking for: https://github.com/semiaddict/raphael-zpd

Edit: Actually it is possible to manipulate SVG elements: Raphael exposes a Node property. So if you could get hold of the Raphael paper object you could try something like:

paper.node.setAttribute("id", "viewport")

2024-12-15 09:46:16

感谢 Clafou 向我指出 raphael-zpd。这引导我找到解决方案。
问题是我能够获取 raphael 对象,但 zpd 仅在尚未绘制任何内容的情况下才起作用,但事实并非如此。

我正在使用 http://www.jsphylosvg.com 它利用了 raphael。解决方案是直接编辑 javascript 文件 jsphylosvg.js:

  1. 搜索 this.svg = Raphael(sDivId, this.canvasSize[0], this.canvasSize[1]); (在我的例子中是第1217行)
  2. 添加:this.svg.ZPD({zoom:true,pan:true,drag:true});直接在该行下面
  3. 保存 jsphylosvg.js

完成。有用!
(当然你需要在你的网页中引用raphael-zpd)

Thanks Clafou for pointing me to raphael-zpd. This lead me to the solution.
The issue is that I was able to get the raphael object but zpd only works if nothing has been drawn yet which is not the case.

I'm using http://www.jsphylosvg.com which makes use of raphael. The solution is to edit the javascript file jsphylosvg.js directly:

  1. search for this.svg = Raphael(sDivId, this.canvasSize[0], this.canvasSize[1]); (line 1217 in my case)
  2. add: this.svg.ZPD({ zoom: true, pan: true, drag: true }); directly below that line
  3. save jsphylosvg.js

done. It works!
(Of course you need to have a reference to raphael-zpd in your web page)

一笑百媚生 2024-12-15 09:46:16

您是否尝试过通过 jQuery 选择器获取它?例如,您可以尝试通过执行以下操作在子 g 元素上设置 id(在您的库构建了 SVG 元素之后):

$('svg > g').attr('id', '视口');

Have you tried getting it via a jQuery selector? You could for example try setting the id on the child g element by doing this (after your library has constructed the SVG element):

$('svg > g').attr('id', 'viewport');

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