Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML Canvas

HTML5 Reference manual


Description

HTML5 The tag is used to draw images (via scripts, usually JavaScript).

However, The element itself has no drawing capability (it is merely a container for graphics) - you must use a script to perform the actual drawing tasks.

The getContext() method can return an object that provides methods and properties for drawing on the canvas.

This manual provides the complete properties and methods of the getContext("2d") object, which can be used to draw text, lines, rectangles, circles, and so on, on the canvas.


Browser support

Internet Explorer Firefox Opera Google Chrome Safari

Supported by Internet Explorer 9, Firefox, Opera, Chrome, and Safari The attributes and methods of the tag.

**Note:** Internet Explorer 8 and earlier IE versions do not support .


Colors, styles, and shadows

property Description
fillStyle Sets or returns the color, gradient, or pattern used to fill the painting.
strokeStyle Sets or returns the color, gradient, or pattern used for the stroke.
shadowColor Sets or returns the color used for the shadow.
shadowBlur Sets or returns the blur level used for the shadow.
shadowOffsetX Sets or returns the horizontal distance between the shadow and the shape.
shadowOffsetY Sets or returns the vertical distance between the shadow and the shape.
Method Description
createLinearGradient() Creates a linear gradient (used on canvas content).
createPattern() Repeats the specified element in the specified direction.
createRadialGradient() Creates a radial/circular gradient (used on canvas content).
addColorStop() Specifies the color and stop position in the gradient object.

Line styles

property Description
lineCap Sets or returns the end cap style of the line.
lineJoin Sets or returns the corner type created when two lines intersect.
lineWidth Sets or returns the current line width.
miterLimit Sets or returns the maximum miter length.

Rectangle

Method Description
rect() Creates a rectangle.
fillRect() Draws a "filled" rectangle.
strokeRect() Draws a rectangle (not filled).
clearRect() Clears the specified pixels within the given rectangle.

Path

Method Description
fill() Fills the current drawing (path).
stroke() Draws the defined path.
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, then creates a line in the canvas from that point to the last specified point.
clip() Clips a region of any shape and size from the original canvas.
quadraticCurveTo() Creates a quadratic Bezier curve.
bezierCurveTo() Creates a cubic Bezier 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 lies in the current path, otherwise returns false.

Transform

Method Description
scale() Scales the current drawing to be larger or smaller.
rotate() Rotates the current drawing.
translate() Remaps the (0,0) position on the canvas.
transform() Replaces the current transformation matrix of the drawing.
setTransform() Resets the current transform to the identity matrix. Then runs transform().

Text

property Description
font Sets or returns the current font property 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 (not filled).
measureText() Returns an object containing the width of the specified text.

Image Drawing

Method Description
drawImage() Draws an image, canvas, or video onto the canvas.

Pixel Operations

property Description
width Returns the width of the ImageData object.
height Returns the height of the ImageData object.
data Returns an object containing the image data of the specified ImageData object.
Method Description
createImageData() Creates a new, blank ImageData object.
getImageData() Returns an ImageData object that copies pixel data for the specified rectangle on the canvas.
putImageData() Puts image data (from the specified ImageData object) back onto the canvas.

Compositing

property Description
globalAlpha Sets or returns the current alpha or transparency value of the drawing.
globalCompositeOperation Sets or returns how a new image is drawn onto an existing image.

Others

Method Description
save() Saves the state of the current environment.
restore() Restores the path state and properties that were previously saved.
createEvent()  
getContext()  
toDataURL()