js-components-canvas-image.md 3.8 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6
# Image对象<a name="ZH-CN_TOPIC_0000001173164737"></a>

图片对象。

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

Z
zengyawen 已提交
7
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="21.8%" id="mcps1.1.6.1.1"><p>属性</p>
Z
zengyawen 已提交
8
</th>
Z
zengyawen 已提交
9
<th class="cellrowborder" valign="top" width="22.34%" id="mcps1.1.6.1.2"><p>类型</p>
Z
zengyawen 已提交
10
</th>
Z
zengyawen 已提交
11
<th class="cellrowborder" valign="top" width="7.9799999999999995%" id="mcps1.1.6.1.3"><p>默认值</p>
Z
zengyawen 已提交
12
</th>
Z
zengyawen 已提交
13
<th class="cellrowborder" valign="top" width="11.73%" id="mcps1.1.6.1.4"><p>必填</p>
Z
zengyawen 已提交
14
</th>
Z
zengyawen 已提交
15
<th class="cellrowborder" valign="top" width="36.15%" id="mcps1.1.6.1.5"><p>描述</p>
Z
zengyawen 已提交
16 17 18
</th>
</tr>
</thead>
Z
zengyawen 已提交
19
<tbody><tr><td class="cellrowborder" valign="top" width="21.8%" headers="mcps1.1.6.1.1 "><p>src</p>
Z
zengyawen 已提交
20
</td>
Z
zengyawen 已提交
21
<td class="cellrowborder" valign="top" width="22.34%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
22
</td>
Z
zengyawen 已提交
23
<td class="cellrowborder" valign="top" width="7.9799999999999995%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
24
</td>
Z
zengyawen 已提交
25
<td class="cellrowborder" valign="top" width="11.73%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
26
</td>
Z
zengyawen 已提交
27
<td class="cellrowborder" valign="top" width="36.15%" headers="mcps1.1.6.1.5 "><p>图片资源的路径。</p>
Z
zengyawen 已提交
28 29
</td>
</tr>
Z
zengyawen 已提交
30
<tr><td class="cellrowborder" valign="top" width="21.8%" headers="mcps1.1.6.1.1 "><p>width</p>
Z
zengyawen 已提交
31
</td>
Z
zengyawen 已提交
32
<td class="cellrowborder" valign="top" width="22.34%" headers="mcps1.1.6.1.2 "><p>&lt;length&gt;</p>
Z
zengyawen 已提交
33
</td>
Z
zengyawen 已提交
34
<td class="cellrowborder" valign="top" width="7.9799999999999995%" headers="mcps1.1.6.1.3 "><p>0px</p>
Z
zengyawen 已提交
35
</td>
Z
zengyawen 已提交
36
<td class="cellrowborder" valign="top" width="11.73%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
37
</td>
Z
zengyawen 已提交
38
<td class="cellrowborder" valign="top" width="36.15%" headers="mcps1.1.6.1.5 "><p>图片的宽度。</p>
Z
zengyawen 已提交
39 40
</td>
</tr>
Z
zengyawen 已提交
41
<tr><td class="cellrowborder" valign="top" width="21.8%" headers="mcps1.1.6.1.1 "><p>height</p>
Z
zengyawen 已提交
42
</td>
Z
zengyawen 已提交
43
<td class="cellrowborder" valign="top" width="22.34%" headers="mcps1.1.6.1.2 "><p>&lt;length&gt;</p>
Z
zengyawen 已提交
44
</td>
Z
zengyawen 已提交
45
<td class="cellrowborder" valign="top" width="7.9799999999999995%" headers="mcps1.1.6.1.3 "><p>0px</p>
Z
zengyawen 已提交
46
</td>
Z
zengyawen 已提交
47
<td class="cellrowborder" valign="top" width="11.73%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
48
</td>
Z
zengyawen 已提交
49
<td class="cellrowborder" valign="top" width="36.15%" headers="mcps1.1.6.1.5 "><p>图片的高度。</p>
Z
zengyawen 已提交
50 51
</td>
</tr>
Z
zengyawen 已提交
52
<tr><td class="cellrowborder" valign="top" width="21.8%" headers="mcps1.1.6.1.1 "><p>onload</p>
Z
zengyawen 已提交
53
</td>
Z
zengyawen 已提交
54
<td class="cellrowborder" valign="top" width="22.34%" headers="mcps1.1.6.1.2 "><p>Function</p>
Z
zengyawen 已提交
55
</td>
Z
zengyawen 已提交
56
<td class="cellrowborder" valign="top" width="7.9799999999999995%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
57
</td>
Z
zengyawen 已提交
58
<td class="cellrowborder" valign="top" width="11.73%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
59
</td>
Z
zengyawen 已提交
60
<td class="cellrowborder" valign="top" width="36.15%" headers="mcps1.1.6.1.5 "><p>图片加载成功后触发该事件,无参数。</p>
Z
zengyawen 已提交
61 62
</td>
</tr>
Z
zengyawen 已提交
63
<tr><td class="cellrowborder" valign="top" width="21.8%" headers="mcps1.1.6.1.1 "><p>onerror</p>
Z
zengyawen 已提交
64
</td>
Z
zengyawen 已提交
65
<td class="cellrowborder" valign="top" width="22.34%" headers="mcps1.1.6.1.2 "><p>Function</p>
Z
zengyawen 已提交
66
</td>
Z
zengyawen 已提交
67
<td class="cellrowborder" valign="top" width="7.9799999999999995%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
68
</td>
Z
zengyawen 已提交
69
<td class="cellrowborder" valign="top" width="11.73%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
70
</td>
Z
zengyawen 已提交
71
<td class="cellrowborder" valign="top" width="36.15%" headers="mcps1.1.6.1.5 "><p>图片加载失败后触发该事件,无参数。</p>
Z
zengyawen 已提交
72 73 74 75 76 77 78 79
</td>
</tr>
</tbody>
</table>

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

```
Z
zengyawen 已提交
80 81 82 83 84 85 86 87 88 89 90
<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
</div>
```

```
//xxx.js
export default {
  onShow(){
    const el =this.$refs.canvas
91
    var ctx =el.getContext('2d');  
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101 102
    var img = new Image();
    img.src = 'common/images/example.jpg';
    img.onload = function() {
    console.log('Image load success');
    ctx.drawImage(img, 0, 0, 360, 250);
   };
    img.onerror = function() {
    console.log('Image load fail');
    };
  }
}
Z
zengyawen 已提交
103 104
```

Z
update  
zengyawen 已提交
105
![](figures/1-2.png)
Z
zengyawen 已提交
106