Canvas Reference

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. (GetContext ()方法返回一个对象,该对象显示用于在画布上绘制的属性和方法。)

Colors, Styles, and Shadows

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

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

Rectangles (长方形)

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

Paths

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

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

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

Image Drawing (画影珠)

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

Pixel Manipulation

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

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

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.


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部