Canvas对象
========

Canvas组件提供画布，用于自定义绘制图形。具体用法请参考[CanvasRenderingContext2D对象](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-canvas-canvasrenderingcontext2d)。

#### 创建Canvas组件

在pages/index目录下的hml文件中创建一个Canvas组件。

<!-- xxx.hml -->
<div class="container">
  <canvas></canvas>
</div>

/\* xxx.css \*/
.container {
    width: 100%;
    height: 100%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #F1F3F5;
}

canvas {
    background-color: #00ff73;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/04/v3/lCTLagaGQ7WtqzEbc5j70Q/zh-cn_image_0000002625187705.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=EC69DC5884DE4DE3FF290E5AC57831DCB54B98DD636DF8A763401E6772B9CA4A)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/d3/v3/pv8XygwKSw6cnGM2atwElg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=062B3D520A0BD6BB9A594F4C6BD22A915B4C627BB156EE46B1F3BD13A753930F)

*   Canvas组件默认背景色与父组件的背景色一致。
    
*   Canvas默认宽高为width: 300px，height: 150px。
    

#### 添加样式

Canvas组件设置宽（width）、高（height）、背景色（background-color）及边框样式（border）。

<!-- xxx.hml -->
<div class="container">
  <canvas></canvas>
</div>

/\* xxx.css \*/
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #F1F3F5;
    width: 100%;
    height: 100%;
}

canvas {
    width: 500px;
    height: 500px;
    background-color: #fdfdfd;
    border: 5px solid red;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/d4/v3/avmxRmLoQXGpFaBs3hHY3g/zh-cn_image_0000002625107849.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=164D4A90EDE7F00DCCFF2599398E4EAB275CC05A60D103C88CBC23B33DB7D544)

#### 添加事件

Canvas添加长按事件，长按后可获取Canvas组件的dataUrl值（toDataURL方法返回的图片信息），打印在下方文本区域内。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e3/v3/y8Nn0bz9Ts2QTvFPXNNyyA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=4A29D50BE81811AC82433CC63493B402951431046D42A79CB06930D3F5A27807)

promptAction相关接口参考[弹窗](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-promptaction)。

<!-- xxx.hml -->
<div class="container">
    <canvas ref="canvas1" onlongpress="getUrl"></canvas>
    <text>dataURL</text>
    <text class="content">{{ dataURL }}</text>
</div>

/\* xxx.css \*/
.container {
    width: 100%;
    height: 100%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #F1F3F5;
}

canvas {
    width: 500px;
    height: 500px;
    background-color: #fdfdfd;
    border: 5px solid red;
    margin-bottom: 50px;
}

.content {
    border: 5px solid blue;
    padding: 10px;
    width: 90%;
    height: 400px;
    overflow: scroll;
}

// xxx.js
import promptAction from '@ohos.promptAction';

export default {
    data: {
        dataURL: null,
    },
    onShow() {
        let el = this.$refs.canvas1;
        let ctx = el.getContext("2d");
        ctx.strokeRect(100, 100, 300, 300);
    },
    getUrl() {
        let el = this.$refs.canvas1
        let dataUrl = el.toDataURL()
        this.dataURL = dataUrl;
        promptAction.showToast({ duration: 2000, message: "long press,get dataURL" })
    }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/19/v3/N-C5CApsQD2dskPhUCndcQ/zh-cn_image_0000002594668332.gif?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=1E2EB036F52F9693049E370F893A5483A8A376C46918CE3CD826FB43BBED7DDA)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/5a/v3/K6I3kfc_S9yKGfLACaPkHQ/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073321Z&HW-CC-Expire=86400&HW-CC-Sign=C04200B209F78829DF92BF6ECA4E0D3C43197B1B472B5CA8C8A70EA2F88D24CD)

画布不支持在onInit和onReady中进行创建。