1e41f4b71Sopenharmony_ci# CanvasRenderingContext2D 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciUse **CanvasRenderingContext2D** to draw objects such as graphics, texts, line segments, and images on the **<canvas>** component. For details, see [CanvasRenderingContext2D](../reference/apis-arkui/arkui-js/js-components-canvas-canvasrenderingcontext2d.md). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Drawing Line Segments 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciUse **moveTo** and **lineTo** to draw a line segment. Use the **closePath** method to end current path, obtaining a closed path. Set **quadraticCurveTo** (quadratic bezier curve) or **bezierCurveTo** (cubic bezier curve) to draw a graphic. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```html 13e41f4b71Sopenharmony_ci<!-- xxx.hml --> 14e41f4b71Sopenharmony_ci<div class="container"> 15e41f4b71Sopenharmony_ci <canvas ref="canvas1"></canvas> 16e41f4b71Sopenharmony_ci <select @change="change"> 17e41f4b71Sopenharmony_ci <option value="value1"> line </option> 18e41f4b71Sopenharmony_ci <option value="value2"> quadratic </option> 19e41f4b71Sopenharmony_ci <option value="value3"> bezier </option> 20e41f4b71Sopenharmony_ci <option value="value4"> arc/ellipse </option> 21e41f4b71Sopenharmony_ci <option value="value5"> lineJoin/miterLimit </option> 22e41f4b71Sopenharmony_ci </select> 23e41f4b71Sopenharmony_ci</div> 24e41f4b71Sopenharmony_ci``` 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```css 28e41f4b71Sopenharmony_ci/* xxx.css */ 29e41f4b71Sopenharmony_ci.container{ 30e41f4b71Sopenharmony_ci width: 100%; 31e41f4b71Sopenharmony_ci height: 100%; 32e41f4b71Sopenharmony_ci flex-direction: column; 33e41f4b71Sopenharmony_ci justify-content: center; 34e41f4b71Sopenharmony_ci align-items: center; 35e41f4b71Sopenharmony_ci background-color: #F1F3F5; 36e41f4b71Sopenharmony_ci} 37e41f4b71Sopenharmony_cicanvas{ 38e41f4b71Sopenharmony_ci width: 600px; 39e41f4b71Sopenharmony_ci height: 500px; 40e41f4b71Sopenharmony_ci background-color: #fdfdfd; 41e41f4b71Sopenharmony_ci border: 5px solid red; 42e41f4b71Sopenharmony_ci} 43e41f4b71Sopenharmony_ciselect{ 44e41f4b71Sopenharmony_ci margin-top: 50px; 45e41f4b71Sopenharmony_ci width: 250px; 46e41f4b71Sopenharmony_ci height: 100px; 47e41f4b71Sopenharmony_ci background-color: white; 48e41f4b71Sopenharmony_ci} 49e41f4b71Sopenharmony_ci``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```js 53e41f4b71Sopenharmony_ci// xxx.js 54e41f4b71Sopenharmony_ciexport default { 55e41f4b71Sopenharmony_ci data:{ 56e41f4b71Sopenharmony_ci el: null, 57e41f4b71Sopenharmony_ci ctx: null, 58e41f4b71Sopenharmony_ci }, 59e41f4b71Sopenharmony_ci onShow(){ 60e41f4b71Sopenharmony_ci this.el = this.$refs.canvas1; 61e41f4b71Sopenharmony_ci this.ctx = this.el.getContext("2d",{antialias: true}); 62e41f4b71Sopenharmony_ci // Clear the contents on the canvas. 63e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 64e41f4b71Sopenharmony_ci // Create a drawing path. 65e41f4b71Sopenharmony_ci this.ctx.beginPath(); 66e41f4b71Sopenharmony_ci // Square off the endpoints of the line. 67e41f4b71Sopenharmony_ci this.ctx.lineCap = 'butt'; 68e41f4b71Sopenharmony_ci // Border width 69e41f4b71Sopenharmony_ci this.ctx.lineWidth = 15; 70e41f4b71Sopenharmony_ci // Create a drawing path. 71e41f4b71Sopenharmony_ci this.ctx.beginPath(); 72e41f4b71Sopenharmony_ci // Move a drawing path from the current point to a target position. 73e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 100); 74e41f4b71Sopenharmony_ci // Connect the current point to a target position. 75e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 100); 76e41f4b71Sopenharmony_ci // Stroke a path. 77e41f4b71Sopenharmony_ci this.ctx.stroke(); 78e41f4b71Sopenharmony_ci this.ctx.beginPath(); 79e41f4b71Sopenharmony_ci // Round the endpoints of the line. 80e41f4b71Sopenharmony_ci this.ctx.lineCap = 'round'; 81e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 200); 82e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 200); 83e41f4b71Sopenharmony_ci this.ctx.stroke(); 84e41f4b71Sopenharmony_ci // Square off the endpoints of the line. 85e41f4b71Sopenharmony_ci this.ctx.beginPath(); 86e41f4b71Sopenharmony_ci this.ctx.lineCap = 'square'; 87e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 300); 88e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 300); 89e41f4b71Sopenharmony_ci this.ctx.stroke(); 90e41f4b71Sopenharmony_ci }, 91e41f4b71Sopenharmony_ci change(e){ 92e41f4b71Sopenharmony_ci if(e.newValue == 'value1'){ 93e41f4b71Sopenharmony_ci this.el = this.$refs.canvas1; 94e41f4b71Sopenharmony_ci this.ctx = this.el.getContext("2d",{antialias: true}); 95e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 96e41f4b71Sopenharmony_ci // Top 97e41f4b71Sopenharmony_ci this.ctx.beginPath(); 98e41f4b71Sopenharmony_ci this.ctx.lineCap = 'butt'; 99e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 100); 100e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 100); 101e41f4b71Sopenharmony_ci this.ctx.stroke(); 102e41f4b71Sopenharmony_ci // Center 103e41f4b71Sopenharmony_ci this.ctx.beginPath(); 104e41f4b71Sopenharmony_ci this.ctx.lineCap = 'round'; 105e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 200); 106e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 200); 107e41f4b71Sopenharmony_ci this.ctx.stroke(); 108e41f4b71Sopenharmony_ci // Bottom 109e41f4b71Sopenharmony_ci this.ctx.beginPath(); 110e41f4b71Sopenharmony_ci this.ctx.lineCap = 'square'; 111e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 300); 112e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 300); 113e41f4b71Sopenharmony_ci this.ctx.stroke(); 114e41f4b71Sopenharmony_ci }else if(e.newValue == 'value2'){ 115e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 116e41f4b71Sopenharmony_ci // Top 117e41f4b71Sopenharmony_ci this.ctx.beginPath(); 118e41f4b71Sopenharmony_ci this.ctx.moveTo(100, 150); 119e41f4b71Sopenharmony_ci // Path of the quadratic bezier curve 120e41f4b71Sopenharmony_ci this.ctx.quadraticCurveTo(300, 50, 500, 150); 121e41f4b71Sopenharmony_ci this.ctx.stroke(); 122e41f4b71Sopenharmony_ci // Left 123e41f4b71Sopenharmony_ci this.ctx.beginPath(); 124e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 150); 125e41f4b71Sopenharmony_ci this.ctx.quadraticCurveTo(250, 250, 250, 400); 126e41f4b71Sopenharmony_ci this.ctx.stroke(); 127e41f4b71Sopenharmony_ci // Right 128e41f4b71Sopenharmony_ci this.ctx.beginPath(); 129e41f4b71Sopenharmony_ci this.ctx.moveTo(400, 150); 130e41f4b71Sopenharmony_ci this.ctx.quadraticCurveTo(350, 250, 350, 400); 131e41f4b71Sopenharmony_ci this.ctx.stroke(); 132e41f4b71Sopenharmony_ci }else if(e.newValue == 'value3'){ 133e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 134e41f4b71Sopenharmony_ci // Bottom 135e41f4b71Sopenharmony_ci this.ctx.beginPath(); 136e41f4b71Sopenharmony_ci this.ctx.moveTo(100, 200); 137e41f4b71Sopenharmony_ci // Path of the cubic bezier curve 138e41f4b71Sopenharmony_ci this.ctx.bezierCurveTo(150, 100, 200, 100,250, 200); 139e41f4b71Sopenharmony_ci this.ctx.stroke(); 140e41f4b71Sopenharmony_ci // Left 141e41f4b71Sopenharmony_ci this.ctx.beginPath(); 142e41f4b71Sopenharmony_ci this.ctx.moveTo(350, 200); 143e41f4b71Sopenharmony_ci this.ctx.bezierCurveTo(400, 100, 450, 100,500, 200); 144e41f4b71Sopenharmony_ci this.ctx.stroke(); 145e41f4b71Sopenharmony_ci // Right 146e41f4b71Sopenharmony_ci this.ctx.beginPath(); 147e41f4b71Sopenharmony_ci this.ctx.moveTo(200, 350); 148e41f4b71Sopenharmony_ci this.ctx.bezierCurveTo(250, 500, 350, 500, 400, 350); 149e41f4b71Sopenharmony_ci this.ctx.stroke(); 150e41f4b71Sopenharmony_ci }else if(e.newValue == 'value4'){ 151e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 152e41f4b71Sopenharmony_ci this.ctx.beginPath(); 153e41f4b71Sopenharmony_ci this.ctx.moveTo(100, 200); 154e41f4b71Sopenharmony_ci // Arc 155e41f4b71Sopenharmony_ci this.ctx.arcTo(150, 300, 350, 300, 150); 156e41f4b71Sopenharmony_ci this.ctx.stroke(); 157e41f4b71Sopenharmony_ci this.ctx.beginPath(); 158e41f4b71Sopenharmony_ci // Ellipse 159e41f4b71Sopenharmony_ci this.ctx.ellipse(400, 250, 50, 100, Math.PI * 0.25, Math.PI * 0.5 , Math.PI , 1); 160e41f4b71Sopenharmony_ci this.ctx.stroke(); 161e41f4b71Sopenharmony_ci }else if(e.newValue == 'value5'){ 162e41f4b71Sopenharmony_ci this.ctx.clearRect(0, 0, 600, 500); 163e41f4b71Sopenharmony_ci // Upper left 164e41f4b71Sopenharmony_ci this.ctx.beginPath(); 165e41f4b71Sopenharmony_ci // Draw a sector centered at the common endpoint of connected line segments. 166e41f4b71Sopenharmony_ci this.ctx.lineJoin = 'round'; 167e41f4b71Sopenharmony_ci this.ctx.moveTo(100, 100); 168e41f4b71Sopenharmony_ci this.ctx.lineTo(200, 200); 169e41f4b71Sopenharmony_ci this.ctx.lineTo(100, 250); 170e41f4b71Sopenharmony_ci this.ctx.stroke(); 171e41f4b71Sopenharmony_ci // lower left 172e41f4b71Sopenharmony_ci this.ctx.beginPath(); 173e41f4b71Sopenharmony_ci // Fill a triangular between the common endpoint of connected segments. 174e41f4b71Sopenharmony_ci this.ctx.lineJoin = 'bevel'; 175e41f4b71Sopenharmony_ci this.ctx.moveTo(100, 300); 176e41f4b71Sopenharmony_ci this.ctx.lineTo(200, 400); 177e41f4b71Sopenharmony_ci this.ctx.lineTo(100, 450); 178e41f4b71Sopenharmony_ci this.ctx.stroke(); 179e41f4b71Sopenharmony_ci // Top right 180e41f4b71Sopenharmony_ci this.ctx.beginPath(); 181e41f4b71Sopenharmony_ci // Distance between the internal angle and exterior angle at the intersection of lines 182e41f4b71Sopenharmony_ci this.ctx.lineJoin = 'miter'; 183e41f4b71Sopenharmony_ci this.ctx.miterLimit = 3; 184e41f4b71Sopenharmony_ci this.ctx.moveTo(400, 100); 185e41f4b71Sopenharmony_ci this.ctx.lineTo(450, 200); 186e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 250); 187e41f4b71Sopenharmony_ci // Draw a closed path. 188e41f4b71Sopenharmony_ci this.ctx.closePath(); 189e41f4b71Sopenharmony_ci this.ctx.stroke(); 190e41f4b71Sopenharmony_ci // Lower right 191e41f4b71Sopenharmony_ci this.ctx.beginPath(); 192e41f4b71Sopenharmony_ci this.ctx.lineJoin = 'miter'; 193e41f4b71Sopenharmony_ci this.ctx.miterLimit = 10; 194e41f4b71Sopenharmony_ci this.ctx.moveTo(400, 300); 195e41f4b71Sopenharmony_ci this.ctx.lineTo(450, 400); 196e41f4b71Sopenharmony_ci this.ctx.lineTo(400, 450); 197e41f4b71Sopenharmony_ci this.ctx.closePath(); 198e41f4b71Sopenharmony_ci this.ctx.stroke(); 199e41f4b71Sopenharmony_ci } 200e41f4b71Sopenharmony_ci }, 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci## Drawing Border 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ciGlobally define the canvas (**el**) and brush (**ctx**), and create a rectangle with the border width of 5 upon initialization. Change the border width (**lineWidth**), color (**strokeStyle**), and line dash level (**setLineDash**). Add the **change** event to the **<select>** component. The **change** event will be triggered upon selection and the changed image will be displayed. 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci```html 214e41f4b71Sopenharmony_ci<!-- xxx.hml --> 215e41f4b71Sopenharmony_ci<div class="container"> 216e41f4b71Sopenharmony_ci <canvas ref="canvas1"></canvas> 217e41f4b71Sopenharmony_ci <select @change="change"> 218e41f4b71Sopenharmony_ci <option value="value1">strokeRect</option> 219e41f4b71Sopenharmony_ci <option value="value2">arc</option> 220e41f4b71Sopenharmony_ci <option value="value3">lineDashRect</option> 221e41f4b71Sopenharmony_ci <option value="value4">fillRect</option> 222e41f4b71Sopenharmony_ci </select> 223e41f4b71Sopenharmony_ci</div> 224e41f4b71Sopenharmony_ci``` 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci```css 229e41f4b71Sopenharmony_ci/* xxx.css */ 230e41f4b71Sopenharmony_ci.container{ 231e41f4b71Sopenharmony_ci width: 100%; 232e41f4b71Sopenharmony_ci height: 100%; 233e41f4b71Sopenharmony_ci flex-direction: column; 234e41f4b71Sopenharmony_ci justify-content: center; 235e41f4b71Sopenharmony_ci align-items: center; 236e41f4b71Sopenharmony_ci background-color: #F1F3F5; 237e41f4b71Sopenharmony_ci} 238e41f4b71Sopenharmony_cicanvas{ 239e41f4b71Sopenharmony_ci width: 600px; 240e41f4b71Sopenharmony_ci height: 500px; 241e41f4b71Sopenharmony_ci background-color: #fdfdfd; 242e41f4b71Sopenharmony_ci border: 5px solid red; 243e41f4b71Sopenharmony_ci} 244e41f4b71Sopenharmony_ciselect{ 245e41f4b71Sopenharmony_ci margin-top: 50px; 246e41f4b71Sopenharmony_ci width: 250px; 247e41f4b71Sopenharmony_ci height: 100px; 248e41f4b71Sopenharmony_ci background-color: white; 249e41f4b71Sopenharmony_ci} 250e41f4b71Sopenharmony_ci``` 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci```js 255e41f4b71Sopenharmony_ci// xxx.js 256e41f4b71Sopenharmony_ciexport default { 257e41f4b71Sopenharmony_ci data:{ 258e41f4b71Sopenharmony_ci el: null, 259e41f4b71Sopenharmony_ci ctx: null, 260e41f4b71Sopenharmony_ci }, 261e41f4b71Sopenharmony_ci onShow(){ 262e41f4b71Sopenharmony_ci this.el = this.$refs.canvas1; 263e41f4b71Sopenharmony_ci this.ctx = this.el.getContext("2d",{antialias: true}); 264e41f4b71Sopenharmony_ci this.ctx.lineWidth = 5; 265e41f4b71Sopenharmony_ci this.ctx.strokeRect(200, 150, 200, 200); 266e41f4b71Sopenharmony_ci }, 267e41f4b71Sopenharmony_ci change(e){ 268e41f4b71Sopenharmony_ci if(e.newValue == 'value1'){ 269e41f4b71Sopenharmony_ci // Clear the contents on the canvas. 270e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 271e41f4b71Sopenharmony_ci // Border width 272e41f4b71Sopenharmony_ci this.ctx.lineWidth = 5; 273e41f4b71Sopenharmony_ci // Border color 274e41f4b71Sopenharmony_ci this.ctx.strokeStyle = '#110000'; 275e41f4b71Sopenharmony_ci // Line dash level of the border 276e41f4b71Sopenharmony_ci this.ctx.setLineDash([0,0]); 277e41f4b71Sopenharmony_ci // Draw a stroked rectangle. 278e41f4b71Sopenharmony_ci this.ctx.strokeRect(200, 150, 200, 200); 279e41f4b71Sopenharmony_ci }else if (e.newValue == 'value2'){ 280e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 281e41f4b71Sopenharmony_ci this.ctx.lineWidth = 30; 282e41f4b71Sopenharmony_ci this.ctx.strokeStyle = '#0000ff'; 283e41f4b71Sopenharmony_ci this.ctx.setLineDash([0,0]); 284e41f4b71Sopenharmony_ci // Draw a circle. 285e41f4b71Sopenharmony_ci this.ctx.arc(300, 250, 150,0,6.28); 286e41f4b71Sopenharmony_ci // Draw borders. 287e41f4b71Sopenharmony_ci this.ctx.stroke(); 288e41f4b71Sopenharmony_ci }else if (e.newValue == 'value3'){ 289e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 290e41f4b71Sopenharmony_ci this.ctx.lineWidth = 5; 291e41f4b71Sopenharmony_ci this.ctx.setLineDash([5,5]); 292e41f4b71Sopenharmony_ci this.ctx.strokeRect(200, 150, 200, 200); 293e41f4b71Sopenharmony_ci }else if (e.newValue == 'value4'){ 294e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 295e41f4b71Sopenharmony_ci // Draw and fill a rectangle. 296e41f4b71Sopenharmony_ci this.ctx.fillStyle = '#0000ff'; 297e41f4b71Sopenharmony_ci this.ctx.fillRect(200, 150, 200, 200); 298e41f4b71Sopenharmony_ci } 299e41f4b71Sopenharmony_ci }, 300e41f4b71Sopenharmony_ci} 301e41f4b71Sopenharmony_ci``` 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci## Setting Gradient Fill Colors 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ciAdd the **createLinearGradient** and **createRadialGradient** attributes to create a gradient container, use the **addColorStop** method to add multiple color blocks to form a gradient color, and set **fillStyle** as **gradient** to apply the gradient color to a rectangle. Then set the shadow blur level by using **shadowBlur**, the shadow color by using **shadowColor**, and the shadow offset by using **shadowOffset**. 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci```html 313e41f4b71Sopenharmony_ci<!-- xxx.hml --> 314e41f4b71Sopenharmony_ci<div class="container"> 315e41f4b71Sopenharmony_ci <canvas ref="canvas1"></canvas> 316e41f4b71Sopenharmony_ci <select @change="change"> 317e41f4b71Sopenharmony_ci <option value="value1">LinearGradient</option> 318e41f4b71Sopenharmony_ci <option value="value2">RadialGradient</option> 319e41f4b71Sopenharmony_ci <option value="value3">shadowBlur</option> 320e41f4b71Sopenharmony_ci <option value="value4">shadowOffset</option> 321e41f4b71Sopenharmony_ci </select> 322e41f4b71Sopenharmony_ci</div> 323e41f4b71Sopenharmony_ci``` 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci```css 327e41f4b71Sopenharmony_ci/* xxx.css */ 328e41f4b71Sopenharmony_ci.container{ 329e41f4b71Sopenharmony_ci width: 100%; 330e41f4b71Sopenharmony_ci height: 100%; 331e41f4b71Sopenharmony_ci flex-direction: column; 332e41f4b71Sopenharmony_ci justify-content: center; 333e41f4b71Sopenharmony_ci align-items: center; 334e41f4b71Sopenharmony_ci background-color: #F1F3F5; 335e41f4b71Sopenharmony_ci} 336e41f4b71Sopenharmony_cicanvas{ 337e41f4b71Sopenharmony_ci width: 600px; 338e41f4b71Sopenharmony_ci height: 500px; 339e41f4b71Sopenharmony_ci background-color: #fdfdfd; 340e41f4b71Sopenharmony_ci border: 5px solid red; 341e41f4b71Sopenharmony_ci} 342e41f4b71Sopenharmony_ciselect{ 343e41f4b71Sopenharmony_ci margin-top: 50px; 344e41f4b71Sopenharmony_ci width: 250px; 345e41f4b71Sopenharmony_ci height: 100px; 346e41f4b71Sopenharmony_ci background-color: white; 347e41f4b71Sopenharmony_ci} 348e41f4b71Sopenharmony_ci``` 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci```js 352e41f4b71Sopenharmony_ci// xxx.js 353e41f4b71Sopenharmony_ciexport default { 354e41f4b71Sopenharmony_ci data:{ 355e41f4b71Sopenharmony_ci el: null, 356e41f4b71Sopenharmony_ci ctx: null, 357e41f4b71Sopenharmony_ci }, 358e41f4b71Sopenharmony_ci onShow(){ 359e41f4b71Sopenharmony_ci this.el = this.$refs.canvas1; 360e41f4b71Sopenharmony_ci this.ctx = this.el.getContext("2d",{antialias: true}); 361e41f4b71Sopenharmony_ci // Create a linear gradient. 362e41f4b71Sopenharmony_ci let gradient = this.ctx.createLinearGradient(100,100, 400,300); 363e41f4b71Sopenharmony_ci // Add gradient colors. 364e41f4b71Sopenharmony_ci gradient.addColorStop(0.0, 'red'); 365e41f4b71Sopenharmony_ci gradient.addColorStop(0.7, 'white'); 366e41f4b71Sopenharmony_ci gradient.addColorStop(1.0, 'green'); 367e41f4b71Sopenharmony_ci // Apply the gradient. 368e41f4b71Sopenharmony_ci this.ctx.fillStyle = gradient; 369e41f4b71Sopenharmony_ci this.ctx.fillRect(100, 100, 400, 300); 370e41f4b71Sopenharmony_ci }, 371e41f4b71Sopenharmony_ci change(e){ 372e41f4b71Sopenharmony_ci if(e.newValue == 'value1'){ 373e41f4b71Sopenharmony_ci // Clear the contents on the canvas. 374e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 375e41f4b71Sopenharmony_ci let gradient = this.ctx.createLinearGradient(100,100, 400,300); 376e41f4b71Sopenharmony_ci gradient.addColorStop(0.0, 'red'); 377e41f4b71Sopenharmony_ci gradient.addColorStop(0.7, 'white'); 378e41f4b71Sopenharmony_ci gradient.addColorStop(1.0, 'green'); 379e41f4b71Sopenharmony_ci this.ctx.fillStyle = gradient; 380e41f4b71Sopenharmony_ci // Set the level of shadow blur. 381e41f4b71Sopenharmony_ci this.ctx.shadowBlur = 0; 382e41f4b71Sopenharmony_ci // Set the distance that shadows will be offset vertically. 383e41f4b71Sopenharmony_ci this.ctx.shadowOffsetY = 0; 384e41f4b71Sopenharmony_ci // Set the distance that shadows will be offset horizontally. 385e41f4b71Sopenharmony_ci this.ctx.shadowOffsetX = 0; 386e41f4b71Sopenharmony_ci this.ctx.fillRect(100, 100, 400, 300); 387e41f4b71Sopenharmony_ci }else if(e.newValue == 'value2'){ 388e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 389e41f4b71Sopenharmony_ci // Create a radial gradient. 390e41f4b71Sopenharmony_ci let gradient = this.ctx.createRadialGradient(300,250,20,300,250,100); 391e41f4b71Sopenharmony_ci gradient.addColorStop(0.0, 'red'); 392e41f4b71Sopenharmony_ci gradient.addColorStop(0.7, 'white'); 393e41f4b71Sopenharmony_ci gradient.addColorStop(1.0, 'green'); 394e41f4b71Sopenharmony_ci this.ctx.shadowBlur = 0; 395e41f4b71Sopenharmony_ci this.ctx.shadowOffsetY = 0; 396e41f4b71Sopenharmony_ci this.ctx.shadowOffsetX = 0; 397e41f4b71Sopenharmony_ci this.ctx.fillStyle = gradient; 398e41f4b71Sopenharmony_ci this.ctx.fillRect(100, 100, 400, 300); 399e41f4b71Sopenharmony_ci }else if(e.newValue == 'value3'){ 400e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 401e41f4b71Sopenharmony_ci let gradient = this.ctx.createLinearGradient(100,100, 400,400); 402e41f4b71Sopenharmony_ci gradient.addColorStop(0.0, 'red'); 403e41f4b71Sopenharmony_ci gradient.addColorStop(0.5, 'white'); 404e41f4b71Sopenharmony_ci gradient.addColorStop(1, '#17ea35'); 405e41f4b71Sopenharmony_ci // Set the level of shadow blur. 406e41f4b71Sopenharmony_ci this.ctx.shadowBlur = 30; 407e41f4b71Sopenharmony_ci // Set the shadow color. 408e41f4b71Sopenharmony_ci this.ctx.shadowColor = 'rgb(229, 16, 16)'; 409e41f4b71Sopenharmony_ci this.ctx.fillStyle = gradient; 410e41f4b71Sopenharmony_ci this.ctx.fillRect(100, 100, 400, 300); 411e41f4b71Sopenharmony_ci }else if(e.newValue == 'value4'){ 412e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 413e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 414e41f4b71Sopenharmony_ci let gradient = this.ctx.createRadialGradient(300,250,20,300,250,200); 415e41f4b71Sopenharmony_ci gradient.addColorStop(0.0, 'red'); 416e41f4b71Sopenharmony_ci gradient.addColorStop(0.5, 'white'); 417e41f4b71Sopenharmony_ci gradient.addColorStop(1, '#17ea35'); 418e41f4b71Sopenharmony_ci // Set the level of shadow blur. 419e41f4b71Sopenharmony_ci this.ctx.shadowBlur = 30; 420e41f4b71Sopenharmony_ci this.ctx.shadowOffsetY = 30; 421e41f4b71Sopenharmony_ci // Set the shadow color. 422e41f4b71Sopenharmony_ci this.ctx.shadowColor = 'rgb(23, 1, 1)'; 423e41f4b71Sopenharmony_ci this.ctx.fillStyle = gradient; 424e41f4b71Sopenharmony_ci this.ctx.fillRect(100, 100, 400, 300); 425e41f4b71Sopenharmony_ci } 426e41f4b71Sopenharmony_ci }, 427e41f4b71Sopenharmony_ci} 428e41f4b71Sopenharmony_ci``` 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci## Filling Texts 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ciCreate a text and use the **fillText** method to write the text on the canvas. Use the **globalAlpha** attribute to change the baseline transparency to avoid the text being hidden by the baseline. Then set the **textAlign** and **textBaseline** attributes to determine the text position based on the baseline. 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci```html 439e41f4b71Sopenharmony_ci<!-- xxx.hml --> 440e41f4b71Sopenharmony_ci<div class="container"> 441e41f4b71Sopenharmony_ci <canvas ref="canvas1"></canvas> 442e41f4b71Sopenharmony_ci <select @change="change"> 443e41f4b71Sopenharmony_ci <option value="value1">text</option> 444e41f4b71Sopenharmony_ci <option value="value2">textBaseline</option> 445e41f4b71Sopenharmony_ci <option value="value3">textAlign</option> 446e41f4b71Sopenharmony_ci </select> 447e41f4b71Sopenharmony_ci</div> 448e41f4b71Sopenharmony_ci``` 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci```css 452e41f4b71Sopenharmony_ci/* xxx.css */ 453e41f4b71Sopenharmony_ci.container{ 454e41f4b71Sopenharmony_ci width: 100%; 455e41f4b71Sopenharmony_ci height: 100%; 456e41f4b71Sopenharmony_ci flex-direction: column; 457e41f4b71Sopenharmony_ci justify-content: center; 458e41f4b71Sopenharmony_ci align-items: center; 459e41f4b71Sopenharmony_ci background-color: #F1F3F5; 460e41f4b71Sopenharmony_ci} 461e41f4b71Sopenharmony_cicanvas{ 462e41f4b71Sopenharmony_ci width: 600px; 463e41f4b71Sopenharmony_ci height: 500px; 464e41f4b71Sopenharmony_ci background-color: #fdfdfd; 465e41f4b71Sopenharmony_ci border: 5px solid red; 466e41f4b71Sopenharmony_ci} 467e41f4b71Sopenharmony_ciselect{ 468e41f4b71Sopenharmony_ci margin-top: 50px; 469e41f4b71Sopenharmony_ci width: 250px; 470e41f4b71Sopenharmony_ci height: 100px; 471e41f4b71Sopenharmony_ci background-color: white; 472e41f4b71Sopenharmony_ci} 473e41f4b71Sopenharmony_ci``` 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ci```js 477e41f4b71Sopenharmony_ci// xxx.js 478e41f4b71Sopenharmony_ciexport default { 479e41f4b71Sopenharmony_ci data:{ 480e41f4b71Sopenharmony_ci el: null, 481e41f4b71Sopenharmony_ci ctx: null, 482e41f4b71Sopenharmony_ci }, 483e41f4b71Sopenharmony_ci onShow(){ 484e41f4b71Sopenharmony_ci this.el = this.$refs.canvas1; 485e41f4b71Sopenharmony_ci this.ctx = this.el.getContext("2d",{antialias: true}); 486e41f4b71Sopenharmony_ci // Create a text. 487e41f4b71Sopenharmony_ci let text = "Hello World"; 488e41f4b71Sopenharmony_ci // Set the font. 489e41f4b71Sopenharmony_ci this.ctx.font = '30px'; 490e41f4b71Sopenharmony_ci this.ctx.fillText("with:"+this.ctx.measureText(text).width, 200, 300); 491e41f4b71Sopenharmony_ci // Set the fill text. 492e41f4b71Sopenharmony_ci this.ctx.fillText(text, 200, 250); 493e41f4b71Sopenharmony_ci }, 494e41f4b71Sopenharmony_ci change(e){ 495e41f4b71Sopenharmony_ci if(e.newValue == 'value1'){ 496e41f4b71Sopenharmony_ci // Clear the contents on the canvas. 497e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 498e41f4b71Sopenharmony_ci // Start a new path. 499e41f4b71Sopenharmony_ci this.ctx.beginPath(); 500e41f4b71Sopenharmony_ci // Initialize the textAlign value. 501e41f4b71Sopenharmony_ci this.ctx.textAlign = 'left'; 502e41f4b71Sopenharmony_ci // Initialize textBaseline. 503e41f4b71Sopenharmony_ci this.ctx.textBaseline = 'alphabetic'; 504e41f4b71Sopenharmony_ci // Set the font. 505e41f4b71Sopenharmony_ci this.ctx.font = '30px'; 506e41f4b71Sopenharmony_ci let text = "Hello World"; 507e41f4b71Sopenharmony_ci // Obtain the font width. 508e41f4b71Sopenharmony_ci this.ctx.fillText("with:"+this.ctx.measureText(text).width, 200, 300); 509e41f4b71Sopenharmony_ci // Set the fill text. 510e41f4b71Sopenharmony_ci this.ctx.fillText(text, 200, 250); 511e41f4b71Sopenharmony_ci }else if(e.newValue == 'value2'){ 512e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 513e41f4b71Sopenharmony_ci this.ctx.beginPath(); 514e41f4b71Sopenharmony_ci // Set the Alpha value. 515e41f4b71Sopenharmony_ci this.ctx.globalAlpha = 0.1; 516e41f4b71Sopenharmony_ci // Set the line width. 517e41f4b71Sopenharmony_ci this.ctx.lineWidth = 10; 518e41f4b71Sopenharmony_ci // Set the line segment color. 519e41f4b71Sopenharmony_ci this.ctx.strokeStyle = '#0000ff'; 520e41f4b71Sopenharmony_ci // Move from the current point to a target position. 521e41f4b71Sopenharmony_ci this.ctx.moveTo(0, 240); 522e41f4b71Sopenharmony_ci // Connect the current point to a target position. 523e41f4b71Sopenharmony_ci this.ctx.lineTo(600, 240); 524e41f4b71Sopenharmony_ci this.ctx.stroke(); 525e41f4b71Sopenharmony_ci this.ctx.font = '35px'; 526e41f4b71Sopenharmony_ci this.ctx.globalAlpha = 1; 527e41f4b71Sopenharmony_ci // Initialize the textAlign value. 528e41f4b71Sopenharmony_ci this.ctx.textAlign = 'left'; 529e41f4b71Sopenharmony_ci // Set textBaseline. 530e41f4b71Sopenharmony_ci this.ctx.textBaseline = 'top'; 531e41f4b71Sopenharmony_ci this.ctx.fillText('Top', 50, 240); 532e41f4b71Sopenharmony_ci this.ctx.textBaseline = 'bottom'; 533e41f4b71Sopenharmony_ci this.ctx.fillText('Bottom', 200, 240); 534e41f4b71Sopenharmony_ci this.ctx.textBaseline = 'middle'; 535e41f4b71Sopenharmony_ci this.ctx.fillText('Middle', 400, 240); 536e41f4b71Sopenharmony_ci }else if(e.newValue == 'value3'){ 537e41f4b71Sopenharmony_ci // Clear the contents on the canvas. 538e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 539e41f4b71Sopenharmony_ci this.ctx.beginPath(); 540e41f4b71Sopenharmony_ci this.ctx.globalAlpha = 0.1; 541e41f4b71Sopenharmony_ci this.ctx.lineWidth = 10; 542e41f4b71Sopenharmony_ci this.ctx.strokeStyle = '#0000ff'; 543e41f4b71Sopenharmony_ci this.ctx.moveTo(300, 0); 544e41f4b71Sopenharmony_ci this.ctx.lineTo(300, 500); 545e41f4b71Sopenharmony_ci this.ctx.stroke(); 546e41f4b71Sopenharmony_ci this.ctx.font = '35px'; 547e41f4b71Sopenharmony_ci this.ctx.globalAlpha = 1; 548e41f4b71Sopenharmony_ci // Initialize textBaseline. 549e41f4b71Sopenharmony_ci this.ctx.textBaseline = 'alphabetic'; 550e41f4b71Sopenharmony_ci // Set textAlign. 551e41f4b71Sopenharmony_ci this.ctx.textAlign = 'left'; 552e41f4b71Sopenharmony_ci this.ctx.fillText('textAlign=left',300, 100); 553e41f4b71Sopenharmony_ci this.ctx.textAlign = 'center'; 554e41f4b71Sopenharmony_ci this.ctx.fillText('textAlign=center',300, 250); 555e41f4b71Sopenharmony_ci this.ctx.textAlign = 'right'; 556e41f4b71Sopenharmony_ci this.ctx.fillText('textAlign=right',300, 400); 557e41f4b71Sopenharmony_ci } 558e41f4b71Sopenharmony_ci } 559e41f4b71Sopenharmony_ci} 560e41f4b71Sopenharmony_ci``` 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci> **NOTE** 565e41f4b71Sopenharmony_ci> 566e41f4b71Sopenharmony_ci> In the **ltr** layout mode, the value **start** equals **left**. In the **rtl** layout mode, the value **start** equals **right**. 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci## Adding Images 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciAfter creating an image object, use the **drawImage** attribute to draw the image and set animation styles such as scaling, translating, and rotating. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci```html 575e41f4b71Sopenharmony_ci<!-- xxx.hml --> 576e41f4b71Sopenharmony_ci<div class="container"> 577e41f4b71Sopenharmony_ci <div class="content"> 578e41f4b71Sopenharmony_ci <canvas ref="canvas0"></canvas> 579e41f4b71Sopenharmony_ci <text onclick="change">change</text> 580e41f4b71Sopenharmony_ci <canvas ref="canvas1"></canvas> 581e41f4b71Sopenharmony_ci <text onclick="rotate">rotate</text> 582e41f4b71Sopenharmony_ci <canvas ref="canvas2"></canvas> 583e41f4b71Sopenharmony_ci <text onclick="scale">scale</text> 584e41f4b71Sopenharmony_ci <canvas ref="canvas3"></canvas> 585e41f4b71Sopenharmony_ci <text onclick="translate" style="width: 300px;">translate</text> 586e41f4b71Sopenharmony_ci <canvas ref="canvas4"></canvas> 587e41f4b71Sopenharmony_ci <text onclick="transform" style="width: 300px;">transform</text> 588e41f4b71Sopenharmony_ci <canvas ref="canvas5"></canvas> 589e41f4b71Sopenharmony_ci <text onclick="setTransform" style="width: 300px;">setTransform</text> 590e41f4b71Sopenharmony_ci <canvas ref="canvas6"></canvas> 591e41f4b71Sopenharmony_ci </div> 592e41f4b71Sopenharmony_ci</div> 593e41f4b71Sopenharmony_ci``` 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci```css 597e41f4b71Sopenharmony_ci/* xxx.css */ 598e41f4b71Sopenharmony_ci.container{ 599e41f4b71Sopenharmony_ci width: 100%; 600e41f4b71Sopenharmony_ci flex-direction: column; 601e41f4b71Sopenharmony_ci background-color: #F1F3F5; 602e41f4b71Sopenharmony_ci align-items: center; 603e41f4b71Sopenharmony_ci} 604e41f4b71Sopenharmony_cicanvas{ 605e41f4b71Sopenharmony_ci width: 600px; 606e41f4b71Sopenharmony_ci height: 300px; 607e41f4b71Sopenharmony_ci margin-bottom: 100px; 608e41f4b71Sopenharmony_ci background-color: #fdfdfd; 609e41f4b71Sopenharmony_ci border: 5px solid red; 610e41f4b71Sopenharmony_ci} 611e41f4b71Sopenharmony_ci.content{ 612e41f4b71Sopenharmony_ci width: 80%; 613e41f4b71Sopenharmony_ci margin-top: 50px; 614e41f4b71Sopenharmony_ci margin-bottom: 50px; 615e41f4b71Sopenharmony_ci display: flex; 616e41f4b71Sopenharmony_ci flex-wrap: wrap; 617e41f4b71Sopenharmony_ci justify-content: space-around; 618e41f4b71Sopenharmony_ci} 619e41f4b71Sopenharmony_citext{ 620e41f4b71Sopenharmony_ci font-size: 35px; 621e41f4b71Sopenharmony_ci width: 200px; 622e41f4b71Sopenharmony_ci height: 80px; 623e41f4b71Sopenharmony_ci color: white; 624e41f4b71Sopenharmony_ci border-radius: 20px; 625e41f4b71Sopenharmony_ci text-align: center; 626e41f4b71Sopenharmony_ci background-color: #6060e7; 627e41f4b71Sopenharmony_ci margin-bottom: 30px; 628e41f4b71Sopenharmony_ci} 629e41f4b71Sopenharmony_ci``` 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci 632e41f4b71Sopenharmony_ci```js 633e41f4b71Sopenharmony_ci// xxx.js 634e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 635e41f4b71Sopenharmony_ciexport default { 636e41f4b71Sopenharmony_ci data:{ 637e41f4b71Sopenharmony_ci compositeOperation: 'source-over' 638e41f4b71Sopenharmony_ci }, 639e41f4b71Sopenharmony_ci onShow(){ 640e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas0.getContext("2d"); 641e41f4b71Sopenharmony_ci // Create an image object. 642e41f4b71Sopenharmony_ci let img = new Image(); 643e41f4b71Sopenharmony_ci // Set the image path. 644e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 645e41f4b71Sopenharmony_ci // Set the image width. 646e41f4b71Sopenharmony_ci img.width= 150; 647e41f4b71Sopenharmony_ci // Set the image height. 648e41f4b71Sopenharmony_ci img.height=150; 649e41f4b71Sopenharmony_ci // Create an image tiling container. 650e41f4b71Sopenharmony_ci var pat = ctx.createPattern(img, 'repeat');ctx.fillStyle = pat; 651e41f4b71Sopenharmony_ci ctx.fillRect(0, 0, 600, 300); 652e41f4b71Sopenharmony_ci }, 653e41f4b71Sopenharmony_ci change(){ 654e41f4b71Sopenharmony_ci // Obtain the brush after the canvas is created. 655e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas1.getContext("2d"); 656e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,1000); 657e41f4b71Sopenharmony_ci if(this.compositeOperation == "source-over"){ 658e41f4b71Sopenharmony_ci this.compositeOperation = "destination-over"; 659e41f4b71Sopenharmony_ci }else{ 660e41f4b71Sopenharmony_ci this.compositeOperation = "source-over"; 661e41f4b71Sopenharmony_ci } 662e41f4b71Sopenharmony_ci ctx.globalCompositeOperation = this.compositeOperation; 663e41f4b71Sopenharmony_ci let img = new Image(); 664e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 665e41f4b71Sopenharmony_ci // Invoked when the image is successfully obtained. 666e41f4b71Sopenharmony_ci img.onload = function() { 667e41f4b71Sopenharmony_ci ctx.drawImage(img, 150, 20, 200, 200); 668e41f4b71Sopenharmony_ci }; 669e41f4b71Sopenharmony_ci let img1 = new Image(); 670e41f4b71Sopenharmony_ci img1.src = 'common/images/3.png'; 671e41f4b71Sopenharmony_ci img1.onload = function() { 672e41f4b71Sopenharmony_ci // Draw an image. 673e41f4b71Sopenharmony_ci ctx.drawImage(img1, 250, 80, 200, 200); 674e41f4b71Sopenharmony_ci }; 675e41f4b71Sopenharmony_ci // A method is triggered when the image fails to be obtained. 676e41f4b71Sopenharmony_ci img1.onerror = function() { 677e41f4b71Sopenharmony_ci promptAction.showToast({message:"error",duration:2000}) 678e41f4b71Sopenharmony_ci }; 679e41f4b71Sopenharmony_ci }, 680e41f4b71Sopenharmony_ci rotate(){ 681e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas2.getContext("2d"); 682e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,300); 683e41f4b71Sopenharmony_ci // Rotate the image. 684e41f4b71Sopenharmony_ci ctx.rotate(10 * Math.PI / 180); 685e41f4b71Sopenharmony_ci let img = new Image(); 686e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 687e41f4b71Sopenharmony_ci img.onload = function() { 688e41f4b71Sopenharmony_ci ctx.drawImage(img, 300, 0, 100, 100); 689e41f4b71Sopenharmony_ci }; 690e41f4b71Sopenharmony_ci }, 691e41f4b71Sopenharmony_ci scale(){ 692e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas3.getContext("2d"); 693e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,200); 694e41f4b71Sopenharmony_ci // Scale the image. 695e41f4b71Sopenharmony_ci ctx.scale(1.3,1.2); 696e41f4b71Sopenharmony_ci let img = new Image(); 697e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 698e41f4b71Sopenharmony_ci img.onload = function() { 699e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 0, 50, 50); 700e41f4b71Sopenharmony_ci }; 701e41f4b71Sopenharmony_ci }, 702e41f4b71Sopenharmony_ci translate(){ 703e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas4.getContext("2d"); 704e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,300); 705e41f4b71Sopenharmony_ci ctx.translate(10,0); 706e41f4b71Sopenharmony_ci let img = new Image(); 707e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 708e41f4b71Sopenharmony_ci img.onload = function() { 709e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 50, 300, 200); 710e41f4b71Sopenharmony_ci }; 711e41f4b71Sopenharmony_ci }, 712e41f4b71Sopenharmony_ci transform(){ 713e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas5.getContext("2d"); 714e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,300); 715e41f4b71Sopenharmony_ci ctx.transform(1.1, 0.1, 0.1, 1, 10, 0); 716e41f4b71Sopenharmony_ci let img = new Image(); 717e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 718e41f4b71Sopenharmony_ci img.onload = function() { 719e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 50, 100, 100); 720e41f4b71Sopenharmony_ci }; 721e41f4b71Sopenharmony_ci }, 722e41f4b71Sopenharmony_ci setTransform(){ 723e41f4b71Sopenharmony_ci let ctx = this.$refs.canvas6.getContext("2d"); 724e41f4b71Sopenharmony_ci ctx.clearRect(0,0,600,300); 725e41f4b71Sopenharmony_ci ctx.setTransform(1.1, 0.1, 0.1, 1, 10, 0); 726e41f4b71Sopenharmony_ci let img = new Image(); 727e41f4b71Sopenharmony_ci img.src = 'common/images/2.png'; 728e41f4b71Sopenharmony_ci img.onload = function() { 729e41f4b71Sopenharmony_ci ctx.drawImage(img, 0, 50, 100, 100); 730e41f4b71Sopenharmony_ci }; 731e41f4b71Sopenharmony_ci }, 732e41f4b71Sopenharmony_ci} 733e41f4b71Sopenharmony_ci``` 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci> **NOTE** 738e41f4b71Sopenharmony_ci> - Unlike the **transform()** function, the **setTransform()** function resets the existing transformation matrix and creates a transformation matrix even if it uses the same parameters. 739e41f4b71Sopenharmony_ci> 740e41f4b71Sopenharmony_ci> - The following formulas calculate coordinates of the transformed graph. **x** and **y** represent coordinates before transformation, and **x'** and **y'** represent coordinates after transformation. 741e41f4b71Sopenharmony_ci> x' = scaleX \* x + skewY \* y + translateX 742e41f4b71Sopenharmony_ci> 743e41f4b71Sopenharmony_ci> y' = skewX \* x + scaleY \* y + translateY 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci## Adding Methods 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ciUse the **save** method to save the brush style, and use the **restore** method to restore the saved settings. In the following example, set the brush to red. After the brush setting is saved, clear the canvas and change the brush to blue. In this moment, directly using the brush will get a blue rectangle; using the brush after the restore operation will get a red rectangle. 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ci```html 752e41f4b71Sopenharmony_ci<!-- xxx.hml --> 753e41f4b71Sopenharmony_ci<div class="container"> 754e41f4b71Sopenharmony_ci <div class="content"> 755e41f4b71Sopenharmony_ci <canvas ref="canvas"></canvas> 756e41f4b71Sopenharmony_ci </div> 757e41f4b71Sopenharmony_ci <div class="content"> 758e41f4b71Sopenharmony_ci <text onclick="save">save</text> 759e41f4b71Sopenharmony_ci <text onclick="clear">clear</text> 760e41f4b71Sopenharmony_ci <text onclick="restore">restore</text> 761e41f4b71Sopenharmony_ci </div> 762e41f4b71Sopenharmony_ci</div> 763e41f4b71Sopenharmony_ci``` 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci 766e41f4b71Sopenharmony_ci```css 767e41f4b71Sopenharmony_ci/* xxx.css */ 768e41f4b71Sopenharmony_ci.container{ 769e41f4b71Sopenharmony_ci width: 100%; 770e41f4b71Sopenharmony_ci height: 100%; 771e41f4b71Sopenharmony_ci flex-direction: column; 772e41f4b71Sopenharmony_ci background-color: #F1F3F5; 773e41f4b71Sopenharmony_ci align-items: center; 774e41f4b71Sopenharmony_ci} 775e41f4b71Sopenharmony_cicanvas{ 776e41f4b71Sopenharmony_ci margin-top: 300px; 777e41f4b71Sopenharmony_ci width: 600px; 778e41f4b71Sopenharmony_ci height: 500px; 779e41f4b71Sopenharmony_ci background-color: #fdfdfd; 780e41f4b71Sopenharmony_ci border: 5px solid red; 781e41f4b71Sopenharmony_ci} 782e41f4b71Sopenharmony_ci.content{ 783e41f4b71Sopenharmony_ci width: 80%; 784e41f4b71Sopenharmony_ci margin-top: 50px; 785e41f4b71Sopenharmony_ci margin-bottom: 50px; 786e41f4b71Sopenharmony_ci display: flex; 787e41f4b71Sopenharmony_ci flex-wrap: wrap; 788e41f4b71Sopenharmony_ci justify-content: space-around; 789e41f4b71Sopenharmony_ci} 790e41f4b71Sopenharmony_citext{ 791e41f4b71Sopenharmony_ci width: 150px; 792e41f4b71Sopenharmony_ci height: 80px; 793e41f4b71Sopenharmony_ci color: white; 794e41f4b71Sopenharmony_ci border-radius: 20px; 795e41f4b71Sopenharmony_ci text-align: center; 796e41f4b71Sopenharmony_ci background-color: #6060e7; 797e41f4b71Sopenharmony_ci margin-bottom: 30px; 798e41f4b71Sopenharmony_ci} 799e41f4b71Sopenharmony_ci``` 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci```js 803e41f4b71Sopenharmony_ci// xxx.js 804e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 805e41f4b71Sopenharmony_ciexport default { 806e41f4b71Sopenharmony_ci data:{ 807e41f4b71Sopenharmony_ci ctx: '', 808e41f4b71Sopenharmony_ci }, 809e41f4b71Sopenharmony_ci onShow(){ 810e41f4b71Sopenharmony_ci this.ctx = this.$refs.canvas.getContext("2d"); 811e41f4b71Sopenharmony_ci this.ctx.fillStyle = "red" 812e41f4b71Sopenharmony_ci this.ctx.fillRect(200, 150, 200, 200); 813e41f4b71Sopenharmony_ci }, 814e41f4b71Sopenharmony_ci save(){ 815e41f4b71Sopenharmony_ci // Save the brush setting. 816e41f4b71Sopenharmony_ci this.ctx.save(); 817e41f4b71Sopenharmony_ci promptAction.showToast({message:"save succeed"}); 818e41f4b71Sopenharmony_ci }, 819e41f4b71Sopenharmony_ci clear(){ 820e41f4b71Sopenharmony_ci this.ctx.clearRect(0,0,600,500); 821e41f4b71Sopenharmony_ci // Change the brush color. 822e41f4b71Sopenharmony_ci this.ctx.fillStyle = "#2133d2"; 823e41f4b71Sopenharmony_ci }, 824e41f4b71Sopenharmony_ci restore(){ 825e41f4b71Sopenharmony_ci this.ctx.beginPath(); 826e41f4b71Sopenharmony_ci // Restore the brush setting. 827e41f4b71Sopenharmony_ci this.ctx.restore(); 828e41f4b71Sopenharmony_ci this.ctx.fillRect(200, 150, 200, 200); 829e41f4b71Sopenharmony_ci }, 830e41f4b71Sopenharmony_ci} 831e41f4b71Sopenharmony_ci``` 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci 834