js-components-canvas-canvas.md 6.9 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# canvas组件<a name="ZH-CN_TOPIC_0000001127284886"></a>

提供画布组件。用于自定义绘制图形。

## 权限列表<a name="section11257113618419"></a>



## 子组件<a name="section9288143101012"></a>

不支持。

## 属性<a name="section2907183951110"></a>

支持[通用属性](js-components-common-attributes.md)

## 样式<a name="section5775351116"></a>

支持[通用样式](js-components-common-styles.md)

## 事件<a name="section1729055142211"></a>

支持[通用事件](js-components-common-events.md)

## 方法<a name="section47669296127"></a>

除支持[通用方法](js-components-common-methods.md)外,还支持如下方法:

Z
zengyawen 已提交
29
### getContext<a name="section18710131144813"></a>
Z
update  
zengyawen 已提交
30

Z
zengyawen 已提交
31
getContext\(type: '2d', options?:  ContextAttrOptions\): CanvasRendering2dContext
Z
update  
zengyawen 已提交
32 33 34 35 36

获取canvas绘图上下文。不支持在onInit和onReady中进行调用。

-   参数

Z
zengyawen 已提交
37
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="21.04%" id="mcps1.1.5.1.1"><p>参数名</p>
Z
update  
zengyawen 已提交
38
    </th>
Z
zengyawen 已提交
39
    <th class="cellrowborder" valign="top" width="15.260000000000002%" id="mcps1.1.5.1.2"><p>参数类型</p>
Z
update  
zengyawen 已提交
40
    </th>
Z
zengyawen 已提交
41
    <th class="cellrowborder" valign="top" width="7.5200000000000005%" id="mcps1.1.5.1.3"><p>必填</p>
Z
update  
zengyawen 已提交
42
    </th>
Z
zengyawen 已提交
43
    <th class="cellrowborder" valign="top" width="56.18%" id="mcps1.1.5.1.4"><p>描述</p>
Z
update  
zengyawen 已提交
44 45 46
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
47
    <tbody><tr><td class="cellrowborder" valign="top" width="21.04%" headers="mcps1.1.5.1.1 "><p>type</p>
Z
update  
zengyawen 已提交
48
    </td>
Z
zengyawen 已提交
49
    <td class="cellrowborder" valign="top" width="15.260000000000002%" headers="mcps1.1.5.1.2 "><p>string</p>
Z
update  
zengyawen 已提交
50
    </td>
Z
zengyawen 已提交
51
    <td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.5.1.3 "><p>是</p>
Z
update  
zengyawen 已提交
52
    </td>
Z
zengyawen 已提交
53
    <td class="cellrowborder" valign="top" width="56.18%" headers="mcps1.1.5.1.4 "><p>设置为'2d',返回值为2D绘制对象,该对象可用于在画布组件上绘制矩形、文本、图片等。</p>
Z
update  
zengyawen 已提交
54 55
    </td>
    </tr>
Z
zengyawen 已提交
56
    <tr><td class="cellrowborder" valign="top" width="21.04%" headers="mcps1.1.5.1.1 "><p>options<sup>6+</sup></p>
Z
update  
zengyawen 已提交
57
    </td>
Z
zengyawen 已提交
58
    <td class="cellrowborder" valign="top" width="15.260000000000002%" headers="mcps1.1.5.1.2 "><p><a href="#table87141136171717">ContextAttrOptions</a></p>
Z
update  
zengyawen 已提交
59
    </td>
Z
zengyawen 已提交
60
    <td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.5.1.3 "><p>否</p>
Z
update  
zengyawen 已提交
61
    </td>
Z
zengyawen 已提交
62
    <td class="cellrowborder" valign="top" width="56.18%" headers="mcps1.1.5.1.4 "><p>当前仅支持配置是否开启抗锯齿功能,默认为关闭。</p>
Z
update  
zengyawen 已提交
63 64 65 66 67 68 69 70
    </td>
    </tr>
    </tbody>
    </table>

    **表 1**  ContextAttrOptions

    <a name="table87141136171717"></a>
Z
zengyawen 已提交
71
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p>参数名</p>
Z
update  
zengyawen 已提交
72
    </th>
Z
zengyawen 已提交
73
    <th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p>类型</p>
Z
update  
zengyawen 已提交
74
    </th>
Z
zengyawen 已提交
75
    <th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p>说明</p>
Z
update  
zengyawen 已提交
76 77 78
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
79
    <tbody><tr><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p>antialias</p>
Z
update  
zengyawen 已提交
80
    </td>
Z
zengyawen 已提交
81
    <td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p>boolean</p>
Z
update  
zengyawen 已提交
82
    </td>
Z
zengyawen 已提交
83
    <td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p>是否开启抗锯齿功能,默认为false。</p>
Z
update  
zengyawen 已提交
84 85 86 87 88 89 90
    </td>
    </tr>
    </tbody>
    </table>

-   返回值

Z
zengyawen 已提交
91
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="19.77%" id="mcps1.1.3.1.1"><p>类型</p>
Z
update  
zengyawen 已提交
92
    </th>
Z
zengyawen 已提交
93
    <th class="cellrowborder" valign="top" width="80.23%" id="mcps1.1.3.1.2"><p>说明</p>
