| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
- ํ๋ก์ ์ํคํ ์ฒ
- next.js
- HTML
- HttpOnly Cookie
- tailwindcss
- cookie ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ฐ๋๋ผ JS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ
- ์ฝ๋ฉ๋ ํ
- jest
- ์ฝ๋ฉ์ผ๊ธฐ
- typescript
- BFF์ํคํ ์ฒ
- ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
- ํ๋ก ํธ์๋
- react
- BFF ์ํคํ ์ฒ
- ์ํ์ฝ๋ฉ
- canvas js
- JAVA Script
- cookie ๋ณด์
- javascript
- CSS
- canvas
- clone coding
- ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ
- HttyOnly Cookie
- ๋ ธ๋ง๋์ฝ๋
- canvas image
- ๋ ธ๋ง๋ ์ฝ๋
- JS
- Today
- Total
Coding Archive
canvas js๋ก ๊ตฌํํ๊ธฐ (6) - canvas drawing image[์บ๋ฒ์ค์ ์ด๋ฏธ์ง ์ถ๋ ฅํ๊ธฐ - ์ฌ์ง์ฒจ๋ถ] ๋ณธ๋ฌธ
canvas js๋ก ๊ตฌํํ๊ธฐ (6) - canvas drawing image[์บ๋ฒ์ค์ ์ด๋ฏธ์ง ์ถ๋ ฅํ๊ธฐ - ์ฌ์ง์ฒจ๋ถ]
์ฝ๋ฑ์ด 2022. 10. 7. 10:11canvas drawing image
์บ๋ฒ์ค์ ์ฌ์ง์ ์ฒจ๋ถํ ์ ์๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
[ html / css ]
|
1
2
3
4
|
<body>
<canvas id="canvas" width="700" height="700"></canvas>
<input type="file" accept="image/*" id="file" />
</body>
|
cs |
accept ์์ฑ์ ์ถ๊ฐํด ์ฒจ๋ถํ๋ ค๋ file์ ํน์ ํ ์ ์์ต๋๋ค. (accept="image/*" ->๋ชจ๋ ์ด๋ฏธ์ง ํ์ผ์ ํ์ฉํ๋ค๋ ์๋ฏธ)

์ด๋ ๊ฒ input file์ ๋ง๋ค๊ฒ ๋๋ฉด ์์ ๊ธฐ๋ณธ ์คํ์ผ์ฒ๋ผ ๊ทธ๋ ค์ง๊ฒ ๋ฉ๋๋ค.
ํ์ง๋ง css๋ฅผ ๋ณ๊ฒฝํ ์ ์๊ธฐ ๋๋ฌธ์ ์คํ์ผ์ ๋ณ๊ฒฝํ๊ณ ์ถ์ ๊ฒฝ์ฐ๋ผ๋ฉด label์ ์ฌ์ฉํด ํด๊ฒฐํ ์ ์์ต๋๋ค.
|
1
2
3
4
5
6
7
|
<body>
<canvas id="canvas" width="700" height="700"></canvas>
<label for="file">
Add Photo
<input type="file" accept="image/*" id="file" />
</label>
</body>
|
cs |
๋ผ๋ฒจ ํ๊ทธ์ for ์์ฑ์ ์ด์ฉํด input file ํ๊ทธ์ ์ฐ๊ฒฐ์ ์์ผ์ฃผ๋ฉด, label์ ํด๋ฆญํ์ ๋ input file์ ํด๋ฆญํ ๊ฒ์ฒ๋ผ ๋์ํ๊ฒ ๋ฉ๋๋ค.
* ์ด๋<input type="file" accept="image/* "id="file" />์ ์์ ์ฝ๋์ฒ๋ผ label ํ๊ทธ ์์ ๋ฃ์ ์๋, ๋ฐ์ ๋นผ์ ์์ฑํ ์๋ ์์ต๋๋ค.

์ดํ์ css๋ฅผ ์ฌ์ฉํด ์คํ์ผ์ ๋ณ๊ฒฝํด์ค๋๋ค.
|
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
|
body {
padding: 50px;
display: flex;
flex-direction: column;
align-items: center;
background-color: rgba(0, 0, 0, 0.03);
}
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 {
display: none;
}
label {
font-family: "Source Sans Pro";
width: 100px;
padding: 8px 18px;
border: 1.5px #ff6600 solid;
background-color: #ff6600;
border-radius: 5px;
color: white;
cursor: pointer;
font-size: 14px;
text-align: center;
transition: all 0.2s ease-in-out;
}
label:hover {
border: 1.8px #ff6600 solid;
background-color: transparent;
color: #ff6600;
}
|
cs |
input file์ display: none;์ ํด์ฃผ๋ฉด ๋ธ๋ผ์ฐ์ ์์ ์ ๋ณด์ด๊ฒ ๋ฉ๋๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก ์คํ์ผ์ ์ ์ฉ์ํจ label๋ง ๋ณด์ด๊ฒ ๋ฉ๋๋ค.

