| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 image
- ํ๋ก ํธ์๋
- ์ํ์ฝ๋ฉ
- javascript
- tailwindcss
- HTML
- cookie ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์ฝ๋ฉ์ผ๊ธฐ
- BFF์ํคํ ์ฒ
- JAVA Script
- ํ๋ก์ ์ํคํ ์ฒ
- jest
- HttyOnly Cookie
- JS
- ๋ ธ๋ง๋์ฝ๋
- HttpOnly Cookie
- ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
- canvas js
- react
- typescript
- CSS
- ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ
- ๋ ธ๋ง๋ ์ฝ๋
- next.js
- clone coding
- ์ฝ๋ฉ๋ ํ
- cookie ๋ณด์
- BFF ์ํคํ ์ฒ
- canvas
- ๋ฐ๋๋ผ JS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ
- Today
- Total
Coding Archive
canvas js๋ก ๊ตฌํํ๊ธฐ (7) - saving canvas painting [์บ๋ฒ์ค ๊ทธ๋ฆผ ์ ์ฅํ๊ธฐ] ๋ณธ๋ฌธ
canvas js๋ก ๊ตฌํํ๊ธฐ (7) - saving canvas painting [์บ๋ฒ์ค ๊ทธ๋ฆผ ์ ์ฅํ๊ธฐ]
์ฝ๋ฑ์ด 2022. 10. 7. 11:35saving canvas painting
์บ๋ฒ์ค์ ๊ทธ๋ฆฐ ๊ทธ๋ฆผ์ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
[ html / css ]
|
1
2
3
4
|
<body>
<canvas id="canvas" width="700" height="700"></canvas>
<button class="saveBtn">save image</button>
</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
|
body {
padding: 44px;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f7f7f7;
}
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: 40px;
}
button {
background-color: rgba(0, 0, 0, 0.4);
border: none;
border-radius: 25px;
color: #f7f7f7;
height: 50px;
width: 125px;
font-size: 15px;
box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
cursor: pointer;
transition: all ease-in-out 0.15s;
}
button:hover {
background-color: #606060;
scale: 1.1;
}
|
cs |
css๋ก ์บ๋ฒ์ค์ ๋ฒํผ์ ์คํ์ผ์ ๊พธ๋ฉฐ์ค๋๋ค.

