Manwal ng Canvas ng HTML

Manwal ng Canvas ng HTML

HTML <canvas> tag is used to dynamically draw graphics through scripts (usually JavaScript).

For more information about <canvas>, please read our HTML Canvas tutorial.

Color, style, and shadow

属性 描述
fillStyle Set or return the color, gradient, or pattern for the fill
strokeStyle Set or return the color, gradient, or pattern for the stroke
shadowColor Set or return the color of the shadow
shadowBlur Set or return the blur level of the shadow
shadowOffsetX Set or return the horizontal distance of the shadow from the shape
shadowOffsetY Set or return the vertical distance of the shadow from the shape
方法 描述
createLinearGradient() Create a linear gradient (used on canvas content)
createPattern() Repeat the specified element in the specified direction
createRadialGradient() Create a radial/linear gradient (used on canvas content)
addColorStop() Specify the color and stop position in the gradient object

Line style

属性 描述
lineCap Set or return the style of the line's endpoint
lineJoin Set or return the type of corner created when two lines intersect
lineWidth Set or return the current line width
miterLimit Set or return the maximum miter length

Rectangle

方法 描述
rect() Create a rectangle
fillRect() Draw a 'filled' rectangle
strokeRect() Draw a rectangle (no fill)
clearRect() Clear specified pixels within the given rectangle

Path

方法 描述
fill() Fill the current drawing (path)
stroke() Draw the defined path
beginPath() Start a new path or reset the current path
moveTo() Move the path to a specified point on the canvas without creating a line
closePath() Create a path that goes back to the starting point from the current point
lineTo() Add a new point and create a line from this point to the last specified point on the canvas
clip() Cut any shape and size area from the original canvas
quadraticCurveTo() 创建二次贝塞尔曲线
bezierCurveTo() 创建三次方贝塞尔曲线
arc() 创建弧/曲线(用于创建圆形或部分圆)
arcTo() 创建两切线之间的弧/曲线
isPointInPath() 如果指定的点位于当前路径中,则返回 true,否则返回 false

转换

方法 描述
scale() 缩放当前绘图至更大或更小
rotate() 旋转当前绘图
translate() 重新映射画布上的 (0,0) 位置
transform() 替换绘图的当前转换矩阵
setTransform() 将当前转换重置为单位矩阵。然后运行 transform()

文本

属性 描述
font 设置或返回文本内容的当前字体属性
textAlign 设置或返回文本内容的当前对齐方式
textBaseline 设置或返回在绘制文本时使用的当前文本基线
方法 描述
fillText() 在画布上绘制“被填充的”文本
strokeText() 在画布上绘制文本(无填充)
measureText() 返回包含指定文本宽度的对象

图像绘制

方法 描述
drawImage() 向画布上绘制图像、画布或视频

像素操作

属性 描述
width 返回 ImageData 对象的宽度
height 返回 ImageData 对象的高度
data 返回一个对象,其包含指定的 ImageData 对象的图像数据
方法 描述
createImageData() 创建新的、空白的 ImageData 对象
getImageData() 返回 ImageData 对象,该对象为画布上指定的矩形复制像素数据
putImageData() 把图像数据(从指定的 ImageData 对象)放回画布上

合成

属性 描述
globalAlpha 设置或返回绘图的当前 alpha 或透明值
globalCompositeOperation 设置或返回新图像如何绘制到已有的图像上

其他

方法 描述
save() 保存当前环境的状态
restore() 返回之前保存过的路径状态和属性
createEvent()  
getContext()  
toDataURL()