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

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

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

728x90

 

โ˜๏ธ๋ฌธ์ œ์  : ์บ๋ฆญํ„ฐ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ชจ๋“  ์š”์†Œ ์ดˆ๊ธฐํ™”


 

์ด์ „์— ๋งŒ๋“  ๋ฒ„ํŠผ์ด ์ž‘๋™ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด๋ฉด ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ, 

๋ฐ”๋กœ ์บ”๋ฒ„์Šค ๋‚ด์— ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ทจ์†Œ ๋ฒ„ํŠผ (์—†์• ๊ธฐ ๋ฒ„ํŠผ)์„ ๋ˆ„๋ฅด๋ฉด

ํ•ด๋‹น ์š”์†Œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ง€์›Œ์ง€๋Š” ์ ์ด์—ˆ๋‹ค. 

 

๊ฐ€๋ น ์บ๋ฆญํ„ฐ๋ฅผ ํด๋ฆญ & ๋…ธํŠธ๋ถ ํด๋ฆญ ํ•ด์„œ 

๋…ธํŠธ๋ถ์ด ์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“  ํ›„, ์ฑ…์ƒ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์–ด ์ฑ…์ƒ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด

ํฐ์ƒ‰์œผ๋กœ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.

์ฆ‰, ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ ํ”„๋กœํ•„์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

 

 

 

 

 

๐Ÿ€์›์ธ : canvas api ๋Š” ํ•œ๊ฒน!


I think maybe you misunderstand what a Canvas is.A canvas is essentially a 2 dimensional grid of pixels along an 'X' axis and a 'Y' axis. You use the API to draw pixels onto that canvas, so when you draw an image you're basically drawing the pixels that make up that image onto your canvas. The reason there is NO method that lets you just remove an image, is because the Canvas doesn't know there's an image there in the first place, it just see pixels.

https://stackoverflow.com/questions/3458244/removing-an-image-from-a-canvas-in-html5

 

Removing an image from a canvas in HTML5

there's an example, which loads 2 images: canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); var img1 = new Image(); img.src = "/path/to/image/img1.png"; ...

stackoverflow.com

 

์ฒ˜์Œ์—” ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ๋งˆ๋‹ค ๊ทธ ์š”์†Œ๋“ค์ด ๋ ˆ์ด์–ด๋กœ ์žˆ๋Š” ์ค„ ์•Œ์•˜๋‹ค.

๊ทธ๋ž˜์„œ ๋’ค๋กœ ๊ฐ€๊ธฐ๋‚˜, ๋ฐฉ๊ธˆ ๋„ฃ์€ ์ด๋ฏธ์ง€๋งŒ ๋บ„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ๋“ฑ

์ง€์šฐ๋Š” ๋ฐฉ๋ฒ•์ด ํฐ์ƒ‰์œผ๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ ๋ง๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ ์ฐพ์•„๋ณด๋‹ค๊ฐ€

๋‚ด๊ฐ€ canvas api ์— ๋Œ€ํ•ด ์ž˜๋ชป ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค๋Š” ๊ฑธ ์ฐพ์•˜๋‹ค.

 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ํฌํ† ์ƒต์ฒ˜๋Ÿผ ๋ ˆ์ด์–ด๋กœ ์ž‘์šฉํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, 

์ •๋ง ์บ”๋ฒ„์Šค์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ๋ฉด ์œ„์— ๊ณ„์† ๋ง๋Œ€์„œ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ์‹์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. 

๊ทธ๋ฆผ์„ ๋„ฃ์—ˆ๋“ , ๋ฐฐ๊ฒฝ์„ ๋„ฃ์—ˆ๋“  ์ดํ›„์— ๊ฐ™์€ ์ง€์ ์— ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ์š”์†Œ๋ฅผ ๊ทธ๋ ค๋„ฃ์œผ๋ฉด 

