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

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

[6] ์บ๋ฆญํ„ฐ๋งŒ ๋ฐ”๊พธ๊ธฐ ์„ฑ๊ณต + ์•„์ดํ…œ ๋ณ€๊ฒฝ ์‹œ ๊ทธ๋ฆผ๋“ค ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ + ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ ์œ ์ง€ํ•˜๊ธฐ

728x90

์บ๋ฆญํ„ฐ๋ฅผ ๋ฐ”๊พธ๋ฉด ๋ชจ๋“  ๊ฒŒ ์ดˆ๊ธฐํ™” ๋˜๊ณ  

๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‹ค์‹œ ์„ ํƒํ•ด์•ผ ํ–ˆ๋˜ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด 

๋ช‡ ๊ฐ€์ง€์˜ ํ•ด๊ฒฐ์ฑ…์„ ์ƒ๊ฐํ•ด๋ดค์—ˆ๋‹ค.

 

https://mayongee.tistory.com/626

 

[5] Canvas Api ๋Š” ํ•œ๊ฒน์ด์•ผ - ์ด๋ฏธ์ง€ ๊ต์ฒด ๋ฒ„ํŠผ ์ˆ˜์ •ํ•˜๊ธฐ

โ˜๏ธ๋ฌธ์ œ์  : ์บ๋ฆญํ„ฐ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ชจ๋“  ์š”์†Œ ์ดˆ๊ธฐํ™” ์ด์ „์— ๋งŒ๋“  ๋ฒ„ํŠผ์ด ์ž‘๋™ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด๋ฉด ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ, ๋ฐ”๋กœ ์บ”๋ฒ„์Šค ๋‚ด์— ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ทจ์†Œ ๋ฒ„ํŠผ (์—†์• ๊ธฐ ๋ฒ„ํŠผ)์„

mayongee.tistory.com

 

 

2. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ canvas ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฐฐ๊ฒฝ ์ƒ‰๊ณผ ๋ณ„๊ฐœ๋กœ ์ด๋ฏธ์ง€ url ์„ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ 

์ด์ „ ๋‹จ๊ณ„์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ (๋ฐฐ๊ฒฝ์€ ํˆฌ๋ช…์œผ๋กœ)
=> ๋ฐฐ๊ฒฝ์ด ๋ณ€๊ฒฝ ๋  ๋•Œ -> ๋ฐฐ๊ฒฝ ๊น”๊ณ  ๋‹ค์‹œ ๋กœ๋“œ
=> ๋‹ค๋ฅธ ๋ฒ„ํŠผ ๋ˆ„๋ฅผ ๋•Œ -> ์—…๋ฐ์ดํŠธ
์ด๋Ÿฐ ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ...?

๊ฐ€๋Šฅํ•œ์ง€ ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๐Ÿ’ก ๋จผ์ € ์บ๋ฆญํ„ฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์บ๋ฆญํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์ด์ „ ์บ”๋ฒ„์Šค ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•ด๋†“๊ณ , ์บ๋ฆญํ„ฐ๋ฅผ ๊ทธ ์œ„์— ๋กœ๋“œํ•ด์ค€๋‹ค 

๋ผ๋Š” ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ํ•˜๋‹ค๊ฐ€ ์•„ ๋ฐ”๋ณด๋„ค ๊นจ๋‹ฌ์•˜๋‹ค. 

 

๊ทธ ์ „ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด๋ดค์ž, ์–ด์ฐจํ”ผ ๊ทธ ์œ„์— ๋ง์”Œ์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์—

=> ์บ๋ฆญํ„ฐ๊ฐ€ ๋…ธํŠธ๋ถ ์œ„๋กœ ์™€๋ฒ„๋ฆฌ๋Š” ์“ธ๋ชจ์—†๋Š” ์ด๋ฏธ์ง€๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค...!

 

 

์ˆœ์„œ๋Œ€๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ๋ญ๊ฐ€ ์žˆ์„๊นŒ ํ•˜๋‹ค๊ฐ€ 

ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๋ฉด ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹จ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

const fixedItemArray = [];

const drawImage = (buttonClass) => {
  const image = new Image();
  image.src = findingImgSrc(buttonClass);
  context.drawImage(
    image,
    0,
    0,
    CANVAS_WIDTH,
    CANVAS_HEIGHT
  );
};

const fixedItemButtonClick = (event) => {
  let buttonClass = event.target.className;
  fixedItemArray.push(buttonClass);
  context.beginPath();
  drawImage(buttonClass);
};

