Raphael JS Text Along path

前端 未结 2 1517
天涯浪人
天涯浪人 2021-01-05 01:58

I am looking for an example or some confirmation on a concept. Looking to use Raphael JS on an app and want to be able to warp text similar to how graphic design application

2条回答
  •  臣服心动
    2021-01-05 02:42

    This isn't too difficult using path.getPointAtLength, as Kevin Nielsen suggests:

    path = paper.path("M50,100c40,-50 270,50 300,0").attr("stroke", "#CCC");
    message = "I want to do this in RaphaelJS";
    
    //since not every letter is the same width, get the placement for each letter 
    //along the length of the string
    //however, Raphael appears to group the width of letters into intervals of 4px,
    //so this won't be perfect        
    for (; c < message.length; c += 1) {
            letter = paper.text(0, 0, message[c]).attr({"text-anchor" : "start"});
        letters.push(letter);
        places.push(message_length);
        //spaces get a width of 0, so set min at 4px
        message_length += Math.max(4, letter.getBBox().width);
    }
    
    ratio = path.getTotalLength() / message_length;
    fontsize = 10 * ratio;
    
    for (c = 0; c < letters.length; c += 1) {
        letters[c].attr("font-size", fontsize + "px"); 
        p = path.getPointAtLength(places[c] * ratio);
        //there does appear to be a bug in p.alpha around 180. Here's the fix
        letters[c].attr({ x: p.x, y: p.y, transform: 'r' + (p.alpha < 180 ? p.alpha + 180 : p.alpha)});
    }
    

    jsFiddle

提交回复
热议问题