Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I place text inside an SVG circle with React JS?

I have the following code:

import React from 'react';

var SVGComponent = React.createClass({
    render: function() {
        return <svg {...this.props}>{this.props.children}</svg>;
    }
});

var Circle = React.createClass({
    render: function() {
        return <circle {...this.props}>{this.props.children}</circle>;
    }
});

var MakeCircles = React.createClass({

    render: function () {
        return(
            <div>
                <SVGComponent height="110" width="500">
                    <Circle 
                        cx="50" cy="55" r="45"
                        fill="none"
                        stroke="#F0CE01" strokeWidth="4" />     
                </SVGComponent>
            </div>
        );
    }
});
export default MakeCircles

I'm trying to get some text inside the circle but finding it absolutely difficult. Is there something/some add-on that can help me with this?

like image 742
D'Arcy Rail-Ip Avatar asked Aug 28 '15 21:08

D'Arcy Rail-Ip


1 Answers

A circle is a layer, and so is a Text node. You have to have them as separate layers and make them look as if they belong together:

<SVGComponent height="110" width="500">
    <Circle cx="50" cy="55" r="45" fill="none" stroke="#F0CE01" strokeWidth="4" />
    <text textAnchor="middle" x="250" y="55">Circle Text</text>
</SVGComponent>
like image 159
owenconti Avatar answered Oct 22 '22 00:10

owenconti