๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Vanilla JS

[๋ฐ”๋‹๋ผJS๋กœ ๊ทธ๋ฆผํŒ ๋งŒ๋“ค๊ธฐ] Filling Mode

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 4. 27.
๋ฐ˜์‘ํ˜•

[์ถœ์ฒ˜-์œ ํŠœ๋ธŒ ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders]

https://youtu.be/2m_WnYeI1pA

 

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);
}

x, y ์ขŒํ‘œ์— ์‚ฌ๊ฐํ˜•์ด ์ƒ๊น€

 

 

 

ํ•˜์ง€๋งŒ 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);
}

fill์€ ์ž˜ ๋˜๋Š”๋ฐ ๋ญ”๊ฐ€๊ฐ€ ์ด์ƒํ•˜๋‹ค. ์„ ์„ ๊ทธ๋ฆฌ๋Š” ์ค‘์— ๋‹ค์‹œ fill๋กœ ๋ณ€ํ•จ.

 

 

 

 

๊ทธ๋ž˜์„œ filling variable์ด ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค.

function handleCanvasClick() {
    if (filling) {
        ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);  // canvas๋งŒํผ ์ปค์•ผ ํ•œ๋‹ค
    }
}

์ž‘๋™์ด ์ž˜ ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€