const onCharacterBtnClick = (event) => {
  let buttonClass = event.target.className;
  //์บ๋ฆญํ„ฐ๊ฐ€ ์บ”๋ฒ„์Šค์— ์—†๋Š” ๊ฒฝ์šฐ -> ์ถ”ํ›„ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๊ฐ€ ์„ธํŒ…๋œ ๋ฒ„์ „์œผ๋กœ ๋ณ€๊ฒฝ
  if (!isCharacterOnCanvas) {
    drawImage(buttonClass);
    isCharacterOnCanvas = true;
  } else if (isCharacterOnCanvas) {
    // ์บ๋ฆญํ„ฐ๊ฐ€ ์ด๋ฏธ ์บ”๋ฒ„์Šค์— ์žˆ๋Š” ๊ฒฝ์šฐ - ์ดˆ๊ธฐํ™” ํ›„ ๊ทธ๋ ค์ฃผ๊ธฐ
    context.beginPath();
    context.fillStyle = 'white';
    context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    console.log(fixedItemArray);
    drawImage(buttonClass);
    fixedItemArray.map((item) => drawImage(item));
    isCharacterOnCanvas = true;
  }
};

 

์•„์ดํ…œ๋“ค์€ ์บ”๋ฒ„์Šค์— ๊ทธ๋ ค์งˆ ๋•Œ ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ง„๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์บ๋ฆญํ„ฐ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ๋‹ค์‹œ ๊ทธ๋ ค์ง„๋‹ค.

 

 

์งœ์ž”~~ ๊นœ๋ฐ•์ž„ ์—†์ด ์ž˜ ๋ฐ”๋€Œ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

 

์•„ ๊ทผ๋ฐ ์•„์ง ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜๋Š” ๊ฑด ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

ํ  ๊ทผ๋ฐ ์–ด์ฐจํ”ผ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๊ฑด,, ๋ฐฐ๊ฒฝ๊ณผ๋Š” ๋ถ„๋ฆฌํ•ด๋„ ๋˜๋Š” ๊ธฐ๋Šฅ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค. 

๋ฐฐ๊ฒฝ + ์บ๋ฆญํ„ฐ & ์•„์ดํ…œ ๋ฐฐ์น˜ ํ›„, ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์•ˆ๋‚ดํ•ด์ค˜๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค. 

 

 

๋ฌดํŠผ ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์„ ํ™•์žฅ์‹œํ‚ค๋ฉด 

๋…ธํŠธ๋ถ๋„ ์ฑ…์ƒ ๋’ค๋กœ ๊ฐ€๋Š” ๊ฑธ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ..? ์ƒ๊ฐ๋„ ๋“ค์—ˆ๋‹ค. 

 

์ง€๊ธˆ์€ ์ฑ…์ƒ๊ณผ ๋…ธํŠธ๋ถ์„ ๊ณ ์ •๋œ ์•„์ดํ…œ ๋ฐฐ์—ด ์•ˆ์— ๊ฐ™์ด ๋„ฃ๊ณ  ์žˆ๋Š”๋ฐ, 

- ๋ฐฐ์—ด ์ˆœ์„œ๋ฅผ ์ฑ…์ƒ ๋จผ์ €๋กœ ๋ฐ”๊พผ ํ›„ ๊ทธ๋ ค์ฃผ๊ธฐ

- ๊ฐ™์€ ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์•„์ดํ…œ์ด ์ค‘๋ณต๋˜๋ฉด ๊ธฐ์กด์˜ ์•„์ดํ…œ์€ ์ง€์›Œ์ฃผ๊ธฐ

์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€ ์ˆ˜์ •์ด ํ•„์š”ํ•˜๋‹ค. 

 

const fixedItemButtonClick = (event) => {
  let buttonClass = event.target.className;
  //๊ฐ™์€ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์‚ญ์ œํ•ด์ค€ ํ›„ ๋„ฃ์–ด์ฃผ๊ธฐ
  if (buttonClass.includes('desk')) {
    fixedItemArray = fixedItemArray.filter(
      (item) => !item.includes('desk')
    );
    fixedItemArray.push(buttonClass);
  }
  if (buttonClass.includes('laptop')) {
    fixedItemArray = fixedItemArray.filter(
      (item) => !item.includes('laptop')
    );
    fixedItemArray.push(buttonClass);
  }
  context.beginPath();
  console.log(fixedItemArray);
  fixedItemArray.sort().map((item) => drawImage(item));
};

 

๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋ฅผ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ–ˆ๊ณ !

