1# OffscreenCanvasRenderingContext2D
2
3>  **NOTE**
4>
5>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
6
7
8Use **OffscreenCanvasRenderingContext2D** to draw rectangles, images, and texts on an [OffscreenCanvas](js-components-canvas-offscreencanvas.md) object.
9
10
11## Attributes
12
13In addition to the attributes that are supported by [CanvasRenderingContext2D](js-components-canvas-canvasrenderingcontext2d.md), the following attributes are supported.
14
15| Name    | Type    | Description                                      |
16| ------ | ------ | ---------------------------------------- |
17| filter | string | Image filter.<br>Available options are as follows:<br>- **blur**: applies the Gaussian blur for the image.<br>- **brightness**: applies a linear multiplication to the image to make it look brighter or darker.<br>- **contrast**: adjusts the image contrast.<br>- **drop-shadow**: sets a shadow effect for the image.<br>- **grayscale**: converts the image to a grayscale image.<br>- **hue-rotate**: applies hue rotation to the image.<br>- **invert**: inverts the image.<br>- **opacity**: sets the image opacity.<br>- **saturate**: sets the image saturation.<br>- **sepia**: converts the image to sepia.|
18
19**Example**
20```html
21<!-- xxx.hml -->
22<div style="width: 500px; height: 500px;">
23  <canvas ref="canvasId" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
24</div>
25```
26
27```js
28// xxx.js
29export default {
30  onShow(){
31    var ctx = this.$refs.canvasId.getContext('2d');
32    var offscreen = new OffscreenCanvas(360, 500);
33    var offCanvas2 = offscreen.getContext("2d");
34    var img = new Image();
35    img.src = 'common/images/flower.jpg';
36    offCanvas2.drawImage(img, 0, 0, 100, 100);
37    offCanvas2.filter = 'blur(5px)';
38    offCanvas2.drawImage(img, 100, 0, 100, 100);
39
40    offCanvas2.filter = 'grayscale(50%)';
41    offCanvas2.drawImage(img, 200, 0, 100, 100);
42
43    offCanvas2.filter = 'hue-rotate(90deg)';
44    offCanvas2.drawImage(img, 0, 100, 100, 100);
45
46    offCanvas2.filter = 'invert(100%)';
47    offCanvas2.drawImage(img, 100, 100, 100, 100);
48
49    offCanvas2.filter = 'drop-shadow(8px 8px 10px green)';
50    offCanvas2.drawImage(img, 200, 100, 100, 100);
51
52    offCanvas2.filter = 'brightness(0.4)';
53    offCanvas2.drawImage(img, 0, 200, 100, 100);
54
55    offCanvas2.filter = 'opacity(25%)';
56    offCanvas2.drawImage(img, 100, 200, 100, 100);
57
58    offCanvas2.filter = 'saturate(30%)';
59    offCanvas2.drawImage(img, 200, 200, 100, 100);
60
61    offCanvas2.filter = 'sepia(60%)';
62    offCanvas2.drawImage(img, 0, 300, 100, 100);
63
64    offCanvas2.filter = 'contrast(200%)';
65    offCanvas2.drawImage(img, 100, 300, 100, 100);
66    var bitmap = offscreen.transferToImageBitmap();
67    ctx.transferFromImageBitmap(bitmap);
68  }
69}
70```
71
72## Methods
73
74In addition to the methods that are supported by **CanvasRenderingContext2D**, the following methods are supported.
75
76
77### isPointInPath
78
79isPointInPath(path?: Path2D, x: number, y: number): boolean
80
81Checks whether a specified point is in the path area.
82
83**Parameters**
84| Name | Type  | Mandatory  | Description                           |
85| ---- | ------ | ---- | ----------------------------- |
86| path | Path2D | No   | Path used for checking. If this parameter is not set, the current path is used.|
87| x    | number | Yes   | X-coordinate of the point used for checking.                   |
88| y    | number | Yes   | Y-coordinate of the point used for checking.                   |
89
90**Return value**
91| Type     | Description           |
92| ------- | ------------- |
93| boolean | Whether a specified point is in the path area.|
94
95**Example**
96```html
97<!-- xxx.hml -->
98<div class="container" style="width: 500px; height: 500px;">
99    <text class="textsize">In path:{{textValue}}</text>
100    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
101</div>
102```
103
104```css
105/* xxx.css */
106.container {
107    display: flex;
108    flex-direction: column;
109    background-color: #F1F3F5;
110    align-items: center;
111    justify-content: center;
112    width: 100%;
113    height: 100%;
114}
115
116canvas {
117    width: 600px;
118    height: 600px;
119    background-color: #fdfdfd;
120    border: none;
121}
122
123.textsize {
124    font-size: 40px;
125}
126```
127
128```js
129// xxx.js
130export default {
131  data: {
132    textValue: 0
133  },
134  onShow(){
135    var canvas = this.$refs.canvas.getContext('2d');
136    var offscreen = new OffscreenCanvas(500,500);
137    var offscreenCanvasCtx = offscreen.getContext("2d");
138
139    offscreenCanvasCtx.rect(10, 10, 100, 100);
140    offscreenCanvasCtx.fill();
141    this.textValue = offscreenCanvasCtx.isPointInPath(30, 70);
142
143    var bitmap = offscreen.transferToImageBitmap();
144    canvas.transferFromImageBitmap(bitmap);
145  }
146}
147```
148
149![en-us_image_0000001224354967](figures/en-us_image_0000001224354967.png)
150
151### isPointInStroke
152
153isPointInStroke(path?: Path2D, x: number, y: number): boolean
154
155Checks whether a specified point is on the edge line of a path.
156
157**Parameters**
158| Name | Type  | Mandatory  | Description                           |
159| ---- | ------ | ---- | ----------------------------- |
160| path | Path2D | No   | Path used for checking. If this parameter is not set, the current path is used.|
161| x    | number | Yes   | X-coordinate of the point used for checking.                   |
162| y    | number | Yes   | Y-coordinate of the point used for checking.                   |
163
164**Return value**
165| Type     | Description           |
166| ------- | ------------- |
167| boolean | Whether a specified point is in the path area.|
168
169**Example**
170```html
171<!-- xxx.hml -->
172<div class="container" style="width: 500px; height: 500px;">
173    <text class="textsize">In stroke:{{textValue}}</text>
174    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
175</div>
176```
177
178```css
179/* xxx.css */
180.container {
181    display: flex;
182    flex-direction: column;
183    background-color: #F1F3F5;
184    align-items: center;
185    justify-content: center;
186    width: 100%;
187    height: 100%;
188}
189
190canvas {
191    width: 600px;
192    height: 600px;
193    background-color: #fdfdfd;
194    border: none;
195}
196
197.textsize {
198    font-size: 40px;
199}
200```
201
202```js
203// xxx.js
204export default {
205  data: {
206    textValue: 0
207  },
208  onShow(){
209    var canvas = this.$refs.canvas.getContext('2d');
210    var offscreen = new OffscreenCanvas(500,500);
211    var offscreenCanvasCtx = offscreen.getContext("2d");
212
213    offscreenCanvasCtx.rect(10, 10, 100, 100);
214    offscreenCanvasCtx.stroke();
215    this.textValue = offscreenCanvasCtx.isPointInStroke(50, 10);
216
217    var bitmap = offscreen.transferToImageBitmap();
218    canvas.transferFromImageBitmap(bitmap);
219  }
220}
221```
222
223![en-us_image_0000001178875308](figures/en-us_image_0000001178875308.png)
224
225### resetTransform
226
227resetTransform(): void
228
229**Example**
230```html
231<!-- xxx.hml -->
232<div class="container" style="width: 500px; height: 500px;">
233    <text class="textsize">In path:{{textValue}}</text>
234    <canvas ref="canvas" style="width: 500px; height: 500px; background-color: #ffff00;"></canvas>
235</div>
236```
237
238```css
239/* xxx.css */
240.container {
241    display: flex;
242    flex-direction: column;
243    background-color: #F1F3F5;
244    align-items: center;
245    justify-content: center;
246    width: 100%;
247    height: 100%;
248}
249
250canvas {
251    width: 600px;
252    height: 600px;
253    background-color: #fdfdfd;
254    border: none;
255}
256
257.textsize {
258    font-size: 40px;
259}
260```
261
262```js
263// xxx.js
264export default {
265  data:{
266   textValue:0
267  },
268  onShow(){
269   var canvas = this.$refs.canvas.getContext('2d');
270   var offscreen = new OffscreenCanvas(500,500);
271   var offscreenCanvasCtx = offscreen.getContext("2d");
272
273   offscreenCanvasCtx.transform(1, 0, 1.7, 1, 0, 0);
274   offscreenCanvasCtx.fillStyle = '#a9a9a9';
275   offscreenCanvasCtx.fillRect(40, 40, 50, 20);
276   offscreenCanvasCtx.fillRect(40, 90, 50, 20);
277
278   // Non-skewed rectangles
279   offscreenCanvasCtx.resetTransform();
280   offscreenCanvasCtx.fillStyle = '#ff0000';
281   offscreenCanvasCtx.fillRect(40, 40, 50, 20);
282   offscreenCanvasCtx.fillRect(40, 90, 50, 20);
283
284   var bitmap = offscreen.transferToImageBitmap();
285   canvas.transferFromImageBitmap(bitmap);
286  }
287}
288```
289
290![en-us_image_0000001179035242](figures/en-us_image_0000001179035242.png)
291