How do I write something directly inside the circle?











up vote
0
down vote

favorite












I would like to write something directly inside the circle but I'm not sure how to. I know I could just write ctx.fillText(" test", 10, 30); and it would move to the right but I feel like that's an inefficient way of doing it.



How would I achieve this? Below is an image of what I'm referring to.



enter image description here



import React, {Component} from 'react';

class Login extends Component {
componentDidMount() {
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");

ctx.beginPath();
ctx.arc(100,75,50,0,2*Math.PI);
ctx.fillText("hey", 10, 30);
ctx.stroke();
}

render() {
return (
<div>
<canvas id="myCanvas" width="240" height="200"/>
</div>
);
}
}

export default Login;









share|improve this question


























    up vote
    0
    down vote

    favorite












    I would like to write something directly inside the circle but I'm not sure how to. I know I could just write ctx.fillText(" test", 10, 30); and it would move to the right but I feel like that's an inefficient way of doing it.



    How would I achieve this? Below is an image of what I'm referring to.



    enter image description here



    import React, {Component} from 'react';

    class Login extends Component {
    componentDidMount() {
    let c = document.getElementById("myCanvas");
    let ctx = c.getContext("2d");

    ctx.beginPath();
    ctx.arc(100,75,50,0,2*Math.PI);
    ctx.fillText("hey", 10, 30);
    ctx.stroke();
    }

    render() {
    return (
    <div>
    <canvas id="myCanvas" width="240" height="200"/>
    </div>
    );
    }
    }

    export default Login;









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to write something directly inside the circle but I'm not sure how to. I know I could just write ctx.fillText(" test", 10, 30); and it would move to the right but I feel like that's an inefficient way of doing it.



      How would I achieve this? Below is an image of what I'm referring to.



      enter image description here



      import React, {Component} from 'react';

      class Login extends Component {
      componentDidMount() {
      let c = document.getElementById("myCanvas");
      let ctx = c.getContext("2d");

      ctx.beginPath();
      ctx.arc(100,75,50,0,2*Math.PI);
      ctx.fillText("hey", 10, 30);
      ctx.stroke();
      }

      render() {
      return (
      <div>
      <canvas id="myCanvas" width="240" height="200"/>
      </div>
      );
      }
      }

      export default Login;









      share|improve this question













      I would like to write something directly inside the circle but I'm not sure how to. I know I could just write ctx.fillText(" test", 10, 30); and it would move to the right but I feel like that's an inefficient way of doing it.



      How would I achieve this? Below is an image of what I'm referring to.



      enter image description here



      import React, {Component} from 'react';

      class Login extends Component {
      componentDidMount() {
      let c = document.getElementById("myCanvas");
      let ctx = c.getContext("2d");

      ctx.beginPath();
      ctx.arc(100,75,50,0,2*Math.PI);
      ctx.fillText("hey", 10, 30);
      ctx.stroke();
      }

      render() {
      return (
      <div>
      <canvas id="myCanvas" width="240" height="200"/>
      </div>
      );
      }
      }

      export default Login;






      javascript html5 reactjs debugging






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 13:49









      sp92

      2359




      2359
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can use measureText to get the width of the your text and calculate coordinate for starting point for the text:



          class App extends React.Component {
          componentDidMount() {
          let c = document.getElementById("myCanvas");
          let ctx = c.getContext("2d");

          const x_arc = 100;
          const y_arc = 75;
          const radius = 50;

          const text = "hey";
          const text_width = ctx.measureText(text).width;
          const x_text = x_arc - text_width / 2;
          const y_text = y_arc;

          ctx.beginPath();
          ctx.arc(x_arc, y_arc, radius, 0, 2 * Math.PI);
          ctx.fillText("hey", x_text, y_text);
          ctx.stroke();
          }

          render() {
          return (
          <div>
          <canvas id="myCanvas" width="240" height="200" />
          </div>
          );
          }
          }


          Here the link to sandbox: https://codesandbox.io/s/j377qol3ww



          enter image description here






          share|improve this answer





















          • thanks man, learned something new
            – sp92
            Nov 8 at 16:14


















          up vote
          0
          down vote













          Well, if you do the math:



          the x of the circle is: 100
          the y of the circle is: 75
          So, the text should be around (95, 80)


          Note: the (x, y) coordinate of the text is dependent on the one of the circle. That's been said, you can make it more dynamic:



          const circleCords = { x: 100, y: 75 }

          // You can adjust the x, y of the text. Because `5` is just for testing.
          // I mean by that, that you would have to get the width of the text and then calculate how much you'd subtract from the circleCords!
          const textCords = { x: circleCords - 5, y: circleCords + 5 }

          ctx.arc(circleCords.x, circleCords.y, 50, 0,2*Math.PI);
          ctx.fillText("hey", textCords.x, textCords.y);



          If your text's width is always constant, you'll be fine by subtracting random number as desired. In this case 5.




          Hope that could help!






          share|improve this answer





















          • ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
            – sp92
            Nov 8 at 14:59











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209081%2fhow-do-i-write-something-directly-inside-the-circle%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You can use measureText to get the width of the your text and calculate coordinate for starting point for the text:



          class App extends React.Component {
          componentDidMount() {
          let c = document.getElementById("myCanvas");
          let ctx = c.getContext("2d");

          const x_arc = 100;
          const y_arc = 75;
          const radius = 50;

          const text = "hey";
          const text_width = ctx.measureText(text).width;
          const x_text = x_arc - text_width / 2;
          const y_text = y_arc;

          ctx.beginPath();
          ctx.arc(x_arc, y_arc, radius, 0, 2 * Math.PI);
          ctx.fillText("hey", x_text, y_text);
          ctx.stroke();
          }

          render() {
          return (
          <div>
          <canvas id="myCanvas" width="240" height="200" />
          </div>
          );
          }
          }


          Here the link to sandbox: https://codesandbox.io/s/j377qol3ww



          enter image description here






          share|improve this answer





















          • thanks man, learned something new
            – sp92
            Nov 8 at 16:14















          up vote
          1
          down vote



          accepted










          You can use measureText to get the width of the your text and calculate coordinate for starting point for the text:



          class App extends React.Component {
          componentDidMount() {
          let c = document.getElementById("myCanvas");
          let ctx = c.getContext("2d");

          const x_arc = 100;
          const y_arc = 75;
          const radius = 50;

          const text = "hey";
          const text_width = ctx.measureText(text).width;
          const x_text = x_arc - text_width / 2;
          const y_text = y_arc;

          ctx.beginPath();
          ctx.arc(x_arc, y_arc, radius, 0, 2 * Math.PI);
          ctx.fillText("hey", x_text, y_text);
          ctx.stroke();
          }

          render() {
          return (
          <div>
          <canvas id="myCanvas" width="240" height="200" />
          </div>
          );
          }
          }


          Here the link to sandbox: https://codesandbox.io/s/j377qol3ww



          enter image description here






          share|improve this answer





















          • thanks man, learned something new
            – sp92
            Nov 8 at 16:14













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can use measureText to get the width of the your text and calculate coordinate for starting point for the text:



          class App extends React.Component {
          componentDidMount() {
          let c = document.getElementById("myCanvas");
          let ctx = c.getContext("2d");

          const x_arc = 100;
          const y_arc = 75;
          const radius = 50;

          const text = "hey";
          const text_width = ctx.measureText(text).width;
          const x_text = x_arc - text_width / 2;
          const y_text = y_arc;

          ctx.beginPath();
          ctx.arc(x_arc, y_arc, radius, 0, 2 * Math.PI);
          ctx.fillText("hey", x_text, y_text);
          ctx.stroke();
          }

          render() {
          return (
          <div>
          <canvas id="myCanvas" width="240" height="200" />
          </div>
          );
          }
          }


          Here the link to sandbox: https://codesandbox.io/s/j377qol3ww



          enter image description here






          share|improve this answer












          You can use measureText to get the width of the your text and calculate coordinate for starting point for the text:



          class App extends React.Component {
          componentDidMount() {
          let c = document.getElementById("myCanvas");
          let ctx = c.getContext("2d");

          const x_arc = 100;
          const y_arc = 75;
          const radius = 50;

          const text = "hey";
          const text_width = ctx.measureText(text).width;
          const x_text = x_arc - text_width / 2;
          const y_text = y_arc;

          ctx.beginPath();
          ctx.arc(x_arc, y_arc, radius, 0, 2 * Math.PI);
          ctx.fillText("hey", x_text, y_text);
          ctx.stroke();
          }

          render() {
          return (
          <div>
          <canvas id="myCanvas" width="240" height="200" />
          </div>
          );
          }
          }


          Here the link to sandbox: https://codesandbox.io/s/j377qol3ww



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 15:48









          Iwan

          1,4391021




          1,4391021












          • thanks man, learned something new
            – sp92
            Nov 8 at 16:14


















          • thanks man, learned something new
            – sp92
            Nov 8 at 16:14
















          thanks man, learned something new
          – sp92
          Nov 8 at 16:14




          thanks man, learned something new
          – sp92
          Nov 8 at 16:14












          up vote
          0
          down vote













          Well, if you do the math:



          the x of the circle is: 100
          the y of the circle is: 75
          So, the text should be around (95, 80)


          Note: the (x, y) coordinate of the text is dependent on the one of the circle. That's been said, you can make it more dynamic:



          const circleCords = { x: 100, y: 75 }

          // You can adjust the x, y of the text. Because `5` is just for testing.
          // I mean by that, that you would have to get the width of the text and then calculate how much you'd subtract from the circleCords!
          const textCords = { x: circleCords - 5, y: circleCords + 5 }

          ctx.arc(circleCords.x, circleCords.y, 50, 0,2*Math.PI);
          ctx.fillText("hey", textCords.x, textCords.y);



          If your text's width is always constant, you'll be fine by subtracting random number as desired. In this case 5.




          Hope that could help!






          share|improve this answer





















          • ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
            – sp92
            Nov 8 at 14:59















          up vote
          0
          down vote













          Well, if you do the math:



          the x of the circle is: 100
          the y of the circle is: 75
          So, the text should be around (95, 80)


          Note: the (x, y) coordinate of the text is dependent on the one of the circle. That's been said, you can make it more dynamic:



          const circleCords = { x: 100, y: 75 }

          // You can adjust the x, y of the text. Because `5` is just for testing.
          // I mean by that, that you would have to get the width of the text and then calculate how much you'd subtract from the circleCords!
          const textCords = { x: circleCords - 5, y: circleCords + 5 }

          ctx.arc(circleCords.x, circleCords.y, 50, 0,2*Math.PI);
          ctx.fillText("hey", textCords.x, textCords.y);



          If your text's width is always constant, you'll be fine by subtracting random number as desired. In this case 5.




          Hope that could help!






          share|improve this answer





















          • ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
            – sp92
            Nov 8 at 14:59













          up vote
          0
          down vote










          up vote
          0
          down vote









          Well, if you do the math:



          the x of the circle is: 100
          the y of the circle is: 75
          So, the text should be around (95, 80)


          Note: the (x, y) coordinate of the text is dependent on the one of the circle. That's been said, you can make it more dynamic:



          const circleCords = { x: 100, y: 75 }

          // You can adjust the x, y of the text. Because `5` is just for testing.
          // I mean by that, that you would have to get the width of the text and then calculate how much you'd subtract from the circleCords!
          const textCords = { x: circleCords - 5, y: circleCords + 5 }

          ctx.arc(circleCords.x, circleCords.y, 50, 0,2*Math.PI);
          ctx.fillText("hey", textCords.x, textCords.y);



          If your text's width is always constant, you'll be fine by subtracting random number as desired. In this case 5.




          Hope that could help!






          share|improve this answer












          Well, if you do the math:



          the x of the circle is: 100
          the y of the circle is: 75
          So, the text should be around (95, 80)


          Note: the (x, y) coordinate of the text is dependent on the one of the circle. That's been said, you can make it more dynamic:



          const circleCords = { x: 100, y: 75 }

          // You can adjust the x, y of the text. Because `5` is just for testing.
          // I mean by that, that you would have to get the width of the text and then calculate how much you'd subtract from the circleCords!
          const textCords = { x: circleCords - 5, y: circleCords + 5 }

          ctx.arc(circleCords.x, circleCords.y, 50, 0,2*Math.PI);
          ctx.fillText("hey", textCords.x, textCords.y);



          If your text's width is always constant, you'll be fine by subtracting random number as desired. In this case 5.




          Hope that could help!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 14:02









          Hasan Sh

          30417




          30417












          • ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
            – sp92
            Nov 8 at 14:59


















          • ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
            – sp92
            Nov 8 at 14:59
















          ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
          – sp92
          Nov 8 at 14:59




          ah ok but how did you derive 95, 80 from 100, 75 by doing the math? Also, I tried this and tried changing up coords but I can't see test at all.
          – sp92
          Nov 8 at 14:59


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209081%2fhow-do-i-write-something-directly-inside-the-circle%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()