<img>
와 비슷한데, alt
와 src
속성은 없다.<canvas>
에 그려지는 이미지에는 영향을 주지 않는다.<video>
, <audio>
, <picture>
처럼 오래된 브라우저를 위한 대체 컨텐츠를 지원한다.</canvas>
)가 필요하다.<!-- 기본 구조 -->
<canvas id="canvas1" width="150" height="150"></canvas>
<!-- 대체 컨텐츠 선언한 경우 -->
<canvas id="canvas1" width="150" height="150">
<img src="images/clock.png" width="150" height="150" alt=""/>
</canvas>
<canvas>
엘리먼트는 고정 크기의 드로잉 영역을 생성하고 하나 이상의 렌더링 컨텍스(rendering contexts)를 노출하여, 출력할 컨텐츠를 생성하고 다루게 됩니다.
getContext()
메서드는 랜더링 컨텍스트와 함수들을 반환한다.<canvas>
에서 다른 contextType을 전달하거나 다른 <canvas>
에서 getContext()
를 호출// 기본 구조 const _ctx = canvasElement.getContext(contextType);
const $canvas = document.querySelector('#canvas1');
const _ctx = $canvas.getContext('2d');
fillRect(x, y, width, height)
: 색칠된 직사각형 그리기clearRect(x, y, width, height)
: 직사각형 윤곽선 그리기strokeRect(x, y, width, height)
: 특정 부분 삭제fillStyle
: 도형을 채우는 색, stringstrokeStyle
: 도형의 윤곽선 색, stringimageSmoothingEnabled
: 도형을 확대했을 때, 깨진 픽셀을 흐리게 처리할 건지 설정, booleanfont
: 글자를 그릴 때 사용되는 현재 글자 스타일, string,"크기 스타일"fillText("text", x, y)
: 문자열(text)를 정해진 위치(x, y)에 출력textBaseline
: 베이스 라인 설정, string (ex. "top", "hanging");strokeText("text", x, y)
: 테두리 있는 문자열(text)를 정해진 위치(x, y)에 출력출처
MDN - Canvas
MDN - getContext
MDN - Canvas/텍스트 그리기
MDN - Canvas/도형 그리기