JS) Canvas API

Ceciliaยท2022๋…„ 12์›” 27์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
26/36
post-thumbnail

https://developer.mozilla.org/ko/docs/Web/API/Canvas_API




๐Ÿ” Canvas API


Canvas API๋Š” JavaScript์™€ HTML <canvas> ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ†ตํ•ด ๊ทธ๋ž˜ํ”ฝ์„ ๊ทธ๋ฆฌ๊ธฐ์œ„ํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค๋„ ์• ๋‹ˆ๋ฉ”์ด์…˜, ๊ฒŒ์ž„ ๊ทธ๋ž˜ํ”ฝ, ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”, ์‚ฌ์ง„ ์กฐ์ž‘ ๋ฐ ์‹ค์‹œ๊ฐ„ ๋น„๋””์˜ค ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.





๋จผ์ €, HTML์—์„œ ์•„๋ž˜์˜ <canvas> ํƒœ๊ทธ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•œ๋‹ค.

<canvas id="canvas"></canvas>


๊ทธ๋ฆฌ๊ณ  Javascript์—์„œ
<canvas> ํƒœ๊ทธ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ๋’ค getContext() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์บ”๋ฒ„์Šค์˜ ๋“œ๋กœ์ž‰ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 150, 100);


์ผ์ • ์ง€์ ๋ถ€ํ„ฐ ์›ํ•˜๋Š” ์ง€์ ๊นŒ์ง€ ๋ผ์ธ์„ ๊ทธ๋ฆฌ๊ณ ์ž ํ•œ๋‹ค๋ฉด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ ค๋ณผ ์ˆ˜ ์žˆ๋‹ค.

moveTo(x, y)
lineTo(x, y)


moveTo์™€ lineTo๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.beginPath(); // Start a new path
ctx.moveTo(30, 50); // Move the pen to (30, 50)
ctx.lineTo(150, 100); // Draw a line to (150, 100)
ctx.stroke(); // Render the path
profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€