There are 2 ways to do this.
The easier way is to make use of the Raphael .print()
method. This can turn text into paths. Each character gets its own path. Then you can iterate over each character and move and rotate it appropriately using .translate()
and .angle()
.
The harder way is to implement text on path for both SVG and VML for a Raphael .text()
.
Here's a quick and rough start for Method 1 with no rotation using .print()
and this font:
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());
}
};?
Note: The above code is very rough and has some important positioning problems, but I think the general approach is the way to go for putting text on a path with Raphael.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…