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

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

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

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

https://youtu.be/j4cAIJutjVI

 

๋‹ค์Œ ๋‹จ๊ณ„๋กœ๋Š” 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);
}

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€