[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
๋ค์ ๋จ๊ณ๋ก๋ jsRange ์์ด๋๋ฅผ ์ถ๊ฐํ๋๊ฒ ํ์ํ๋ค.
์ถ๊ฐํ ๊ฒโผ
const range = document.getElementById("jsRange");
// range ์ด๋ฒคํธ๋ input์ ๋ฐ์ํ ๊ฒ์ด๋ค.
if(range) {
range.addEventListener("input", handleRangeChange);
}
function handleRangeChange(event) {
console.log(event);
}
app.js
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.getElementsByClassName("jsColor");
const range = document.getElementById("jsRange");
canvas.width = 700;
canvas.height = 700;
ctx.strokeStyle = "#2c2c2c";
ctx.lineWidth = 2.5;
let painting = 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;
}
function handleRangeChange(event) {
console.log(event);
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
if(range) {
range.addEventListener("input", handleRangeChange);
}
ํ์ง๋ง ์ฐ๋ฆฌ๊ฐ ์ฐพ๊ณ ์ถ์๊ฑด ์ด๊ฒ์ด ์๋.. (๋๊ผฌ์ค ์)
event.target.value๋ฅผ ์ด์ฌํ ์ฐพ๋๋ค.
๋ค์ ์ฝ๋๋ฅผ ์์ ํ๋ค.
function handleRangeChange(event) {
console.log(event.target.value);
}
step์ ๋ฐ๋ฅผ ์์ง์ผ๋๋ง๋ค ์ผ๋ง์ฉ ์ฆ๊ฐ์ํค๋์ง ์ค์ ํ๋ ๊ฒ์ด๋ค.
ํ์ฌ๋ step์ 0.1๋ก ์ค์ ํด๋จ์.
์๋ฅผ ๋ณด๋ฉด 0.1์ฉ ์ฆ๊ฐํ๋๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ค์์ผ๋ก handleRangeChange ํจ์์๋ค๊ฐ override๋ฅผ ํด์ค๋ค.
function handleRangeChange(event) {
const size = event.target.value;
ctx.lineWidth = size;
}
์๋ก๊ณ ์นจ์ ํ๋ฉด ๋ค์ 2.5step(max๊ฐ 5step์ด๋๊น ์ค๊ฐ์ง์ )์ผ๋ก ๋์๊ฐ๋ค.
์ด์ fill(์์ ์ฑ์ฐ๊ธฐ)๋ฅผ ํ ๊ฒ์ด๋ค!
FILL ๋ฒํผ์ ํด๋ฆญํ๋ฉด PAINT ๋ฒํผ์ผ๋ก ๋ฐ๋๋๋ก ๋ง๋ ๋ค.
์ถ๊ฐํ ๊ฒโผ
const mode = document.getElementById("jsMode");
let filling = false;
if(mode) {
mode.addEventListener("click", handleModeClick);
}
function handleModeClick() {
if(filling === true) {
filling = false;
mode.innerText = "Fill";
} else {
filling = true;
mode.innerText = "Paint";
}
}
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");
canvas.width = 700;
canvas.height = 700;
ctx.strokeStyle = "#2c2c2c";
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;
}
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";
}
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
if(range) {
range.addEventListener("input", handleRangeChange);
}
if(mode) {
mode.addEventListener("click", handleModeClick);
}
'JavaScript > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Saving the Image (0) | 2021.04.28 |
---|---|
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Filling Mode (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Changing Color (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Recap! (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] 2D Context (0) | 2021.04.25 |
๋๊ธ