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.
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
add a comment |
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.
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
add a comment |
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.
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
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.
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
javascript html5 reactjs debugging
asked Nov 8 at 13:49
sp92
2359
2359
add a comment |
add a comment |
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
thanks man, learned something new
– sp92
Nov 8 at 16:14
add a comment |
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!
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 seetest
at all.
– sp92
Nov 8 at 14:59
add a comment |
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
thanks man, learned something new
– sp92
Nov 8 at 16:14
add a comment |
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
thanks man, learned something new
– sp92
Nov 8 at 16:14
add a comment |
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
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
answered Nov 8 at 15:48
Iwan
1,4391021
1,4391021
thanks man, learned something new
– sp92
Nov 8 at 16:14
add a comment |
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
add a comment |
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!
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 seetest
at all.
– sp92
Nov 8 at 14:59
add a comment |
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!
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 seetest
at all.
– sp92
Nov 8 at 14:59
add a comment |
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!
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!
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 seetest
at all.
– sp92
Nov 8 at 14:59
add a comment |
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 seetest
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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