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/ec/v3/TnLDyD98QKepCWtetnhPmQ/zh-cn_image_0000002634008762.png?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=4246A40CF7BD1AB756C24D363A80B91CDABBDFFA9602664FC4FE65AF0DFD23FE)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e4/v3/nVnFxDStSmmnxfYLG_T11w/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=46489A0E650E9AE59E281F353336176FC4231BC41A1C2808400F30BB87D17957)

*   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/35/v3/wSiR8cT3SE6nBT4UFBLYww/zh-cn_image_0000002664207909.png?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=E46B309A066DD91BE014198377B1BFC00CAB7ADB41E771D8DE6AA876EE9B3665)

#### 添加事件

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

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/9c/v3/s7m0qFOnSUihV07aMmTWng/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=A0BA1A1080E55FD42B19B3580F0F60F45366F4437AA848EC4C09C8DE5679DD57)

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/6e/v3/1x9aWnPNRl6T7yxk-xErbg/zh-cn_image_0000002664327969.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=FE9D4A36AF835C951DB2D833693B3CFE7D79C7293364A5754D6458F8360A6177)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/5a/v3/2qhgnz-PTwSkiLCRxi33Hw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132322Z&HW-CC-Expire=86400&HW-CC-Sign=FF56800A4BD10708B2BD86D6D854CACB73088C83C323CD0B5F6A7C9DCDE3E6B0)

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