๋‹ค๋ฅธ ์•„์ดํ…œ์„ ๋ฐ”๊ฟ€ ๋•Œ๋„ ์ˆœ์„œ๊ฐ€ ๋’ค์ฃฝ๋ฐ•์ฃฝ ๋˜๋Š” ์ผ์ด ์‚ฌ๋ผ์กŒ๋‹ค! ์•ผํ˜ธ!

 

 

 

 

....?? ๊ทผ๋ฐ ๋ฌธ๋“ ์• ์ดˆ๋ถ€ํ„ฐ ์บ๋ฆญํ„ฐ ํ•˜๋‚˜, ์ฑ…์ƒ ํ•˜๋‚˜, ๋…ธํŠธ๋ถ ํ•˜๋‚˜,, ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋‚˜๋งŒ ๋“ค์–ด๊ฐ€์žˆ๋Š” ๋ฐฐ์—ด ๋งŒ๋“ค์–ด์„œ ์–ด๋Š ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋˜ canvas ์— ๊ทธ๋ ค์ฃผ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋ƒ....???? ๋ผ๋Š” ์ƒ๊ฐ์ด ์Šค์ณ๊ฐ”๋‹ค.... 

 

๋Š์•„๋‹ˆ...???? 

 

๊ทธ๋Ÿผ ์ž. ์ •๋ฆฌํ•ด๋ณด์ž. 

์ง€๊ธˆ ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฑด

1. ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ. 

2. ์บ๋ฆญํ„ฐ, ์ฑ…์ƒ, ๋…ธํŠธ๋ถ ์ด๋ฏธ์ง€๋ฅผ ํ•œ ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ํ•œ๋ฒˆ์— ๊ทธ๋ ค์ฃผ๊ธฐ. 

์ด๋‹ค.

2๋ฒˆ์€ ๋ฆฌํŽ™ํ† ๋ง์— ๊ฐ€๊นŒ์šด ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„  ๋ช…๋ฐฑํ•œ ์˜ค๋ฅ˜์ธ 1๋ฒˆ ๋จผ์ € ํ•ด๊ฒฐํ•ด๋ณด๊ธฐ๋กœ!

2-1. input ์œผ๋กœ ๋ฐ›์•„์˜ค๋Š” ์ปฌ๋Ÿฌ๋ช… ์ €์žฅ

let backgroundColor = 'whtie';


const onColorChange = (event) => {
  context.beginPath();
  const pickColor = event.target.value;
  context.strokeStyle = pickColor;
  context.fillStyle = pickColor;
  backgroundColor = pickColor;
};

์ปฌ๋Ÿฌ๋ฅผ ์ง€์ •ํ•  ๋•Œ ์ „์—ญ ๋ณ€์ˆ˜์ธ backgroundColor ์— ์„ ํƒํ•œ ์ปฌ๋Ÿฌ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 

backgroundColor ๋Š” ๊ธฐ๋ณธ์œผ๋กœ white ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค. 

 

const onCharacterBtnClick = (event) => {
	...
	// ์บ๋ฆญํ„ฐ๊ฐ€ ์ด๋ฏธ ์บ”๋ฒ„์Šค์— ์žˆ๋Š” ๊ฒฝ์šฐ - ์ดˆ๊ธฐํ™” ํ›„ ๊ทธ๋ ค์ฃผ๊ธฐ
    context.beginPath();
    context.fillStyle = backgroundColor;
    context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    console.log(fixedItemArray);
    drawImage(buttonClass);
    fixedItemArray.map((item) => drawImage(item));
  }

๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ฆผ์„ ๋‹ค์‹œ ๊ทธ๋ ค์ค„ ๋•Œ ๊น”์•„์ฃผ๋Š” ๋ฐฐ๊ฒฝ์— background ์ปฌ๋Ÿฌ๋ฅผ ๋„ฃ์–ด์คฌ๋‹ค.

์˜ค! ๊ทธ๋žฌ๋”๋‹ˆ ์ด์ œ ์บ๋ฆญํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๊ฐ€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

๋Œ€์‹  ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๋ฅผ ๋ฐ”๊พธ๋ฉด ๋ชจ๋“  ์•„์ดํ…œ์ด ๋‹ค ์‚ฌ๋ผ์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค.

์•„๊นŒ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์•„์ดํ…œ์„ ๋ฐฐ๊ฒฝ์ด ๋ณ€๊ฒฝ ๋œ ํ›„์— ๋‹ค์‹œ ๋ฟŒ๋ ค์ฃผ์ž.

