1e41f4b71Sopenharmony_ci# OffscreenCanvasRenderingContext2D
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci**OffscreenCanvasRenderingContext2D** allows you to draw rectangles, text, images, and other objects on an offscreen canvas, which is a new buffer created by the GPU outside of the current buffer. For details, see [OffscreenCanvasRenderingContext2D](../reference/apis-arkui/arkui-js/js-offscreencanvasrenderingcontext2d.md).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciIn the following example, you first create an offscreen canvas, and then create a **getContext2d** object on the canvas, which is an image, and finally set the **filter** attribute for the image.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci```html
8e41f4b71Sopenharmony_ci<!-- xxx.hml -->
9e41f4b71Sopenharmony_ci<div class="container">
10e41f4b71Sopenharmony_ci  <canvas ref="canvas1"></canvas>
11e41f4b71Sopenharmony_ci  <select @change="change()">
12e41f4b71Sopenharmony_ci    <option value="blur(5px)">blur</option>
13e41f4b71Sopenharmony_ci    <option value="grayscale(50%)">grayscale</option>
14e41f4b71Sopenharmony_ci    <option value="hue-rotate(45deg)">hue-rotate</option>
15e41f4b71Sopenharmony_ci    <option value="invert(100%)">invert</option>
16e41f4b71Sopenharmony_ci    <option value="drop-shadow(8px 8px 10px green)">drop-shadow</option>
17e41f4b71Sopenharmony_ci    <option value="brightness(0.4)">brightness</option>
18e41f4b71Sopenharmony_ci    <option value="opacity(0.25)">opacity</option>
19e41f4b71Sopenharmony_ci    <option value="saturate(30%)">saturate</option>
20e41f4b71Sopenharmony_ci    <option value="sepia(60%)">sepia</option>
21e41f4b71Sopenharmony_ci    <option value="contrast(200%)">contrast</option>
22e41f4b71Sopenharmony_ci  </select>
23e41f4b71Sopenharmony_ci</div>
24e41f4b71Sopenharmony_ci```
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci```css
27e41f4b71Sopenharmony_ci/* xxx.css */
28e41f4b71Sopenharmony_ci.container{
29e41f4b71Sopenharmony_ci  width: 100%;
30e41f4b71Sopenharmony_ci  height: 100%;
31e41f4b71Sopenharmony_ci  flex-direction: column;
32e41f4b71Sopenharmony_ci  justify-content: center;
33e41f4b71Sopenharmony_ci  align-items: center;
34e41f4b71Sopenharmony_ci  background-color: #F1F3F5;
35e41f4b71Sopenharmony_ci}
36e41f4b71Sopenharmony_cicanvas{
37e41f4b71Sopenharmony_ci  width: 600px;
38e41f4b71Sopenharmony_ci  height: 500px;
39e41f4b71Sopenharmony_ci  background-color: #fdfdfd;
40e41f4b71Sopenharmony_ci  border: 5px solid red;
41e41f4b71Sopenharmony_ci}
42e41f4b71Sopenharmony_ciselect{
43e41f4b71Sopenharmony_ci  margin-top: 50px;
44e41f4b71Sopenharmony_ci  width: 250px;
45e41f4b71Sopenharmony_ci  height: 100px;
46e41f4b71Sopenharmony_ci  background-color: white;
47e41f4b71Sopenharmony_ci}
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci```js
51e41f4b71Sopenharmony_ci// xxx.js
52e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction';
53e41f4b71Sopenharmony_ciexport default {
54e41f4b71Sopenharmony_ci  data:{
55e41f4b71Sopenharmony_ci    el: null,
56e41f4b71Sopenharmony_ci    ctx: null,
57e41f4b71Sopenharmony_ci    offscreen: null,
58e41f4b71Sopenharmony_ci    offCanvas: null,
59e41f4b71Sopenharmony_ci    img: null,
60e41f4b71Sopenharmony_ci  },
61e41f4b71Sopenharmony_ci  onShow(){
62e41f4b71Sopenharmony_ci    this.ctx = this.$refs.canvas1.getContext("2d");
63e41f4b71Sopenharmony_ci    this.offscreen = new OffscreenCanvas(600, 500);
64e41f4b71Sopenharmony_ci    this.offCanvas = this.offscreen.getContext("2d");
65e41f4b71Sopenharmony_ci    this.img = new Image();
66e41f4b71Sopenharmony_ci    this.img.src = 'common/images/2.png';
67e41f4b71Sopenharmony_ci    // Invoked when the image is successfully obtained.
68e41f4b71Sopenharmony_ci    let _this = this;
69e41f4b71Sopenharmony_ci    this.img.onload = function() {
70e41f4b71Sopenharmony_ci      _this.offCanvas.drawImage(_this.img, 100, 100, 400, 300);
71e41f4b71Sopenharmony_ci    };
72e41f4b71Sopenharmony_ci    this.img.onerror = function() {
73e41f4b71Sopenharmony_ci      promptAction.showToast({message:"error",duration:2000})
74e41f4b71Sopenharmony_ci    };
75e41f4b71Sopenharmony_ci    var bitmap = this.offscreen.transferToImageBitmap();    this.ctx.transferFromImageBitmap(bitmap);
76e41f4b71Sopenharmony_ci  },
77e41f4b71Sopenharmony_ci  change(e){
78e41f4b71Sopenharmony_ci    this.offCanvas.filter = e.newValue;this.offCanvas.drawImage(this.img, 100, 100, 400, 300);
79e41f4b71Sopenharmony_ci    var bitmap = this.offscreen.transferToImageBitmap();
80e41f4b71Sopenharmony_ci    this.ctx.transferFromImageBitmap(bitmap);
81e41f4b71Sopenharmony_ci  },
82e41f4b71Sopenharmony_ci}
83e41f4b71Sopenharmony_ci```
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci![en-us_image_0000001232162292](figures/en-us_image_0000001232162292.gif)
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci## Determining the Position
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ciUse **isPointInPath** to determine whether a coordinate is within the path area and use **isPointInStroke** to determine whether a coordinate is on the edge of the path.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci```html
95e41f4b71Sopenharmony_ci<!-- xxx.hml -->
96e41f4b71Sopenharmony_ci<div class="container">
97e41f4b71Sopenharmony_ci  <div class="content">
98e41f4b71Sopenharmony_ci    <text>Coordinate: {{X}}, {{Y}}</text>
99e41f4b71Sopenharmony_ci    <text>In path:{{textValue}}</text>
100e41f4b71Sopenharmony_ci    <text>In stroke:{{textValue1}}</text>
101e41f4b71Sopenharmony_ci  </div>
102e41f4b71Sopenharmony_ci  <canvas ref="canvas"></canvas>
103e41f4b71Sopenharmony_ci  <button onclick="change">Add(50)</button>
104e41f4b71Sopenharmony_ci</div>
105e41f4b71Sopenharmony_ci```
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci```css
109e41f4b71Sopenharmony_ci/* xxx.css */
110e41f4b71Sopenharmony_ci.container{
111e41f4b71Sopenharmony_ci  width: 100%;
112e41f4b71Sopenharmony_ci  height: 100%;
113e41f4b71Sopenharmony_ci  flex-direction: column;
114e41f4b71Sopenharmony_ci  justify-content: center;
115e41f4b71Sopenharmony_ci  align-items: center;
116e41f4b71Sopenharmony_ci  background-color: #F1F3F5;
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_cicanvas{
119e41f4b71Sopenharmony_ci  width: 600px;
120e41f4b71Sopenharmony_ci  height: 500px;
121e41f4b71Sopenharmony_ci  background-color: #fdfdfd;
122e41f4b71Sopenharmony_ci  border: 5px solid red;
123e41f4b71Sopenharmony_ci}
124e41f4b71Sopenharmony_ci.content{
125e41f4b71Sopenharmony_ci  flex-direction: column;
126e41f4b71Sopenharmony_ci  justify-content: center;
127e41f4b71Sopenharmony_ci  align-items: center; 
128e41f4b71Sopenharmony_ci}
129e41f4b71Sopenharmony_citext{
130e41f4b71Sopenharmony_ci  font-size: 30px;
131e41f4b71Sopenharmony_ci  width: 300px;
132e41f4b71Sopenharmony_ci  height: 80px;
133e41f4b71Sopenharmony_ci  text-align: center;
134e41f4b71Sopenharmony_ci}
135e41f4b71Sopenharmony_cibutton{
136e41f4b71Sopenharmony_ci  width: 180px;
137e41f4b71Sopenharmony_ci  height: 75px;
138e41f4b71Sopenharmony_ci  margin-top: 50px;
139e41f4b71Sopenharmony_ci}
140e41f4b71Sopenharmony_ci```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci```js
144e41f4b71Sopenharmony_ci// xxx.js
145e41f4b71Sopenharmony_ciexport default {
146e41f4b71Sopenharmony_ci  data: {
147e41f4b71Sopenharmony_ci    textValue: 0,
148e41f4b71Sopenharmony_ci    textValue1: 0,
149e41f4b71Sopenharmony_ci    X:0,
150e41f4b71Sopenharmony_ci    Y:250,
151e41f4b71Sopenharmony_ci  },
152e41f4b71Sopenharmony_ci  onShow(){
153e41f4b71Sopenharmony_ci    let canvas = this.$refs.canvas.getContext('2d');
154e41f4b71Sopenharmony_ci    let offscreen = new OffscreenCanvas(500,500);
155e41f4b71Sopenharmony_ci    let offscreenCanvasCtx = offscreen.getContext("2d");
156e41f4b71Sopenharmony_ci    let offscreenCanvasCtx1 = offscreen.getContext("2d");
157e41f4b71Sopenharmony_ci    offscreenCanvasCtx1.arc(this.X, this.Y, 2, 0, 6.28);
158e41f4b71Sopenharmony_ci    offscreenCanvasCtx.lineWidth=20;
159e41f4b71Sopenharmony_ci    offscreenCanvasCtx.rect(200,150, 200, 200);
160e41f4b71Sopenharmony_ci    offscreenCanvasCtx.stroke();
161e41f4b71Sopenharmony_ci    this.textValue1 = offscreenCanvasCtx.isPointInStroke(this.X, this.Y)?'true':'false';
162e41f4b71Sopenharmony_ci    this.textValue = offscreenCanvasCtx.isPointInPath(this.X, this.Y)?'true':'false';
163e41f4b71Sopenharmony_ci    let bitmap = offscreen.transferToImageBitmap();
164e41f4b71Sopenharmony_ci    canvas.transferFromImageBitmap(bitmap);
165e41f4b71Sopenharmony_ci  },
166e41f4b71Sopenharmony_ci  change(){
167e41f4b71Sopenharmony_ci    if(this.X < 500){
168e41f4b71Sopenharmony_ci      this.X = this.X+50;
169e41f4b71Sopenharmony_ci    }else{
170e41f4b71Sopenharmony_ci      this.X = 0;
171e41f4b71Sopenharmony_ci    }
172e41f4b71Sopenharmony_ci    let canvas = this.$refs.canvas.getContext('2d');
173e41f4b71Sopenharmony_ci    let offscreen = new OffscreenCanvas(500,500);
174e41f4b71Sopenharmony_ci    let offscreenCanvasCtx = offscreen.getContext("2d");
175e41f4b71Sopenharmony_ci    let offscreenCanvasCtx1 = offscreen.getContext("2d");
176e41f4b71Sopenharmony_ci    offscreenCanvasCtx1.arc(this.X, this.Y, 1, 0, 6.28)
177e41f4b71Sopenharmony_ci    offscreenCanvasCtx.lineWidth=20
178e41f4b71Sopenharmony_ci    offscreenCanvasCtx.rect(200,150, 200, 200);
179e41f4b71Sopenharmony_ci    offscreenCanvasCtx.stroke();
180e41f4b71Sopenharmony_ci    this.textValue1 = offscreenCanvasCtx.isPointInStroke(this.X, this.Y)?'true':'false';
181e41f4b71Sopenharmony_ci    this.textValue = offscreenCanvasCtx.isPointInPath(this.X, this.Y)?'true':'false';
182e41f4b71Sopenharmony_ci    let bitmap = offscreen.transferToImageBitmap();
183e41f4b71Sopenharmony_ci    canvas.transferFromImageBitmap(bitmap);
184e41f4b71Sopenharmony_ci  }
185e41f4b71Sopenharmony_ci}
186e41f4b71Sopenharmony_ci```
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci![en-us_image_0000001178084014](figures/en-us_image_0000001178084014.gif)
189