Raphael JS: how to change the color of certain letters within a text-element?

后端 未结 1 1363
梦谈多话
梦谈多话 2020-12-19 08:41

I have the following code:

        var set = paper.set();
        var text = paper.text(0, 0, \'bla1 bla2\' ).attr({ fill: \'blue\'});
        set.push(text)         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 09:13

    You can try something like this:

    HTML:

    JS:

    var text = "this is some colored text";
    var paper = Raphael('canvas', '100%', document.documentElement.clientHeight/2 );
    var colors = ["#ffc000", "#1d1d1d", "#e81c6e", "#7c7c7c", "#00aff2"];
    var letterSpace = 5;
    var xPos = 10;
    var yPos = 10;
    
    textNodes = text.split(' ');
    
    for( var i=0; i < textNodes.length; ++i) {       
        var textColor = colors[i];
        var textNode = paper.text( xPos , yPos , textNodes[i]);
            textNode.attr({
                'text-anchor': 'start',
                'font-size' : 12,
                'fill' : textColor
            });
        xPos = xPos + textNode.getBBox().width + letterSpace;
    }
    ​
    

    Demo: http://jsfiddle.net/aamir/zsS7L/

    0 讨论(0)
提交回复
热议问题