const onCanvasClick = () => {
  if (isFilling) {
    context.beginPath();
    context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    fixedItemArray.map((item) => drawImage(item));
  }
};

์ฑ„์šฐ๊ธฐ ๋ชจ๋“œ์ผ ๋•Œ canvas ๋ฅผ ํด๋ฆญํ•˜๋ฉด 

์›๋ž˜๋Š” ๋ฐฐ๊ฒฝ์ƒ‰๋งŒ ๋ณ€๊ฒฝ๋์ง€๋งŒ, ์ด์   ๊ธฐ์กด ์•„์ดํ…œ๋“ค๋„ ๊ทธ๋ ค์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค. 

๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝํ•˜๋‹ˆ ์ž๋ฆฌ ๋น„์›€

์–ด.. ์„ ์ƒ๋‹˜...? ์–ด๋”” ๊ฐ€์…จ์–ด์š”...?

 

 

์ด๊ฑธ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์•„๊นŒ ๋– ์˜ฌ๋ ธ๋˜ ์บ๋ฆญํ„ฐ, ๋…ธํŠธ๋ถ, ์ฑ…์ƒ์„ ํ•œ ๋ฐฐ์—ด์•ˆ์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

ํ•„์—ฐ์ ์ธ ๋ฐฉ๋ฒ•์ด์˜€๊ตฐ!

id ๊ฐ€ ์ค‘๊ตฌ๋‚œ๋ฐฉ,,

id ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•ด์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋ฐฐ์—ด ๋‚ด์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. 

๋ฐฐ์—ด ๋ง๊ณ  ์ด๋Ÿด ๋•Œ obj ๋ฅผ ์จ์•ผํ•˜๋Š”๊ฒƒ ์•„๋‹Œ๊ฐ€ ๋ผ๋Š” ์ƒ๊ฐ๋„ ์Šค์ณ๊ฐ”๋‹ค. obj ๋กœ ๋งŒ๋“ค๋ฉด.. ์ข€ ๋” ๊น”๋”ํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์งˆ ๊ฒƒ ๊ฐ™๋‹ค. 

 

๊ทธ๋ž˜์„œ ์•„์ดํ…œ์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์„ ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ,

๋™์‹œ์— ์ดˆ๊ธฐ ์บ”๋ฒ„์Šค๋ฅผ ๋นˆ ์บ”๋ฒ„์Šค๊ฐ€ ์•„๋‹Œ ๊ธฐ๋ณธ ์•„์ดํ…œ์ด ์žˆ๋Š” ๋ชจ์Šต์œผ๋กœ ์„ธํŒ…ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. 

const profileItems = {
  character: 'character_quokka_button',
  desk: 'desk3_button',
  laptop: 'laptop1_button',
};

 

๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋˜ ๊ธฐ์กด์˜ ์ด๋ฏธ์ง€๋“ค์„ ์ˆœ์„œ ์ง€์žฅ์—†์ด ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

const drawOrderedItems = () => {
  Object.values(profileItems).map((item) =>
    drawImage(item)
  );
};

const changeProfileItems = (event) => {
  let buttonClass = event.target.className;
  if (buttonClass.includes('character')) {
    profileItems['character'] = buttonClass;
  }
  if (buttonClass.includes('desk')) {
    profileItems['desk'] = buttonClass;
  }
  if (buttonClass.includes('laptop')) {
    profileItems['laptop'] = buttonClass;
  }
  context.beginPath();
  context.fillStyle = backgroundColor || 'white';
  context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
  drawOrderedItems();
};

๋ฐฐ๊ฒฝ์ปฌ๋Ÿฌ๋„ ์ง€์ •ํ•˜์ง€ ์•Š์•„ ์—†๋‹ค๋ฉด white ๋ฅผ ๋„ฃ์–ด์ฃผ๋„๋ก ์ž‘์„ฑํ–ˆ๋‹ค. 

์• ์ดˆ์— ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ๊ทธ๋ ค์ง€๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด์„œ sort ๋“ฑ์„ ํ™œ์šฉํ•ด ์žฌ์ •๋ ฌ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค. 

 

ํ•˜์ง€๋งŒ ์ด๋ฏธ์ง€ ๋กœ๋“œ๊ฐ€ ๋Šฆ์„ ์‹œ, ์บ๋ฆญํ„ฐ๋งŒ ์žˆ๊ฑฐ๋‚˜ ํ•˜๋Š” ํ˜„์ƒ์ด ๊ณ„์† ๋ฐœ๊ฒฌ๋˜์–ด

window ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

window.onload = drawOrderedItems;

๋‹ค๋ฅธ ๋Œ€์•ˆ์œผ๋กœ DOMCotentLoaded๋„ ์žˆ์—ˆ์ง€๋งŒ, ์ด๋ฏธ์ง€ ๋กœ๋“œ๋Š” ๊ธฐ๋‹ค๋ ค์ฃผ์ง€ ์•Š๋Š”๋‹คํ•ด์„œ 

window.onload ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์ด ์•ฑ์˜ ์ฒ˜์Œ ๋ณด์—ฌ์ง€๋Š” ๊ธฐ๋ณธ ํ”„๋กœํ•„ ๋ชจ์–‘์€ ์ด๋ ‡๋‹ค.

์‹ฌ-ํ”Œ!

 

์ด์ œ ๊ฑฐ์˜ ์™„์„ฑ๋œ ๊ฒƒ ๊ฐ™์€๋ฐ..!!!

ํ•˜๋˜ ์ฐจ์—,,,, ํŽœ ์ปฌ๋Ÿฌ๋ฅผ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ๋„ ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๊ฐ€ ๊ฐ™์ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฌ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

 

์•ฝ๊ฐ„ ๋‹นํ™ฉํ•ด์„œ ์‚ด์ง ๋ฉˆ์นซํ•จใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

ํ  ์•„๋ฌด๋ž˜๋„ ์ด ๋ฌธ์ œ๋Š”,, 

๊ทธ๋ฆฌ๊ธฐ ๋ฒ„ํŠผ๊ณผ ์ฑ„์šฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜, 

์•„๋‹˜ ํ”„๋กœํ•„์„ 1์ฐจ ์™„์„ฑํ•˜๊ณ  ๊พธ๋ฏธ๊ธฐ ๋ชจ๋“œ์— ๋“ค์–ด๊ฐ€์„œ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ํ—ˆ์šฉํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ํ•ด์•ผ๊ฒ ๋‹ค.

๋ญ ๋ฌดํŠผ ๋ฒ„ํŠผ์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์•ผํ•˜๋Š” ๊ฑด ๊ฐ™์€ ๋‚ด์šฉ์ด์ง€๋งŒ. 

 

 

 

 

๋‹ค์Œ์— ํ•ด๊ฒฐํ•  ๋ฌธ์ œ

1. ๊ทธ๋ฆฌ๊ธฐ๋ชจ๋“œ์™€ ์ฑ„์šฐ๊ธฐ ๋ชจ๋“œ ๋ถ„๋ฆฌํ•˜๊ธฐ

2. ๋งŒ์•ฝ ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋„ฃ๋Š”๋‹ค๋ฉด ?

 

 

 

 

 

 

 

์ฐธ๊ณ  ๋ฌธํ•ญ

https://kihyeoksong.tistory.com/71

 

ํŽ˜์ด์ง€ ๋กœ๋“œ ํ›„ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰! window.onload, ready, DOMContentLoaded ๋ญ˜ ์จ์•ผํ• ๊นŒ?!

์ฝ”๋”ฉ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ€๋” ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋ฐ ์—ฌ๋Ÿฌ ์ด์œ ๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ ๊ทธ์ค‘ ํ•˜๋‚˜๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๊ธฐ ์ „์— ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€

kihyeoksong.tistory.com

 

https://developer.mozilla.org/ko/docs/Web/API/Document/DOMContentLoaded_event

 

Window: DOMContentLoaded ์ด๋ฒคํŠธ - Web API | MDN

DOMContentLoaded ์ด๋ฒคํŠธ๋Š” HTML ๋ฌธ์„œ๊ฐ€ ์™„์ „ํžˆ ๊ตฌ๋ฌธ ๋ถ„์„๋˜๊ณ  ๋ชจ๋“  ์ง€์—ฐ๋œ ์Šคํฌ๋ฆฝํŠธ(<script defer src="…">์™€ <script type="module">)๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋˜๊ณ  ์‹คํ–‰๋  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€, ์„œ๋ธŒํ”„๋ ˆ์ž„, ๋น„๋™๊ธฐ ์Šคํฌ

developer.mozilla.org

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

 

Window: load event - Web APIs | MDN

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images, except those that are loaded lazily. This is in contrast to DOMContentLoaded, which is fired as soon as the page DO

developer.mozilla.org

 

728x90
๋ฐ˜์‘ํ˜•