svg 创建文本 create text

本文介绍如何在SVG中动态创建文本元素,并提供了一个示例代码片段,演示了如何使用JavaScript通过SVG文档对象模型来添加和设置文本。

既然svg无法显示换行文本,则必须创建多个文本。怎么创建呢,下面是个网上的例子,不太好找,把它转过来。

(http://osdir.com/ml/text.xml.svg.devel/2002-08/msg01252.html)

 

node=svgdoc.createElement("text");
node.setAttribute("id",yourId);
node.setAttribute("x",yourx);
node.setAttribute("y",youry);
node.setAttribute("style",yourstyle);
textnode=svgdoc.createTextNode("Hello Dynamic Text");
node.appendChild(textnode);
yourparent.appendChild(node);

this should do the job...

the only difference is that you have to create a special textNode that will be
appended to the text-element, which is then appended to it's parent.

Hope this helps,
Andreas


--- In svg-developers@xxxx, "dennis_joel_david" <dennis.myren@xxxx> wrote:
> Isnt it possible todynamically create a text element and setting its
> text?
> I am trying to use this function to achieve this.
> This doesnt work, but it seems like when I have already a text
> element with this id, and where text is more than empty string
> already, there is no problem.
> What am I doing wrong?
> Because it has to be possible, because I dont know how many text
> elements will be created during the session when my SVG document
> loads.
> Thank you
> Dennis
> function createTextElement(text, x, y)
> {
> Ind = _osvgdoc.createElement('text');
> Ind.setAttribute('id', '1100');
> Ind.setAttribute('x', x + (width / 2));
> Ind.setAttribute('y', y + (height / 2) + 10);
> Ind.setAttribute('style', 'text-anchor:middle; font-size:16; font-
> family:Arial; fill:black;');
> Ind.setData('kalle');
> //Ind.getFirstChild().setData('kalle');
> parent.appendChild(Ind);
> }

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值