Canvas Reference

The HTML5 <canvas> element is used for drawing graphics via scripting (commonly JavaScript). But the <canvas> element does not have drawing opportunities on its own. To draw the graphics, you must use a script because the <canvas> element is only a container for graphics.

The getContext() method returns an object which presents properties and methods for drawing on the canvas.

Colors, Styles, and Shadows

PropertyDescription
fillStyleSets or returns the color, gradient, or pattern used to fill the shapes.
strokeStyleSets or returns the color, gradient, or pattern used for the lines around the shapes.
shadowColorSets or returns the color of the shadows.
shadowBlurSets or returns the blur level of the shadows.
shadowOffsetXSets or returns the x offset of the shadow.
shadowOffsetYSets or returns the y offset of the shadow.
MethodDescription
createLinearGradient()Creates a linear gradient for using on the canvas content.
createPattern()Repeats a particular element in the specified direction.
createRadialGradient()Creates a circular/radial gradient for using on the canvas content.
addColorStop()Defines colors and stop positions in the gradient object.

Line Styles

PropertyDescription
lineCapSets or returns the style of the line’s end caps.
lineJoinSets or returns the type of drawn corners.
lineWidthSets or returns the width of the current line.
miterLimitSets or returns the maximum miter length.

Rectangles

MethodDescription
rect()Creates rectangles.
fillRect()Draws filled rectangles.
strokeRect()Draws rectangular outlines.
clearRect()Clears the specified pixels within a particular rectangle.

Paths

MethodDescription
fill()Fills the path to the canvas.
stroke()Draws the defined path.
beginPath()Begins a new path or resets the current path.
moveTo()Moves the path to the defined point in the canvas without drawing a line.
closePath()Adds a path from the current point back to the start point.
lineTo()Adds a line to the current sub-path.
clip()Restricts the drawing to a particular area.
quadraticCurveTo()Adds a quadratic Bézier curve.
bezierCurveTo()Adds a cubic Bézier curve.
arc()Adds an arc/curve for creating circles or parts of circles.
arcTo()Adds an arc/curve between two tangents.
isPointInPath()Defines whether the specified point is in the current path, or not.

Transformations

MethodDescription
scale()Scales up or scales down the current drawing.
rotate()Rotates the current drawing.
translate()Adjusts the coordinate system of the canvas.
transform()Applies transformation matrix to the canvas.
setTransform()Is similar to transform(). Can be used to skew, scale, and translate the canvas.

Text

PropertyDescription
fontSets the appearance of a text drawn on the canvas.
textAlignSets the alignment of a text drawn on the canvas.
textBaselineSets the vertical alignment of a text drawn on the canvas.
MethodDescription
fillText()Adds text to the canvas.
strokeText()Adds outlined text to the canvas.
measureText()Measures the text width.

Image Drawing

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

Pixel Manipulation

PropertyDescription
widthReturns an ImageData object’s width.
heightReturns an ImageData object’s height.
dataReturns an object which contains image data of a specified ImageData object.
MethodDescription
createImageData()Creates a newImageData object.
getImageData()Returns an ImageData object which copies the pixel data for the specified rectangle.
putImageData()Puts the image data onto the canvas from the ImageData object.

Compositing

PropertyDescription
globalAlphaSets or returns the current alpha/transparency/ value.
globalCompositeOperationSets or returns the type of compositing operation when a new image is drawn.

Other

MethodDescription
save()Saves the current state of a context.
restore()Returns the path state and attributes saved previously.
createEvent()Creates an event object.
getContext()Returns a drawing context.
toDataURL()Returns a data URL.

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *