HTML DOM 캔버스 객체
캔버스 개체
HTML5 <canvas> 태그는 JavaScript를 사용하여 즉석에서 그래픽을 그리는 데 사용됩니다.
캔버스 개체에 액세스
getElementById()를 사용하여 <canvas> 요소에 액세스할 수 있습니다.
예시
var x = document.getElementById("myCanvas");
캔버스 개체 만들기
document.createElement() 메서드를 사용하여 <canvas> 요소를 만들 수 있습니다.
예시
var x = document.createElement("CANVAS");
참고: <canvas> 요소에는 자체적으로 그리기 기능이 없습니다(그래픽용 컨테이너일 뿐입니다). 실제로 그래픽을 그리려면 스크립트를 사용해야 합니다.
getContext() 메서드는 캔버스에 그리기 위한 메서드와 속성을 제공하는 객체를 반환합니다.
이 참조에서는 캔버스에 텍스트, 선, 상자, 원 등을 그리는 데 사용할 수 있는 getContext("2d") 개체의 속성과 메서드를 다룹니다.
색상, 스타일 및 그림자
Property |
Description |
fillStyle |
Sets or returns the color, gradient, or pattern used to fill the drawing |
strokeStyle |
Sets or returns the color, gradient, or pattern used for strokes |
shadowColor |
Sets or returns the color to use for shadows |
shadowBlur |
Sets or returns the blur level for shadows |
shadowOffsetX |
Sets or returns the horizontal distance of the shadow from the shape |
shadowOffsetY |
Sets or returns the vertical distance of the shadow from the shape |
선 스타일
Property |
Description |
lineCap |
Sets or returns the style of the end caps for a line |
lineJoin |
Sets or returns the type of corner created, when two lines meet |
lineWidth |
Sets or returns the current line width |
miterLimit |
Sets or returns the maximum miter length |
직사각형
경로
Method |
Description |
fill() |
Fills the current drawing (path) |
stroke() |
Actually draws the path you have defined |
beginPath() |
Begins a path, or resets the current path |
moveTo() |
Moves the path to the specified point in the canvas, without creating a line |
closePath() |
Creates a path from the current point back to the starting point |
lineTo() |
Adds a new point and creates a line from that point to the last specified point in the canvas |
clip() |
Clips a region of any shape and size from the original canvas |
quadraticCurveTo() |
Creates a quadratic Bézier curve |
bezierCurveTo() |
Creates a cubic Bézier curve |
arc() |
Creates an arc/curve (used to create circles, or parts of circles) |
arcTo() |
Creates an arc/curve between two tangents |
isPointInPath() |
Returns true if the specified point is in the current path, otherwise false |
변환
Method |
Description |
scale() |
Scales the current drawing bigger or smaller |
rotate() |
Rotates the current drawing |
translate() |
Remaps the (0,0) position on the canvas |
transform() |
Replaces the current transformation matrix for the drawing |
setTransform() |
Resets the current transform to the identity matrix. Then runs
transform() |
텍스트
Property |
Description |
font |
Sets or returns the current font properties for text content |
textAlign |
Sets or returns the current alignment for text content |
textBaseline |
Sets or returns the current text baseline used when drawing text |
Method |
Description |
fillText() |
Draws "filled" text on the canvas |
strokeText() |
Draws text on the canvas (no fill) |
measureText() |
Returns an object that contains the width of the specified text |
이미지 그리기
Method |
Description |
drawImage() |
Draws an image, canvas, or video onto the canvas |
픽셀 조작
Property |
Description |
width |
Returns the width of an ImageData object |
height |
Returns the height of an ImageData object |
data |
Returns an object that contains image data of a specified ImageData object |
Method |
Description |
createImageData() |
Creates a new, blank ImageData object |
getImageData() |
Returns an ImageData object that copies the pixel data for the specified rectangle on a canvas |
putImageData() |
Puts the image data (from a specified ImageData object) back onto the canvas |
합성
Property |
Description |
globalAlpha |
Sets or returns the current alpha or transparency value of the drawing |
globalCompositeOperation |
Sets or returns how a new image are drawn onto an existing image |
다른
Method |
Description |
save() |
Saves the state of the current context |
restore() |
Returns previously saved path state and attributes |
createEvent() |
|
getContext() |
|
toDataURL() |
|
표준 속성 및 이벤트
캔버스 객체는 표준 속성 과 이벤트 도 지원합니다 .
관련 페이지
HTML 튜토리얼: HTML5 캔버스
HTML 참조: HTML <canvas> 태그