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

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

[2] Canvas API ํ™œ์šฉํ•˜๊ธฐ

728x90

Canvas API ํ™œ์šฉํ•˜๊ธฐ

 

canvas api ๋Š” ๋…ธ๋งˆ๋“œ์ฝ”๋” ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์ฒ˜์Œ ์ ‘ํ–ˆ๋Š”๋ฐ, 

์‚ฌ์‹ค ์ฒ˜์Œ ๋“ค์—ˆ์„ ๋• ๋„ˆ๋ฌด ๋ณต์žกํ•œ ๋Š๋‚Œ์ด์—ˆ๊ณ , ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์•˜๋‹ค.

(์ •๋ง ๋ง ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ ์น˜๊ธฐ๋งŒ ํ–ˆ์—ˆ๋‹ค)

์ดˆ์‹ฌ์ž๋Š” ๋…ธ๋งˆ๋“œ์ฝ”๋” ๊ฐ•์˜๋ฅผ ์ถ”์ฒœํ•œ๋‹ค! ๋ฌด๋ฃŒ๊ณ  ๋‹ˆ๊ผฌ์Œค์ด ์„ค๋ช…๋„ ์ž˜ํ•ด์ฃผ์‹ฌ!

 

์•ฝ ๋‘๋‹ฌ ๊ฐ„์˜ ๊ฐœ๋ฐœ ๊ณต๋ถ€ ๊ณต๋ฐฑ์ด ์žˆ์—ˆ๋˜ ํ„ฐ๋ผ, 

๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ cavas api ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์„ ๋ณต์Šตํ•˜๋ฉฐ mdn ๋ฌธ์„œ๋„ ๊ฐ™์ด ๋ดค๋Š”๋ฐ, 

์˜ˆ์ „์˜ ๊ธฐ์–ต์ด ๋ฌด์ƒ‰ํ•˜๊ฒŒ ์ดํ•ด๊ฐ€ ์ž˜ ๋๊ณ  ์žฌ๋ฐŒ์—ˆ๋‹ค.

๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ๋“ค์„ ๋ณต์žกํ•˜๊ฒŒ๋งŒ ์ƒ๊ฐํ•œ ๊ฒƒ ์•„๋‹Œ๊ฐ€ ์‹ถ๋‹ค. 

 

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

๐Ÿ‘‡ mdn ํ•œ๊ตญ์–ด ๋ฒ„์ „ (์ „๋ถ€ ๋‹ค ๋ฒˆ์—ญ์ด ๋˜์–ด์žˆ์ง„ ์•Š๋‹ค)

 

์บ”๋ฒ„์Šค(Canvas) ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• - Web API | MDN

<canvas> HTML ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ์ด ํŠœํ† ๋ฆฌ์–ผ์„ ์‹œ์ž‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ํŽ˜์ด์ง€์˜ ๋์—์„œ ์บ”๋ฒ„์Šค 2D ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ๋˜๊ณ , ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ๋ฅผ ๊ทธ๋ฆฌ๊ฒŒ

developer.mozilla.org

 

๐Ÿ‘‡ mdn ์˜์–ด ๋ฒ„์ „

 

 

Canvas API - Web APIs | MDN

The Canvas API provides a means for drawing graphics via JavaScript and the HTML <canvas> element. Among other things, it can be used for animation, game graphics, data visualization, photo manipulation, and real-time video processing.

developer.mozilla.org

 

๐Ÿ‘‡ ๋…ธ๋งˆํŠธ์ฝ”๋” ๊ฐ•์˜ - ๋ฌด๋ฃŒ

 

๋ฐ”๋‹๋ผ JS๋กœ ๊ทธ๋ฆผ ์•ฑ ๋งŒ๋“ค๊ธฐ – ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders

HTML, CSS, JS

nomadcoders.co

 

 

Canvas API ๋ž€ ๋ฌด์—‡์ธ๊ฐ€

๋ง ๊ทธ๋Œ€๋กœ ์›นํ™”๋ฉด์„ ์บ”๋ฒ„์Šค๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์žฌ๋ฐŒ๋Š” ๋„๊ตฌ๋‹ค. 

