jquery - 使用 jQuery 修改 Raphael SVG 图像

标签 jquery svg raphael

我有一个小型应用程序,需要为弧生成文本路径标签。我通过拉斐尔绘制弧线,效果很好。然而 Raphael 不支持 textPaths。我们可以通过 jQuery 将它们添加到 svg 元素,但由于某种原因它们不会渲染。当我静态复制动态生成的代码时,它呈现得很好。

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="600">
  <desc>Created with Raphaël</desc><defs></defs>
  <a title="This is a test entry">
    <path fill="none" stroke="#1e79a0" d="M395.2627944162883,355A110,110,0,0,1,199.5099996593139,344.74103073833805" style="stroke-width: 20px; " stroke-width="20" id="This_is_a_test_entry"></path>
  </a>
  <text>
    <textpath xlink:href="#This_is_a_test_entry">This is a test entry</textpath>
  </text>
</svg>

使用上面的代码,您永远不会看到弧的标签。但是,当以下内容被硬编码时,它会按预期呈现:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="600">
  <a title="Bacon">
    <path id="Arc" fill="none" stroke="#1e79a0" d="M395.2627944162883,355A110,110,0,0,1,201.13265490709162,348.2208261467985" style="stroke-width: 20;" stroke-width="20">
    </path>
  </a>
  <text>
    <textPath xlink:href="#Arc">This is an Arc</textPath>
  </text>
</svg>

任何关于为什么我看不到我的整个 svg 的见解将不胜感激。

编辑:

我已经对我的 javascript 进行了更改,我认为它已经差不多完成了,但 textPath 仍然无法呈现。这是当前的代码,主要感谢 Femi:

function drawRange(start, end, R, range, id, title) {
                    var color = "hsb(".concat(Math.round(R) / 200, ",", end / 360, ", .75)");
                    var key_position = key[id];
                    var svg_bits = document.getElementsByTagName('svg')[0].childNodes;
                    var svgns = document.createElementNS('http://www.w3.org/2000/svg', "path");
                    var path;
                    range.attr({title: title});
                    range.animate({arc: [start, end, R]}, 900, ">");
                    //Add an id to the path element that was just drawn.  This doesn't seem to help though.
                    for(var i = 0; i < svg_bits.length; i++) {
                        if (svg_bits[i].tagName == "a" && svg_bits[i].getAttribute('title') == title){
                            path = svg_bits[i].childNodes[0];
                        }
                    }
                    path.setAttribute('id', title);
                    //Add the name to the key, set the color and unhide the element.
                    $(key_position).html(range.attr("title"));
                    $(key_position).css('color', Raphael.getRGB(color).hex);
                    $(key_position).show();
                };



function makeSVG(tag, attrs) {
                    var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
                    for (var k in attrs)
                        el.setAttribute(k, attrs[k]);
                    return el;
                };

function addLabel(label) {
                    var text = makeSVG("text", {});
                    var title = makeSVG("textPath", {"xlink:href":'#' + label.replace(/\s/g, '_')});
                    title.appendChild(document.createTextNode(label));
                    text.appendChild(title);
                    r.canvas.appendChild(text);
                };

我得到了 textPath 的边界框 0x0,但没有 textPath。

最佳答案

您可能需要删除第一个示例中出现的 xlink,或者添加适当的 XML 命名空间定义。

编辑:当您添加 textPath 元素时,您可能需要使用正确的元素名称(textPath,而不是 textpath)。

编辑:相当有趣,因为它是浏览器和 jQuery 改变路径的组合。要获得正确的东西,请使用此函数(从 jquery's append not working with svg element? 的答案中解放出来):

function makeSVG(tag, attrs){
   var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
   for (var k in attrs)
     el.setAttribute(k, attrs[k]);
   return el;
 }

然后执行以下操作:

var paper = Raphael("notepad", 320, 200);

// do all your other stuff here
...

var text = makeSVG("text", {});
       var c = makeSVG("textPath", {"xlink:href":"#Arc"});
       c.appendChild(document.createTextNode("This is an Arc"));
       text.appendChild(c);
paper.canvas.appendChild(text);

这将为您提供正确的 SVG:但是,它仍然无法正确绘制,这很奇怪。

关于jquery - 使用 jQuery 修改 Raphael SVG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6670968/

相关文章:

javascript - d3 不会将命名空间属性附加到 svg 元素

javascript - 如何缩短这个函数(addClass,attr)

javascript - 不透明度之间的延迟 : 0 and starting animation to opacity: 1 in Raphael JS

jquery - 什么时候通过 AJAX 提交表单比较好,什么时候通过服务器端提交比较好?

javascript - Ajax 加载新按钮,我需要加载新选择器吗?

javascript - 在 localStorage 中存储和检索动态填充的下拉列表

javascript - 如何不同时切换所有菜单

javascript - 使用 D3 和 d3.slider 显示 SVG 元素 : Uncaught ReferenceError: svg is not defined

html - 将 svg 文件添加到选项标签中

html - 在 RaphaelJS 中自动将文本换行到一定宽度?