ui-js-components-canvas.md 2.9 KB
Newer Older
H
HelloCrease 已提交
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 29 30 31 32 33 34
# <canvas> Development


The **<canvas>** component provides a canvas for drawing customized graphics. For details, see [canvas](../reference/arkui-js/js-components-canvas-canvas.md).


## Creating a <canvas> Component

Create a **<canvas>** component in the .hml file under **pages/index**.


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


```
/* xxx.css */
.container{
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
canvas{
  background-color: #00ff73;
}
```

![en-us_image_0000001232162316](figures/en-us_image_0000001232162316.png)

E
ester.zhou 已提交
35
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
H
HelloCrease 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
> - The default background color of the **&lt;canvas&gt;** component is the same as that of the parent component.
> 
> - The default width and height of **&lt;canvas&gt;** are 300 px and 150 px, respectively.


## Adding Styles

Set **width**, **height**, **background-color**, and **border** of the **&lt;canvas&gt;** component.


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


```
/* xxx.css */
.container{
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
canvas{
  width: 500px;
E
ester.zhou 已提交
64 65
  height: 500px;  
  background-color: #fdfdfd;  
H
HelloCrease 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  border: 5px solid red;
}
```

![en-us_image_0000001231843104](figures/en-us_image_0000001231843104.png)


## Adding Events

Add the long press event to the **&lt;canvas&gt;** component. When the event is triggered, the value of **dataUrl** (image information returned by the **toDataURL** method) of the **&lt;canvas&gt;** component can be obtained and printed in the text area below.


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


```
E
ester.zhou 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* 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;
}
H
HelloCrease 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
```


```
// xxx.js
import prompt from '@system.prompt';
export default {
  data:{
    dataURL:null,
    antialia: false,
    porc:'open',
  },
  onShow(){
    let el = this.$refs.canvas1;
E
ester.zhou 已提交
126
    let ctx = el.getContext("2d"); 
H
HelloCrease 已提交
127 128 129
    ctx.strokeRect(100,100,300,300);
  },
  getUrl(){
E
ester.zhou 已提交
130 131
    let el = this.$refs.canvas1
    let dataUrl = el.toDataURL()
H
HelloCrease 已提交
132
    this.dataURL = dataUrl;
E
ester.zhou 已提交
133
    prompt.showToast({duration:2000,message:"long press,get dataURL"})
H
HelloCrease 已提交
134 135 136 137 138 139
  }
}
```

![en-us_image_0000001276003513](figures/en-us_image_0000001276003513.gif)

E
ester.zhou 已提交
140
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
H
HelloCrease 已提交
141
> The **&lt;canvas&gt;** component cannot be created in **onInit** or **onReady**.