[ js ]
|
1
2
3
|
const canvas = document.querySelector("#canvas");
const imgFile = document.querySelector("#file");
const ctx = canvas.getContext("2d");
|
cs |
๋จผ์ canvas์ input file์ ์ ๊ทผํ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ๊ณ , 2d ๋ชจ๋์ ๊ทธ๋ฆฌ๊ธฐ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ ์บ๋ฒ์ค์ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋๋ก ํฉ๋๋ค.
|
1
2
3
4
5
6
|
const canvas = document.querySelector("#canvas");
const imgFile = document.querySelector("#file");
const ctx = canvas.getContext("2d");
imgFile.addEventListener("change", drawImg);
// = imgFile.onchange = drawImg;
|
cs |
input file์ change ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ , ๋ณ๊ฒฝ์ด ์ผ์ด๋ ๋๋ง๋ค drawImg ํจ์๊ฐ ํธ์ถ๋๋๋ก ํด์ค๋๋ค.
* ์ด๋ฒคํธ ํ์ ์ค change๋ ํผ ์ปจํธ๋กค ๊ฐ(value ๊ฐ)์ด ๋ณ๊ฒฝ๋์์ ๋ ๋ฐ์ํ๋ฉฐ, input(text, radio, checkbox), textarea, select ํ๊ทธ์ ์ ์ฉ๋ฉ๋๋ค.
๋ธ๋ผ์ฐ์ ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์์ง ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค.
ํ์ผ์ ์ ์ ๊ฐ ํ์ผ์ ์ ํํ์ ๋๋ง ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ณด์ด๊ฒ ๋๊ณ , ํ์ผ์ ์ ํํ๊ฒ ๋๋ฉด ๊ทธ ํ์ผ์ ์ด์ ๋ธ๋ผ์ฐ์ ์ ๋ฉ๋ชจ๋ฆฌ ์์ ์๊ฒ ๋ฉ๋๋ค.
ํ์ผ url์ ๊ฐ์ง๊ณ ๊ทธ ํ์ผ์ ์ ๊ทผํด์ ๋ณผ ์ ์๊ฒ ํด์ผํฉ๋๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ด ํ์ํฉ๋๋ค.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
const canvas = document.querySelector("#canvas");
const imgFile = document.querySelector("#file");
const ctx = canvas.getContext("2d");
function drawImg(event) {
//1
const file = event.target.files[0];
//2
const url = URL.createObjectURL(file);
//3
const img = new Image();
//4
img.src = url;
//5
img.onload = function () {
ctx.drawImage(img, 0, 0, 700, 700);
};
}
imgFile.addEventListener("change", drawImg);
|
cs |
1. ๋จผ์ ์ฒจ๋ถ๋๋ ํ์ผ์ ๊ฐ์ ธ์ต๋๋ค.
2. ๋ธ๋ผ์ฐ์ ์ ๋ฉ๋ชจ๋ฆฌ์์ ๊ทธ ํ์ผ์ url์ ์ป์ด์ต๋๋ค.
3. new Image()๋ฅผ ์ฌ์ฉํด ์๋ก์ด ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ด ์ค๋๋ค.
const image = new Image()๋ html์์ <img src=""/> ํ๊ทธ๋ฅผ ์ฐ๋ ๊ฒ๊ณผ ๋์ผํฉ๋๋ค.
4. ์ด๋ฏธ์ง์ src์ ๋ธ๋ผ์ฐ์ ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ฆฌํค๋ url์ ๋ฃ์ด์ค๋๋ค.
5. ๊ทธ๋ค์ ์ด๋ฏธ์ง์ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐํด์ค๋๋ค.
์ด๋ฏธ์ง๊ฐ ๋ก๋๋์์ ๋, ์ฆ onload์ผ ๋, ํจ์๋ฅผ ์คํ์ํฌ ๊ฑด๋ฐ, ์ฌ๊ธฐ์๋ drawImage()๋ผ๋ ctx method๋ฅผ ํธ์ถํฉ๋๋ค.
* drawImage(image, x์ขํ, y์ขํ, ๋๋น, ๋์ด)
drawImage()๋ ์ด๋ฏธ์ง์ ์์น๋ฅผ ํ์๋ก ํฉ๋๋ค. ๋๋น์ ๋์ด๋ ์ต์ ์ผ๋ก ์๋ต ์์๋ ์๋ณธ ํฌ๊ธฐ๋๋ก ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅํฉ๋๋ค.

์บ๋ฒ์ค์ ์ด๋ฏธ์ง ํ์ผ์ด ๋ฌด์ฌํ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.