javascript 如何向 raphael js 元素添加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8140228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:26:25  来源:igfitidea点击:

How to add text to a raphael js element

javascripttextraphael

提问by pahnin

I want to add text to a element in raphael js, I have added text with

我想向 raphael js 中的元素添加文本,我添加了文本

r.text(30, 20, "ellipse").attr({fill: color});

But how to add this text to

但是如何将此文本添加到

ec = r.ellipse(190, 100, 30, 20);

regards

问候

回答by Anurag Uniyal

Raphael does not have child/parent relationship between elements, so you will set same position for them e.g.

Raphael 元素之间没有子/父关系,因此您将为它们设置相同的位置,例如

ec = paper.ellipse(190, 100, 30, 20);
paper.text(190, 100, "ellipse").attr({fill: '#ff0000'});

So if you want a ellipse with text, create your own JavaScript object which handles positioning of both.

因此,如果您想要带有文本的椭圆,请创建您自己的 JavaScript 对象来处理两者的定位。

or alternate way is to group elements via sete.g.

或替代方法是通过set例如对元素进行分组

var eltext = paper.set();
el = paper.ellipse(0, 0, 30, 20);
text = paper.text(0, 0, "ellipse").attr({fill: '#ff0000'})
eltext.push(el);
eltext.push(text);
eltext.translate(100,100)

回答by Donovant

You can easly add text to youR elements creating a text Raphael element and add as attribute textinto your element.

您可以轻松地将文本添加到您的元素,创建一个文本 Raphael 元素,并将文本作为属性添加到您的元素中。

 elText = r.text(.....);
 yourEl.attr({text:elText});