<canvas>태그를 이용한 리사이징(2)

unow30·2022년 3월 22일
0

링크: Basic usage of canvas

A skeleton template

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"/>
  <script type="application/javascript">
    function draw() {
      var canvas = document.getElementById('canvas');
      if (canvas.getContext) {
        var ctx = canvas.getContext('2d');

        ctx.fillStyle = 'rgb(200, 0, 0)';
        ctx.fillRect(10, 10, 50, 50);

        ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';
        ctx.fillRect(30, 30, 50, 50);
      }
    }
  </script>
 </head>
 <body onload="draw();">
   <canvas id="canvas" width="150" height="150"></canvas>
 </body>
</html>
  • <body>의 page load event시 draw()가 실행한다.
  • <canvas>를 선택하고 렌더링 컨텍스트에 접근하기 위해 getContext('2d')를 실행한다.
  • fillStyle로 그려질 이미지의 스타일(색상)을 정한다.
  • fillRect로 x좌표,y좌표,width,height를 지정한다.

0개의 댓글