๋ฐ์ํ
[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
Fill ๋ฒํผ์ ๋๋ฅด๊ณ ์์์ ์ ํํ ํ canvas๋ฅผ ํด๋ฆญํ๋ฉด
๊ทธ ์์์ผ๋ก canvas๊ฐ ์ฑ์์ง๋ ๊ฒ์ ํ ๊ฒ์ด๋ค.
fillRect()
width์ height์ ์ํด์ ๊ฒฐ์ ๋ ์ฌ์ด์ฆ๋ก (x, y) ์์น์ ์์น ๋ ์ฌ๊ฐํ์ ๊ทธ๋ฆผ.
์ถ๊ฐํ ๊ฒโผ
ctx.fillRect(50, 20, 100, 49); // x, y, width, height
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;
ctx.fillRect(50, 20, 100, 49); // x, y, width, height
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);
}
ํ์ง๋ง fillRect()์ด strokeStyle์ ์ ์ฉํ์ง ๋ชปํ๊ณ ์๋ค.
fillStyle()๋ก ๋์ ํ์.
app.js์ ๋ค์ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
ctx.fillStyle = "green";
์์ ํ ๊ฒโผ
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
ctx.strokeStyle = color;
ctx.fillStyle = color;
}
const INITIAL_COLOR = "#2c2c2c"
ctx.strokeStyle = INITIAL_COLOR;
ctx.fillStyle = INITIAL_COLOR;
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"
canvas.width = 700;
canvas.height = 700;
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";
}
}
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);
}
๊ทธ๋ค์์ clicking์ ๊ดํ addEventListener์ ํ๋ ๋ ๋ง๋ ๋ค.
(canvas๋ฅผ clickํ๋ ๊ฒ)
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.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() {
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE); // canvas๋งํผ ์ปค์ผ ํ๋ค
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", handleCanvasClick);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
if(range) {
range.addEventListener("input", handleRangeChange);
}
if(mode) {
mode.addEventListener("click", handleModeClick);
}
๊ทธ๋์ filling variable์ด ํ์ํ ๊ฒ์ด๋ค.
function handleCanvasClick() {
if (filling) {
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE); // canvas๋งํผ ์ปค์ผ ํ๋ค
}
}
๋ฐ์ํ
'JavaScript > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Saving the Image (0) | 2021.04.28 |
---|---|
[๋ฐ๋๋ผ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 |
๋๊ธ