paintJS์ ๊ธฐ๋ฅ ์ค ์ค์ํ ๊ธฐ๋ฅ์ผ๋ก, canvas
์ ์ฒด๋ฅผ ์ํ๋ ์์์ผ๋ก ์ฑ์๋ฃ๋ fill
๊ธฐ๋ฅ์ ์ถ๊ฐํ์๋ค.
canvas
์ ๊ณต์๋ฌธ์๋ฅผ ํตํด ํ์ธํ๋ฉด, CanvasRenderingContext2D๋ถ๋ถ์ fill and stroke styles ํญ๋ชฉ์์ fillStyle
์ ์ง์ ํ ์ ์๋ค๋ ์ ์ ํ์ธํ ์ ์๋ค. ๋ฐ๋ผ์, strokeStyle
๊ณผ ๊ฐ์ ์์์ ํ๋๋ก ์ง์ ํด์ฃผ์ด์ผํ๊ณ , fillRect()
ํจ์๋ฅผ ํตํด์ canvas
๋ฅผ ์ฑ์ธ ๊ฒ์ด๊ธฐ ๋๋ฌธ์, ๊ธฐ๋ณธ strokeStyle
๊ฐ๊ณผ canvas.width
,canvas.heigth
์ ๊ฐ์ธ INITIAL_COLOR
์ CANVAS_SIZE
์ ๊ฐ์ ๊ฐ๊ฐ "#2c2c2c"
์ 500
์ผ๋ก ์ ์ธํด์ฃผ์๋ค. ๋ํ ์ด๊ธฐ์ ctx.fillStyle = INITIAL_COLOR;
๊ฐ์ผ๋ก ์ ์ํด์ฃผ์๋ค.
๊ธฐ์กด์ handleColorClick
ํจ์๋ ์ฌ์ฉ์๊ฐ ์์ div
๋ฅผ ํด๋ฆญํ๋ ๊ฒฝ์ฐ ctx.strokeStyle
์ ํด๋น ์์ ๊ฐ์ผ๋ก ํต์ผ์์ผ์ฃผ๋ ๋ด์ฉ์ ํจ์์์ผ๋, fill
๋ชจ๋์ ์ถ๊ฐ๋ฅผ ์ํด์ ctx.fillStyle
์ ๊ฐ ๋ํ ํด๋น ์์ ๊ฐ์ผ๋ก ํต์ผ์์ผ์ฃผ๋๋ก ํ์๋ค.
๊ธฐ์กด์ handleModeClick
์ filling
์ ๊ฐ์ true
ํน์ false
๋ก ๋ฐ๊พธ์ด์ฃผ๊ณ , ํด๋น button
์ innerText
๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ ๊ฒ์ ๊ทธ์ณค์ผ๋, filling
์ด true
๊ฐ ๋๋ ๊ฒฝ์ฐ, ctx.fillStyle
์ ๊ฐ์ ํ์ฌ ctx.strokeStyle
์ ๊ฐ๊ณผ ๊ฐ๊ฒ ํ๋๋ก ํ์๋ค.
canvas.addEventListener("click", handleCanvasClick);
๋ฅผ ํตํด ์ถ๊ฐ๋ handleCanvasClick
ํจ์๋ canvas
์ "click"
์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ if(filling)
์ ํตํด filling
์ true
์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ๋ง์ฝ true
์ธ ๊ฒฝ์ฐ,
ctx.fillRect(0,0,CANVAS_SIZE,CANVAS_SIZE);
๋ฅผ ํตํด canvas
๋ฅผ fillStyle
์ ์์์ผ๋ก ์น ํด์ฃผ์๋ค. 0,0์ ํด๋น canvas
์์์ ์์น๋ฅผ ์๋ฏธํ๊ณ , CANVAS_SIZE
,CANVAS_SIZE
๋ ์์น ๋๋ Rect์ ๊ฐ๋ก์ ์ธ๋ก์ ๊ธธ์ด (width & height) ๋ฅผ ์๋ฏธํ๋ค. ๋ฐ๋ผ์, 0,0์์์ CANVAS_SIZE
๋งํผ์ ๊ธธ์ด๋ฅผ ๊ฐ๋ Rect๋ ์ ์ฒด canvas
๋ฅผ ์๋ฏธํ๊ฒ ๋๋ค.