hex*_*hex 8 javascript svg canvas raphael
有谁知道如何将文本附加到Raphaël的路径?像http://www.w3.org/TR/SVG11/images/text/toap02.svg 这样的东西我知道jQuery SVG可以做到这一点,但我找不到一个简单的方法来使用Raphaëljs.我想将此文本附加到贝塞尔曲线并移动它.
Pet*_*tai 12
有两种方法可以做到这一点.
更简单的方法是使用Raphael .print()
方法.这可以将文本转换为路径.每个角色都有自己的路径.然后你可以迭代每个角色并使用.translate()
和移动并适当地旋转它.angle()
.
这是方法1的一个快速而粗略的开始,没有使用旋转.print()
和这个字体:
window.onload = function() {
var i, newP,
R = Raphael("canvas",500,400),
// Create our set of letter paths
t = R.print(100, 0, "this is a test", R.getFont("whoa"), 30),
// Create the path to follow
p = R.path("M 50 100 C 100 50 150 0 200 50 C 250 100 300 150 350 100" +
" C 400 50 450 50 450 50").attr("stroke", "none"),
pLen = p.getTotalLength(), // Length of path to follow
tLen = t.length, // Number of characters
oneL = pLen/tLen; // Average length of 1 character
// You can also use .getBBox() for each character instead
// Position each character
for (i = 0; i < tLen; i++) {
// Get x,y of the path to follow
newP = p.getPointAtLength(i * oneL);
// Move the letter
t[i].translate((i * oneL)-newP.x, newP.y);
t[i].attr("fill", Raphael.getColor());
}
};?
Run Code Online (Sandbox Code Playgroud)
注意:上面的代码非常粗糙,并且有一些重要的定位问题,但我认为通常的方法是将文本放在Raphael的路径上.
raphael 2.1 print()函数不再返回一组路径,而是包含所有字母的单个路径.所以这里的所有解决方案都不适用于raphael 2.1(当前版本).我开发了以下小插件,将printLetters()方法添加到纸上,单独打印字母并返回一个集合,就像旧的print()方法一样.此外,该插件还支持将此文本与路径对齐.例如,要使用插件对齐路径上的文本,您只需执行以下操作:
var r = Raphael(0, 0, 500, 500);
var path1 = "M 50 100 C 100 50 150 0 200 50" +
" C 250 100 300 150 350 100" +
" C 400 50 450 50 450 50";
var text1 = r.printLetters(20, 150, "habia una vez una vaca",
r.getFont("my underwood"), 30, null, null, path1).attr({
fill : "red",
stroke : "black"
});
Run Code Online (Sandbox Code Playgroud)
插件代码是:
(function() {
/**
* do the job of putting all letters in a set returned bu printLetters in a path
* @param p - can be a rpahael path obejct or string
*/
var _printOnPath = function(text, paper, p) {
if(typeof(p)=="string")
p = paper.path(p).attr({stroke: "none"});
for ( var i = 0; i < text.length; i++) {
var letter = text[i];
var newP = p.getPointAtLength(letter.getBBox().x);
var newTransformation = letter.transform()+
"T"+(newP.x-letter.getBBox().x)+","+
(newP.y-letter.getBBox().y-letter.getBBox().height);
//also rotate the letter to correspond the path angle of derivative
newTransformation+="R"+
(newP.alpha<360 ? 180+newP.alpha : newP.alpha);
letter.transform(newTransformation);
}
};
/** print letter by letter, and return the set of letters (paths), just like the old raphael print() method did. */
Raphael.fn.printLetters = function(x, y, str, font, size,
letter_spacing, line_height, onpath) {
letter_spacing=letter_spacing||size/1.5;
line_height=line_height||size;
this.setStart();
var x_=x, y_=y;
for ( var i = 0; i < str.length; i++) {
if(str.charAt(i)!='\n') {
var letter = this.print(x_,y_,str.charAt(i),font,size);
x_+=letter_spacing;
}
else {
x_=x;
y_+=line_height;
}
}
var set = this.setFinish();
if(onpath) {
_printOnPath(set, this, onpath);
}
return set;
};
})();
Run Code Online (Sandbox Code Playgroud)