ํ”ฝ์…€์ด ๋Œ€์ฒด๋˜์–ด ํ•˜๋‚˜์˜ ์บ”๋ฒ„์Šค๋กœ ์ž‘์šฉํ•˜๋Š” ๊ฒƒ..!

 

 

๊ทธ๋Ÿผ ์• ์ดˆ์— ๋‚ด๊ฐ€ ์ด๋ฏธ์ง€ ํ•˜๋‚˜๋งŒ ๋นผ๊ณ  ์ด์ „์œผ๋กœ ๋Œ๋ฆฌ๊ณ  ์‹ถ์€ ๋ฐฉ์‹์ด ์ „ํ˜€ ๋จนํžˆ์ง€ ์•Š๋Š” ์ƒํ™ฉ.

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์•„์•ผ ํ–ˆ๋‹ค.

 

 

๐Ÿ’กํ•ด๊ฒฐ ๋ฐฉ๋ฒ• : ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ


 

 

์บ๋ฆญํ„ฐ์˜ ๊ฒฝ์šฐ, ๊ท€ ๋ชจ์–‘์ด ๋‹ฌ๋ผ ๊ฒน์น˜๋ฉด ๊ณค๋ž€ํ•˜๋‹ค.

์ด๋ ‡๊ฒŒ ๋˜์–ด๋ฒ„๋ฆผ

// ๊ฐ™์€ ์นดํ…Œ๊ณ ๋ฆฌ ๋‚ด์— ์žˆ๋Š” ๊ฒƒ๋“ค๋งŒ ์„œ๋กœ ๋Œ€์น˜ ๋˜๋„๋ก, ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๋Œ€์น˜ ๋˜์ง€ ์•Š๋„๋ก
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);
    drawImage(buttonClass);
    isCharacterOnCanvas = true;
  }
};

๊ทธ๋ž˜์„œ ์บ๋ฆญํ„ฐ๋ฅผ ๋ฐ”๊ฟ€ ์‹œ, ์บ๋ฆญํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํฐ์ƒ‰ ๋ฐฐ๊ฒฝ์„ ๊น ํ›„ ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ฆฌ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

 

 

๋™๋ฌผ์„ ์ œ์™ธํ•œ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์ƒ‰๋งŒ ๋‹ค๋ฅธ ๋™์ผํ•œ ์ด๋ฏธ์ง€์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฎ์–ด์”Œ์šด๋‹ค.

const onDeskBtnclick = (event) => {
  let buttonClass = event.target.className;
  console.log(buttonClass);
  context.beginPath();
  drawImage(buttonClass);
  isDeskOnCanvas = true;
};

const onLaptopBtnClick = (event) => {
  let buttonClass = event.target.className;
  context.beginPath();
  drawImage(buttonClass);
  isLaptopOnCanvas = true;
};

 

(์ง€๊ธˆ ๋ณด๋‹ˆ ๋กœ์ง์ด ์™„์ „ ๋˜‘๊ฐ™์•„์„œ ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค.

์บ”๋ฒ„์Šค ์œ„์— ํ•ด๋‹น ์นดํ…Œ๊ณ ๋ฆฌ ๊ทธ๋ฆผ์ด ์žˆ๋ƒ์—†๋ƒ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋Š” ์ œ๊ฑฐํ•ด์•ผ๊ฒ ๋‹ค.

๋™๋ฌผ ์นดํ…Œ๊ณ ๋ฆฌ ์™ธ์— ์บ”๋ฒ„์Šค ์œ„์— ์ด๋ฏธ์ง€ ์—ฌ๋ถ€๊ฐ€ ํ•„์š”์—†์œผ๋‹ˆ!) 

 

 

 

์ด๋ ‡๊ฒŒ ๋กœ์ง์„ ๋ฐ”๊พผ ํ›„ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค.

 

๋‚˜๋ฆ„ ์›ํ•˜๋Š” ๋Š๋‚Œ์ด ๋๋‹ค.

 

 

์ €์žฅ๋œ ์ด๋ฏธ์ง€

 

 

 

 

 

ํ•˜์ง€๋งŒ 

1. ๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๋ฅผ ๋ฐ”๊ฟ€ ์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•ด์•ผํ•˜๋Š” ์ 

2. desk ์™€ laptop ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด desk ๊ฐ€ laptop ์ด๋ฏธ์ง€ ์œ„๋กœ ์˜ฌ๋ผ์˜ค๋Š” ์ 

์ด๋ ‡๊ฒŒ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์šด ์ ์ด ์žˆ๋‹ค. 

 

 

 

 

์ง€๊ธˆ ์ƒ๊ฐํ–ˆ์„ ๋•Œ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์šด UX๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

1. page๋ฅผ ๋งŒ๋“ค์–ด ๋‹จ๊ณ„๋ณ„๋กœ ๊ณ ๋ฅด๊ฒŒ ๋งŒ๋“ ๋‹ค. (์›น์•ฑ ํ˜•์‹)

๋ฐฐ๊ฒฝ ์ปฌ๋Ÿฌ๋ฅผ ์„ ํƒํ•˜์„ธ์š” -> ์บ๋ฆญํ„ฐ๋ฅผ ์„ ํƒํ•˜์„ธ์š” -> ์ฑ…์ƒ์„ ์„ ํƒํ•˜์„ธ์š” -> ๋…ธํŠธ๋ถ์„ ์„ ํƒํ•˜์„ธ์š” ํ•˜๋Š” ์‹์œผ๋กœ 

๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ ์ž๋ž‘ํ•˜๊ธฐ & ์ €์žฅํ•˜๊ธฐ ๋ฒ„ํŠผ ๋ณด์—ฌ์ฃผ๊ธฐ

 

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

์Œ ํ•ด๋ด์•ผ ์•Œ๊ฒ ์ง€๋งŒ, 

์ด์ „ ๋‹จ๊ณ„์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ (๋ฐฐ๊ฒฝ์€ ํˆฌ๋ช…์œผ๋กœ)

=> ๋ฐฐ๊ฒฝ์ด ๋ณ€๊ฒฝ ๋  ๋•Œ -> ๋ฐฐ๊ฒฝ ๊น”๊ณ  ๋‹ค์‹œ ๋กœ๋“œ

=> ๋‹ค๋ฅธ ๋ฒ„ํŠผ ๋ˆ„๋ฅผ ๋•Œ -> ์—…๋ฐ์ดํŠธ

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ...?

 

3. fabric.js ๋ฅผ ์‹œ๋„ํ•ด๋ณด๊ธฐ

์•„๊นŒ ์บ”๋ฒ„์Šค์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ–ˆ๋˜ ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์—์„œ fabric.js ๋ฅผ ์จ๋ณด๋ผ๋Š” ๋ง์ด ์žˆ์—ˆ๋‹ค.

์–ด๋–ค ๊ธฐ๋Šฅ๋“ค์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„์ง€ ์‚ดํŽด๋ณผ ์˜ˆ์ •์ด๋‹ค.

 

 

์šฐ์„ ์€ 2๋ฒˆ ๋ฐฉ์‹์„ ๋จผ์ € ํ•ด๋ณธ ํ›„, 3๋ฒˆ์„ ์‚ดํŽด๋ณด๊ณ , 1๋ฒˆ๋„ ๊ณ ๋ คํ•ด๋ณผ ์ƒ๊ฐ์ด๋‹ค.

๊ทผ๋ฐ ์–ด์ฐจํ”ผ ๋‚˜์ค‘์— ์ด๋ฏธ์ง€ ์ž๋ž‘ํ•˜๊ธฐ (๊ณต์œ ํ•˜๊ธฐ) ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด

ํ˜„ ํŽ˜์ด์ง€ ์™ธ์— ๋‹ค๋ฅธ URL ๋„ ํ•„์š”ํ•œ ์ ์„ ๊ณ ๋ คํ•ด์„œ ์ž˜ ์ƒ๊ฐํ•ด๋ณด์Ÿˆ. 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•