cavan api ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” WebGL ์„ ์‚ฌ์šฉํ•˜๋ฉด 2D ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ 3D ์ž‘์—…๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

https://flaviocopes.com/canvas/

 

HTML Canvas API Tutorial

A guide to the Canvas API, one way offered by browsers to draw to the screen

flaviocopes.com

์ด ์‚ฌ์ดํŠธ์—์„œ๋„ canvas api ๋ฅผ ์ด์šฉํ•œ ์žฌ๋ฐŒ๋Š” ์˜ˆ์‹œ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

1. canvas ์‚ฌ์šฉํ•˜๊ธฐ

canvas ์†์„ฑ์„ ์ด์šฉํ•ด canvas ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

<canvas></canvas>

canvas ์š”์†Œ๋Š” width ์™€ height ๋‘๊ฐ€์ง€ ์†์„ฑ๋งŒ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์†์„ฑ๋“ค์€ ์„ ํƒ์‚ฌํ•ญ์ด๋‹ค.

๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ HTML ์—์„œ ๋”ฐ๋กœ ์†์„ฑ์„ ์จ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

JS ์™€ CSS ์—์„œ canvas ์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

2. ๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ

 

๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ์— ์•ž์„œ์„œ canvas์˜ ์™ผ์ชฝ์œ„ ๋ชจ์„œ๋ฆฌ๊ฐ€ ๊ธฐ์ค€์ด ๋œ๋‹ค. (0,0) ์ง€์ . 

JS ์—์„œ context ๋ฅผ ์„ ์–ธํ•ด ์ค€ ๋’ค ๋ญ”๊ฐ€๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

 

function draw() {
  var canvas = document.getElementById("canvas");
  if (canvas.getContext) {
    var ctx = canvas.getContext("2d");

    ctx.beginPath();
    ctx.moveTo(75, 50);
    ctx.lineTo(100, 75);
    ctx.lineTo(100, 25);
    ctx.fill();
  }
}

 

๋ฉ”์†Œ๋“œ ์„ค๋ช…

beginPath()

- ์ด์ „์˜ ๋„ํ˜•๊ณผ ๋ณ„๊ฐœ๋กœ ์ƒˆ๋กœ์šด ๋„ํ˜•์ด๋‚˜ ์„ ์„ ๊ทธ๋ฆด ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

- ์ปฌ๋Ÿฌ/์„ ์˜ ๊ตต๊ธฐ ๋“ฑ ๋‹ค๋ฅธ ์š”์†Œ ๊ทธ๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ด์ „์— ๊ทธ๋ฆฐ ๋„ํ˜•๋“ค๊ณผ ๋™์ผํ•˜๋‹ค๊ณ  ๊ฐ„์ฃผ๋œ๋‹ค. 

 

moveTo(x, y)

- ํŽœ์„ ์˜ฎ๊ธด๋‹ค๊ณ  ์„ค๋ช… ๋˜์–ด์žˆ๋Š”๋ฐ, ๊ทธ๊ฒƒ๋ณด๋‹จ ํŽœ์„ ๋“ค๊ณ  ์žˆ๋Š” ์†์ด ํ•ด๋‹น ์ขŒํ‘œ๋กœ ์ด๋™ํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋Š” ๊ฒŒ ๋” ํŽธํ–ˆ๋‹ค. 

- (x,y) ์ง€์  ์œ„์— ๋– ์žˆ๋Š” ํŽœ์„ ์žก์€ ์†์„ ์ƒ์ƒํ•ด๋ณด๋ฉด ๋จ!

- ์‹ค์ œ๋กœ ๊ทธ๋ ค์ง€๋Š” ๊ฑด ์—†์Œ. 

- ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ๊ฒฝ๋กœ๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋”ฐ.

 

lineTo(x,y)

- ํ˜„์žฌ์˜ ์œ„์น˜์—์„œ (x, y) ์ง€์ ๊นŒ์ง€ ์‹ค์ œ๋กœ ๊ทธ๋ ค์งˆ ๋ฐ‘๊ทธ๋ฆผ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

