简体   繁体   English

javascript生成时,SVG textPath无法呈现

[英]SVG textPath not rendering when generated by JavaScript

For svg.js I wrote a little plugin adding textPath functionality. 对于svg.js,我编写了一个添加textPath功能的小插件。 The plugin is very concise: 该插件非常简洁:

// textpath plugin
SVG.TextPath = function() {
    this.constructor.call(this, SVG.create('textPath'))
}
SVG.TextPath.prototype = new SVG.Element
SVG.extend(SVG.TextPath, {
    text: function(text) {
        while (this.node.firstChild) this.node.removeChild(this.node.firstChild)
        this.node.appendChild(document.createTextNode(text))
        return this
    }
})
SVG.extend(SVG.Text, {
    path: function(d){
        var textPath = new SVG.TextPath().text(this.content)

        while (this.node.firstChild) this.node.removeChild(this.node.firstChild)

        this.track = this.doc().defs().path(d)
        this.node.appendChild(textPath.node)
        textPath.attr('xlink:href', '#' + this.track)

        return this
    }
})

To create the same output as this example on MDN , the plugin can be used as follows: 在MDN上创建与此示例相同的输出,可以如下使用插件:

// example usage
var draw = SVG('canvas').viewbox(0, 0, 1000, 300)

var text = draw.text('We go up, then we go down, then up again')
text.font({ size: 42.5, family: 'Verdana' })
text.path('M 100 200 C 200 100 300  0 400 100 C 500 200 600 300 700 200 C 800 100 900 100     900 100')

draw.use(text.track).attr({ fill: 'none', 'stroke-width': 1, stroke: '#f09' })

Here is a fiddle of the dynamic version: http://jsfiddle.net/wout/LNuWM/ 这是动态版本的小提琴: http : //jsfiddle.net/wout/LNuWM/

But this is where it goes wrong because the text is not rendered. 但这是错误的地方,因为未渲染文本。 At first I thought something was wrong with my code but when I copied the svg output from the inspector and pasted in a svg document, the text is rendered as expected. 最初,我认为我的代码有问题,但是当我从检查器复制svg输出并粘贴到svg文档中时,文本将按预期呈现。

Here an example of the static version: http://jsfiddle.net/wout/ZbM7K/ 这里是静态版本的示例: http : //jsfiddle.net/wout/ZbM7K/

Is this a browser glitch or am I missing something? 这是浏览器故障还是我错过了什么?

UPDATE: This has now been resolved: http://jsfiddle.net/wout/LNuWM/2/ 更新:现在已解决: http : //jsfiddle.net/wout/LNuWM/2/

This seems to be an error in SVG.js. 这似乎是SV​​G.js中的错误。 The SVG root element gets messed up because of adding the XLink namespace: SVG根元素由于添加了XLink命名空间而变得混乱:

<svg style="position:relative;overflow:hidden;left:0px;top:0px;"xlink="http://www.w3.org/1999/xlink" ...
---------------------------------------------------------------^
|
--------this should read [SPACE]xmlns:xlink=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM