| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
- ๋ ธ๋ง๋ ์ฝ๋
- ์ฝ๋ฉ๋ ํ
- canvas js
- BFF ์ํคํ ์ฒ
- tailwindcss
- typescript
- BFF์ํคํ ์ฒ
- HTML
- CSS
- JS
- react
- javascript
- cookie ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ํ๋ก ํธ์๋
- clone coding
- canvas
- jest
- next.js
- HttyOnly Cookie
- ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
- canvas image
- ๋ ธ๋ง๋์ฝ๋
- ์ํ์ฝ๋ฉ
- HttpOnly Cookie
- ํ๋ก์ ์ํคํ ์ฒ
- ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ
- ์ฝ๋ฉ์ผ๊ธฐ
- cookie ๋ณด์
- JAVA Script
- ๋ฐ๋๋ผ JS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ
- Today
- Total
Coding Archive
canvas js๋ก ๊ตฌํํ๊ธฐ (5) - canvas drawing text [์บ๋ฒ์ค์ ํ ์คํธ ์ถ๋ ฅํ๊ธฐ] ๋ณธ๋ฌธ
canvas js๋ก ๊ตฌํํ๊ธฐ (5) - canvas drawing text [์บ๋ฒ์ค์ ํ ์คํธ ์ถ๋ ฅํ๊ธฐ]
์ฝ๋ฑ์ด 2022. 10. 5. 23:58canvas drawing text
์บ๋ฒ์ค์ ๋๋ธํด๋ฆญ์ผ๋ก ๊ฐ๋จํ๊ฒ ์ํ๋ ํ ์คํธ๋ฅผ ์ถ๋ ฅํ ์ ์๋ ํจ์๋ฅผ ๊ตฌํํด๋ณด์์ต๋๋ค.
[ html / css ]
|
1
2
3
4
|
<body>
<canvas id="canvas" width="700" height="700"></canvas>
<input type="text" id="text" placeholder="write here" />
</body>
|
cs |
html์์ ์บ๋ฒ์ค, ํ์ ์ด ํ ์คํธ์ธ ์ธํ์ ๋ง๋ค์ด ์ค๋๋ค.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
body {
padding: 50px;
display: flex;
flex-direction: column;
align-items: center;
background-color: rgba(0, 0, 0, 0.05);
}
canvas {
background-color: white;
border-radius: 5%;
box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
margin-bottom: 30px;
}
input[type="text"] {
outline: none;
border: none;
width: 200px;
border-bottom: 1.5px rgba(0, 0, 0, 0.15) solid;
background-color: transparent;
padding: 5px 8px;
font-size: 16px;
transition: all 0.3s;
}
input[type="text"]:hover {
border-bottom: 2px rgba(0, 0, 0, 0.25) solid;
}
input::placeholder {
color: rgba(0, 0, 0, 0.2);
transition: 0.4s;
text-align: center;
}
input:hover::placeholder {
color: rgba(0, 0, 0, 0.3);
}
|
cs |
์ํ๋ ๋ชจ์๋๋ก css๋ฅผ ๊พธ๋ฉฐ ์ค๋๋ค.
[ js ]
1 .
|
1
2
3
|
const canvas = document.querySelector("#canvas");
const text = document.querySelector("#text");
const ctx = canvas.getContext("2d");
|
cs |
๋จผ์ ์บ๋ฒ์ค์ ์ธํ์ ์ ๊ทผํ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ๊ณ , 2d ๋ชจ๋์ ๊ทธ๋ฆฌ๊ธฐ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ ์บ๋ฒ์ค์ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋๋ก ํฉ๋๋ค.
2 .
|
1
2
3
4
5
|
const canvas = document.querySelector("#canvas");
const text = document.querySelector("#text");
const ctx = canvas.getContext("2d");
canvas.addEventListener("dblclick", onTextClick);
|
cs |
์บ๋ฒ์ค์ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐํด ๋๋ธํด๋ฆญํ๋ฉด onTextClickํจ์๊ฐ ํธ์ถ๋๋๋ก ๋ง๋ค์ด ์ค๋๋ค.
3 .
|
1
2
3
4
5
6
7
8
9
10
11
12
|
const canvas = document.querySelector("#canvas");
const text = document.querySelector("#text");
const ctx = canvas.getContext("2d"); function onTextClick(event) {
ctx.lineWidth = 1;
ctx.fillStyle = "black";
ctx.font = "50px Souce Sans Pro";
ctx.fillText(text.value, event.offsetX, event.offsetY);
}
canvas.addEventListener("dblclick", onTextClick);
|
cs |
onTextClickํจ์ ์์ ํ ์คํธ์ ๋๊ป, ์์, ํฌ๊ธฐ, ํฐํธ๋ฅผ ์ง์ ํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ ctx.fillText(text, x, y)๋ฅผ ์ฌ์ฉํด ์ธํ์์ ๊ฐ์ด ๋ค์ด๊ฐ๋๋ก tevt.value๋ฅผ ๋ฃ์ด์ฃผ๊ณ ,
์บ๋ฒ์ค ๋ด์์ ๋ง์ฐ์ค ์ขํ ์์น๋ฅผ ๋ฐํํ๋ offsetX, Y๋ฅผ ๋ฃ์ด์ฃผ์ด ๋๋ธํด๋ฆญํ ๊ณณ์์ ๊ธ์จ๊ฐ ์ถ๋ ฅ๋๋๋ก ํฉ๋๋ค.

ํ ์คํธ๊ฐ ์บ๋ฒ์ค์ ๋ฌธ์ ์์ด ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค.