SVG textPath在JavaScript生成时无法呈现

时间:2013-07-19 10:42:01

标签: javascript svg

对于svg.js我写了一个添加textPath功能的小插件。该插件非常简洁:

// 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
    }
})

要创建与this example on 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' })

这是动态版的小提琴: http://jsfiddle.net/wout/LNuWM/

但这是出错的原因,因为文本没有呈现。起初我认为我的代码出了问题但是当我从检查器复制svg输出并粘贴到svg文档中时,文本按预期呈现。

以下是静态版本的示例: http://jsfiddle.net/wout/ZbM7K/

这是浏览器故障还是我错过了什么?

更新: 这已经解决了: http://jsfiddle.net/wout/LNuWM/2/

1 个答案:

答案 0 :(得分:2)

这似乎是SV​​G.js中的错误。由于添加了XLink命名空间,SVG根元素变得混乱:

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