admin管理员组文章数量:1419191
I was wondering if it's possible to change the attributes of a substring of the text object in Raphael. For example, I want to bold the word wizard in the following string "The magical wizard ruled the world!" in a raphael text object. I have looked at using the Raphael.print() method and I need some of the attributes from text for other portions of the code.
I was wondering if it's possible to change the attributes of a substring of the text object in Raphael. For example, I want to bold the word wizard in the following string "The magical wizard ruled the world!" in a raphael text object. I have looked at using the Raphael.print() method and I need some of the attributes from text for other portions of the code.
Share Improve this question asked Oct 24, 2011 at 20:31 JoshJosh 311 silver badge4 bronze badges2 Answers
Reset to default 4Fonts are set at the element level, much like in regular html. In order to apply a separate font or style to a specific word, you would need to break your text into separate elements.
var start = paper.text(20, 20, "The magical ");
start.attr({ "text-anchor": "start" });
var startBox = start.getBBox();
var bold = paper.text(startBox.width + startBox.x, 20, "wizard ");
bold.attr({ "text-anchor": "start", "font-weight": "bold" });
var boldBox = bold.getBBox();
var end = paper.text(boldBox.width + boldBox.x, 20, "ruled the world!");
end.attr({ "text-anchor": "start" });
A problem with the solution by gilly3 is that the x-coordinate of the elements is absolute. When changing the text, for example bold.attr({"text":"sorcerer"})
, the elements will overlap.
An alternative solution is to pose a text element out of custom tspan
elements with relative positioning. This is also a bit cleaner from an object model perspective. However, it does require some direct manipulation of the text element. Code:
var content = paper.text(20, 20, "The magical").attr({ "text-anchor": "start" });
var txt1=Raphael._g.doc.createTextNode(" wizard ");
var txt2=Raphael._g.doc.createTextNode("ruled the world!");
var svgNS = "http://www.w3/2000/svg";
var ts1 = Raphael._g.doc.createElementNS(svgNS, "tspan");
var ts2 = Raphael._g.doc.createElementNS(svgNS, "tspan");
ts1.setAttribute("font-weight","bold");
ts1.appendChild(txt1);
ts2.appendChild(txt2);
content.node.appendChild(ts1);
content.node.appendChild(ts2);
content.node.children[1].textContent=" sorcerer ";
Disclaimer: Raphael can change relative positioning of tspans when altering x,y,dx,dy
of the parent element, even though svg itself can handle this. A transform string can be used instead. Example:
content.node.setAttribute("x",500); //works
content.attr({"x":500}); //undesired result
content.transform("t100"); //works
本文标签: javascriptRaphaeljs Substring Text attributesStack Overflow
版权声明:本文标题:javascript - Raphaeljs Substring Text attributes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745291207a2651806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论