Z
update  
zengyawen 已提交
94 95 96
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
97
    <tbody><tr><td class="cellrowborder" valign="top" width="19.77%" headers="mcps1.1.3.1.1 "><p><a href="js-components-canvas-canvasrenderingcontext2d.md">CanvasRenderingContext2D</a></p>
Z
update  
zengyawen 已提交
98
    </td>
Z
zengyawen 已提交
99
    <td class="cellrowborder" valign="top" width="80.23%" headers="mcps1.1.3.1.2 "><p>用于在画布组件上绘制矩形、文本<span>、图片等</span>。</p>
Z
update  
zengyawen 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    </td>
    </tr>
    </tbody>
    </table>


### toDataURL<sup>6+</sup><a name="section16338154813483"></a>

toDataURL\(type?: string, quality?: number\): string

生成一个包含图片展示的URL。

-   参数

Z
zengyawen 已提交
114
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="21.04%" id="mcps1.1.5.1.1"><p>参数名</p>
Z
update  
zengyawen 已提交
115
    </th>
Z
zengyawen 已提交
116
    <th class="cellrowborder" valign="top" width="15.260000000000002%" id="mcps1.1.5.1.2"><p>参数类型</p>
Z
update  
zengyawen 已提交
117
    </th>
Z
zengyawen 已提交
118
    <th class="cellrowborder" valign="top" width="7.5200000000000005%" id="mcps1.1.5.1.3"><p>必填</p>
Z
update  
zengyawen 已提交
119
    </th>
Z
zengyawen 已提交
120
    <th class="cellrowborder" valign="top" width="56.18%" id="mcps1.1.5.1.4"><p>描述</p>
Z
update  
zengyawen 已提交
121 122 123
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
124
    <tbody><tr><td class="cellrowborder" valign="top" width="21.04%" headers="mcps1.1.5.1.1 "><p>type</p>
Z
update  
zengyawen 已提交
125
    </td>
Z
zengyawen 已提交
126
    <td class="cellrowborder" valign="top" width="15.260000000000002%" headers="mcps1.1.5.1.2 "><p>string</p>
Z
update  
zengyawen 已提交
127
    </td>
Z
zengyawen 已提交
128
    <td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.5.1.3 "><p>否</p>
Z
update  
zengyawen 已提交
129
    </td>
Z
zengyawen 已提交
130
    <td class="cellrowborder" valign="top" width="56.18%" headers="mcps1.1.5.1.4 "><p>可选参数,用于指定图像格式,默认格式为image/png。</p>
Z
update  
zengyawen 已提交
131 132
    </td>
    </tr>
Z
zengyawen 已提交
133
    <tr><td class="cellrowborder" valign="top" width="21.04%" headers="mcps1.1.5.1.1 "><p>quality</p>
Z
update  
zengyawen 已提交
134
    </td>
Z
zengyawen 已提交
135
    <td class="cellrowborder" valign="top" width="15.260000000000002%" headers="mcps1.1.5.1.2 "><p>number</p>
Z
update  
zengyawen 已提交
136
    </td>
Z
zengyawen 已提交
137
    <td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.5.1.3 "><p>否</p>
Z
update  
zengyawen 已提交
138
    </td>
Z
zengyawen 已提交
139
    <td class="cellrowborder" valign="top" width="56.18%" headers="mcps1.1.5.1.4 "><p>在指定图片格式为image/jpeg或image/webp的情况下,可以从0到1的区间内选择图片的质量。如果超出取值范围,将会使用默认值0.92。</p>
Z
update  
zengyawen 已提交
140 141 142 143 144 145 146
    </td>
    </tr>
    </tbody>
    </table>

-   返回值

Z
zengyawen 已提交
147
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="19.77%" id="mcps1.1.3.1.1"><p>类型</p>
Z
update  
zengyawen 已提交
148
    </th>
Z
zengyawen 已提交
149
    <th class="cellrowborder" valign="top" width="80.23%" id="mcps1.1.3.1.2"><p>说明</p>
Z
update  
zengyawen 已提交
150 151 152
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
153
    <tbody><tr><td class="cellrowborder" valign="top" width="19.77%" headers="mcps1.1.3.1.1 "><p>string</p>
Z
update  
zengyawen 已提交
154
    </td>
Z
zengyawen 已提交
155
    <td class="cellrowborder" valign="top" width="80.23%" headers="mcps1.1.3.1.2 "><p>图像的URL地址。</p>
Z
update  
zengyawen 已提交
156 157 158 159 160
    </td>
    </tr>
    </tbody>
    </table>

Z
zengyawen 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

## 示例<a name="section42931433142318"></a>

```
<!-- xxx.hml -->
<div>
  <canvas ref="canvas1" style="width: 200px; height: 150px; background-color: #ffff00;"></canvas>
  <input type="button" style="width: 180px; height: 60px;" value="fillStyle" onclick="handleClick" />
</div>
```

```
// xxx.js
export default {
  handleClick() {
    const el = this.$refs.canvas1;
    var dataURL = el.toDataURL();
    console.log(dataURL);
    // "data:image/png;base64,xxxxxxxx..."
  }
}
```