HTML5 API - Canvas - 1
HTML5 API 기술 중 가장 많은 설명과 예제가 필요한 기능을 하나 꼽으라면 단연 이번 시간에 설명하려는 Canvas API를 꼽을 수 있을 것입니다.
그만큼 장기간의 학습 시간이 필요한 기술이며, 기존 플래쉬와 같은 그래픽 프로그램에 익숙하지 않은 개발자는 더 욱 오랜 시간에 걸쳐 학습해야 하기 때문입니다.(저 역시도 그래픽과 관련된 프로그램을 전문적으로 다루고 있지 않아 상황 별 좋은 소스를 보여드리긴 힘들꺼 같습니다.ㅠㅠ;;;)
아래는 API에 대한 설명과 관련 예제를 정리한 내용입니다.
1. HTMLCanvasElement.getContext(id): canvas context객체를 생성한다.
id: 반환 될 컨텍스트 객체를 구별하는 아이디 문자열('2d', '3d(현재 미지원)')
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
function getContext() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
-
ctx.beginPath();
-
ctx.rect(10, 10, 85, 85);
-
-
ctx.rect(105, 10, 85, 85);
-
ctx.stroke();
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
getContext();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
2. CanvasRenderingContext2D.fillStyle: 경로(Path)를 통해 그려지는 도형 내부에 채워지는 스타일을 지정한다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function fillStyle() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
ctx.beginPath();
-
ctx.moveTo(10, 10);
-
ctx.lineTo(90, 10);
-
ctx.lineTo(90, 90);
-
ctx.closePath();
-
-
// 스타일 지정
-
ctx.fillStyle = 'red';
-
-
ctx.fill();
-
ctx.stroke();
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
fillStyle();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
3. CanvasRenderingContext2D.fillRect(x, y, w, h): 사각영역에 색을 칠한다.
x: 영역의 왼쪽 위 가로 좌표
y: 영역의 왼쪽 위 세로 좌표
w: 가로 사이즈
h: 세로 사이즈
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function fillRect() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 스타일 지정
-
ctx.fillStyle = 'red';
-
ctx.fillRect(10, 10, 90, 90);
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
fillStyle();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
4. CanvasRenderingContext2D.clearRect(x, y, w, h): 사각영역을 배경으로 채워 삭제된 효과를 준다.
x: 영역의 왼쪽 위 가로 좌표
y: 영역의 왼쪽 위 세로 좌표
w: 가로 사이즈
h: 세로 사이즈
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function clearRect() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 스타일 지정
-
ctx.fillStyle = 'red';
-
ctx.fillRect(10, 10, 90, 90);
-
-
// 클리어
-
ctx.clearRect(15, 15, 80, 80);
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
clearRect();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
5. CanvasRenderingContext2D.strokeRect(x, y, w, h): 사각영역의 테두리를 그린다.
x: 영역의 왼쪽 위 가로 좌표
y: 영역의 왼쪽 위 세로 좌표
w: 가로 사이즈
h: 세로 사이즈
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function strokeRect() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
ctx.strokeRect(10, 10, 90, 90);
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
strokeRect();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
6. CanvasRenderingContext2D.strokeStyle: 테두리 스타일을 지정한다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function strokeStyle() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 스타일 지정
-
ctx.strokeStyle = 'red';
-
-
ctx.strokeRect(10, 10, 90, 90);
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
strokeStyle();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
7. CanvasRenderingContext2D.beginPath(): 그리는 경로의 시작에 반드시 붙어준다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function beginPath() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 경로에 시작을 알려준다.
-
ctx.beginPath();
-
-
ctx.moveTo(10, 10);
-
ctx.lineTo(90, 10);
-
-
ctx.stroke();
-
-
// 경로에 마지막을 알려준다.
-
ctx.closePath();
-
-
-
// 새롭게 경로를 시작한다.
-
ctx.beginPath();
-
-
ctx.moveTo(190, 10);
-
ctx.lineTo(100, 10);
-
ctx.stroke();
-
-
ctx.closePath();
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
beginPath();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
8. CanvasRenderingContext2D.closePath():
1. 그리는 경로를 종료한다.
2. 마지막 지점에서 시작점까지 직선을 그린다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function closePath() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 경로에 시작을 알려준다.
-
ctx.beginPath();
-
-
ctx.moveTo(10, 10);
-
ctx.lineTo(90, 10);
-
ctx.lineTo(10, 90);
-
-
// 마지막 지점에서 시작점까지 직선을 그린다.
-
ctx.closePath();
-
-
ctx.stroke();
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
closePath();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
9. CanvasRenderingContext2D.moveTo(x, y): 시작 포인트를 이동 시킨다.
x: 시작점의 가로 좌표
y: 시작점의 세로 좌표
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function moveTo() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// 경로에 시작을 알려준다.
-
ctx.beginPath();
-
-
ctx.moveTo(10, 10);
-
ctx.moveTo(20, 20);
-
-
// 최종 시작점은 x: 30, y: 30 좌표 지점에서 시작된다.
-
ctx.moveTo(30, 30);
-
-
ctx.lineTo(90, 30);
-
-
// 마지막 지점에서 시작점까지 직선을 그린다.
-
ctx.closePath();
-
-
ctx.stroke();
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
moveTo();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
10. CanvasRenderingContext2D.lineTo(x, y): 시작점부터 x, y 위치까지 직선을 그린다.
x: 직선의 종료 가로 좌표
y: 직선의 종료 세로 좌표
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function lineTo() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
ctx.beginPath();
-
-
ctx.moveTo(10, 10);
-
ctx.moveTo(20, 20);
-
-
ctx.moveTo(30, 30);
-
-
// 시작점부터 x, y 위치까지 직선을 그린다.
-
ctx.lineTo(90, 30);
-
-
ctx.closePath();
-
-
ctx.stroke();
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
lineTo();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
11. CanvasRenderingContext2D.rect(x, y, w, h): 경로가 지정된 사각형을 그린다.
x: 사각 영역의 왼쪽 위 가로 좌표
y: 사각 영역의 왼쪽 위 세로 좌표
w: 사각 영역의 가로 폭
h: 사각 영역의 세로 폭
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function rect() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// rect() 메서드는 이전에 배운 fillRect() 메서드와는 달리 경로가 지정된 도형의 외각선을 그리는 stroke() 메소드를 사용해야한다.
-
ctx.beginPath();
-
-
ctx.rect(10, 10, 90, 90);
-
ctx.stroke();
-
-
ctx.closePath();
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
rect();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
12. CanvasRenderingContext2D.arc(x, y, radius, startAngle, endAngle, anticlockwise): 원이나 호를 그린다.
x: 호가 시작되는 가로 좌표
y: 호가 시작되는 세로 좌표
radius : 호의 반지름
startAngle: 호의 시작 각도 라디안(radian) 단위
endAngle: 호의 종료 각도 라디안(radian) 단위
anticlockwise: 호를 그리는 반향이 반시계 반향 일 때 true
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function arc() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
-
ctx.beginPath();
-
-
ctx.arc(40, 120, 40, 0, (Math.PI / 180) * -90, false);
-
ctx.stroke();
-
-
ctx.closePath();
-
-
ctx.beginPath();
-
-
ctx.arc(40, 40, 40, (Math.PI / 180) * 90, 0, false);
-
ctx.stroke();
-
-
ctx.closePath();
-
-
// 원둘레
-
ctx.beginPath();
-
-
ctx.arc(40, 160, 40, 0, (Math.PI * 2), false);
-
ctx.stroke();
-
-
ctx.closePath();
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
arc();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
13. CanvasRenderingContext2D.arcTo(x1, y1, x2, y2, radius): 두 개의 접점을 연결하는 원호를 그린다.
마지막으로 지정된 현재 좌표(x0,y0)에서 (x1, y1)을 연결하는 직선, 그리고 (x1, y1)과 (x2, y2)를 연결하는 가상의 직선에 동시에 접하는 반지름(radius)의 가상의 원을 기준으로 호를 그린다.
x1: 호가 인접하는 첫번째 직선의 종료 가로 좌표
y1: 호가 인접하는 첫번째 직선의 종료 세로 좌표
x2: 호가 인접하는 두번째 직선의 종료 가로 좌표
y2: 호가 인접하는 두번째 직선의 종료 세로 좌표
radius : 호의 반지름
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function arcTo() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
-
// 원둘레
-
ctx.beginPath();
-
ctx.moveTo(100, 0);
-
ctx.arcTo(200, 0, 200, 200, 100);
-
-
//ctx.closePath();
-
ctx.strokeStyle = 'red';
-
ctx.stroke();
-
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
arcTo();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
14. CanvasRenderingContext2D.stroke(): 경로에 지정된 외각선을 그린다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function stroke() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
-
// 원둘레
-
ctx.beginPath();
-
ctx.moveTo(100, 0);
-
ctx.arcTo(200, 0, 200, 200, 100);
-
-
//ctx.closePath();
-
ctx.strokeStyle = 'red';
-
-
// 외각선을 그린다.
-
ctx.stroke();
-
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
stroke();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
15. CanvasRenderingContext2D.fill(): 경로에 지정된 도형의 색을 채운다.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Canvas</title>
-
</head>
-
<body>
-
<script type="text/javascript">
-
//<![CDATA[
-
-
-
function fill() {
-
-
var cvs = document.getElementById('canvas_test')
-
, ctx = cvs.getContext('2d');
-
-
// rect() 메서드는 이전에 배운 fillRect() 메서드와는 달리 경로가 지정된 도형의 외각선을 그리는 stroke() 메소드를 사용해야한다.
-
ctx.beginPath();
-
-
ctx.rect(10, 10, 90, 90);
-
-
ctx.fillStyle = 'red';
-
ctx.fill();
-
-
ctx.strokeStyle = 'green';
-
ctx.stroke();
-
-
ctx.closePath();
-
-
-
}
-
-
-
-
function bind(elem, type, handler, capture) {
-
type = typeof type === 'string' && type || '';
-
handler = handler || function () { ; };
-
capture = capture || false;
-
-
if (elem.addEventListener) {
-
elem.addEventListener(type, handler, capture);
-
}
-
else if (elem.attachEvent) {
-
elem.attachEvent('on' + type, handler);
-
}
-
-
return elem;
-
};
-
-
-
bind(window, 'load', function () {
-
fill();
-
});
-
-
-
//]]>
-
</script>
-
<canvas id="canvas_test" width="200" height="200" style="border:black 1px solid"></canvas>
-
</body>
-
</html>
실행결과:
레퍼런스 참조사이트: