文档管理中心

CanvasRenderingContext2D对象

使用CanvasRenderingContext2D在canvas画布组件上进行绘制,绘制对象可以是矩形、文本。

说明

从API版本23开始,预览器已取消JS文件48KB的大小限制。对于API版本22及之前,JS文件大小不能超过48KB。

示例:

收起
自动换行
深色代码主题
复制
  1. <!-- xxx.hml -->
  2. <div>
  3. <canvas ref="canvas1" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  4. <input type="button" style="width: 180px; height: 60px;" value="fillStyle" onclick="handleClick" />
  5. </div>
收起
自动换行
深色代码主题
复制
  1. // xxx.js
  2. export default {
  3. handleClick() {
  4. const el = this.$refs.canvas1;
  5. const ctx = el.getContext('2d');
  6. ctx.beginPath();
  7. ctx.arc(100, 75, 50, 0, 6.28);
  8. ctx.stroke();
  9. },
  10. }

fillRect()

填充一个矩形。

参数:

展开
参数 类型 描述
x number 指定矩形左上角点的x坐标。
y number 指定矩形左上角点的y坐标。
width number 指定矩形的宽度。
height number 指定矩形的高度。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.fillRect(20, 20, 200, 150);

fillStyle

指定绘制的填充色。

参数:

展开
参数 类型 描述
color <color> 设置填充区域的颜色。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.fillStyle = '#0000ff';
  2. ctx.fillRect(20, 20, 150, 100);

strokeRect()

绘制具有边框的矩形,矩形内部不填充。

参数:

展开
参数 类型 描述
x number 指定矩形的左上角x坐标。
y number 指定矩形的左上角y坐标。
width number 指定矩形的宽度。
height number 指定矩形的高度。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.strokeRect(30, 30, 200, 150);

fillText()

绘制填充类文本。

参数:

展开
参数 类型 描述
text string 需要绘制的文本内容。
x number 需要绘制的文本的左下角x坐标。
y number 需要绘制的文本的左下角y坐标。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.font = '35px sans-serif';
  2. ctx.fillText("Hello World!", 20, 60);

lineWidth

指定绘制线条的宽度值。

参数:

展开
参数 类型 描述
lineWidth number 设置绘制线条的宽度。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.lineWidth = 5;
  2. ctx.strokeRect(25, 25, 85, 105);

strokeStyle

设置描边的颜色。

参数:

展开
参数 类型 描述
color <color> 指定描边使用的颜色

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.lineWidth = 10;
  2. ctx.strokeStyle = '#0000ff';
  3. ctx.strokeRect(25, 25, 155, 105);

stroke()5+

进行边框绘制操作。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.moveTo(25, 25);
  2. ctx.lineTo(25, 105);
  3. ctx.strokeStyle = 'rgb(0,0,255)';
  4. ctx.stroke();

beginPath()5+

创建一个新的绘制路径。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.beginPath();
  2. ctx.lineWidth = 6;
  3. ctx.strokeStyle = '#0000ff';
  4. ctx.moveTo(15, 80);
  5. ctx.lineTo(280, 80);
  6. ctx.stroke();

moveTo()5+

路径从当前点移动到指定点。

参数:

展开
参数 类型 描述
x number 指定位置的x坐标。
y number 指定位置的y坐标。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.beginPath();
  2. ctx.moveTo(10, 10);
  3. ctx.lineTo(280, 160);
  4. ctx.stroke();

lineTo()5+

从当前点到指定点进行路径连接。

参数:

展开
参数 类型 描述
x number 指定位置的x坐标。
y number 指定位置的y坐标。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.beginPath();
  2. ctx.moveTo(10, 10);
  3. ctx.lineTo(280, 160);
  4. ctx.stroke();

closePath()5+

结束当前路径形成一个封闭路径。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.beginPath();
  2. ctx.moveTo(30, 30);
  3. ctx.lineTo(110, 30);
  4. ctx.lineTo(70, 90);
  5. ctx.closePath();
  6. ctx.stroke();

font

设置文本绘制中的字体样式。

参数:

展开
参数 类型 描述
value string 字体样式支持:sans-serif, serif, monospace,该属性默认值为30px HYQiHei-65S。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.font = '30px sans-serif';
  2. ctx.fillText("Hello World", 20, 60);

textAlign

设置文本绘制中的文本对齐方式。

参数:

展开
参数 类型 描述
align string

可选值为:

- left(默认):文本左对齐;

- right:文本右对齐;

- center:文本居中对齐;

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.strokeStyle = '#0000ff';
  2. ctx.moveTo(140, 10);
  3. ctx.lineTo(140, 160);
  4. ctx.stroke();
  5. ctx.font = '18px sans-serif';
  6. // Show the different textAlign values
  7. ctx.textAlign = 'left';
  8. ctx.fillText('textAlign=left', 140, 100);
  9. ctx.textAlign = 'center';
  10. ctx.fillText('textAlign=center',140, 120);
  11. ctx.textAlign = 'right';
  12. ctx.fillText('textAlign=right',140, 140);

arc()5+

绘制弧线路径。

参数:

展开
参数 类型 必填 描述
x number 弧线圆心的x坐标值,单位:vp。
y number 弧线圆心的y坐标值,单位:vp。
radius number 弧线的圆半径,单位:vp。
startAngle number 弧线的起始弧度,单位:弧度。
endAngle number 弧线的终止弧度,单位:弧度。
anticlockwise boolean

是否逆时针绘制圆弧。

true:逆时针方向绘制弧线。

false:顺时针方向绘制弧线。

默认值:false。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.beginPath();
  2. ctx.arc(100, 75, 50, 0, 6.28);
  3. ctx.stroke();

rect()5+

创建矩形路径。

参数:

展开
参数 类型 必填 描述
x number 指定矩形的左上角x坐标值,单位:vp。
y number 指定矩形的左上角y坐标值,单位:vp。
width number 指定矩形的宽度,单位:vp。
height number 指定矩形的高度,单位:vp。

示例:

收起
自动换行
深色代码主题
复制
  1. ctx.rect(20, 20, 100, 100); // Create a 100*100 rectangle at (20, 20)
  2. ctx.stroke(); // Draw it