[miniProjects] 22_Drawing App

๋ณด๋ฆฌยท2023๋…„ 6์›” 16์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
23/47

22_Drawing App

๐Ÿ’ป ์ฃผ์ œ : ์บ”๋ฒ„์Šค ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•œ ๋“œ๋กœ์ž‰.

https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API

const canvas = document.getElementById('canvas');
const increaseBtn = document.getElementById('increase');
const decreaseBtn = document.getElementById('decrease');
const sizeEL = document.getElementById('size');
const colorEl = document.getElementById('color');
const clearEl = document.getElementById('clear');

const ctx = canvas.getContext('2d');

let size = 10;
let isPressed = false;
colorEl.value = 'black';
let color = colorEl.value;
let x;
let y;

canvas.addEventListener('mousedown', (e) => {
    isPressed = true;

    x = e.offsetX;
    y = e.offsetY;
})

document.addEventListener('mouseup', (e) => {
    isPressed = false;

    x = undefined;
    y = undefined;
})

canvas.addEventListener('mousemove', (e) => {
    if(isPressed) {
        const x2 = e.offsetX;
        const y2 = e.offsetY;

        drawCircle(x2, y2);
        drawLine(x, y, x2, y2);

        x = x2;
        y = y2;
    }
})

function drawCircle(x, y) {
    ctx.beginPath();
    ctx.arc(x, y, size, 0, Math.PI * 2);
    ctx.fillStyle = color;
    ctx.fill();
}

function drawLine(x1, y1, x2, y2) {
    ctx.beginPath();
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.strokeStyle = color;
    ctx.lineWidth = size * 2;
    ctx.stroke();
}

function updateSizeOnScreen() {
    sizeEL.innerText = size;
}

increaseBtn.addEventListener('click', () => {
    size += 5;

    if(size > 50) {
        size = 50;
    }

    updateSizeOnScreen()
})

decreaseBtn.addEventListener('click', () => {
    size -= 5;

    if(size < 5) {
        size = 5;
    }

    updateSizeOnScreen();
})

colorEl.addEventListener('change', (e) => color = e.target.value);

clearEl.addEventListener('click', () => ctx.clearRect(0,0, canvas.width, canvas.height));

// drawCircle(100, 200);
// // X์ถ• 300์„ ์‹œ์ž‘์œผ๋กœ Y์ถ•์—์„œ 300์„ ๋‚ด๋ฆฌ๊ณ  300-500๋งŒํผ ๊ฐ„๋‹ค. 
// drawLine(300, 300, 300, 500);
  • canvas ๋ณ€์ˆ˜๋Š” document.getElementById('canvas')๋ฅผ ํ†ตํ•ด canvas์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. increaseBtn, decreaseBtn, sizeEL, colorEl, clearEl์€ ๊ฐ๊ฐ 'increase', 'decrease', 'size', 'color', 'clear' ID๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ctx ๋ณ€์ˆ˜๋Š” canvas.getContext('2d')๋ฅผ ํ†ตํ•ด 2D ๊ทธ๋ž˜ํ”ฝ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  • size ๋ณ€์ˆ˜๋Š” ๊ทธ๋ฆฌ๊ธฐ ๋„๊ตฌ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์€ 10์œผ๋กœ ์„ค์ •๋œ๋‹ค.
  • isPressed ๋ณ€์ˆ˜๋Š” ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”Œ๋ž˜๊ทธ๋‹ค. colorEl์˜ ์ดˆ๊ธฐ๊ฐ’์€ 'black'์œผ๋กœ ์„ค์ •๋˜๋ฉฐ, color ๋ณ€์ˆ˜๋Š” ํ˜„์žฌ ์„ ํƒ๋œ ์ƒ‰์ƒ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. x์™€ y ๋ณ€์ˆ˜๋Š” ํ˜„์žฌ ๋งˆ์šฐ์Šค์˜ X ์ขŒํ‘œ์™€ Y ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • canvas์—๋Š” 'mousedown', 'mouseup', 'mousemove' ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ ๋ˆ„๋ฅด๊ณ  ์žˆ์„ ๋•Œ('mousedown') isPressed๋ฅผ true๋กœ ์„ค์ •ํ•˜๊ณ , ํ˜„์žฌ ๋งˆ์šฐ์Šค์˜ ์ขŒํ‘œ๋ฅผ x์™€ y์— ์ €์žฅํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ ๋†“์•˜์„ ๋•Œ('mouseup') isPressed๋ฅผ false๋กœ ์„ค์ •ํ•˜๊ณ , x์™€ y๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ ์›€์ง์ผ ๋•Œ('mousemove') isPressed๊ฐ€ true์ธ ๊ฒฝ์šฐ์—๋งŒ ๊ทธ๋ฆฌ๊ธฐ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ํ˜„์žฌ ์ขŒํ‘œ์™€ ์ด์ „ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์›๊ณผ ์„ ์„ ๊ทธ๋ฆฐ๋‹ค.
  • drawCircle ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ์ขŒํ‘œ์— ์›์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜๋‹ค. drawLine ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋‘ ์ขŒํ‘œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜๋‹ค.
  • updateSizeOnScreen ํ•จ์ˆ˜๋Š” ํ™”๋ฉด์— ํ˜„์žฌ ๋„๊ตฌ ํฌ๊ธฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ๋ณด์—ฌ์ค€๋‹ค.
  • 'increaseBtn'๊ณผ 'decreaseBtn'์€ ๊ฐ๊ฐ ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋„๊ตฌ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ์‹œํ‚ค๊ณ  updateSizeOnScreen ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ™”๋ฉด์— ํฌ๊ธฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  • 'colorEl'์€ 'change' ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋ฉฐ, ์„ ํƒ๋œ ์ƒ‰์ƒ ๊ฐ’์„ color ๋ณ€์ˆ˜์— ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  • 'clearEl'์€ ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋ฉฐ, ์บ”๋ฒ„์Šค๋ฅผ ์ง€์šฐ๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€