- moveTo(1,1) ํ›„ lineTo(3,3) ๋ฅผ ์ ๋Š”๋‹ค๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์˜ˆ๋น„ ์„ ์ด ๊ทธ์–ด์ง€๋Š” ๊ฒƒ.  

(์ดํ›„ ์ƒ‰์น  ํ˜น์€ ์„ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋„ํ˜•์ด ๋ณด์ด์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค)

 

fill()

- lineTo() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋งŒ๋“  ๋„ํ˜•์˜ ๋ฉด์ ์„ ์ฑ„์šด๋‹ค. 

- fillStyle = 'color' ๋กœ ์ปฌ๋Ÿฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

stroke()

- lineTo() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋งŒ๋“  ๋„ํ˜•์˜ ์„ ์„ ์น ํ•œ๋‹ค.

- strokeStyle = 'color'๋กœ ์ปฌ๋Ÿฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

3. ์ด๋ฏธ์ง€ ์‚ฌ์šฉํ•˜๊ธฐ

https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images

 

Using images - Web APIs | MDN

Until now we have created our own shapes and applied styles to them. One of the more exciting features of <canvas> is the ability to use images. These can be used to do dynamic photo compositing or as backdrops of graphs, for sprites in games, and so forth

developer.mozilla.org

 

const img = new Image(); // Create new img element
img.src = "myImage.png"; // Set source path

ctx.drawImage(image, dx, dy, dWidth, dHeight)

Image() ๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ , src ์— ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์จ์ฃผ๋ฉด ๋œ๋‹ค. 

์ดํ›„ drawImage() ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์บ”๋ฒ„์Šค์— ๊ทธ๋ ค์ฃผ๋ฉด ๋˜๋Š”๋ฐ, 

drawImage(์„ ์–ธํ•œ ์ด๋ฏธ์ง€, ์ด๋ฏธ์ง€๊ฐ€ ์‹œ์ž‘ํ•  x์ถ• ์œ„์น˜, ์ด๋ฏธ์ง€๊ฐ€ ์‹œ์ž‘ํ•  y์ถ• ์œ„์น˜, ์ด๋ฏธ์ง€์˜ x ์ถ• ๋„ˆ๋น„, ์ด๋ฏธ์ง€์˜ y์ถ• ๋„ˆ๋น„) ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

context.drawImage(
        image,
        0,  
        0,
        CANVAS_WIDTH,
        CANVAS_HEIGHT
);

 ๊ฐ€๋ น ์ด๋ ‡๊ฒŒ ์จ์ค€ ๊ฒฝ์šฐ์—” 

์ด๋ฏธ์ง€๋ฅผ ์บ”๋ฒ„์Šค (0,0) ์˜ ์ง€์ ๋ถ€ํ„ฐ canvas ํฌ๊ธฐ๋งŒํผ ๊ทธ๋ฆฌ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ, 

๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์บ”๋ฒ„์Šค ์ „์ฒด๋ฅผ ๊ฝ‰ ์ฑ„์šฐ๊ฒ ๋‹จ ๋œป์ด๋‹ค. 

 

ํŒŒ์ผ์ฒจ๋ถ€ํ•œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„ onload() ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ํ™œ์šฉํ•ด ์ด๋ฏธ์ง€๋ฅผ ๊ทธ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค.

const fileInput = document.getElementById("file");

function onFileChange(event) {
  const file = event.target.files[0];
  const url = URL.createObjectURL(file);
  const image = new Image();
  image.src = url;
  image.onload = function () {
    ctx.drawImage(image, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    fileInput.value = null;
  };
}

fileInput.addEventListener("change", onFileChange);

ํŒŒ์ผ์ฒจ๋ถ€๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œ

 

 

 

canvas Api ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์— ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ดค๋‹ค.

์•ž์œผ๋กœ ๋ณด์ถฉํ•  ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ๋” ์ถ”๊ฐ€ํ•ด๋ณผ ์˜ˆ์ •!

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•