[ js ]
|
1
2
3
|
const canvas = document.querySelector("#canvas");
const saveBtn = document.querySelector(".saveBtn");
const ctx = canvas.getContext("2d");
|
cs |
๋จผ์ ์บ๋ฒ์ค์ ์ธ์ด๋ธ ๋ฒํผ์ ์ ๊ทผํ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ๊ณ , 2d ๋ชจ๋์ ๊ทธ๋ฆฌ๊ธฐ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ ์บ๋ฒ์ค์ ๊ทธ๋ฆผ์ ๊ทธ๋ฆด ์ ์๋๋ก ํฉ๋๋ค.
|
1
2
3
4
5
|
const canvas = document.querySelector("#canvas");
const saveBtn = document.querySelector(".saveBtn");
const ctx = canvas.getContext("2d");
saveBtn.addEventListener("click", onSaveClick);
|
cs |
์ธ์ด๋ธ ๋ฒํผ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํด ํด๋ฆญํ ๋๋ง๋ค onSaveClickํจ์๊ฐ ํธ์ถ๋๋๋ก ํฉ๋๋ค.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
const canvas = document.querySelector("#canvas");
const saveBtn = document.querySelector(".saveBtn");
const ctx = canvas.getContext("2d");
saveBtn.addEventListener("click", onSaveClick);
function onSaveClick() {
const a = document.createElement("a");
a.href = canvas.toDataURL();
a.download = "drawing.png";
a.click();
}
|
cs |
onSaveClickํจ์๋ ๊ทธ๋ฆผ์ ์ ์ฅํ๋ ํจ์๋ก, a ํ๊ทธ๋ฅผ ์์ฑํด์ฃผ๊ณ , ์์ฑ์ ์ง์ ํ ํ ๋ค์ด๋ก๋๋ฅผ ์ํํฉ๋๋ค.
์บ๋ฒ์ค์๋ canvas.toDataURL์ด๋ผ๋ ๋ฉ์๋๊ฐ ์๋๋ฐ,
๊ทธ๋ ค์ง ๋ด์ฉ์ base64๋ก ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด๋ก ๋ฐํํด ์ฃผ๋ ๋ฉ์๋์ ๋๋ค. (๊ทธ๋ ค์ง ๋ด์ฉ์ Data URL๋ก ๋ณํํ ๊ฒ)
a ํ๊ทธ๋ฅผ ์ด์ฉํด ๋งํฌ๋ฅผ ๋ง๋ค์ด ์ฃผ๊ณ , ์น์ฌ์ดํธ๋ก ๋งํฌํ๋ ๋์ ์ด ์ด๋ฏธ์ง url๋ก ๋งํฌํด์ค๋๋ค.
๊ทธ๋ค์์ a ํ๊ทธ ์์ ์๋ download๋ผ๋ ์์ฑ์ ๋ํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ a ํ๊ทธ ๊ฐ์ ๋ก ํด๋ฆญ ์ด๋ฒคํธ ๋ฐ์์์ผ ๋ค์ด ๋ก๋๊ฐ ์คํ๋๋๋ก ํฉ๋๋ค.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
const canvas = document.querySelector("#canvas");
const saveBtn = document.querySelector(".saveBtn");
const ctx = canvas.getContext("2d");
function onSaveClick() {
if (confirm("Are you sure you want to save this image?")) {
const a = document.createElement("a");
a.href = canvas.toDataURL();
a.download = "drawing.png";
a.click();
}
}
saveBtn.addEventListener("click", onSaveClick); |
cs |
confirm() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ์ธ๊ณผ ์ทจ์ ๋ฒํผ์ ๊ฐ์ง ๋ฉ์ธ์ง ์ฐฝ์ ๋์ด ํ,
if ์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํด ํ์ธ์ ๋๋ ์ ๊ฒฝ์ฐ์๋ง ์ด๋ฏธ์ง๊ฐ ๋ค์ด๋ก๋๋๋๋ก ํด์ค๋๋ค.
|
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
41
42
43
44
45
46
47
48
49
50
|
const canvas = document.querySelector("#canvas");
const saveBtn = document.querySelector(".saveBtn");
const ctx = canvas.getContext("2d");
// ์บ๋ฒ์ค ๋์ง ์คํ์ผ๊ณผ ์ ์คํ์ผ ์ค์ ํ๊ธฐ
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 700, 700);
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.strokeStyle = "coral";
// ์ ๊ทธ๋ฆฌ๊ธฐ
let painting = false;
function onMouseMove(event) {
if (!painting) {
ctx.beginPath();
ctx.moveTo(event.offsetX, event.offsetY);
} else {
ctx.lineTo(event.offsetX, event.offsetY);
ctx.stroke();
}
}
function startPainting() {
painting = true;
}
function stopPainting() {
painting = false;
}
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
// ์บ๋ฒ์ค ์ด๋ฏธ์ง ์ ์ฅํ๊ธฐ
function onSaveClick() {
if (confirm("Are you sure you want to save this image?")) {
const a = document.createElement("a");
a.href = canvas.toDataURL();
a.download = "drawing.png";
a.click();
}
}
saveBtn.addEventListener("click", onSaveClick);
|
cs |
์ ์ ๋ง๋ค์๋ ์ ๊ทธ๋ฆฌ๊ธฐ ํจ์๋ฅผ ์ถ๊ฐํด์ฃผ์์ต๋๋ค.

์บ๋ฒ์ค์ ๊ทธ๋ฆผ์ ๊ทธ๋ฆฐ ํ ์ธ์ด๋ธ ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ฌด์ฌํ ์ ์ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.