[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ํด ๋ณด์!
์ฐ์ canvas์ ๋ฐฐ๊ฒฝ์์ ์ง์ ํด์ฃผ์.
(๋ฐฐ๊ฒฝ์์ ์ง์ ํด์ฃผ์ง์์ผ๋ฉด ๋ฐฐ๊ฒฝ์ด ํฌ๋ช ํ๊ฒ ๋์ด)
ctx.fillStyle = "white";
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
๋ง์ฝ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ๊ณ ์ถ์ง์๋ค๋ฉด ์ด๋ ๊ฒ ํ๋ฉด ๋๋ค.
์ถ๊ฐํ ๊ฒโผ
function handleCM(event) {
event.preventDefault();
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", handleCanvasClick);
canvas.addEventListener("contextmenu", handleCM); // ์ถ๊ฐํ ๋ถ๋ถ
}
app.js
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.getElementsByClassName("jsColor");
const range = document.getElementById("jsRange");
const mode = document.getElementById("jsMode");
const INITIAL_COLOR = "#2c2c2c"
const CANVAS_SIZE = 700;
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
ctx.strokeStyle = INITIAL_COLOR;
ctx.fillStyle = INITIAL_COLOR;
ctx.lineWidth = 2.5;
let painting = false;
let filling = false;
function stopPainting() {
painting = false;
}
function startPainting() {
painting = true;
}
function onMouseMove(event) {
const x = event.offsetX;
const y = event.offsetY;
if(!painting){
ctx.beginPath();
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
ctx.stroke();
}
}
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
ctx.strokeStyle = color;
ctx.fillStyle = color;
}
function handleRangeChange(event) {
const size = event.target.value;
ctx.lineWidth = size;
}
function handleModeClick() {
if(filling === true) {
filling = false;
mode.innerText = "Fill";
} else {
filling = true;
mode.innerText = "Paint";
}
}
function handleCanvasClick() {
if (filling) {
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE); // canvas๋งํผ ์ปค์ผ ํ๋ค
}
}
function handleCM(event) {
event.preventDefault();
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", handleCanvasClick);
canvas.addEventListener("contextmenu", handleCM);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
if(range) {
range.addEventListener("input", handleRangeChange);
}
if(mode) {
mode.addEventListener("click", handleModeClick);
}
์ด๋ ๊ฒ ํ๋ฉด ๋ง์ฐ์ค ์ฐํด๋ฆญ์ ํด๋ 'context menu'๊ฐ ๋จ์ง ์๋๋ค.
๋ค์์ผ๋ก save ๋ฒํผ์ ๋๋ฌ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ ์ ์๊ฒ ๋ง๋ค์ด ๋ณด์.
save ๋ฒํผ์ ์๋ง์ eventlistener๋ฅผ ์ถ๊ฐํด ๋ณด์.
๋จผ์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋ฉ์ ํ๋ค.
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.getElementsByClassName("jsColor");
const range = document.getElementById("jsRange");
const mode = document.getElementById("jsMode");
const saveBtn = document.getElementById("jsSave"); // ์ถ๊ฐ๋ ๋ถ๋ถ
const INITIAL_COLOR = "#2c2c2c"
const CANVAS_SIZE = 700;
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
ctx.strokeStyle = INITIAL_COLOR;
ctx.fillStyle = INITIAL_COLOR;
ctx.lineWidth = 2.5;
let painting = false;
let filling = false;
function stopPainting() {
painting = false;
}
function startPainting() {
painting = true;
}
function onMouseMove(event) {
const x = event.offsetX;
const y = event.offsetY;
if(!painting){
ctx.beginPath();
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
ctx.stroke();
}
}
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
ctx.strokeStyle = color;
ctx.fillStyle = color;
}
function handleRangeChange(event) {
const size = event.target.value;
ctx.lineWidth = size;
}
function handleModeClick() {
if(filling === true) {
filling = false;
mode.innerText = "Fill";
} else {
filling = true;
mode.innerText = "Paint";
}
}
function handleCanvasClick() {
if (filling) {
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE); // canvas๋งํผ ์ปค์ผ ํ๋ค
}
}
function handleCM(event) {
event.preventDefault();
}
function handleSaveClick() { // ์ถ๊ฐ๋ ๋ถ๋ถ
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", handleCanvasClick);
canvas.addEventListener("contextmenu", handleCM);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
if(range) {
range.addEventListener("input", handleRangeChange);
}
if(mode) {
mode.addEventListener("click", handleModeClick);
}
if(saveBtn) { // ์ถ๊ฐ๋ ๋ถ๋ถ
saveBtn.addEventListener("click", handleSaveClick);
}
๋งจ ๋จผ์ ํ์ํ๊ฑด canvas์ ๋ฐ์ดํฐ๋ฅผ image์ฒ๋ผ ์ป๋ ๊ฒ์ด๋ค.
function handleSaveClick() {
const image = canvas.toDataURL("image/jpeg");
console.log(image);
}
๋ค์์ผ๋ก ์กด์ฌํ์ง ์๋ link๋ฅผ ๋ง๋ ๋ค. (fake link)
๋ธ๋ผ์ฐ์ ์๊ฒ ์ด ๋งํฌ๋ก ๊ฐ๋ ๋์ URL์ ๋ค์ด๋ก๋ํ๋ผ๊ณ ์ง์ํ๋ ๊ฒ(?)์ ๋ง๋ ๋ค.
function handleSaveClick() {
const image = canvas.toDataURL("image/jpeg");
const link = document.createElement("a");
link.href = image;
link.download = "PaintJS";
link.click();
}
โป ๊ณ ํ์ง๋ก ์ ์ฅํ๊ณ ์ถ์ผ๋ฉด DataURL์ ์ง์ ํ์ง ์์ผ๋ฉด ๋๋ค.
(๊ธฐ๋ณธ๊ฐ์ด png)
const image = canvas.toDataURL();
'JavaScript > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Filling Mode (0) | 2021.04.27 |
---|---|
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Brush Size (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Changing Color (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Recap! (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] 2D Context (0) | 2021.04.25 |
๋๊ธ