HTML5 绘制矩形实例教程
绘制矩形
要绘制矩形,请指定 x 和 y 坐标(左上角)以及矩形的高度和宽度。有三种矩形方法:
fillRect()
strokeRect()
clearRect()
fillRect()方法
fillRect()方法用于以当前颜色、渐变或图案填充矩形。
语法:
ctx.fillRect(x, y, width, height)
参数
类型
说明
x
numbe
r
The x-coordinate (in pixels), the upper-left corner of the
rectangle in relation to the coordinates of the canvas.
y
numbe
r
The y-coordinate (in pixels), of the upper-left corner of
the rectangle in relation to the coordinates of the
canvas.
width
numbe
r
The width (in pixels), of the rectangle.