๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿช€ ํ† ์ด ํ”„๋กœ์ ํŠธ/๐Ÿ–ผ๏ธ

[3] ์‚ฌ์šฉํ•  ์ด๋ฏธ์ง€ ์ œ์ž‘ & ์บ”๋ฒ„์Šค์— ์ด๋ฏธ์ง€ ๋„ฃ๋Š” ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

728x90

 

๋ณธ๊ฒฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ ค๋ดค๋‹ค.

 

์ด๋Ÿฐ ๋Š๋‚Œ์œผ๋กœ ์™„์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋‹ค์–‘ํ•œ ์š”์†Œ์™€ ์บ๋ฆญํ„ฐ๋ฅผ ๊ทธ๋ ธ๋‹ค.

๊ตฌํ˜„ ํ›„ ํ•„์š”ํ•œ ์š”์†Œ๋“ค์ด ์žˆ๋‹ค๋ฉด ๋” ๊ทธ๋ ค์„œ ์ถ”๊ฐ€ํ•ด ๋ณผ ์˜ˆ์ •!

 

 

์บ๋ฆญํ„ฐ์™€ ๋…ธํŠธ๋ถ ๋ฒ„ํŠผ๋งŒ ๋งŒ๋“ค์–ด๋ดค๋‹ค.

๊ฐ๊ฐ์˜ ๋ฒ„ํŠผ์— eventListener ๋กœ click ์‹œ ์ด๋ฏธ์ง€๊ฐ€ ์บ”๋ฒ„์Šค์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. 

๊ฐ™์€ ๋ฒ„ํŠผ์„ ๋‹ค์‹œ ํด๋ฆญ ์‹œ ์ด๋ฏธ์ง€๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.

ํด๋ฆญํ•œ ๋ฒ„ํŠผ์˜ ๊ตฌ๋ถ„์„ ์œ„ํ•ด, ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฒ„ํŠผ์˜ ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ์„ค์ •ํ–ˆ๋‹ค. 

 

 

 

 

 

์ด์Šˆ 1. ๋™์ผํ•œ ์บ๋ฆญํ„ฐ ํด๋ฆญ ์‹œ img ์™€ button ์˜ ํฌ๊ธฐ๊ฐ€ ๋งž์ง€ ์•Š์•„ target์„ ์ฐพ๊ธฐ ์–ด๋ ค์šด ์ 

๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  ์‹ถ์€๋ฐ, ์ž๊พธ ์ด๋ฏธ์ง€๊ฐ€ ์•ž์— ์žˆ์–ด ์ด๋ฏธ์ง€๊ฐ€ target ์ด ๋๋‹ค. 

์–ด๋–ป๊ฒŒ ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, ๋‘˜ ์ค‘์— ์–ด๋–ค๊ฒŒ ํด๋ฆญ์ด ๋ผ๋„ ๊ฐ™์€ className ์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด,

ํ•ด๋‹นํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ canvas ์— ๊ทธ๋ ค์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. 

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ) ๋ฒ„ํŠผ๊ณผ ์ด๋ฏธ์ง€์— ๊ฐ™์€ class ๋ช…์„ ์ž…๋ ฅํ•จ

 

<button id="chin_button" class="chin_button img_button">
        <img
          class="chin_button"
          src="../assets/character/chinchilla.PNG"
        />
</button>

 

  let buttonClass = event.target.className;
  if (isCharacterOnCanvas === false) {
    const image = new Image();
    if (buttonClass === 'chin_button') {
      image.src = '../assets/character/chinchilla.PNG';
    } else if (buttonClass === 'quokka_button') {
      image.src = '../assets/character/quokka.PNG';
    } else if (buttonClass === 'rabbit_button') {
      image.src = '../assets/character/rabbit.PNG';
    } else if (buttonClass === 'bear_button') {
      image.src = '../assets/character/bear.PNG';
    }
      context.drawImage(
        image,
        0,
        0,
        CANVAS_WIDTH,
        CANVAS_HEIGHT
      );
    isCharacterOnCanvas = true;

 

 

 

 

 

๋ฒ„ํŠผ์€ ์ž˜ ์ž‘๋™ ๋์ง€๋งŒ, ๋ฒ„ํŠผ๊ณผ ์ด๋ฏธ์ง€ ์‚ฌ์ด์— ๊ฐ„๊ฒฉ์ด ์žˆ์—ˆ๋‹ค. 

 

 

CSS ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๊ณ  button ์˜ padding ์„ ์—†์• ์ค˜๋„ ์•„๋ž˜ ๋ฏธ์„ธํ•œ ๊ฐ„๊ฒฉ์ด ์ƒ๊ฒผ๋‹ค. ํ  ์™œ ์ƒ๊ธด๊ฑฐ์ง€..?

์ด๋ฏธ์ง€๋งŒ ํด๋ฆญ ๋จ
๋ฒ„ํŠผ์ด ํด๋ฆญ๋จ

 

 

์ด์Šˆ2. ๋ฒ„ํŠผ๊ณผ ์ด๋ฏธ์ง€ ์‚ฌ์ด์˜ gap ์—†์• ๊ธฐ

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• 1) ์ด๋ฏธ์ง€ ํƒœ๊ทธ ๋Œ€์‹ , css ๋‚ด์—์„œ background-image ์†์„ฑ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ

 

canvas ๋‚ด์— ์ด๋ฏธ์ง€ ๋กœ๋“œ๋Š” ์ž˜ ๋˜์ง€๋งŒ,

button ๋‚ด์— background ์ ์šฉ์ด ์•ˆ๋๋‹ค.

 

์™œ์ง€..? ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ...

 

.quokka_button {
  width: 100px;
  height: 100px;
  background-image: url('../assets/character/quokka.PNG');
}

 

 

๋ญ๊ฐ€ ์ž˜๋ชป๋๋‚˜ ์ด๊ฒƒ ์ €๊ฒƒ ์‹œ๋„ํ•ด๋ณด๋‹ค ์•Œ๊ฒŒ ๋๋‹ค.

๋กœ๋“œ ์•ˆ๋œ๊ฒŒ ์•„๋‹ˆ๋ผ ์ด๋ฏธ์ง€๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ๋ณด์ด์ง€ ์•Š์•˜๋˜ ๊ฒƒ...

 

.quokka_button {
  width: 100px;
  height: 100px;
  background-image: url("../assets/character/quokka.PNG");
  /* background-position: center; */
  background-size: contain; //์‚ฌ์ด์ฆˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๋‹ˆ ์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ๋ณด์ž„.
}

 

background-size ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋‹ˆ ์ด๋ฏธ์ง€๊ฐ€ ๋ฒ„ํŠผ ํฌ๊ธฐ์— ๋งž๊ฒŒ ๋“ค์–ด๊ฐ”๋‹ค!

 

 

 

ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•์€ ์ ์ ˆํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ,,,,

 

ํด๋ฆญ ์‹œ style.background = ‘tomato’ ์ด๋ ‡๊ฒŒ ์ฃผ๋Š” ๋ฐ”๋žŒ์—

background ์†์„ฑ์— ์žˆ๋˜ ์ด๋ฏธ์ง€๊ฐ€ color๋กœ ๋Œ€์ฒด๋œ ๊ฒƒ์ด๋‹ค.

 

 

๋Œ๋ ค์ค˜ ๋‚ด ์ฟผ์นด...!

 

 

 

 

๊ทธ๋ž˜์„œ ์ด ๋ฐฉ๋ฒ•์€ ์“ฐ์ง€ ์•Š๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

์ด์ „ ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ์•„๊ฐ€์„œ 

๋ฒ„ํŠผ๊ณผ ์ด๋ฏธ์ง€ ์‚ฌ์ด์˜ ๊ฐญ์„ ํ•ด๊ฒฐํ•ด๋ณด์ž!

 

ํ—ˆ๋ฌดํ•˜๊ฒŒ๋„ 

button ์˜ border ๋ฅผ ์—†์• ๋‹ˆ ํ‹ˆ๋„ ์‚ฌ๋ผ์กŒ๋‹ค.

์Šคํƒ€์ผ๋„ ๋ฐฐ๊ฒฝ ์ƒ‰์„ ๋ฐ”๊พธ๋Š” ๋Œ€์‹ , 

border ๋””์ž์ธ๋งŒ ์•ฝ๊ฐ„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฑธ๋กœ ํด๋ฆญ๋œ ๋ฒ„ํŠผ์— ์ฐจ๋ณ„์„ฑ์„ ์คฌ๋‹ค. 

 

 

 

 

์˜ค ๊ท€์—ฌ์›Œ

 

 

 

 

 

 

์ž˜ ๋˜๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค!

 

๋…ธํŠธ๋ถ๋งŒ ์—†์• ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ฟผ์นด๊นŒ์ง€ ์‚ฌ๋ผ์ง!

 

 

 

 

์ด ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ ํ›„ ๋‹ค์Œ ํŽธ์— ์ž‘์„ฑํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 

๊ทธ๋ž˜๋„ ๊ตฌ์ƒ‰์„ ๊ฐ–์ถ”๋‹ˆ ๋„ˆ๋ฌด ๊ท€์—ฝ๋‹ค!

๋นจ๋ฆฌ ์™„์„ฑ ์‹œ์ผœ๋ณด๊ณ  ์‹ถ๋‹ค

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•