๋ฐ์ํ
[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
Recap ํ์ ์ฐ๋ฆฌ๊ฐ ํ ์ผ์ ์์์ ๋ฐ๊พธ๋ ๊ฒ์ด๋ค.
index.html
<div class="controls__colors" id="jsColors">
<div class="controls__color jsColor" style="background-color: black;"></div>
<div class="controls__color jsColor" style="background-color: white"></div>
<div class="controls__color jsColor" style="background-color: #ff3b3c"></div>
<div class="controls__color jsColor" style="background-color: #ff9500"></div>
<div class="controls__color jsColor" style="background-color: #ffcc00"></div>
<div class="controls__color jsColor" style="background-color: #4cd963"></div>
<div class="controls__color jsColor" style="background-color: #5ac8fa"></div>
<div class="controls__color jsColor" style="background-color: #0579ff"></div>
<div class="controls__color jsColor" style="background-color: #5856d6"></div>
</div>
app.js
const colors = document.getElementsByClassName("jsColor");
console.log(Array.from(colors));
๊น๋นกํ๊ณ closePath()๋ฅผ ์์ง์....
function handleColorClick(event) {
console.log(event.target.style);
}
Array.from(colors).forEach(color =>
color.addEventListener("click", handleColorClick)
);
์ข ๋ ๋ค๋ฌ์ด๋ณด์.
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
console.log(color);
}
๊ทธ๋ฆฌ๊ณ ์ง๊ธ์ strokeStyle์ด ๊ฒ์ ์์ผ๋ก ์ง์ ๋์ด์์ง๋ง
strokeStyle์ overrideํ๋ฉด handleColorClick ํจ์์์๋ strokeStyle์ด target์ ์๋ ์์์ด ๋๋ ๊ฒ์ด๋ค.
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
ctx.strokeStyle = color;
}
์ฐ๋ฆฌ๊ฐ ํ๊ฑด ctx.strokeStyle์ ๋ฐ๊พผ ๊ฒ ๋ฐ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ color์ target์ผ๋ก๋ถํฐ ๋ฐ์์ ๋ฃ์ ๊ฒ ๋ฟ์ด๋ค.
๋ฐ์ํ
'JavaScript > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Filling Mode (0) | 2021.04.27 |
---|---|
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Brush Size (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Recap! (0) | 2021.04.27 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] 2D Context (0) | 2021.04.25 |
[๋ฐ๋๋ผJS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ] Canvas Events (2) | 2021.04.24 |
๋๊ธ